2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/command'
|
|
|
|
require 'rubygems/source_index'
|
|
|
|
require 'rubygems/dependency_list'
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
class Gem::Commands::CleanupCommand < Gem::Command
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super 'cleanup',
|
2007-11-10 02:48:56 -05:00
|
|
|
'Clean up old versions of installed gems in the local repository',
|
2008-03-31 18:40:06 -04:00
|
|
|
:force => false, :test => false, :install_dir => Gem.dir
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
add_option('-d', '--dryrun', "") do |value, options|
|
|
|
|
options[:dryrun] = true
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def arguments # :nodoc:
|
|
|
|
"GEMNAME name of gem to cleanup"
|
|
|
|
end
|
|
|
|
|
|
|
|
def defaults_str # :nodoc:
|
|
|
|
"--no-dryrun"
|
|
|
|
end
|
|
|
|
|
|
|
|
def usage # :nodoc:
|
|
|
|
"#{program_name} [GEMNAME ...]"
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
say "Cleaning up installed gems..."
|
|
|
|
primary_gems = {}
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
Gem.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
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
gems_to_cleanup = []
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
unless options[:args].empty? then
|
|
|
|
options[:args].each do |gem_name|
|
|
|
|
specs = Gem.cache.search(/^#{gem_name}$/i)
|
|
|
|
specs.each do |spec|
|
|
|
|
gems_to_cleanup << spec
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
Gem.source_index.each do |name, spec|
|
|
|
|
gems_to_cleanup << spec
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
gems_to_cleanup = gems_to_cleanup.select { |spec|
|
|
|
|
primary_gems[spec.name].version != spec.version
|
|
|
|
}
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
uninstall_command = Gem::CommandManager.instance['uninstall']
|
|
|
|
deplist = Gem::DependencyList.new
|
|
|
|
gems_to_cleanup.uniq.each do |spec| deplist.add spec end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
deps = deplist.strongly_connected_components.flatten.reverse
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
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}"
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
options[:args] = [spec.name]
|
|
|
|
options[:version] = "= #{spec.version}"
|
|
|
|
options[:executables] = false
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
uninstaller = Gem::Uninstaller.new spec.name, options
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
begin
|
|
|
|
uninstaller.uninstall
|
|
|
|
rescue Gem::DependencyRemovalException,
|
|
|
|
Gem::GemNotInHomeException => e
|
|
|
|
say "Unable to uninstall #{spec.full_name}:"
|
|
|
|
say "\t#{e.class}: #{e.message}"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
say "Clean Up Complete"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
|