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, :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 37
37:   def execute
38:     say "Cleaning up installed gems..."
39:     primary_gems = {}
40: 
41:     Gem::Specification.each do |spec|
42:       if primary_gems[spec.name].nil? or
43:          primary_gems[spec.name].version < spec.version then
44:         primary_gems[spec.name] = spec
45:       end
46:     end
47: 
48:     gems_to_cleanup = unless options[:args].empty? then
49:                         options[:args].map do |gem_name|
50:                           Gem::Specification.find_all_by_name gem_name
51:                         end.flatten
52:                       else
53:                         Gem::Specification.to_a
54:                       end
55: 
56:     gems_to_cleanup = gems_to_cleanup.select { |spec|
57:       primary_gems[spec.name].version != spec.version
58:     }
59: 
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:     original_path = Gem.path
66: 
67:     deps.each do |spec|
68:       if options[:dryrun] then
69:         say "Dry Run Mode: Would uninstall #{spec.full_name}"
70:       else
71:         say "Attempting to uninstall #{spec.full_name}"
72: 
73:         options[:args] = [spec.name]
74: 
75:         uninstall_options = {
76:           :executables => false,
77:           :version => "= #{spec.version}",
78:         }
79: 
80:         uninstall_options[:user_install] = Gem.user_dir == spec.base_dir
81: 
82:         uninstaller = Gem::Uninstaller.new spec.name, uninstall_options
83: 
84:         begin
85:           uninstaller.uninstall
86:         rescue Gem::DependencyRemovalException, Gem::InstallError,
87:                Gem::GemNotInHomeException, Gem::FilePermissionError => e
88:           say "Unable to uninstall #{spec.full_name}:"
89:           say "\t#{e.class}: #{e.message}"
90:         end
91:       end
92: 
93:       # Restore path Gem::Uninstaller may have change
94:       Gem.use_paths(*original_path)
95:     end
96: 
97:     say "Clean Up Complete"
98:   end

[Validate]