我有一个带有Rake任务的Rakefile
,我通常会从命令行调用它:
rake blog:post Title
我想写一个Ruby脚本来多次调用Rake任务,但我看到的唯一解决方案是使用 (反号)或system
。
这样做的正确方法是什么?
发布于 2008-08-06 15:24:01
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}
发布于 2013-03-07 06:14:37
这适用于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
正如克努特所说,如果你想多次调用,可以使用reenable
。
发布于 2017-01-25 06:09:16
在加载了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')
https://stackoverflow.com/questions/3530
复制相似问题