How to call Rake tasks that are defined in the standard Rakefile from an other Ruby script?

前端 未结 2 534
小蘑菇
小蘑菇 2021-02-04 07:41

Is it possible to call a task which is defined in a Rakefile - not in somefile.rake - from an other Ruby script?

I was hoping that creating a n

相关标签:
2条回答
  • 2021-02-04 08:41

    You forgot to add your new rake to the current Rake Application:

    $LOAD_PATH.unshift File.dirname(__FILE__)
    
    require 'rake'
    require 'pp'
    
    rake = Rake::Application.new
    Rake.application = rake
    rake.init
    rake.load_rakefile
    
    rake[:hello].invoke
    

    or just

    $LOAD_PATH.unshift File.dirname(__FILE__)
    require 'rake'
    require 'pp'
    
    Rake.application.init
    Rake.application.load_rakefile
    
    Rake.application[:hello].invoke
    
    0 讨论(0)
  • 2021-02-04 08:43

    Just load the Rakefile:

    ==> foo.rb <==
    require 'rubygems'
    require 'rake'
    
    load 'Rakefile'
    
    Rake::Task[:some_task].invoke
    
    ==> Rakefile <==
    task :some_task do
      puts "some_task"
    end
    

    Rake::Application is all about command-line processing, default rakefiles, output, etc. You might not need any of that.

    0 讨论(0)
提交回复
热议问题