| Class | Gem::Commands::SourcesCommand |
| In: |
lib/rubygems/commands/sources_command.rb
|
| Parent: | Gem::Command |
# File lib/rubygems/commands/sources_command.rb, line 8
8: def initialize
9: super 'sources',
10: 'Manage the sources and cache file RubyGems uses to search for gems'
11:
12: add_option '-a', '--add SOURCE_URI', 'Add source' do |value, options|
13: options[:add] = value
14: end
15:
16: add_option '-l', '--list', 'List sources' do |value, options|
17: options[:list] = value
18: end
19:
20: add_option '-r', '--remove SOURCE_URI', 'Remove source' do |value, options|
21: options[:remove] = value
22: end
23:
24: add_option '-u', '--update', 'Update source cache' do |value, options|
25: options[:update] = 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: end
# File lib/rubygems/commands/sources_command.rb, line 38
38: def execute
39: options[:list] = !(options[:add] || options[:remove] || options[:clear_all] || options[:update])
40:
41: if options[:clear_all] then
42: sic = Gem::SourceInfoCache
43: remove_cache_file 'user', sic.user_cache_file
44: remove_cache_file 'latest user', sic.latest_user_cache_file
45: remove_cache_file 'system', sic.system_cache_file
46: remove_cache_file 'latest system', sic.latest_system_cache_file
47: end
48:
49: if options[:add] then
50: source_uri = options[:add]
51:
52: sice = Gem::SourceInfoCacheEntry.new nil, nil
53: begin
54: sice.refresh source_uri, true
55:
56: Gem::SourceInfoCache.cache_data[source_uri] = sice
57: Gem::SourceInfoCache.cache.update
58: Gem::SourceInfoCache.cache.flush
59:
60: Gem.sources << source_uri
61: Gem.configuration.write
62:
63: say "#{source_uri} added to sources"
64: rescue URI::Error, ArgumentError
65: say "#{source_uri} is not a URI"
66: rescue Gem::RemoteFetcher::FetchError => e
67: say "Error fetching #{source_uri}:\n\t#{e.message}"
68: end
69: end
70:
71: if options[:update] then
72: Gem::SourceInfoCache.cache true
73: Gem::SourceInfoCache.cache.flush
74:
75: say "source cache successfully updated"
76: end
77:
78: if options[:remove] then
79: source_uri = options[:remove]
80:
81: unless Gem.sources.include? source_uri then
82: say "source #{source_uri} not present in cache"
83: else
84: begin # HACK figure out how to get the cache w/o update
85: Gem::SourceInfoCache.cache
86: rescue Gem::RemoteFetcher::FetchError
87: end
88:
89: Gem::SourceInfoCache.cache_data.delete source_uri
90: Gem::SourceInfoCache.cache.update
91: Gem::SourceInfoCache.cache.flush
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[:list] then
100: say "*** CURRENT SOURCES ***"
101: say
102:
103: Gem.sources.each do |source|
104: say source
105: end
106: end
107: end