2011-05-19 50 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。