Class: LibGems::Commands::PristineCommand

Inherits:
LibGems::Command show all
Includes:
VersionOption
Defined in:
lib/libgems/commands/pristine_command.rb

Instance Attribute Summary

Attributes inherited from LibGems::Command

#command, #defaults, #options, #program_name, #summary

Instance Method Summary collapse

Methods included from VersionOption

#add_platform_option, #add_prerelease_option, #add_version_option

Methods inherited from LibGems::Command

add_common_option, #add_extra_args, #add_option, add_specific_extra_args, #begins?, build_args, build_args=, common_options, extra_args, extra_args=, #get_all_gem_names, #get_one_gem_name, #get_one_optional_argument, #handle_options, #handles?, #invoke, #merge_options, #remove_option, #show_help, #show_lookup_failure, specific_extra_args, specific_extra_args_hash, #when_invoked

Methods included from UserInteraction

#methname

Methods included from DefaultUserInteraction

ui, #ui, ui=, #ui=, use_ui, #use_ui

Constructor Details

#initializePristineCommand

Returns a new instance of PristineCommand.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/libgems/commands/pristine_command.rb', line 11

def initialize
  super 'pristine',
        'Restores installed gems to pristine condition from files located in the gem cache',
        :version => LibGems::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

Instance Method Details

#argumentsObject

:nodoc:



25
26
27
# File 'lib/libgems/commands/pristine_command.rb', line 25

def arguments # :nodoc:
  "GEMNAME       gem to restore to pristine condition (unless --all)"
end

#defaults_strObject

:nodoc:



29
30
31
# File 'lib/libgems/commands/pristine_command.rb', line 29

def defaults_str # :nodoc:
  "--all"
end

#descriptionObject

:nodoc:



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/libgems/commands/pristine_command.rb', line 33

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

#executeObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/libgems/commands/pristine_command.rb', line 51

def execute
  gem_name = nil

  specs = if options[:all] then
            LibGems::SourceIndex.from_installed_gems.map do |name, spec|
              spec
            end
          else
            gem_name = get_one_gem_name
            LibGems::SourceIndex.from_installed_gems.find_name(gem_name,
                                                        options[:version])
          end

  if specs.empty? then
    raise LibGems::Exception,
          "Failed to find gem #{gem_name} #{options[:version]}"
  end

  install_dir = LibGems.dir # TODO use installer option

  raise LibGems::FilePermissionError.new(install_dir) unless
    File.writable?(install_dir)

  say "Restoring gem(s) to pristine condition..."

  specs.each do |spec|
    gem = Dir[File.join(LibGems.dir, 'cache', spec.file_name)].first

    if gem.nil? then
      alert_error "Cached gem for #{spec.full_name} not found, use `gem install` to restore"
      next
    end

    # TODO use installer options
    installer = LibGems::Installer.new gem, :wrappers => true, :force => true
    installer.install

    say "Restored #{spec.full_name}"
  end
end

#usageObject

:nodoc:



47
48
49
# File 'lib/libgems/commands/pristine_command.rb', line 47

def usage # :nodoc:
  "#{program_name} [args]"
end