Default Task for Namespace in Rake

Default task for namespace in Rake

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'

Rake Default Task and Namespaces

You can define a task with the same name as your namespace. It is not as pretty as having the default task defined inside the namespace itself I think.

desc "runs bar & baz in foo"
task foo: ["foo:bar", "foo:baz"]

namespace :foo do
desc "bar in foo"
task :bar do
puts "bar"
end

desc "baz in foo"
task :baz do
puts "baz"
end
end

And that's how they get listed:

rake foo                               # runs bar & baz in foo
rake foo:bar # bar in foo
rake foo:baz # baz in foo

How to add a Rake task to the default Rake task?

Typically your Rakefile will have something like this:

task :default => [:spec]

You just need to add more tasks into this list.

Rake Default namespace runs tests

The default rake task is defined in rails/railties/Rakefile, and it runs all unit tests by default.

Sample Image

Rake task variable under namespace

This is not specific to Rake, it is just a consequence of lexical scope and the way Ruby handles local variables, declaring them on first use.

First you assign a value to path:

path = "/home/tomcat/tomcat"

Then you create the stage namespace and reassign the variable:

path = "/home/tomcat/stage-tomcat"

Note that this line is executed whatever task you specify, as it is not in any tasks.

Next you create the java_deploy task, but it doesn’t get run yet. This task refers to the path variable, but when the task is called the value of it might have changed.

Later, when defining the production namespace this variable gets reassigned again. Importantly this is still the same variable:

path = "/home/tomcat/production-tomcat"

When the task is actually run, it refers to the path variable and the value of this variable is the latest value assigned to it, which is /home/tomcat/production-tomcat.

When you remove the first assignment to path, then the variable doesn’t exist in the top level. This means that when you assign to path in each namespace definition you are declaring a new (and separate) local variable in each case.

ruby rake tasks in a namespace shared but not :all

You could express the dependencies directly on the depending tasks, like

namespace :build do
task :development => :delete do
puts "development!"
end
task :production => :delete do
puts "production!"
end
end

task :delete do
puts "delete!"
end

Overriding rails' default rake tasks

If you define a rake task that already exists, its execution gets appended to the original task's execution; both tasks will be executed.

If you want to redefine a task you need to clear the original task first:

Rake::Task["db:test:prepare"].clear

It's also useful to note that once a task has been executed in rake, it won't execute again even if you call it again. This is by design but you can call .reset on a task to allow it to be run again.



Related Topics



Leave a reply



Submit