如何在Ruby脚本中运行Rake任务?
问题描述:
我有一个 Rakefile
,带有一个Rake任务,通常我会从命令行调用它:
I have a Rakefile
with a Rake task that I would normally call from the command line:
rake blog:post Title
我想写一个Ruby脚本多次调用该Rake任务,但我看到的唯一解决方案是使用``(反引号)或 system
炮轰。
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
.
执行此操作的正确方法是什么?
What's the right way to do this?
答
来自 timocracy.com :
require 'rake'
def capture_stdout
s = StringIO.new
oldstdout = $stdout
$stdout = s
yield
s.string
ensure
$stdout = oldstdout
end
Rake.application.rake_require 'metric_fetcher', ['../../lib/tasks']
results = capture_stdout {Rake.application['metric_fetcher'].invoke}