Class Gem::Commands::CleanupCommand
In: lib/rubygems/commands/cleanup_command.rb
Parent: Gem::Command

Methods

execute   new  

Public Class methods

[Source]

    # File lib/rubygems/commands/cleanup_command.rb, line 7
 7:   def initialize
 8:     super 'cleanup',
 9:           'Clean up old versions of installed gems in the local repository',
10:           :force => false, :test => false, :install_dir => Gem.dir
11: 
12:     add_option('-d', '--dryrun', "") do |value, options|
13:       options[:dryrun] = true
14:     end
15:   end

Public Instance methods

[Source]

    # File lib/rubygems/commands/cleanup_command.rb, line 29
29:   def execute
30:     say "Cleaning up installed gems..."
31:     primary_gems = {}
32: 
33:     Gem.source_index.each do |name, spec|
34:       if primary_gems[spec.name].nil? or
35:          primary_gems[spec.name].version < spec.version then
36:         primary_gems[spec.name] = spec
37:       end
38:     end
39: 
40:     gems_to_cleanup = []
41: 
42:     unless options[:args].empty? then
43:       options[:args].each do |gem_name|
44:         specs = Gem.cache.search(/^#{gem_name}$/i)
45:         specs.each do |spec|
46:           gems_to_cleanup << spec
47:         end
48:       end
49:     else
50:       Gem.source_index.each do |name, spec|
51:         gems_to_cleanup << spec
52:       end
53:     end
54: 
55:     gems_to_cleanup = gems_to_cleanup.select { |spec|
56:       primary_gems[spec.name].version != spec.version
57:     }
58: 
59:     uninstall_command = Gem::CommandManager.instance['uninstall']
60:     deplist = Gem::DependencyList.new
61:     gems_to_cleanup.uniq.each do |spec| deplist.add spec end
62: 
63:     deps = deplist.strongly_connected_components.flatten.reverse
64: 
65:     deps.each do |spec|
66:       if options[:dryrun] then
67:         say "Dry Run Mode: Would uninstall #{spec.full_name}"
68:       else
69:         say "Attempting to uninstall #{spec.full_name}"
70: 
71:         options[:args] = [spec.name]
72:         options[:version] = "= #{spec.version}"
73:         options[:executables] = false
74: 
75:         uninstaller = Gem::Uninstaller.new spec.name, options
76: 
77:         begin
78:           uninstaller.uninstall
79:         rescue Gem::DependencyRemovalException,
80:                Gem::GemNotInHomeException => e
81:           say "Unable to uninstall #{spec.full_name}:"
82:           say "\t#{e.class}: #{e.message}"
83:         end
84:       end
85:     end
86: 
87:     say "Clean Up Complete"
88:   end

[Validate]