guillermo / rake-hooks

Rake hooks let you add callbacks to rake tasks.
MIT License
70 stars 21 forks source link

Pass current task to after/before block #15

Open philihp opened 6 years ago

philihp commented 6 years ago

In situations where a before or after block is run on multiple tasks, it can be helpful to know which task is actually about to run. For example, I want something to run before any task:

before *Rake.application.top_level_tasks do |t|
  puts "Excuse me, I am about to run #{t}..."
end
after *Rake.application.top_level_tasks do |t|
  puts "...Thank you for letting me run #{t}"
end

This mimics how you can get the current task from within a task

task :sometask do |t|
  puts "I am running #{t}"
end