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

Methods

defaults_str   execute   new  

Included Modules

Gem::LocalRemoteOptions

Public Class methods

[Source]

    # File lib/rubygems/commands/sources_command.rb, line 12
12:   def initialize
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

Public Instance methods

[Source]

    # File lib/rubygems/commands/sources_command.rb, line 40
40:   def defaults_str
41:     '--list'
42:   end

[Source]

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

[Validate]