Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a Rake Task run after all other tasks? (i.e. a Rake AfterBuild task)

Tags:

ruby

rake

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.)

like image 891
Charley Rathkopf Avatar asked Nov 06 '09 18:11

Charley Rathkopf


People also ask

How to execute Rake task?

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.

What is Rake task in Ruby on Rails?

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.

What is a Rakefile?

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.

What is Rack task?

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 .


2 Answers

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
like image 161
Patrick Ritchie Avatar answered Oct 09 '22 14:10

Patrick Ritchie


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 }
like image 25
Nate Avatar answered Oct 09 '22 14:10

Nate