Rake before task hook

2019-02-12 07:17发布

Is there a straight forward way to modify a Rake task to run some bit of code before running the existing task? I'm looking for something equivalent to enhance, that runs at the beginning rather than the end of the task.

Rake::Task['lame'].enhance(['i_run_afterwards_ha_ha'])

标签: ruby rake
2条回答
对你真心纯属浪费
2楼-- · 2019-02-12 08:03

Or you could use the rake-hooks gem to do before and after hooks:

https://github.com/guillermo/rake-hooks

namespace :greetings do 
    task :hola    do puts "Hola!" end ;
    task :bonjour do puts "Bonjour!" end ;
    task :gday    do puts "G'day!" end ;  
end 

before "greetings:hola", "greetings:bonjour", "greetings:gday" do
  puts "Hello!"
end

rake greetings:hola # => "Hello! Hola!" 
查看更多
Explosion°爆炸
3楼-- · 2019-02-12 08:07

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
查看更多
登录 后发表回答