Rake Args... The New Hottness!

|

A colleague of mind, Andy Kappen, just turned me onto some new hottness in Rake 0.8.0. Now, Andy doesn't have a blog, so I can't give him any link luv, but I can say that he and his crew held the #4 spot for Rock Band, so you know he's got some street cred. Unfortunately, a hardware failure and subsequent long turnaround time for repairs caused them to drop to the #22 spot.

Anyway, here are the Rake changes we'll focus on:

/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb

  
  class Task
    def invoke
    end
  end
  

/lib/ruby/gems/1.8/gems/rake-0.8.0/lib/rake.rb

  
  class Task
    def invoke(*args)
    end
  end
  

See the difference? In case you missed it, we can now call rake tasks with an arguments list (*args) instead of using environment variables.

Let's take a concrete example. Say you wanted to write a rake task to parse a given set of revisions from your subversion log. With Rake 0.7.3, the code might have looked like this:

  
  namespace :subversion do
    desc"Parse subversion log"
    task :parse_log do
      puts "parsing: svn log -r #{ENV['end_rev']}:#{ENV['start_rev']}"
    end
  end
  

And you could call this task with:

  
  rake subversion:parse_log start_rev=200 end_rev=250
  

Now, there is nothing wrong with that. But, with the new hottness that 0.8.0 and *args provides, you can refactor your task to:

  
  namespace :subversion do
    desc"Parse subversion log"
    task :parse_log, :start_rev, :end_rev do |t, args|
      puts "parsing: svn log -r #{args.end_rev}:#{args.start_rev}"
    end
  end
  

And call it like this:

  
  rake subversion:parse_log[200,250]
  

An added benefit of this new hottness is that our task descriptions will list the expected variables:

  
  > rake -T
  .
  rake subversion:parse_log[start_rev,end_rev]  # Parse subversion log
  

Simply sizzlin!