djberg96 ([info]djberg96) wrote,
@ 2009-09-11 20:55:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Current mood: chipper

Rake tasks to build gems
For gems where you may want to ship both a source bundle and a binary bundle, consider using Rake tasks instead of hard coding a gem spec directly in your Rake task. The first step is to remove this line from your .gemspec file:

Gem::Builder.new(spec).build

Why? Because we're going to eval the gemspec file and, in the case of the binary gem, modify the spec before we build it. Here are the Rake tasks I'm using that let me use a single gemspec and build either version without a lot of fuss:
desc 'Build a standard gem'
task :build_gem => :clean do
  rm_rf('lib') if File.exists?('lib')
  spec = eval(IO.read('foo.gemspec'))
  Gem::Builder.new(spec).build
end

desc 'Build a binary gem'
task :build_binary_gem => [:build] do
   file = 'ext/foo.' + CONFIG['DLEXT']
   mkdir_p('lib')
   mv(file, 'lib')

   spec = eval(IO.read('foo.gemspec'))
   spec.extensions = nil
   spec.files = spec.files.reject{ |f| f.include?('ext/') }
   spec.platform = Gem::Platform::CURRENT

   Gem::Builder.new(spec).build
end

The standard gem is pretty straightforward. In the case of the binary gem, we need to build the binary, copy it to a 'lib' directory, remove the 'extensions' specification item, remove any 'ext' files (we don't want to ship build files do we? no), set the platform to the current platform and then, finally, build the gem.

Enjoy.



Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…