2011-05-19 93 views
8

我有一个需要在安装宝石后执行一些rake任务的宝石。安装后使用http://blog.costan.us/2008/11/post-install-post-update-scripts-for.html中描述的方法执行代码存在一种骇人的方式。如何在红宝石宝石中使用钩子

但是Gem类现在有钩子,其中一个是post_install钩子。我试图添加一行像

Gem.post_install { puts 'post hook example' } 

到Rakefile中,它似乎并没有在安装过程中执行。这条线应该放置在什么地方才能被挂钩?

+0

check [this one](http://stackoverflow.com/questions/223151/how-can-i-write-a-hook-that-gets-called-when-a-rubygem-is-installed) – 2011-05-19 19:38:55

+0

OP已经链接到那个帖子... – theIV 2011-05-19 19:42:07

+0

也在寻找这个!有任何想法吗? – 2015-11-06 10:42:52

回答

6

创建于lib/rubygems_plugin.rb

文件在这个文件中,您可以定义自定义挂钩。例如:

Gem.post_install do 
    puts "post_install called for gem" 
end 

不需要任何东西。
输出示例:

Successfully built RubyGem 
    Name: post_install_test 
    Version: 0.1.0 
    File: post_install_test-0.1.0.gem 
post_install called for gem 
Successfully installed post_install_test-0.1.0 
1 gem installed 

我只找到该文档中的source

如果这不起作用,或者您对post install hook的更改似乎并不总是更新,请在重建和安装gem之前彻底卸载gem。