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

http://github.com/agross/netopenspace · Ruby · 95 lines · 86 code · 9 blank · 0 comment · 0 complexity · 821e261242ac8a6fe4bc65637f3655f3 MD5 · raw file

  1. require 'rubygems/command'
  2. require 'rubygems/format'
  3. require 'rubygems/installer'
  4. require 'rubygems/version_option'
  5. class Gem::Commands::PristineCommand < Gem::Command
  6. include Gem::VersionOption
  7. def initialize
  8. super 'pristine',
  9. 'Restores installed gems to pristine condition from files located in the gem cache',
  10. :version => Gem::Requirement.default
  11. add_option('--all',
  12. 'Restore all installed gems to pristine',
  13. 'condition') do |value, options|
  14. options[:all] = value
  15. end
  16. add_version_option('restore to', 'pristine condition')
  17. end
  18. def arguments # :nodoc:
  19. "GEMNAME gem to restore to pristine condition (unless --all)"
  20. end
  21. def defaults_str # :nodoc:
  22. "--all"
  23. end
  24. def description # :nodoc:
  25. <<-EOF
  26. The pristine command compares the installed gems with the contents of the
  27. cached gem and restores any files that don't match the cached gem's copy.
  28. If you have made modifications to your installed gems, the pristine command
  29. will revert them. After all the gem's files have been checked all bin stubs
  30. for the gem are regenerated.
  31. If the cached gem cannot be found, you will need to use `gem install` to
  32. revert the gem.
  33. EOF
  34. end
  35. def usage # :nodoc:
  36. "#{program_name} [args]"
  37. end
  38. def execute
  39. gem_name = nil
  40. specs = if options[:all] then
  41. Gem.source_index.map do |name, spec|
  42. spec
  43. end
  44. else
  45. gem_name = get_one_gem_name
  46. Gem.source_index.find_name(gem_name, options[:version])
  47. end
  48. if specs.empty? then
  49. raise Gem::Exception,
  50. "Failed to find gem #{gem_name} #{options[:version]}"
  51. end
  52. install_dir = Gem.dir # TODO use installer option
  53. raise Gem::FilePermissionError.new(install_dir) unless
  54. File.writable?(install_dir)
  55. say "Restoring gem(s) to pristine condition..."
  56. specs.each do |spec|
  57. gem = spec.cache_gem
  58. if gem.nil? then
  59. require 'rubygems/remote_fetcher'
  60. say "Cached gem for #{spec.full_name} not found, attempting to fetch..."
  61. dep = Gem::Dependency.new spec.name, spec.version
  62. Gem::RemoteFetcher.fetcher.download_to_cache dep
  63. gem = spec.cache_gem
  64. end
  65. # TODO use installer options
  66. installer = Gem::Installer.new gem, :wrappers => true, :force => true
  67. installer.install
  68. say "Restored #{spec.full_name}"
  69. end
  70. end
  71. end