Here is example rake task code which you can use and modify the next time you’re setting up new Rake tasks for a Rails app from scratch. The example includes the syntax for setting the default task, running multiple tasks in order, and a task which includes multiple arguments. This coding syntax works with Rails 3.1.
# /lib/tasks/mytask.rake
namespace :mytask do
task :default => 'mytask:full_run'
desc "run all tasks in proper order"
task :full_run => [:example_task_one, :example_task_two] do
puts "All tasks ran and completed at #{Time.now}"
end
desc "example task without arguments"
task :example_task_one => :environment do
# task code goes here
puts "Example Task One completed"
end
desc "example task with arguments"
task :example_task_two, [:arg1, :arg2] => :environment do |t, args|
# if no arguments, display docs
if args.count == 0
puts ''
puts "rake mytask:example_task[arg1,arg2]"
puts "arg1: description of first argument"
puts "arg2: description of second argument"
puts ''
puts "example:"
puts "rake mytask:example_task[123,'456']"
puts ''
else
# task code goes here
puts "Running task with arg1: #{args.arg1}, and arg2: #{args.arg2}"
puts "Example Task Two completed"
end
end
end
Special thanks to Jason Seifer for his Rake Tutorial which made this possible.