Using Multiple Versions of Rake

Rake has become a critical component of my development toolchain. So much so that I have developed extensions, such as Chrysalis, to assist with common aspects of the build cycle.

However, due to time constraints, Chrysalis does not support the latest version of Rake (currently 0.8.3). The 0.8 branch reworked some of the internals, and Chrysalis was developed against 0.7.3. As such, I often need to quickly switch between multiple versions of Rake.

Thankfully, the executable RubyGems places in the bin directory makes this easy. I had long been curious how this works, so I took the time to figure it out.

The executable scripts generated by RubyGems, in this case for Rake, look like the following:

require 'rubygems'
 
version = ">= 0"
 
if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
  version = $1
  ARGV.shift
end
 
gem 'rake', version
load 'rake'

There is a bit of regular expression magic that works on the first argument to the command, accessed through ARGV.first. The =~ is the positive match operator for regular expressions. If a match is found, a portion of it will be stored in the global variable $1.

The goal of the RubyGems-generated script is to match a user-supplied version number, without interfering with any other arguments the actual script may expect. In order to do so, the version argument is surrounded by underscores. This is enforced by using the ^ and $ as achors for the beginning and end of the string, respectively.

The result of all this is a simple way to switch between versions easily on the command line.

$ rake _0.7.3_ --version
$ rake _0.8.3_ --version

Comments

Lonna Hanson
says:
April 8, 2009 at 10:41 AM

Jared,
Your writings are always so interesting. I enjoy reading them.
Mom

Arlis Hanson
says:
April 9, 2009 at 9:46 PM

I’m always impressed by what you write but due to the age of my knowledge, I cannot comment. I’m interested in your politics and persuasions however? Do you care to comment on our President’s remark that ours is not a Christian nation?

Jared Hanson
says:
April 9, 2009 at 10:19 PM

Hmm… that sounds like an invitation to cause trouble.

I’ll make it a point to write more on these topics. These issues deserve thorough consideration, however, so it may take me a while to do research and find references.

In the meantime, you may want to consider that some of America’s recent high-profile policies are decidedly non-Christian. I’m thinking specifically about the use of torture as an interrogation technique (which Obama has reversed, thankfully). Any distance placed between America as a nation and Christianity as a faith may be better for the faith.

Food for thought. More to come.

Post a comment