how to distribute a ruby script via homebrew

喜你入骨 提交于 2019-12-01 12:11:14

There are two issues with your script:

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"

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

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

I found the answer to my own question, actually it's a technique used by someone on the net, basically do something like this

#!/usr/bin/env ruby
DBMGR_HOME = File.expand_path('../..', __FILE__)
$LOAD_PATH.unshift(File.join(DBMGR_HOME, 'lib'))
require 'dbmgr'

And the recipe can be like this:

https://github.com/callahanrts/homebrew-dbmgr/blob/master/dbmgr.rb
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!