Rake Before Task Hook

Rake before task hook

You can use the dependency of Rake task to do that, and the fact that Rake allows you to redefine existing task.

Rakefile

task :your_task do
puts 'your_task'
end
task :before do
puts "before"
end
task :your_task => :before

As result

$ rake your_task
before
your_task

A General Method to be invoked before every rake execution

This seems to be a bit awkward, But it may help others.

Rake.application.top_level_tasks

will return an array of information including Rake name and its arguments.
Reference attached below.

pry(main)> a = Rake.application.top_level_tasks
=> ["import_data[client1,", "data.txt]"]

Is there a way to hook a before and after suite, in a Rakefile?

If your specs failed, maybe that's why nothing after Rake::Task["spec"].execute gets executed. Instead of using the Rakefile, why not use Rspec's before(:suite) and after(:suite) hooks? You can put that code in your spec_helper.rb if you have one.

https://www.relishapp.com/rspec/rspec-core/v/2-0/docs/hooks/before-and-after-hooks#before/after-blocks-defined-in-config-are-run-in-order



Related Topics



Leave a reply



Submit