2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
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-09-14 04:59:02 -04:00
|
|
|
'Clean up old versions of installed gems',
|
2017-11-28 17:30:28 -05:00
|
|
|
:force => false, :install_dir => Gem.dir,
|
|
|
|
:check_dev => true
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-09-14 04:59:02 -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
|
|
|
|
2017-11-28 17:30:28 -05:00
|
|
|
add_option('-D', '--[no-]check-development',
|
|
|
|
'Check development dependencies while uninstalling',
|
|
|
|
'(default: true)') do |value, options|
|
|
|
|
options[:check_dev] = value
|
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
add_option('--[no-]user-install',
|
|
|
|
'Cleanup in user\'s home directory instead',
|
|
|
|
'of GEM_HOME.') do |value, options|
|
|
|
|
options[:user_install] = value
|
|
|
|
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-09-14 04:59:02 -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-09-14 04:59:02 -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
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if options[:args].empty?
|
2013-01-04 17:58:15 -05:00
|
|
|
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
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
say "Clean up complete"
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
verbose do
|
2013-01-04 17:58:15 -05:00
|
|
|
skipped = @default_gems.map { |spec| spec.full_name }
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
"Skipped default gems: #{skipped.join ', '}"
|
2013-01-04 17:58:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_gems
|
2016-03-03 19:29:40 -05:00
|
|
|
@original_home = Gem.dir
|
|
|
|
@original_path = Gem.path
|
|
|
|
|
2013-01-04 17:58:15 -05:00
|
|
|
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
|
2019-02-14 07:59:03 -05:00
|
|
|
@gems_to_cleanup.each { |spec| deplist.add spec }
|
2013-01-04 17:58:15 -05:00
|
|
|
|
|
|
|
deps = deplist.strongly_connected_components.flatten
|
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
|
2018-11-21 05:20:47 -05:00
|
|
|
@candidate_gems = unless options[:args].empty?
|
2013-01-04 17:58:15 -05:00
|
|
|
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
|
2019-02-14 07:59:03 -05:00
|
|
|
gems_to_cleanup = @candidate_gems.select do |spec|
|
2013-01-04 17:58:15 -05:00
|
|
|
@primary_gems[spec.name].version != spec.version
|
2019-02-14 07:59:03 -05:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2019-02-14 07:59:03 -05:00
|
|
|
default_gems, gems_to_cleanup = gems_to_cleanup.partition do |spec|
|
2013-01-04 17:58:15 -05:00
|
|
|
spec.default_gem?
|
2019-02-14 07:59:03 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
uninstall_from = options[:user_install] ? Gem.user_dir : @original_home
|
|
|
|
|
2019-02-14 07:59:03 -05:00
|
|
|
gems_to_cleanup = gems_to_cleanup.select do |spec|
|
2018-08-27 06:05:04 -04:00
|
|
|
spec.base_dir == uninstall_from
|
2019-02-14 07:59:03 -05:00
|
|
|
end
|
2016-03-03 19:29:40 -05: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
|
2018-11-21 05:20:47 -05:00
|
|
|
@primary_gems[spec.name].version < spec.version
|
2013-01-04 17:58:15 -05:00
|
|
|
@primary_gems[spec.name] = spec
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2013-01-04 17:58:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def uninstall_dep(spec)
|
2017-11-28 17:30:28 -05:00
|
|
|
return unless @full.ok_to_remove?(spec.full_name, options[:check_dev])
|
2011-08-04 21:00:01 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if options[:dryrun]
|
2013-01-04 17:58:15 -05:00
|
|
|
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
|