2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/command'
|
|
|
|
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
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
Gem::Specification.each do |spec|
|
2008-03-31 18:40:06 -04:00
|
|
|
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
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
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
|
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
|
|
|
|
2011-08-04 21:00:01 -04:00
|
|
|
original_path = Gem.path
|
|
|
|
|
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}",
|
|
|
|
}
|
|
|
|
|
2011-08-04 21:00:01 -04:00
|
|
|
uninstall_options[:user_install] = Gem.user_dir == spec.base_dir
|
2009-06-09 17:38:59 -04:00
|
|
|
|
|
|
|
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,
|
2011-08-04 21:00:01 -04:00
|
|
|
Gem::GemNotInHomeException, Gem::FilePermissionError => e
|
2008-03-31 18:40:06 -04:00
|
|
|
say "Unable to uninstall #{spec.full_name}:"
|
|
|
|
say "\t#{e.class}: #{e.message}"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
2011-08-04 21:00:01 -04:00
|
|
|
|
|
|
|
# Restore path Gem::Uninstaller may have change
|
|
|
|
Gem.use_paths(*original_path)
|
2007-11-10 02:48:56 -05:00
|
|
|
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
|
|
|
|