如何在Ruby脚本中运行Rake任务? [英] How do I run Rake tasks within a Ruby script?

查看:176
本文介绍了如何在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}

这篇关于如何在Ruby脚本中运行Rake任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆