Created
June 1, 2014 20:47
-
-
Save carlossanchezp/22b46d9d590c31b7c1d5 to your computer and use it in GitHub Desktop.
Read Gemfile.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment