obvio171 obvio171 - 3 months ago 7
Ruby Question

Default task for namespace in Rake

Given something like:

namespace :my_tasks do
task :foo do
do_something
end

task :bar do
do_something_else
end

task :all => [:foo, :bar]
end


How do I make
:all
be the default task, so that running
rake my_tasks
will call it (instead of having to call
rake my_tasks:all
)?

Answer

Place it outside the namespace like this:

namespace :my_tasks do
  task :foo do
    do_something
  end

  task :bar do
    do_something_else
  end

end

task :all => ["my_tasks:foo", "my_tasks:bar"]

Also... if your tasks require arguments then:

namespace :my_tasks do
  task :foo, :arg1, :arg2 do |t, args|
    do_something
  end

  task :bar, :arg1, :arg2  do |t, args|
    do_something_else
  end

end

task :my_tasks, :arg1, :arg2 do |t, args|
  Rake::Task["my_tasks:foo"].invoke( args.arg1, args.arg2 )
  Rake::Task["my_tasks:bar"].invoke( args.arg1, args.arg2 )
end

Notice how in the 2nd example you can call the task the same name as the namespace, ie 'my_tasks'

Comments