Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use hooks in ruby gems

Tags:

ruby

rubygems

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?

like image 996
dimus Avatar asked May 19 '11 19:05

dimus


1 Answers

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.

like image 139
Oscar Barrett Avatar answered Oct 28 '22 03:10

Oscar Barrett