|
This post originated from an RSS feed registered with Ruby Buzz
by Jay Fields.
|
Original Post: Rake: Task Overwriting
Feed Title: Jay Fields Thoughts
Feed URL: http://blog.jayfields.com/rss.xml
Feed Description: Thoughts on Software Development
|
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jay Fields
Latest Posts From Jay Fields Thoughts
|
|
By default Rake Tasks append behavior every time they are defined. The following example shows that both definitions are executed.
require 'rubygems'
require 'rake'
task :the_task do
p "one"
end
task :the_task do
p "two"
end
Rake::Task[:the_task].invoke
I like this behavior, but sometimes you want to overwrite a task instead of appending to what's already there.
When you no longer want the existing behavior the overwrite method can come in handy.
require 'rubygems'
require 'rake'
class Rake::Task
def overwrite(&block)
@actions.clear
enhance(&block)
end
end
task :the_task do
p "one"
end
Rake::Task[:the_task].overwrite do
p "two"
end
Rake::Task[:the_task].invoke
The overwrite method is good, but sometimes you want to redefine the task using one of the specialized Rake tasks. I recently wanted to redefine the test task, so I created the abandon method to remove the existing definition.
require 'rubygems'
require 'rake'
require 'rake/testtask'
class Rake::Task
def abandon
@actions.clear
end
end
task :the_task do
p "one"
end
Rake::Task[:the_task].abandon
Rake::TestTask.new(:the_task) do |t|
t.libs << "test"
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
Rake::Task[:the_task].invoke
Hopefully you wont need this type of thing very often, but it can be handy when you want to overwrite a task that has been previously by a framework you've included.

Read: Rake: Task Overwriting