Deploy a shell script with Ruby gem and install in

2020-07-06 09:42发布

问题:

I wish to place a shell script in my gem's bin dir, along with other Ruby programs that belong to the package. I wish to have this shell script installed in the bin directory as-is (that is, no wrappers). Is that possible with Ruby gems at all? I would be happy with post-install hooks if not otherwise possible. Anybody has any experience with this?

回答1:

This issue is described here: https://github.com/rubygems/rubygems/issues/88

If the gem you're developing is intended only for your own use, you can simply install it with

gem install --no-wrapper my_gem

I think you'd best write a ruby script which runs your bash script. Here's an example on how to do that:


bin/test_gem

#!/usr/bin/env ruby

bin_dir = File.expand_path(File.dirname(__FILE__))
shell_script_path = File.join(bin_dir, 'test_gem.sh')

`#{shell_script_path}`

bin/test_gem.sh

#!/bin/sh

echo "Hello World!"

test_gem.gemspec

spec.files = [
  # ...
  'bin/test_gem', 'bin/test_gem.sh'
]

# ...

spec.executables = ['test_gem']

NOTE: Don't forget to set both files in the bin folder to executable!

Note that while test_gem.sh is registered with the files Rubygems command, it's not registered as executables: it will just be placed in the installed gem's dir but not wrapped/shimmed.

If you install your gem (and run rbenv rehash if necessary), calling test_gem will result in the ruby script executing your shell script.