How to use hooks in ruby gems

你离开我真会死。 提交于 2019-12-10 12:50:54

问题


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 placed for the hook to be registered?


回答1:


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.



来源:https://stackoverflow.com/questions/6063725/how-to-use-hooks-in-ruby-gems

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!