2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2019-04-22 07:56:16 -04:00
|
|
|
require_relative '../command'
|
|
|
|
require_relative '../version_option'
|
|
|
|
require_relative '../uninstaller'
|
2013-09-14 04:59:02 -04:00
|
|
|
require 'fileutils'
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Gem uninstaller command line tool
|
|
|
|
#
|
|
|
|
# See `gem help uninstall`
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
class Gem::Commands::UninstallCommand < Gem::Command
|
|
|
|
include Gem::VersionOption
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def initialize
|
|
|
|
super 'uninstall', 'Uninstall gems from the local repository',
|
2012-11-29 01:52:18 -05:00
|
|
|
:version => Gem::Requirement.default, :user_install => true,
|
2014-09-13 23:30:02 -04:00
|
|
|
:check_dev => false, :vendor => false
|
2009-06-09 17:38:59 -04:00
|
|
|
|
|
|
|
add_option('-a', '--[no-]all',
|
|
|
|
'Uninstall all matching versions'
|
2018-10-21 20:27:02 -04:00
|
|
|
) do |value, options|
|
2009-06-09 17:38:59 -04:00
|
|
|
options[:all] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
add_option('-I', '--[no-]ignore-dependencies',
|
|
|
|
'Ignore dependency requirements while',
|
|
|
|
'uninstalling') do |value, options|
|
|
|
|
options[:ignore] = value
|
|
|
|
end
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
add_option('-D', '--[no-]check-development',
|
2012-11-29 01:52:18 -05:00
|
|
|
'Check development dependencies while uninstalling',
|
|
|
|
'(default: false)') do |value, options|
|
|
|
|
options[:check_dev] = value
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
add_option('-x', '--[no-]executables',
|
|
|
|
'Uninstall applicable executables without',
|
|
|
|
'confirmation') do |value, options|
|
|
|
|
options[:executables] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
add_option('-i', '--install-dir DIR',
|
|
|
|
'Directory to uninstall gem from') do |value, options|
|
|
|
|
options[:install_dir] = File.expand_path(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
add_option('-n', '--bindir DIR',
|
2018-08-27 06:05:04 -04:00
|
|
|
'Directory to remove executables from') do |value, options|
|
2009-06-09 17:38:59 -04:00
|
|
|
options[:bin_dir] = File.expand_path(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
add_option('--[no-]user-install',
|
|
|
|
'Uninstall from user\'s home directory',
|
|
|
|
'in addition to GEM_HOME.') do |value, options|
|
|
|
|
options[:user_install] = value
|
|
|
|
end
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
add_option('--[no-]format-executable',
|
|
|
|
'Assume executable names match Ruby\'s prefix and suffix.') do |value, options|
|
|
|
|
options[:format_executable] = value
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
add_option('--[no-]force',
|
|
|
|
'Uninstall all versions of the named gems',
|
|
|
|
'ignoring dependencies') do |value, options|
|
|
|
|
options[:force] = value
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
add_option('--[no-]abort-on-dependent',
|
|
|
|
'Prevent uninstalling gems that are',
|
|
|
|
'depended on by other gems.') do |value, options|
|
|
|
|
options[:abort_on_dependent] = value
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
add_version_option
|
|
|
|
add_platform_option
|
2014-09-13 23:30:02 -04:00
|
|
|
|
|
|
|
add_option('--vendor',
|
|
|
|
'Uninstall gem from the vendor directory.',
|
|
|
|
'Only for use by gem repackagers.') do |value, options|
|
2018-11-21 05:20:47 -05:00
|
|
|
unless Gem.vendor_dir
|
2014-09-13 23:30:02 -04:00
|
|
|
raise OptionParser::InvalidOption.new 'your platform is not supported'
|
|
|
|
end
|
|
|
|
|
|
|
|
alert_warning 'Use your OS package manager to uninstall vendor gems'
|
|
|
|
options[:vendor] = true
|
|
|
|
options[:install_dir] = Gem.vendor_dir
|
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def arguments # :nodoc:
|
|
|
|
"GEMNAME name of gem to uninstall"
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def defaults_str # :nodoc:
|
2012-12-06 00:26:46 -05:00
|
|
|
"--version '#{Gem::Requirement.default}' --no-force " +
|
2009-06-09 17:38:59 -04:00
|
|
|
"--user-install"
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def description # :nodoc:
|
|
|
|
<<-EOF
|
|
|
|
The uninstall command removes a previously installed gem.
|
|
|
|
|
|
|
|
RubyGems will ask for confirmation if you are attempting to uninstall a gem
|
|
|
|
that is a dependency of an existing gem. You can use the
|
|
|
|
--ignore-dependencies option to skip this check.
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def usage # :nodoc:
|
|
|
|
"#{program_name} GEMNAME [GEMNAME ...]"
|
|
|
|
end
|
|
|
|
|
2018-11-27 22:08:14 -05:00
|
|
|
def check_version # :nodoc:
|
|
|
|
if options[:version] != Gem::Requirement.default and
|
|
|
|
get_all_gem_names.size > 1
|
|
|
|
alert_error "Can't use --version with multiple gems. You can specify multiple gems with" \
|
|
|
|
" version requirements using `gem uninstall 'my_gem:1.0.0' 'my_other_gem:~>2.0.0'`"
|
|
|
|
terminate_interaction 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def execute
|
2018-11-27 22:08:14 -05:00
|
|
|
check_version
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if options[:all] and not options[:args].empty?
|
2013-09-18 17:29:41 -04:00
|
|
|
uninstall_specific
|
2018-11-21 05:20:47 -05:00
|
|
|
elsif options[:all]
|
2013-09-14 04:59:02 -04:00
|
|
|
uninstall_all
|
|
|
|
else
|
|
|
|
uninstall_specific
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def uninstall_all
|
2020-06-10 13:46:05 -04:00
|
|
|
specs = Gem::Specification.reject {|spec| spec.default_gem? }
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
specs.each do |spec|
|
|
|
|
options[:version] = spec.version
|
2018-05-30 09:01:35 -04:00
|
|
|
uninstall_gem spec.name
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
|
2020-04-05 07:42:08 -04:00
|
|
|
alert "Uninstalled all gems in #{options[:install_dir] || Gem.dir}"
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def uninstall_specific
|
2012-11-29 01:52:18 -05:00
|
|
|
deplist = Gem::DependencyList.new
|
2019-04-02 07:48:18 -04:00
|
|
|
original_gem_version = {}
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2018-11-27 22:08:14 -05:00
|
|
|
get_all_gem_names_and_versions.each do |name, version|
|
2019-04-02 07:48:18 -04:00
|
|
|
original_gem_version[name] = version || options[:version]
|
|
|
|
|
|
|
|
gem_specs = Gem::Specification.find_all_by_name(name, original_gem_version[name])
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
say("Gem '#{name}' is not installed") if gem_specs.empty?
|
|
|
|
gem_specs.each do |spec|
|
2012-11-29 01:52:18 -05:00
|
|
|
deplist.add spec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
deps = deplist.strongly_connected_components.flatten.reverse
|
2011-08-23 18:58:06 -04:00
|
|
|
|
2019-04-02 07:48:18 -04:00
|
|
|
gems_to_uninstall = {}
|
|
|
|
|
2018-11-27 22:08:14 -05:00
|
|
|
deps.each do |dep|
|
2019-04-02 07:48:18 -04:00
|
|
|
unless gems_to_uninstall[dep.name]
|
|
|
|
gems_to_uninstall[dep.name] = true
|
|
|
|
|
|
|
|
unless original_gem_version[dep.name] == Gem::Requirement.default
|
|
|
|
options[:version] = dep.version
|
|
|
|
end
|
|
|
|
|
|
|
|
uninstall_gem(dep.name)
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
def uninstall_gem(gem_name)
|
|
|
|
uninstall(gem_name)
|
|
|
|
rescue Gem::GemNotInHomeException => e
|
|
|
|
spec = e.spec
|
|
|
|
alert("In order to remove #{spec.name}, please execute:\n" +
|
|
|
|
"\tgem uninstall #{spec.name} --install-dir=#{spec.installation_path}")
|
|
|
|
rescue Gem::UninstallError => e
|
|
|
|
spec = e.spec
|
|
|
|
alert_error("Error: unable to successfully uninstall '#{spec.name}' which is " +
|
|
|
|
"located at '#{spec.full_gem_path}'. This is most likely because" +
|
|
|
|
"the current user does not have the appropriate permissions")
|
|
|
|
terminate_interaction 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def uninstall(gem_name)
|
|
|
|
Gem::Uninstaller.new(gem_name, options).uninstall
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|