加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 22:22:38 所属栏目:百科 来源:网络整理
导读:有没有办法验证 Ruby程序内部是否有最新版本的gem?也就是说,有没有办法通过编程方式来捆绑过时的#{gemname}? 我试着看着bundler的源代码,但我找不到一个直接的方式.目前我正在做这件事,这是脆弱,缓慢,如此不善: IO.popen(%w{/usr/bin/env bundle outdated
有没有办法验证 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读