/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

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