How to extend rails rake tasks?


#1

Does anyone know how to extend the buit-in rake tasks like test:unit.
I want to custom load some fixtures in a particular order.

Thanks,
Zack


#2

On Tuesday 02 May 2006 10:34 am, Zack C. wrote:

Does anyone know how to extend the buit-in rake tasks like test:unit.
I want to custom load some fixtures in a particular order.

Basically you extend rake to allow a redefine_task method. I have not
had as
much luck getting an alias_task method to work. I think both of those
would
be nice additions to Rake :wink:

This should get you started:
http://article.gmane.org/gmane.comp.lang.ruby.rails/27426

-Jackson


#3

Jackson,
Thanks for the idea. I tried the technique to override the
clone_structure task as shown in the link and get the following error
(see below). Any ideas? I’m using rake 0.7.1 and in checking Rake’s
source code the resolve_args() is under the TaskManager module which
is included when rake is ran from the command line. Any help is much
appreciated.

Thanks,
Zack

$ rake db:test:clone_structure
(in /Users/zackchandler/dev/tabarca)
rake aborted!
undefined method `resolve_args’ for Rake::Task:Class
/Users/zackchandler/dev/tabarca/rakefile:10
(See full trace by running task with --trace)
$

— custom.rake —
module Rake
class Task

 # Clear all existing actions for the given task and then set the
 # action for the task to the given block.
 def self.redefine_task(args, &block)
   task_name, deps = resolve_args(args)
   TASKS.delete(task_name.to_s)
   define_task(args, &block)
 end

end
end

Clear all existing actions for the given task and then set the

action for the task to the given block.

def redefine_task(args, &block)
Rake::Task.redefine_task(args, &block)
end

Alias one task name to another task name. This let’s a following

task rename the original task and still depend upon it.

def alias_task(new_name, old_name)
Rake::Task::TASKS[new_name.to_s] =
Rake::Task::TASKS.delete(old_name.to_s)
end

namespace :db do
namespace:test do
# Delete the original clone_structure_to_test task and create a new
# one that uses the SQL DDL files to set up the database.
desc “Faster replacement for the original :clone_structure”
redefine_task :clone_structure => :environment do
abcs = ActiveRecord::Base.configurations
mysql -p #{abcs["test"]["database"]} < #{RAILS_ROOT}/db/create.sql
end
end
end


#4

Scott,
Very nice! I did not know abouut the enhance method.

Thanks,
Zack


#5

On May 2, 2006, at 11:34 AM, Zack C. wrote:

Does anyone know how to extend the buit-in rake tasks like
test:unit. I want to custom load some fixtures in a particular order.

Thanks,
Zack


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Rake has the ability to extend tasks, here is an example:

namespace ‘myspace’ do

desc ‘Do something before calling test’
task :doit do
# do your stuff here
end
end

Here we plop this onto the test task as a dependency, so myspace:doit

will be run first when doing rake test (or just rake, since test is

default)
Rake::Task[:test].enhance([‘myspace:doit’])

-Scott