ruby 解析Gemfile.lock,从rubygems下载所有宝石,然后将它们上传到geminabox的本地实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 解析Gemfile.lock,从rubygems下载所有宝石,然后将它们上传到geminabox的本地实例相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env ruby

require 'rubygems'
require 'bundler'
require 'fileutils'
require 'net/http'
require 'net/https'
require 'uri'

TMP_DIR = "/tmp/gems"

FileUtils.rm_rf(TMP_DIR) if File.exists?(TMP_DIR)
FileUtils.mkdir TMP_DIR

GEMINABOX_SERVER = ""
GEMINABOX_USER   = ""
GEMINABOX_PASS   = ""

# inspect Gemfile.lock
lockfile = Bundler::LockfileParser.new(Bundler.read_file("Gemfile.lock"))

to_mirror = {}

uri = URI(GEMINABOX_SERVER)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == 'https'

lockfile.specs.each do |s|
  possible_gem_name = "#{s.name}-#{s.version.to_s}.gem"

  req = Net::HTTP::Head.new("/gems/#{possible_gem_name}")
  req.basic_auth GEMINABOX_USER, GEMINABOX_PASS

  response = http.request(req)

  if response.is_a? Net::HTTPNotFound
    puts "#{possible_gem_name} not found"
    to_mirror[s.name] = s.version.to_s
  elsif response.is_a? Net::HTTPOK
    puts "#{possible_gem_name} is already mirrored"
  else
    puts "Got an unexpected response for #{possible_gem_name}: #{response.body}"
  end

end

to_mirror.each do |name, version|
  puts "Going to download #{name} - #{version}"
  Dir.chdir TMP_DIR do
    cmd = "gem fetch #{name} -v #{version}"
    if !system(cmd)
      warn "Error while downloading #{name} - #{version}"
    end
  end
end

# push gems to local mirror
Dir['/tmp/gems/*gem'].each do |gem|
  cmd = "gem inabox #{gem}"
  puts cmd
  system cmd
end

以上是关于ruby 解析Gemfile.lock,从rubygems下载所有宝石,然后将它们上传到geminabox的本地实例的主要内容,如果未能解决你的问题,请参考以下文章

ruby Gemfile.lock的

ruby 在Gemfile.lock中指定git分支

Github之深入解析Ruby Gems缓存

Rails:Gemfile.lock 需要“找不到捆绑程序”(2.2.11)。 (Gem::GemNotFoundException)

手动测试Ruby CLI

net/http gem 不在 Ruby 的默认库中吗?