2011-01-18 19:08:49 -05:00
|
|
|
######################################################################
|
|
|
|
# This file is imported from the rubygems project.
|
|
|
|
# DO NOT make modifications in this repo. They _will_ be reverted!
|
|
|
|
# File a patch instead and assign it to Ryan Davis or Eric Hodel.
|
|
|
|
######################################################################
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/command'
|
|
|
|
require 'rubygems/source_index'
|
|
|
|
require 'rubygems/dependency_list'
|
2009-06-09 17:38:59 -04:00
|
|
|
require 'rubygems/uninstaller'
|
2007-11-10 02:48:56 -05:00
|
|
|
|
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',
|
2011-01-18 19:08:49 -05:00
|
|
|
:force => 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
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
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
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
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|
|
2009-06-09 17:38:59 -04:00
|
|
|
dep = Gem::Dependency.new gem_name, Gem::Requirement.default
|
|
|
|
specs = Gem.source_index.search dep
|
2008-03-31 18:40:06 -04:00
|
|
|
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
|
|
|
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]
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
uninstall_options = {
|
|
|
|
:executables => false,
|
|
|
|
:version => "= #{spec.version}",
|
|
|
|
}
|
|
|
|
|
|
|
|
if Gem.user_dir == spec.installation_path then
|
|
|
|
uninstall_options[:install_dir] = spec.installation_path
|
|
|
|
end
|
|
|
|
|
|
|
|
uninstaller = Gem::Uninstaller.new spec.name, uninstall_options
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
begin
|
|
|
|
uninstaller.uninstall
|
2009-06-09 17:38:59 -04:00
|
|
|
rescue Gem::DependencyRemovalException, Gem::InstallError,
|
2008-03-31 18:40:06 -04:00
|
|
|
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
|
|
|
|