This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Rake tasks to build gems
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
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.