PageRenderTime 134ms CodeModel.GetById 121ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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