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 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab