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:


desc 'Install the gem'
task :install do
  exec 'gem install pkg/goodies-0.1.gem'
end

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:


desc 'Install the gem'
task :install do
  executable = RUBY_PLATFORM[/java/] && Config::CONFIG[/darwin/] ? 'jgem' : 'gem'
  exec "#{executable} install pkg/goodies-0.1.gem"
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’


desc 'Install the gem'
namespace 'gem' do
  task :install do
    Gem::Installer.new('pkg/goodies-0.1.gem').install
  end
end

I have included this in the goodies gem 0.2, so go ahead and install it!  ‘gem install goodies’

Tags: , , ,