Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edubkendo/fa23d119dd1cf43d52a9 to your computer and use it in GitHub Desktop.
Save edubkendo/fa23d119dd1cf43d52a9 to your computer and use it in GitHub Desktop.
#!/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