Rake before task hook

后端 未结 2 824
终归单人心
终归单人心 2021-01-01 17:58

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

相关标签:
2条回答
  • 2021-01-01 18:29

    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
    
    0 讨论(0)
  • 2021-01-01 18:33

    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!" 
    
    0 讨论(0)
提交回复
热议问题