Build & Install Ruby Gems with Rake
- by kerry
Are you using rake to build your gems? Have you ever wished there were an install task to install it to your machine? I, for one, have written something like this a few times:
1: desc 'Install the gem'
2: task :install do
3: exec 'gem install pkg/goodies-0.1.gem'
4: end
5:
That is pretty straightforward. However, this will not work under JRuby on Mac where the command should be ‘jgem’. So we can enhance it to detect the platform, and host OS:
1: desc 'Install the gem'
2: task :install do
3: executable = RUBY_PLATFORM[/java/] && Config::CONFIG[/darwin/] ? 'jgem' : 'gem'
4: exec "#{executable} install pkg/goodies-0.1.gem"
5: end
This is a little better. I am still not comfortable with the sloppiness of building a shell command and executing it though. It is possible to do it with strictly Ruby. I am also going namespace it to integrate better with the GemPackageTask. Now it will be accessed via ‘rake gem:install’
1: desc 'Install the gem'
2: namespace 'gem' do
3: task :install do
4: Gem::Installer.new('pkg/goodies-0.1.gem').install
5: end
6: end
I have included this in the goodies gem 0.2, so go ahead and install it! ‘gem install goodies’