Deploy a shell script with Ruby gem and install in bin directory

£可爱£侵袭症+ 提交于 2020-01-01 09:06:04

问题


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.



来源:https://stackoverflow.com/questions/23701726/deploy-a-shell-script-with-ruby-gem-and-install-in-bin-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!