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

时间:2021-12-02 01:10:41

I have a Rakefile with a Rake task that I would normally call from the command line:

我有一个带有Rake任务的Rakefile,我通常会从命令行调用它:

rake blog:post Title

I'd like to write a Ruby script that calls that Rake task multiple times, but the only solution I see is shelling out using `` (backticks) or system.

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

What's the right way to do this?

正确的做法是什么?

4 个解决方案

#1


43  

from timocracy.com:

从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}

#2


17  

This works with Rake version 10.0.3:

这适用于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

As knut said, use reenable if you want to invoke multiple times.

正如knut所说,如果您希望多次调用,请使用reenable。

#3


13  

You can use invoke and reenable to execute the task a second time.

您可以使用invoke和reenable第二次执行任务。

Your example call rake blog:post Title seems to have a parameter. This parameter can be used as a parameter in invoke:

您的示例调用rake blog:post Title似乎有一个参数。此参数可作为调用中的参数:

Example:

例子:

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')

Please replace mytask with blog:post and instead the task definition you can require your rakefile.

请将mytask替换为blog:post,并替换需要rakefile的任务定义。

This solution will write the result to stdout - but you did not mention, that you want to suppress output.

这个解决方案将把结果写入stdout—但是您没有提到要抑制输出。


Interesting experiment:

有趣的实验:

You can call the reenable also inside the task definition. This allows a task to reenable himself.

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

Example:

例子:

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')

The result (tested with rake 10.4.2):

结果(用rake 10.4.2测试):

"called mytask (one)"
"called mytask (two)"

#4


1  

In a script with Rails loaded (e.g. rails runner script.rb)

在加载了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')

#1


43  

from timocracy.com:

从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}

#2


17  

This works with Rake version 10.0.3:

这适用于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

As knut said, use reenable if you want to invoke multiple times.

正如knut所说,如果您希望多次调用,请使用reenable。

#3


13  

You can use invoke and reenable to execute the task a second time.

您可以使用invoke和reenable第二次执行任务。

Your example call rake blog:post Title seems to have a parameter. This parameter can be used as a parameter in invoke:

您的示例调用rake blog:post Title似乎有一个参数。此参数可作为调用中的参数:

Example:

例子:

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')

Please replace mytask with blog:post and instead the task definition you can require your rakefile.

请将mytask替换为blog:post,并替换需要rakefile的任务定义。

This solution will write the result to stdout - but you did not mention, that you want to suppress output.

这个解决方案将把结果写入stdout—但是您没有提到要抑制输出。


Interesting experiment:

有趣的实验:

You can call the reenable also inside the task definition. This allows a task to reenable himself.

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

Example:

例子:

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')

The result (tested with rake 10.4.2):

结果(用rake 10.4.2测试):

"called mytask (one)"
"called mytask (two)"

#4


1  

In a script with Rails loaded (e.g. rails runner script.rb)

在加载了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')