Add a fake extension, use extconf.rb to run your code and then simulate successful compilation. Use the links at the end for example code.
RubyGems isn't supposed to run arbitrary code during gem installations, but it supports building extensions for gems. This is a really sweet feature, and makes rubygems a nice tool for cross-platform package management. That's good and all, but the part that we care about is that the process of building an extension starts by running extconf.rb in the extension's directory, which is responsible for producing a Make file that will orchestrate the building process.
Knowing this, the first thing that comes to one's mind is - let's add an extension to the gem, and put the hook code in extconf.rb. However, there's one more issue left. If rubygems believes that your gem's extension hasn't been built properly, it will not finish installing the gem, and it will spit out a nasty error message.
In order to work around that, we need to trick rubygems' build process, so we need to bypass 3 checks:
- a Make file is generated - create an empty Makefile
- make all and make install run successfully - generate a make file that contains empty all and install targets; create fake make binaries to cover the case when the user doesn't have a build environment (Linux/Mac: an executable make with a /bin/sh shebang should work; Windows: an empty nmake.bat should trick the Windows port of rubygems)
- an extension binary is generated - create empty files your_extension_name.so and your_extension_name.dll
I haven't tested out the Windows plan yet, but I have good reasons to believe it should work (I've built gems with real extensions on Windows some time ago).
Adding an extension to your gemspec (assumes you're using hoe or echoe):
Placing your hook in extconf.rb:
Tricking rubygems into thinking an extension was built (see emulate_extension_install):
I found this post when searching for Gem post-install hooks. It looks like there's a legitimate way to do this now; see Gem.post_install at http://rubygems.rubyforge.org/rdoc/Gem.html#M000746ReplyDelete