Class: LibGems::Commands::CleanupCommand

Inherits:
LibGems::Command show all
Defined in:
lib/libgems/commands/cleanup_command.rb

Instance Attribute Summary

Attributes inherited from LibGems::Command

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

Instance Method Summary collapse

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

#initializeCleanupCommand

Returns a new instance of CleanupCommand.



8
9
10
11
12
13
14
15
16
# File 'lib/libgems/commands/cleanup_command.rb', line 8

def initialize
  super 'cleanup',
        'Clean up old versions of installed gems in the local repository',
        :force => false, :test => false, :install_dir => LibGems.dir

  add_option('-d', '--dryrun', "") do |value, options|
    options[:dryrun] = true
  end
end

Instance Method Details

#argumentsObject

:nodoc:



18
19
20
# File 'lib/libgems/commands/cleanup_command.rb', line 18

def arguments # :nodoc:
  "GEMNAME       name of gem to cleanup"
end

#defaults_strObject

:nodoc:



22
23
24
# File 'lib/libgems/commands/cleanup_command.rb', line 22

def defaults_str # :nodoc:
  "--no-dryrun"
end

#descriptionObject

:nodoc:



26
27
28
29
30
31
# File 'lib/libgems/commands/cleanup_command.rb', line 26

def description # :nodoc:
  <<-EOF
The cleanup command removes old gems from GEM_HOME.  If an older version is
installed elsewhere in GEM_PATH the cleanup command won't touch it.
  EOF
end

#executeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/libgems/commands/cleanup_command.rb', line 37

def execute
  say "Cleaning up installed gems..."
  primary_gems = {}

  LibGems.source_index.each do |name, spec|
    if primary_gems[spec.name].nil? or
       primary_gems[spec.name].version < spec.version then
      primary_gems[spec.name] = spec
    end
  end

  gems_to_cleanup = []

  unless options[:args].empty? then
    options[:args].each do |gem_name|
      dep = LibGems::Dependency.new gem_name, LibGems::Requirement.default
      specs = LibGems.source_index.search dep
      specs.each do |spec|
        gems_to_cleanup << spec
      end
    end
  else
    LibGems.source_index.each do |name, spec|
      gems_to_cleanup << spec
    end
  end

  gems_to_cleanup = gems_to_cleanup.select { |spec|
    primary_gems[spec.name].version != spec.version
  }

  deplist = LibGems::DependencyList.new
  gems_to_cleanup.uniq.each do |spec| deplist.add spec end

  deps = deplist.strongly_connected_components.flatten.reverse

  deps.each do |spec|
    if options[:dryrun] then
      say "Dry Run Mode: Would uninstall #{spec.full_name}"
    else
      say "Attempting to uninstall #{spec.full_name}"

      options[:args] = [spec.name]

      uninstall_options = {
        :executables => false,
        :version => "= #{spec.version}",
      }

      if LibGems.user_dir == spec.installation_path then
        uninstall_options[:install_dir] = spec.installation_path
      end

      uninstaller = LibGems::Uninstaller.new spec.name, uninstall_options

      begin
        uninstaller.uninstall
      rescue LibGems::DependencyRemovalException, LibGems::InstallError,
             LibGems::GemNotInHomeException => e
        say "Unable to uninstall #{spec.full_name}:"
        say "\t#{e.class}: #{e.message}"
      end
    end
  end

  say "Clean Up Complete"
end

#usageObject

:nodoc:



33
34
35
# File 'lib/libgems/commands/cleanup_command.rb', line 33

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