I have a Rake task which I have simplified below. I'm using Ruby 1.9 on Windows.
Perhaps you would like to guess the outcome of calling the Rake task "list_all_levels" below? It should be:
"Hello level 1"
"Hello level 2"
"Hello level 3"
But for reasons unknown to me, it prints only "Hello level 1" and then stops.
That is, it always invokes only the first task. If I change the first line to pass the arg "42", it would print "Hello level 42" and then stop.
I'm wondering why does it not invoke the task 3 times and print all 3 lines? And is there any way to get it to work how I would expect?
task :list_all_levels => [] do
Rake::Task[:list].invoke 1
Rake::Task[:list].invoke 2
Rake::Task[:list].invoke 3
end
task :list, [:level] => [] do |t, args|
puts "Hello level #{args.level}"
end
You need to re-enable the task before the new execution, for this, you just need to execute the method reenable. Like the user Mark Rushakoff mentioned.
The issue is that
invoke
only invokes the task if it is needed. Runningrake --trace
shows:So you can see it's trying to invoke the task
:list
two more times. But one thing you can do is to change the body of the main task to:then the
:list
task is needed again and it correctly prints out all 3 statements.The cleaner way to do it is to use
execute
rather thaninvoke
:That changes your
puts
statement to use justargs
rather thanargs.level
for some reason. There are some other caveats with usingexecute
overinvoke
described in the link above.