PageRenderTime 24ms CodeModel.GetById 2ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/tools/Ruby/lib/ruby/site_ruby/1.8/rubygems/commands/sources_command.rb

http://github.com/agross/netopenspace
Ruby | 136 lines | 105 code | 31 blank | 0 comment | 6 complexity | 139a6e3d5c24ce55af15302dc90c53b9 MD5 | raw file
  1require 'rubygems/command'
  2require 'rubygems/remote_fetcher'
  3require 'rubygems/spec_fetcher'
  4require 'rubygems/local_remote_options'
  5
  6class Gem::Commands::SourcesCommand < Gem::Command
  7
  8  include Gem::LocalRemoteOptions
  9
 10  def initialize
 11    require 'fileutils'
 12
 13    super 'sources',
 14          'Manage the sources and cache file RubyGems uses to search for gems'
 15
 16    add_option '-a', '--add SOURCE_URI', 'Add source' do |value, options|
 17      options[:add] = value
 18    end
 19
 20    add_option '-l', '--list', 'List sources' do |value, options|
 21      options[:list] = value
 22    end
 23
 24    add_option '-r', '--remove SOURCE_URI', 'Remove source' do |value, options|
 25      options[:remove] = value
 26    end
 27
 28    add_option '-c', '--clear-all',
 29               'Remove all sources (clear the cache)' do |value, options|
 30      options[:clear_all] = value
 31    end
 32
 33    add_option '-u', '--update', 'Update source cache' do |value, options|
 34      options[:update] = value
 35    end
 36
 37    add_proxy_option
 38  end
 39
 40  def defaults_str
 41    '--list'
 42  end
 43
 44  def execute
 45    options[:list] = !(options[:add] ||
 46                       options[:clear_all] ||
 47                       options[:remove] ||
 48                       options[:update])
 49
 50    if options[:clear_all] then
 51      path = Gem::SpecFetcher.fetcher.dir
 52      FileUtils.rm_rf path
 53
 54      unless File.exist? path then
 55        say "*** Removed specs cache ***"
 56      else
 57        unless File.writable? path then
 58          say "*** Unable to remove source cache (write protected) ***"
 59        else
 60          say "*** Unable to remove source cache ***"
 61        end
 62
 63        terminate_interaction 1
 64      end
 65    end
 66
 67    if options[:add] then
 68      source_uri = options[:add]
 69      uri = URI.parse source_uri
 70
 71      begin
 72        Gem::SpecFetcher.fetcher.load_specs uri, 'specs'
 73        Gem.sources << source_uri
 74        Gem.configuration.write
 75
 76        say "#{source_uri} added to sources"
 77      rescue URI::Error, ArgumentError
 78        say "#{source_uri} is not a URI"
 79        terminate_interaction 1
 80      rescue Gem::RemoteFetcher::FetchError => e
 81        say "Error fetching #{source_uri}:\n\t#{e.message}"
 82        terminate_interaction 1
 83      end
 84    end
 85
 86    if options[:remove] then
 87      source_uri = options[:remove]
 88
 89      unless Gem.sources.include? source_uri then
 90        say "source #{source_uri} not present in cache"
 91      else
 92        Gem.sources.delete source_uri
 93        Gem.configuration.write
 94
 95        say "#{source_uri} removed from sources"
 96      end
 97    end
 98
 99    if options[:update] then
100      fetcher = Gem::SpecFetcher.fetcher
101
102      Gem.sources.each do |update_uri|
103        update_uri = URI.parse update_uri
104        fetcher.load_specs update_uri, 'specs'
105        fetcher.load_specs update_uri, 'latest_specs'
106      end
107
108      say "source cache successfully updated"
109    end
110
111    if options[:list] then
112      say "*** CURRENT SOURCES ***"
113      say
114
115      Gem.sources.each do |source|
116        say source
117      end
118    end
119  end
120
121  private
122
123  def remove_cache_file(desc, path)
124    FileUtils.rm_rf path
125
126    if not File.exist?(path) then
127      say "*** Removed #{desc} source cache ***"
128    elsif not File.writable?(path) then
129      say "*** Unable to remove #{desc} source cache (write protected) ***"
130    else
131      say "*** Unable to remove #{desc} source cache ***"
132    end
133  end
134
135end
136