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.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?