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 alw
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 running rake -T
(to list available tasks). Make sure that at_exit
only does anything if a previous task told it to do so.
rakefile.rb
desc "Lovely task a"
task :a do
puts "a"
end
desc "Lovely task b"
task :b do
puts "b"
end
task :cleanup do
puts "cleanup"
end
at_exit { Rake::Task[:cleanup].invoke if $!.nil? }
shell
$ rake a b
a
b
cleanup
$ rake -T
rake a # Lovely task a
rake b # Lovely task b
cleanup
You also don't need to make it run a task if you don't want to.
at_exit do
puts "cleanup"
end
Or make it run an method
def on_exit_do_cleanup
puts "cleanup"
end
at_exit { on_exit_do_cleanup }
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
do_cleanup = false
desc "Lovely task a"
task :a do
puts "a"
do_cleanup = true
end
desc "Lovely task b"
task :b do
puts "b"
do_cleanup = true
end
task :cleanup do
return unless $!.nil? # No cleanup on error
return unless do_cleanup # No cleanup if flag is false
puts "cleanup"
end
at_exit { Rake::Task[:cleanup].invoke }