Suyog Sakegaonkar
Suyog Sakegaonkar

Reputation: 225

How to execute a script while gem installation?

I am writing a gem, while gem is installed I also want a script to be executed which resides in gem.

Upvotes: 1

Views: 1892

Answers (2)

nmunson
nmunson

Reputation: 902

You aren't supposed to be able to run arbitrary scripts when the gem gets installed, but there is a hack you can use to work around this. RubyGems lets you build extensions for your gem with extconf.rb, and you can trick it to run your script this way. There's a guide on it here: http://blog.costan.us/2008/11/post-install-post-update-scripts-for.html.

For a discussion on why running scripts as part of the install of a gem is not included in RubyGems and is discouraged, follow the pull request comments here: https://github.com/rubygems/rubygems/pull/213.

Upvotes: 4

Matheus Moreira
Matheus Moreira

Reputation: 17030

RubyGems itself will not let you do this, likely for security reasons. The most you can do is print a message to the user:

Gem::Specification.new 'your_gem' do |specification|
  # ...

  specification.post_install_message = 'Thanks for installing!'
end

What you can do is include a setup script with your gem and request that the user runs it. Use the post-install message to let him know that he must run the script.

Upvotes: 2

Related Questions