从Ruby内部验证捆绑包的gem版本

前端之家收集整理的这篇文章主要介绍了从Ruby内部验证捆绑包的gem版本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法验证 Ruby程序内部是否有最新版本的gem?也就是说,有没有办法通过编程方式来捆绑过时的#{gemname}?

我试着看着bundler的源代码,但我找不到一个直接的方式.目前我正在做这件事,这是脆弱,缓慢,如此不善:

IO.popen(%w{/usr/bin/env bundle outdated gemname}) do |proc|
  output = proc.readlines.join("\n")
  return output.include?("Your bundle is up to date!")
end

解决方法

避免外部执行的一种方法

对于Bundler 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

对于bundler 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,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
原文链接:https://www.f2er.com/ruby/266099.html

猜你在找的Ruby相关文章