一种避免外部执行:
对于捆绑的1.2.x
require 'bundler/cli'
# intercepting $stdout into a StringIO
old_stdout, $stdout = $stdout, StringIO.new
# running the same code run in the 'bundler outdated' utility
Bundler::CLI.new.outdated('rails')
# storing the output
output = $stdout.string
# restoring $stdout
$stdout = old_stdout
对于捆绑1.3。X
require 'bundler/cli'
require 'bundler/friendly_errors'
# let's cheat the CLI class with fake exit method
module Bundler
class CLI
desc 'exit', 'fake exit' # this is required by Thor
def exit(*); end # simply do nothing
end
end
# intercepting $stdout into a StringIO
old_stdout, $stdout = $stdout, StringIO.new
# running the same code run in the 'bundler outdated' utility
Bundler.with_friendly_errors { Bundler::CLI.start(['outdated', 'rails']) }
# storing the output
output = $stdout.string
# restoring $stdout
$stdout = old_stdout
谢谢大家,很多答案证实了我的发现:没有API来做到这一点。我选择了一个基于,它提供了一个工作的代码来完成这项任务。 – Pablo 2013-04-08 12:57:56
我认为你选择的答案有一些问题。有一个'exit 1'会终止你的程序执行。你是否能够获得该代码的工作?尝试时发生错误。 – Emil 2013-04-09 20:49:35
他们刚刚在版本1.2.x和1.3.x之间引入了'exit 1',所以现在需要一些猴子补丁来解决问题。 – 2013-04-09 21:50:40