Class Gem::Commands::SourcesCommand
In: lib/rubygems/commands/sources_command.rb
Parent: Gem::Command

Methods

defaults_str   execute   new  

Public Class methods

[Source]

    # File lib/rubygems/commands/sources_command.rb, line 9
 9:   def initialize
10:     super 'sources',
11:           'Manage the sources and cache file RubyGems uses to search for gems'
12: 
13:     add_option '-a', '--add SOURCE_URI', 'Add source' do |value, options|
14:       options[:add] = value
15:     end
16: 
17:     add_option '-l', '--list', 'List sources' do |value, options|
18:       options[:list] = value
19:     end
20: 
21:     add_option '-r', '--remove SOURCE_URI', 'Remove source' do |value, options|
22:       options[:remove] = value
23:     end
24: 
25:     add_option '-c', '--clear-all',
26:                'Remove all sources (clear the cache)' do |value, options|
27:       options[:clear_all] = value
28:     end
29: 
30:     add_option '-u', '--update', 'Update source cache' do |value, options|
31:       options[:update] = value
32:     end
33:   end

Public Instance methods

[Source]

    # File lib/rubygems/commands/sources_command.rb, line 35
35:   def defaults_str
36:     '--list'
37:   end

[Source]

     # File lib/rubygems/commands/sources_command.rb, line 39
 39:   def execute
 40:     options[:list] = !(options[:add] ||
 41:                        options[:clear_all] ||
 42:                        options[:remove] ||
 43:                        options[:update])
 44: 
 45:     if options[:clear_all] then
 46:       path = Gem::SpecFetcher.fetcher.dir
 47:       FileUtils.rm_rf path
 48: 
 49:       if not File.exist?(path) then
 50:         say "*** Removed specs cache ***"
 51:       elsif not File.writable?(path) then
 52:         say "*** Unable to remove source cache (write protected) ***"
 53:       else
 54:         say "*** Unable to remove source cache ***"
 55:       end
 56: 
 57:       sic = Gem::SourceInfoCache
 58:       remove_cache_file 'user',          sic.user_cache_file
 59:       remove_cache_file 'latest user',   sic.latest_user_cache_file
 60:       remove_cache_file 'system',        sic.system_cache_file
 61:       remove_cache_file 'latest system', sic.latest_system_cache_file
 62:     end
 63: 
 64:     if options[:add] then
 65:       source_uri = options[:add]
 66:       uri = URI.parse source_uri
 67: 
 68:       begin
 69:         Gem::SpecFetcher.fetcher.load_specs uri, 'specs'
 70:         Gem.sources << source_uri
 71:         Gem.configuration.write
 72: 
 73:         say "#{source_uri} added to sources"
 74:       rescue URI::Error, ArgumentError
 75:         say "#{source_uri} is not a URI"
 76:       rescue Gem::RemoteFetcher::FetchError => e
 77:         yaml_uri = uri + 'yaml'
 78:         gem_repo = Gem::RemoteFetcher.fetcher.fetch_size yaml_uri rescue false
 79: 
 80:         if e.uri =~ /specs\.#{Regexp.escape Gem.marshal_version}\.gz$/ and
 81:            gem_repo then
 82: 
 83:           alert_warning "RubyGems 1.2+ index not found for:\n\\t\#{source_uri}\n\nWill cause RubyGems to revert to legacy indexes, degrading performance.\n"
 84: 
 85:           say "#{source_uri} added to sources"
 86:         else
 87:           say "Error fetching #{source_uri}:\n\t#{e.message}"
 88:         end
 89:       end
 90:     end
 91: 
 92:     if options[:remove] then
 93:       source_uri = options[:remove]
 94: 
 95:       unless Gem.sources.include? source_uri then
 96:         say "source #{source_uri} not present in cache"
 97:       else
 98:         Gem.sources.delete source_uri
 99:         Gem.configuration.write
100: 
101:         say "#{source_uri} removed from sources"
102:       end
103:     end
104: 
105:     if options[:update] then
106:       fetcher = Gem::SpecFetcher.fetcher
107: 
108:       if fetcher.legacy_repos.empty? then
109:         Gem.sources.each do |update_uri|
110:           update_uri = URI.parse update_uri
111:           fetcher.load_specs update_uri, 'specs'
112:           fetcher.load_specs update_uri, 'latest_specs'
113:         end
114:       else
115:         Gem::SourceInfoCache.cache true
116:         Gem::SourceInfoCache.cache.flush
117:       end
118: 
119:       say "source cache successfully updated"
120:     end
121: 
122:     if options[:list] then
123:       say "*** CURRENT SOURCES ***"
124:       say
125: 
126:       Gem.sources.each do |source|
127:         say source
128:       end
129:     end
130:   end

[Validate]