编写一个Rubygem, 如何在gem 被Install之前运行一段程序?
答案很简单:不能。
我搜了以下, RubyGem中提供了一个API,叫Gem.pre_install。 但是经过研究后, 发现这个API跟他的名字完全不相符, 为此我给rubygems提交了一个Bug, 原文这么写地:
I write a gem called 'gem-fast', I used curl , so I want to make sure 'curl' is installed before user install this gem.
I found that Gem.pre_installs only contains pre_install hooks of gems that already installed. It does not contains the current installing gem
I write my 'rubygems_plugin.rb' like this:
Gem.pre_install do
if curl not installedraise Gem::InstallError, "Your should install curl before install this gem!"
end end
So I use Gem.pre_install to do this. But It doesn't work as I expected! the hook not run brefore the gem installed!
I check the code Of rubygems, found that the design of pre_install, post_install was not right.
不过你可以单独写一个make文件, 在里面作类似的编译检查。这么搞显然大题小作了。
rubygem应该在.gemspec中提供一个类似的字段。 而不是放到rubygems_plugin.rb中
最终能像这样:
Gem::Specification.new do |s| s.pre_install = Proc.new{ #add your pre_install_hook here } end