I'm new to Rake and using it to build .net projects. What I'm interested in is having a Summary task that prints out a summary of what has been done. I want this task to always be called, no matter what tasks rake was invoked with.
Is there an easy way to accomplish this?
Thanks
Update on the question, responding to Patrick's answer what I want is the after task to run once after all other tasks, so the output I want is:
task :test1 do
puts 'test1'
end
task :test2 do
puts 'test2'
end
Rake::Task.tasks.each do |t|
<Insert rake magic here>
# t.enhance do
# puts 'after'
# end
end
$ rake test1
test1
after
$rake test2
test2
after
$rake test1 test2
test1
test2
after
and if
task :test3 =>[:test1, :test2]
puts 'test3'
end
$rake test3
test1
test2
test3
after
Even though the bounty is gone, any further help much appreciated. (Sadily I don't think that I can offer another bounty.)
You should be able to do this with 'enhance':
This will cause 'my_after_task' to be invoked after 'my_task'.
If you want to apply this to all tasks, just loop over all the tasks and call enhance for each:
Full test file:
And the output:
Apart from the ugly
Rake.application.instance_variable_set
, you can enhance the last task on the command-line like so:That should do exactly what you need to do!
You could use the Kernel.trap call and attach to the "Exit" signal. It will fire upon both normal and abnormal exit.
Put this early in your rakefile:
Now any time you make a "final" task it will be executed at the very end of the program. no matter what.
Found this simple elegant answer here that uses the Ruby method
at_exit
.It's worth noting that the method defined after
at_exit
will run every time the rake script is invoked regardless of the task run, or if any task is run. This includes when runningrake -T
(to list available tasks). Make sure thatat_exit
only does anything if a previous task told it to do so.rakefile.rb
shell
You also don't need to make it run a task if you don't want to.
Or make it run an method
And you may want to make sure you only do the cleanup if a task actually ran so that
rake -T
won't also do a cleanup.rakefile.rb
If anyone came here looking for how to run a task before all other tasks (eg, a special env loading task), you can still use the same enhance method, except only the first arguement:
Posting this as a new answer to keep the other one available. This is much less elegant as I have to get into the guts of Rake and manually update the task list, but it works.
Outputs: