【问题标题】:Homebrew formulas for GolangGolang 的自制公式
【发布时间】:2018-06-13 08:50:19
【问题描述】:

我想编写一个 Homebrew 公式来安装 Go 包及其依赖项。到目前为止,这是我所得到的:

class LsGo < Formula
  desc "A more colorful, user-friendly implementation of `ls` written in Go"
  homepage "https://github.com/acarl005/ls-go"
  url "https://github.com/acarl005/ls-go/archive/v0.0.0.tar.gz"
  sha256 "db9ba7300fbbaf92926b2c95fd63e3e936739e359f123b5a45e6ca04b490af51"

  depends_on "go" => :build

  def install
    ENV["GOPATH"] = buildpath
    (buildpath/"src/github.com/acarl005").mkpath
    ln_s buildpath, buildpath/"src/github.com/acarl005/ls-go"
    system "cd", buildpath/"src/github.com/acarl005/ls-go"
    system "go", "get", "./"
    system "cd", "-"
    system "go", "build", "-o", bin/"ls-go"
  end

  test do
    system bin/"ls-go", "--help"
  end
end

但我收到关于在$GOPATH 之外运行go get ./ 的错误。

==> cd /private/tmp/ls-go-20180612-22435-oidqms/ls-go-0.0.0/src/github.com/acarl005/ls-go
==> go get ./
Last 15 lines from /Users/andy/Library/Logs/Homebrew/ls-go/02.go:
2018-06-12 16:31:14 -0700

go
get
./

go get: no install location for directory /private/tmp/ls-go-20180612-22435-oidqms/ls-go-0.0.0 outside GOPATH

这对我来说没有意义。我将ENV["GOPATH"] 设置为/private/tmp/ls-go-20180612-22435-oidqms/ls-go-0.0.0/,然后cd 到该路径的子目录中。为什么说我在$GOPATH之外?

我应该如何获取我的包的依赖项?

编辑:我宁愿避免出售包。

【问题讨论】:

    标签: go homebrew


    【解决方案1】:

    我很确定您的 Homebrew 公式只是一个 Ruby 类,它带有一些 DSL 魔法以使其更友好。这意味着system 在单独的进程中运行命令,因此:

    system "cd", "some_directory"
    

    将在单独的进程中更改当前目录,然后该进程退出而不影响父进程。

    您应该改用Dir.chdir

    Dir.chdir buildpath/"src/github.com/acarl005/ls-go" do
      system "go", "get", "./"
    end
    system "go", "build", "-o", bin/"ls-go"
    

    使用块形式(即Dir.chdir dir do ... end)将更改指令,运行system 命令,然后在继续之前更改回原始目录,因此您不必担心cd - 部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 2015-06-16
      • 1970-01-01
      相关资源
      最近更新 更多