如何通过自制程序分发红宝石脚本 [英] how to distribute a ruby script via homebrew

查看:99
本文介绍了如何通过自制程序分发红宝石脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过自制程序部署简单的ruby脚本?

How can I deploy a simple ruby script via homebrew?

这就是我尝试过的

在名为homebrew-foo

# file https://github.com/foo/homebrew-foo/blob/master/foo.rb
class Foo < Formula
  desc "A command line tool"
  url "https://github.com/foo/foo/archive/master.zip"
  version "5.0.1"

  def install
    bin.install "foo"
    lib.install Dir["lib/*"]
  end
end

另一个存储库包含ruby脚本.这些是文件

The other repository contains the ruby script. These are the files

./foo
./lib/libfile1.rb

这是脚本的作用

#!/usr/bin/env ruby
require './lib/libfile1.rb'
puts "came here"

问题是require失败.

$ brew install foo/foo/foo
$ foo

导致此错误

/Users/user1/.rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in 从/usr/local/bin/foo require': cannot load such file -- ./lib/libfile1.rb (LoadError) from /Users/user1/.rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in require'

/Users/user1/.rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in require': cannot load such file -- ./lib/libfile1.rb (LoadError) from /Users/user1/.rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in require' from /usr/local/bin/foo

$ which foo
/usr/local/bin/foo

我怀疑是因为/usr/local/bin/foo/lib/libfile1.rb

有什么想法是正确的方法吗?

Any ideas whats the proper way to do this?

推荐答案

您的脚本有两个问题:

第一个是您尝试相对于 current 目录相对于require一些文件;即运行脚本的位置,而不是运行脚本的位置.可以使用 Ruby的require_relative :

The first one is you try to require some file relatively to the current directory; i.e. the one from which the script is run, not the one it’s located in. That issue can be fixed by using Ruby’s require_relative:

#!/usr/bin/env ruby
require_relative './lib/libfile1.rb'
puts "came here"

第二个问题是脚本假定lib/目录位于其目录中;并不是因为您的公式将脚本安装在<prefix>/bin/下,而库文件安装在<prefix>/lib/下. Homebrew在该用例中有一个帮助器,称为 Pathname#write_exec_script .它使您可以将所需的所有内容安装在一个目录下,然后在bin/下创建一个调用脚本的可执行文件.

The second issue is the script assumes the lib/ directory is located in its directory; which it’s not because your formula installs the script under <prefix>/bin/ and the library files under <prefix>/lib/. Homebrew has a helper for that use-case called Pathname#write_exec_script. It lets you install everything you need under one single directory, then create an executable under bin/ that calls your script.

您的公式现在看起来像这样:

Your formula now looks like this:

class Foo < Formula
  desc "A command line tool"
  url "https://github.com/foo/foo/archive/master.zip"
  version "5.0.1"

  def install
    libexec.install Dir["*"]
    bin.write_exec_script (libexec/"foo")
  end
end

它将在libexec/下安装所有内容(lib/通常保留给lib文件使用),然后在bin/下添加一个可执行文件,该可执行文件调用您的libexec/foo脚本.

It installs everything under libexec/ (lib/ is usually reserved for lib files), then add an executable under bin/ that calls your libexec/foo script.

这篇关于如何通过自制程序分发红宝石脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆