Created
April 29, 2013 20:03
-
-
Save anonymous/5484324 to your computer and use it in GitHub Desktop.
dogemcutter+http
This file contains hidden or 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/ruby | |
| # encoding: UTF-8 | |
| require 'optparse' | |
| require 'json' | |
| require 'uri' | |
| require 'uri/http' | |
| require 'uri/https' | |
| require 'net/http' | |
| require 'fileutils' | |
| require 'rubygems' | |
| require 'rubygems/remote_fetcher' | |
| require 'rubygems/spec_fetcher' | |
| proto=$0[/.*?\/do(.*)$/, 1] | |
| OptionParser.new do |opts| | |
| opts.banner = "Usage: #{$0} [options] <local directory> <URL>" | |
| opts.on_tail('-h', '--help', 'Show the help message') do | |
| puts opts | |
| if proto == 'gemcutter+http' or proto == 'gemcutter+https' | |
| puts " URL syntax: #{proto}://[USERNAME[:PASSWORD]@]SERVER[:PORT]" | |
| puts " (Recommended SERVER is rubygems.org)" | |
| else | |
| warn "URL syntax for #{proto} is unknown. This script will likely not work with the #{proto} protocol" | |
| end | |
| exit | |
| end | |
| end.parse! | |
| if ARGV.length != 2 | |
| warn "Missing required arguments. See --help for details." | |
| exit 1 | |
| end | |
| local = ARGV[0] | |
| remote = ARGV[1] | |
| # Do some URL parsing | |
| uri = URI(remote) | |
| scheme = nil | |
| if uri.scheme == 'gemcutter+http' | |
| scheme = uri.scheme = 'http' | |
| elsif uri.scheme == 'gemcutter+https' | |
| scheme = uri.scheme = 'https' | |
| end | |
| uri = URI(uri.to_s) | |
| if !scheme | |
| warn "Could not determine the URI scheme" | |
| exit 1 | |
| end | |
| ENV['HOME'] = local | |
| Gem::clear_paths() | |
| Gem.configuration.verbose = true | |
| Gem.sources = [uri.to_s] | |
| puts Gem.user_home | |
| puts Gem.sources | |
| STDERR.puts "Fetching gem spec list..." | |
| fetcher = Gem::SpecFetcher.fetcher | |
| list = fetcher.list(false) | |
| STDERR.puts "Gem spec list contains #{list.values[0].length} gems." | |
| STDERR.puts "Fetching gem specs..." | |
| packages = [] | |
| counter = 0 | |
| list.values[0].each do |tuple| | |
| counter += 1 | |
| STDERR.write "Packages: #{counter}\r" | |
| spec = fetcher.fetch_spec(tuple, list.keys[0]) | |
| packages.push({ | |
| 'name' => spec.name, | |
| 'version' => spec.version, | |
| 'authors' => spec.authors.join(', ').force_encoding('UTF-8'), | |
| 'gem_uri' => URI.join(uri, "/gems/#{spec.name}-#{spec.version}.gem"), | |
| 'homepage_uri' => spec.homepage, | |
| 'info' => spec.summary.force_encoding('UTF-8'), | |
| }) | |
| end | |
| STDERR.puts | |
| STDERR.puts "Writing JSON index" | |
| jsonfile = File.open(File.join(local, 'all_gems.json'), 'w') | |
| json = JSON.pretty_generate(packages) | |
| jsonfile.write(json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment