Rake Hacks: Overriding Tasks, Quick Binary Run, and Intelligent IRB
2008.02.12 22:04I’m doing some things with Rake these days that I like. I have a bad feeling that these things are obvious and unimpressive. But, whatever.
Overriding Tasks
I use Hoe to get my projects started. But I don’t like how the :default task attempts to run all of my tests from the “test” directory. The assumption of Test::Unit also hurts, especially if I’m using RSpec. I usually split my tests into “system” & “unit” categories (and sometimes “integration” also), so I prefer to wire this stuff up in my own way.
Here is how I add the ability to overwrite a task in Rake:
(ripped from http://matthewbass.com/2007/03/07/overriding-existing-rake-tasks/)
Rake::TaskManager.class_eval do
def remove_task(task_name)
@tasks.delete(task_name.to_s)
end
end
def remove_task(task_name)
Rake.application.remove_task(task_name)
end
And then, when I want to override a task:
remove_task :default task :default => :spec
Quick Binary Run
Most of the projects that I’m working on have a binary (a script, really) for the primary interface. But to run it I need to have $project/lib in my RUBYLIB, and remembering to set it is a pain in the ass, so I write a quick :run task.
(NOTE: ‘regenerator’ is the name of one of my projects).
desc "Runs Regenerator"
task :run do |t|
ARGV.shift()
exec "ruby -w -Ilib bin/regenerator #{ARGV.join(' ')}"
end
Intelligent IRB
IRB is handy, but I want to run it in the context of my project. I think Rails does something similar. I do it like this:
desc 'Runs irb in this project\'s context' task :irb do |t| exec 'irb -I lib -r lib/regenerator.rb' end
category: code