使用 Ruby gem 部署 shell 脚本并安装在 bin 目录中 [英] Deploy a shell script with Ruby gem and install in bin directory
问题描述
我希望在我的 gem 的 bin 目录中放置一个 shell 脚本,以及属于该包的其他 Ruby 程序.我希望将这个 shell 脚本按原样安装在 bin 目录中(即没有包装器).Ruby gem 有可能吗?如果不可能的话,我会对安装后的钩子感到满意.有人有这方面的经验吗?
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?
推荐答案
这里描述了这个问题:https://github.com/rubygems/rubygems/issues/88
如果您正在开发的 gem 仅供您自己使用,您可以简单地安装它
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
我认为您最好编写一个运行 bash 脚本的 ruby 脚本.这是一个关于如何做到这一点的示例:
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']
<小时>
注意:不要忘记将 bin
文件夹中的两个文件都设置为可执行!
NOTE: Don't forget to set both files in the bin
folder to executable!
请注意,虽然 test_gem.sh
已通过 files
Rubygems 命令注册,但它并未注册为 executables
:它只会被放置在已安装 gem 的目录,但未包装/填充.
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.
如果你安装了你的 gem(必要时运行 rbenv rehash),调用 test_gem
将导致 ruby 脚本执行你的 shell 脚本.
If you install your gem (and run rbenv rehash if necessary), calling test_gem
will result in the ruby script executing your shell script.
这篇关于使用 Ruby gem 部署 shell 脚本并安装在 bin 目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!