Building & Installing GemSpecs

Author: Samuel Williams When: Wednesday, 24 February 2010

I have created a very simple script to automate the process of building and installing gems. It is very easy to use and makes it easy to test code prior to deployment to a gem server.

build/Rakefile

This file should require little or no customization.


# Automatic Gem Build Script v2.0

require 'fileutils'
require 'rubygems'

MasterGemspec = "gemspec.master"

desc 'Build a gemspec file'
task :build_gemspec do
  $spec = Gem::Specification.load(MasterGemspec)
  
  $versioned_gem = "#{$spec.name}-#{$spec.version}.gem"
  $versioned_gemspec = "#{$spec.name}-#{$spec.version}.gemspec"
  $base_gemspec = "#{$spec.name}.gemspec"
  
  puts "Building #{$versioned_gemspec}..."
  
  File.open($versioned_gemspec, "w") do |f|
    f.write($spec.to_ruby)
  end
  
  puts "Copying file to ../#{$base_gemspec}"
  FileUtils.cp($versioned_gemspec, "../#{$base_gemspec}")
end

task :build_gem => [:build_gemspec] do
  Dir.chdir("../") do
    system("gem build #{$base_gemspec}")
  end
end

task :install_gem => [:build_gem] do
  Dir.chdir("../") do
    system("gem install --local #{$versioned_gem}")
  end
end

build/gemspec.master

This file will require almost every field to be changed, depending on your project, dependencies, etc.


Dir.chdir("../") do
  require 'lib/utopia/version'

  Gem::Specification.new do |s|
    s.name = "utopia"
    s.version = Utopia::VERSION::STRING
    s.author = "Samuel Williams"
    s.email = "samuel.williams@oriontransfer.co.nz"
    s.homepage = "http://www.oriontransfer.co.nz/software/utopia"
    s.platform = Gem::Platform::RUBY
    s.summary = "Utopia is a framework for building websites."
    s.files = FileList["{ext,lib}/**/*"].to_a
    s.require_path = "lib"

	s.add_dependency("mime-types")
	s.add_dependency("rack")
	
	# Dates and Times, etc
	s.add_dependency("activesupport")
	
	s.add_dependency("rack-cache")
	s.add_dependency("rack-contrib")
	
	s.add_dependency("rmagick")
	
	# s.extensions << "ext/xnode/extconf.rb"
  end
end

lib/[project-name]/version.rb

This file needs to have your project's current version in it. This controls the version in the GemSpec file.

module Utopia # Replace this with your own gem name
  module VERSION
    MAJOR = 0
    MINOR = 9
    TINY  = 15

    STRING = [MAJOR, MINOR, TINY].join('.')
  end
end

Usage

Basically, when you create a gem, it will have a file layout similar to the following:

[gem-name]/
			build/gemspec.master (as above, but modified to suit your project)
			build/Rakefile (as above)
			
			lib/[gem-name].rb
			lib/[gem-name]/version.rb
			lib/[gem-name]/[other-src-files]

To install the gem, simply run sudo rake install_gem from the build directory:

$ sudo rake install_gem
(in ./utopia/build)
Building utopia-0.9.14.gemspec...
Copying file to ../utopia.gemspec
WARNING:  no description specified
WARNING:  no rubyforge_project specified
  Successfully built RubyGem
  Name: utopia
  Version: 0.9.14
  File: utopia-0.9.14.gem
Successfully installed utopia-0.9.14
1 gem installed
Installing ri documentation for utopia-0.9.14...
Installing RDoc documentation for utopia-0.9.14...

This will, along with installing the gem, produce a working gem and gemspec file in the root directory. In this case, utopia-0.9.14.gem and utopia.gemspec. These can be used for automatic build and deployment tasks via a git post-commit hook, for example. My server automatically compiles and installs the latest gem using this process.

Comments

Leave a comment

Please note, comments must be formatted using Markdown. Links can be enclosed in angle brackets, e.g. <www.codeotaku.com>.