doucheng9304 2018-06-12 23:50
浏览 39
已采纳

Golang的自制方法

I want to write a Homebrew formula for installing a Go package and its dependencies. Here's what I've got so far:

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

But I get an error about running go get ./ outside the $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

This doesn't make sense to me. I set ENV["GOPATH"] to /private/tmp/ls-go-20180612-22435-oidqms/ls-go-0.0.0/, and I cd'ed into a subdir of that path. Why is it saying I'm outside the $GOPATH?

How should I get the dependencies for my package?

EDIT: I'd prefer to avoid vendoring packages.

  • 写回答

1条回答 默认 最新

  • duandi8613 2018-06-13 00:50
    关注

    I'm pretty sure that your Homebrew formula is just a Ruby class with some DSL magic to make it friendlier. That means that system runs commands in a separate process so this:

    system "cd", "some_directory"
    

    will change the current directory in a separate process and then that process exits without affecting the parent.

    You should use Dir.chdir instead:

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

    Using the block form (i.e. Dir.chdir dir do ... end) will change directives, run the system command, and then change back to the original directory before continuing so you don't have to worry about the cd - part.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?