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.)
Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.
Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.
A Rakefile contains executable Ruby code. Anything legal in a ruby script is allowed in a Rakefile. Now that we understand there is no special syntax in a Rakefile, there are some conventions that are used in a Rakefile that are a little unusual in a typical Ruby program.
Rake is Ruby Make, a standalone Ruby utility that replaces the Unix utility 'make', and uses a 'Rakefile' and . rake files to build up a list of tasks. Putting in a simple word : rake will execute different tasks(basically a set of ruby code) specified in any file with .
You should be able to do this with 'enhance':
Rake::Task["my_task"].enhance do
Rake::Task["my_after_task"].invoke
end
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:
Rake::Task.tasks.each do |t|
t.enhance do
Rake::Task["my_after_task"].invoke
end
end
Full test file:
task :test1 do
puts 'test1'
end
task :test2 do
puts 'test2'
end
Rake::Task.tasks.each do |t|
t.enhance do
puts 'after'
end
end
And the output:
$ rake test1
test1
after
$ rake test2
test2
after
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With