如何在Ruby脚本中运行Rake任务?

我有一个system,它具有Rake任务,通常我会从命令行调用它:

rake blog:post Title

我想编写一个多次调用Rake任务的Ruby脚本,但是我看到的唯一解决方案是使用``(反引号)或system

什么是正确的方法?

Mando Escamilla asked 2020-01-20T06:35:06Z
4个解决方案
43 votes

来自timocracy.com:

require 'rake'
require 'rake/rdoctask'
require 'rake/testtask'
require 'tasks/rails'

def capture_stdout
  s = StringIO.new
  oldstdout = $stdout
  $stdout = s
  yield
  s.string
ensure
  $stdout = oldstdout
end

Rake.application.rake_require '../../lib/tasks/metric_fetcher'
results = capture_stdout {Rake.application['metric_fetcher'].invoke}
titanous answered 2020-01-20T06:35:32Z
17 votes

这适用于Rake 10.0.3版:

require 'rake'
app = Rake.application
app.init
# do this as many times as needed
app.add_import 'some/other/file.rake'
# this loads the Rakefile and other imports
app.load_rakefile

app['sometask'].invoke

正如knut所说,如果要多次调用,请使用reenable

Kelvin answered 2020-01-20T06:35:56Z
13 votes

您可以使用reenableblog:post第二次执行任务。

您的示例呼叫reenable似乎有一个参数。 此参数可用作blog:post中的参数:

例:

require 'rake'
task 'mytask', :title do |tsk, args|
  p "called #{tsk} (#{args[:title]})"
end



Rake.application['mytask'].invoke('one')
Rake.application['mytask'].reenable
Rake.application['mytask'].invoke('two')

请用blog:post替换reenable,而任务定义则可以是rakefile require

此解决方案会将结果写入stdout-但您没有提到要抑制输出。


有趣的实验:

您也可以在任务定义内调用reenable。 这允许任务重新启用自己。

例:

require 'rake'
task 'mytask', :title do |tsk, args|
  p "called #{tsk} (#{args[:title]})"
  tsk.reenable  #<-- HERE
end

Rake.application['mytask'].invoke('one')
Rake.application['mytask'].invoke('two')

结果(用rake 10.4.2测试):

"called mytask (one)"
"called mytask (two)"
knut answered 2020-01-20T06:36:51Z
2 votes

在加载了Rails的脚本中(例如rails runner script.rb

def rake(*tasks)
  tasks.each do |task|
    Rake.application[task].tap(&:invoke).tap(&:reenable)
  end
end

rake('db:migrate', 'cache:clear', 'cache:warmup')
Dorian answered 2020-01-20T06:37:11Z
translate from https://stackoverflow.com:/questions/3530/how-do-i-run-rake-tasks-within-a-ruby-script