开发者

How to use hooks in ruby gems

I have a gem which needs some rake task to be executed after installation of the gem. There is a hackish way to execute a code after install using method described at http://blog.costan.us/2008/11/post-install-post-update-scripts-for.html.

However Gem class now has hooks, and one of them is a post_install hook. I am trying to add a line like

Gem.post_install { puts 'post hook example' } 

into Rakefile and it does not seem to be executed during install. Where should this line to be 开发者_如何学JAVAplaced for the hook to be registered?


Create a file at lib/rubygems_plugin.rb

In this file you can define your custom hooks. For example:

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

No need to require anything.
Example output:

  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

I only found the documentation for this in the source.

If this doesn't work, or your changes to your post install hook don't always seem to update, uninstall the gem completely before rebuilding and installing it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜