Forum: Ruby on Rails How to extend rails rake tasks?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2006-05-03 18:59
(Received via mailing list)
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
D86d09b98afa9b2640f8e93e186804ac?d=identicon&s=25 Jackson Miller (Guest)
on 2006-05-03 18:59
(Received via mailing list)
On Tuesday 02 May 2006 10:34 am, Zack Chandler 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 ;)

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

-Jackson
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2006-05-03 18:59
(Received via mailing list)
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
6993d6b4b6d882f421e1b45ac147f3e0?d=identicon&s=25 Scott Barron (Guest)
on 2006-05-03 19:00
(Received via mailing list)
On May 2, 2006, at 11:34 AM, Zack Chandler 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
> Rails@lists.rubyonrails.org
> 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
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2006-05-03 19:00
(Received via mailing list)
Scott,
  Very nice!  I did not know abouut the enhance method.

Thanks,
Zack
This topic is locked and can not be replied to.