/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
- require 'rubygems/command'
- require 'rubygems/format'
- require 'rubygems/installer'
- require 'rubygems/version_option'
- class Gem::Commands::PristineCommand < Gem::Command
- include Gem::VersionOption
- def initialize
- super 'pristine',
- 'Restores installed gems to pristine condition from files located in the gem cache',
- :version => Gem::Requirement.default
- add_option('--all',
- 'Restore all installed gems to pristine',
- 'condition') do |value, options|
- options[:all] = value
- end
- add_version_option('restore to', 'pristine condition')
- end
- def arguments # :nodoc:
- "GEMNAME gem to restore to pristine condition (unless --all)"
- end
- def defaults_str # :nodoc:
- "--all"
- end
- def description # :nodoc:
- <<-EOF
- The pristine command compares the installed gems with the contents of the
- cached gem and restores any files that don't match the cached gem's copy.
- If you have made modifications to your installed gems, the pristine command
- will revert them. After all the gem's files have been checked all bin stubs
- for the gem are regenerated.
- If the cached gem cannot be found, you will need to use `gem install` to
- revert the gem.
- EOF
- end
- def usage # :nodoc:
- "#{program_name} [args]"
- end
- def execute
- gem_name = nil
- specs = if options[:all] then
- Gem.source_index.map do |name, spec|
- spec
- end
- else
- gem_name = get_one_gem_name
- Gem.source_index.find_name(gem_name, options[:version])
- end
- if specs.empty? then
- raise Gem::Exception,
- "Failed to find gem #{gem_name} #{options[:version]}"
- end
- install_dir = Gem.dir # TODO use installer option
- raise Gem::FilePermissionError.new(install_dir) unless
- File.writable?(install_dir)
- say "Restoring gem(s) to pristine condition..."
- specs.each do |spec|
- gem = spec.cache_gem
- if gem.nil? then
- require 'rubygems/remote_fetcher'
- say "Cached gem for #{spec.full_name} not found, attempting to fetch..."
- dep = Gem::Dependency.new spec.name, spec.version
- Gem::RemoteFetcher.fetcher.download_to_cache dep
- gem = spec.cache_gem
- end
- # TODO use installer options
- installer = Gem::Installer.new gem, :wrappers => true, :force => true
- installer.install
- say "Restored #{spec.full_name}"
- end
- end
- end