Class: Gem::Commands::CleanupCommand

Inherits:
Gem::Command show all
Defined in:
lib/rubygems/commands/cleanup_command.rb

Instance Attribute Summary

Attributes inherited from Gem::Command

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

Instance Method Summary collapse

Methods inherited from Gem::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

#alert, #alert_error, #alert_warning, #ask, #ask_for_password, #ask_yes_no, #choose_from_list, #say, #terminate_interaction

Methods included from DefaultUserInteraction

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

Constructor Details

#initializeCleanupCommand

Returns a new instance of CleanupCommand.



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

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

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

Instance Method Details

#argumentsObject

:nodoc:



17
18
19
# File 'lib/rubygems/commands/cleanup_command.rb', line 17

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

#defaults_strObject

:nodoc:



21
22
23
# File 'lib/rubygems/commands/cleanup_command.rb', line 21

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

#descriptionObject

:nodoc:



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

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



36
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
# File 'lib/rubygems/commands/cleanup_command.rb', line 36

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

  Gem::Specification.each do |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].map do |gem_name|
                        Gem::Specification.find_all_by_name gem_name
                      end.flatten
                    else
                      Gem::Specification.to_a
                    end

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

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

  deps = deplist.strongly_connected_components.flatten.reverse

  original_path = Gem.path

  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}",
      }

      uninstall_options[:user_install] = Gem.user_dir == spec.base_dir

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

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

    # Restore path Gem::Uninstaller may have change
    Gem.use_paths(*original_path)
  end

  say "Clean Up Complete"
end

#usageObject

:nodoc:



32
33
34
# File 'lib/rubygems/commands/cleanup_command.rb', line 32

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