Golang的自制公式

时间:2018-06-12 23:50:53

标签: go homebrew

我想编写一个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

但是我在go get ./之外运行$GOPATH时遇到错误。

==> 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/,然后我进入该路径的子目录。为什么说我在$GOPATH之外?

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

编辑:我宁愿避免销售包裹。

1 个答案:

答案 0 :(得分:2)

我很确定你的Homebrew公式只是一个带有一些DSL魔法的Ruby类,以使它更友好。这意味着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命令,然后在继续之前更改回原始目录,这样您就不必担心{{ 1}}部分。