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',
|
2013-08-26 16:24:51 -04:00
|
|
|
'Clean up old versions of installed gems',
|
2011-01-18 19:08:49 -05:00
|
|
|
:force => false, :install_dir => Gem.dir
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-07-09 19:21:36 -04:00
|
|
|
add_option('-n', '-d', '--dryrun',
|
|
|
|
'Do not uninstall gems') do |value, options|
|
2008-03-31 18:40:06 -04:00
|
|
|
options[:dryrun] = true
|
|
|
|
end
|
2013-01-04 17:58:15 -05:00
|
|
|
|
|
|
|
@candidate_gems = nil
|
|
|
|
@default_gems = []
|
|
|
|
@full = nil
|
|
|
|
@gems_to_cleanup = nil
|
|
|
|
@original_home = nil
|
|
|
|
@original_path = nil
|
|
|
|
@primary_gems = nil
|
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
|
|
|
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
|
2013-08-26 16:24:51 -04:00
|
|
|
The cleanup command removes old versions of gems from GEM_HOME that are not
|
|
|
|
required to meet a dependency. If a gem is installed elsewhere in GEM_PATH
|
|
|
|
the cleanup command won't delete it.
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
If no gems are named all gems in GEM_HOME are cleaned.
|
2009-06-09 17:38:59 -04:00
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def usage # :nodoc:
|
|
|
|
"#{program_name} [GEMNAME ...]"
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
say "Cleaning up installed gems..."
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
if options[:args].empty? then
|
|
|
|
done = false
|
|
|
|
last_set = nil
|
|
|
|
|
|
|
|
until done do
|
|
|
|
clean_gems
|
|
|
|
|
|
|
|
this_set = @gems_to_cleanup.map { |spec| spec.full_name }.sort
|
|
|
|
|
|
|
|
done = this_set.empty? || last_set == this_set
|
|
|
|
|
|
|
|
last_set = this_set
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2013-01-04 17:58:15 -05:00
|
|
|
else
|
|
|
|
clean_gems
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
say "Clean Up Complete"
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
if Gem.configuration.really_verbose then
|
|
|
|
skipped = @default_gems.map { |spec| spec.full_name }
|
|
|
|
|
|
|
|
say "Skipped default gems: #{skipped.join ', '}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_gems
|
|
|
|
get_primary_gems
|
|
|
|
get_candidate_gems
|
|
|
|
get_gems_to_cleanup
|
|
|
|
|
|
|
|
@full = Gem::DependencyList.from_specs
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
deplist = Gem::DependencyList.new
|
2013-01-04 17:58:15 -05:00
|
|
|
@gems_to_cleanup.each do |spec| deplist.add spec end
|
|
|
|
|
|
|
|
deps = deplist.strongly_connected_components.flatten
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
@original_home = Gem.dir
|
|
|
|
@original_path = Gem.path
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
deps.reverse_each do |spec|
|
|
|
|
uninstall_dep spec
|
|
|
|
end
|
2011-08-04 21:00:01 -04:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
Gem::Specification.reset
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
def get_candidate_gems
|
|
|
|
@candidate_gems = 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
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
def get_gems_to_cleanup
|
|
|
|
gems_to_cleanup = @candidate_gems.select { |spec|
|
|
|
|
@primary_gems[spec.name].version != spec.version
|
|
|
|
}
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
default_gems, gems_to_cleanup = gems_to_cleanup.partition { |spec|
|
|
|
|
spec.default_gem?
|
|
|
|
}
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
@default_gems += default_gems
|
|
|
|
@default_gems.uniq!
|
|
|
|
@gems_to_cleanup = gems_to_cleanup.uniq
|
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
def get_primary_gems
|
|
|
|
@primary_gems = {}
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
Gem::Specification.each do |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
|
2013-01-04 17:58:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def uninstall_dep spec
|
|
|
|
return unless @full.ok_to_remove?(spec.full_name)
|
2011-08-04 21:00:01 -04:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
if options[:dryrun] then
|
|
|
|
say "Dry Run Mode: Would uninstall #{spec.full_name}"
|
|
|
|
return
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
say "Attempting to uninstall #{spec.full_name}"
|
2012-12-08 01:01:49 -05:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
uninstall_options = {
|
|
|
|
:executables => false,
|
|
|
|
:version => "= #{spec.version}",
|
|
|
|
}
|
2012-12-08 01:01:49 -05:00
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
uninstall_options[:user_install] = Gem.user_dir == spec.base_dir
|
|
|
|
|
|
|
|
uninstaller = Gem::Uninstaller.new spec.name, uninstall_options
|
|
|
|
|
|
|
|
begin
|
|
|
|
uninstaller.uninstall
|
|
|
|
rescue Gem::DependencyRemovalException, Gem::InstallError,
|
|
|
|
Gem::GemNotInHomeException, Gem::FilePermissionError => e
|
|
|
|
say "Unable to uninstall #{spec.full_name}:"
|
|
|
|
say "\t#{e.class}: #{e.message}"
|
2012-12-08 01:01:49 -05:00
|
|
|
end
|
2013-01-04 17:58:15 -05:00
|
|
|
ensure
|
|
|
|
# Restore path Gem::Uninstaller may have changed
|
|
|
|
Gem.use_paths @original_home, *@original_path
|
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
|