Thursday, February 16, 2006

Add dependency support to custom rake tasks

I previously wrote about creating a NUnitTask for Rake. The NUnitTask is based on the PackageTask that comes standard with Rake. I assumed that the PackageTask would have support for adding dependencies at construction time. This turned out to be a false assumption. To add a PackageTask and dependency for that task they need to be declared separately.
Rake::PackageTask.new "foo","1.2.3" do |pkg|
pkg.need_zip = true
pkg.package_files.include("build_helpers/*.rb")
end
task :package => :test
I had expected it to work much like a standard task and dependency.
#this will not work
Rake::PackageTask.new "foo","1.2.3" => :init do |pkg|
pkg.need_zip = true
pkg.package_files.include("build_helpers/*.rb")
end
Upon discovery, I decided to alter NUnitTask to take a dependency in the expected manner. The only change required is to alter the assignment of the name variable.
def initialize(name=:nunit)
@name = name
..
end
Becomes
def initialize(params=:nunit)
@name = case params
when Hash
task params.keys[0] => params[params.keys[0]]
params.keys[0]
else
params
end
..
end
Hopefully, Jim Weirich can add the same capability to the next version of the Rake Tasks.
Post a Comment