2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2007-11-10 03:05:00 -05:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
|
|
|
require 'fileutils'
|
|
|
|
require 'rubygems'
|
2020-01-31 21:14:04 -05:00
|
|
|
require 'rubygems/installer_uninstaller_utils'
|
2007-11-10 03:05:00 -05:00
|
|
|
require 'rubygems/dependency_list'
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/rdoc'
|
2007-11-10 03:05:00 -05:00
|
|
|
require 'rubygems/user_interaction'
|
|
|
|
|
|
|
|
##
|
|
|
|
# An Uninstaller.
|
2009-06-09 17:38:59 -04:00
|
|
|
#
|
|
|
|
# The uninstaller fires pre and post uninstall hooks. Hooks can be added
|
|
|
|
# either through a rubygems_plugin.rb file in an installed gem or via a
|
|
|
|
# rubygems/defaults/#{RUBY_ENGINE}.rb or rubygems/defaults/operating_system.rb
|
|
|
|
# file. See Gem.pre_uninstall and Gem.post_uninstall for details.
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2007-11-10 03:05:00 -05:00
|
|
|
class Gem::Uninstaller
|
|
|
|
|
|
|
|
include Gem::UserInteraction
|
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
include Gem::InstallerUninstallerUtils
|
|
|
|
|
2007-11-10 03:05:00 -05:00
|
|
|
##
|
2008-09-25 06:13:50 -04:00
|
|
|
# The directory a gem's executables will be installed into
|
|
|
|
|
|
|
|
attr_reader :bin_dir
|
|
|
|
|
|
|
|
##
|
|
|
|
# The gem repository the gem will be installed into
|
|
|
|
|
|
|
|
attr_reader :gem_home
|
|
|
|
|
|
|
|
##
|
|
|
|
# The Gem::Specification for the gem being uninstalled, only set during
|
|
|
|
# #uninstall_gem
|
|
|
|
|
|
|
|
attr_reader :spec
|
|
|
|
|
|
|
|
##
|
|
|
|
# Constructs an uninstaller that will uninstall +gem+
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
def initialize(gem, options = {})
|
2012-11-29 01:52:18 -05:00
|
|
|
# TODO document the valid options
|
2013-09-14 04:59:02 -04:00
|
|
|
@gem = gem
|
|
|
|
@version = options[:version] || Gem::Requirement.default
|
2019-04-26 07:26:21 -04:00
|
|
|
@gem_home = File.realpath(options[:install_dir] || Gem.dir)
|
2020-03-24 02:39:24 -04:00
|
|
|
@plugins_dir = Gem.plugindir(@gem_home)
|
2013-09-14 04:59:02 -04:00
|
|
|
@force_executables = options[:executables]
|
|
|
|
@force_all = options[:all]
|
|
|
|
@force_ignore = options[:ignore]
|
|
|
|
@bin_dir = options[:bin_dir]
|
|
|
|
@format_executable = options[:format_executable]
|
|
|
|
@abort_on_dependent = options[:abort_on_dependent]
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
# Indicate if development dependencies should be checked when
|
|
|
|
# uninstalling. (default: false)
|
|
|
|
#
|
2019-02-14 07:59:03 -05:00
|
|
|
@check_dev = options[:check_dev]
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
if options[:force]
|
|
|
|
@force_all = true
|
|
|
|
@force_ignore = true
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
# only add user directory if install_dir is not set
|
|
|
|
@user_install = false
|
|
|
|
@user_install = options[:user_install] unless options[:install_dir]
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2008-09-25 06:13:50 -04:00
|
|
|
# Performs the uninstall of the gem. This removes the spec, the Gem
|
|
|
|
# directory, and the cached .gem file.
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2007-11-10 03:05:00 -05:00
|
|
|
def uninstall
|
2012-12-19 02:19:10 -05:00
|
|
|
dependency = Gem::Dependency.new @gem, @version
|
|
|
|
|
|
|
|
list = []
|
|
|
|
|
|
|
|
dirs =
|
|
|
|
Gem::Specification.dirs +
|
2019-07-22 08:31:10 -04:00
|
|
|
[Gem.default_specifications_dir]
|
2012-12-19 02:19:10 -05:00
|
|
|
|
|
|
|
Gem::Specification.each_spec dirs do |spec|
|
|
|
|
next unless dependency.matches_spec? spec
|
|
|
|
|
|
|
|
list << spec
|
|
|
|
end
|
2007-11-10 03:05:00 -05:00
|
|
|
|
2019-08-21 14:05:24 -04:00
|
|
|
if list.empty?
|
|
|
|
raise Gem::InstallError, "gem #{@gem.inspect} is not installed"
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
default_specs, list = list.partition do |spec|
|
|
|
|
spec.default_gem?
|
|
|
|
end
|
|
|
|
|
2019-08-21 14:17:11 -04:00
|
|
|
default_specs.each do |default_spec|
|
|
|
|
say "Gem #{default_spec.full_name} cannot be uninstalled because it is a default gem"
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
list, other_repo_specs = list.partition do |spec|
|
|
|
|
@gem_home == spec.base_dir or
|
2012-12-19 02:19:10 -05:00
|
|
|
(@user_install and spec.base_dir == Gem.user_dir)
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
list.sort!
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if list.empty?
|
2019-08-21 14:17:11 -04:00
|
|
|
return unless other_repo_specs.any?
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
other_repos = other_repo_specs.map { |spec| spec.base_dir }.uniq
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
message = ["#{@gem} is not installed in GEM_HOME, try:"]
|
|
|
|
message.concat other_repos.map { |repo|
|
|
|
|
"\tgem uninstall -i #{repo} #{@gem}"
|
|
|
|
}
|
|
|
|
|
|
|
|
raise Gem::InstallError, message.join("\n")
|
2018-11-21 05:20:47 -05:00
|
|
|
elsif @force_all
|
2011-05-31 23:45:05 -04:00
|
|
|
remove_all list
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
elsif list.size > 1
|
2014-09-13 23:30:02 -04:00
|
|
|
gem_names = list.map { |gem| gem.full_name }
|
|
|
|
gem_names << "All versions"
|
2008-09-25 06:13:50 -04:00
|
|
|
|
|
|
|
say
|
2010-11-08 15:58:42 -05:00
|
|
|
_, index = choose_from_list "Select gem to uninstall:", gem_names
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if index == list.size
|
2011-05-31 23:45:05 -04:00
|
|
|
remove_all list
|
2018-11-21 05:20:47 -05:00
|
|
|
elsif index >= 0 && index < list.size
|
2011-05-31 23:45:05 -04:00
|
|
|
uninstall_gem list[index]
|
2007-11-10 03:05:00 -05:00
|
|
|
else
|
2019-02-14 07:59:03 -05:00
|
|
|
say "Error: must enter a number [1-#{list.size + 1}]"
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
else
|
2011-05-31 23:45:05 -04:00
|
|
|
uninstall_gem list.first
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
end
|
2008-06-25 22:06:00 -04:00
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
##
|
|
|
|
# Uninstalls gem +spec+
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def uninstall_gem(spec)
|
2008-09-25 06:13:50 -04:00
|
|
|
@spec = spec
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
unless dependencies_ok? spec
|
2013-09-14 04:59:02 -04:00
|
|
|
if abort_on_dependent? || !ask_if_ok(spec)
|
2011-03-01 04:41:32 -05:00
|
|
|
raise Gem::DependencyRemovalException,
|
|
|
|
"Uninstallation aborted due to dependent gem(s)"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
Gem.pre_uninstall_hooks.each do |hook|
|
|
|
|
hook.call self
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
remove_executables @spec
|
2020-01-31 21:14:04 -05:00
|
|
|
remove_plugins @spec
|
2011-05-31 23:45:05 -04:00
|
|
|
remove @spec
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
regenerate_plugins
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
Gem.post_uninstall_hooks.each do |hook|
|
|
|
|
hook.call self
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec = nil
|
|
|
|
end
|
|
|
|
|
2007-11-10 03:05:00 -05:00
|
|
|
##
|
2020-01-31 21:14:04 -05:00
|
|
|
# Removes installed executables and batch files (windows only) for +spec+.
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def remove_executables(spec)
|
2020-01-31 21:14:04 -05:00
|
|
|
return if spec.executables.empty?
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
executables = spec.executables.clone
|
|
|
|
|
|
|
|
# Leave any executables created by other installed versions
|
|
|
|
# of this gem installed.
|
|
|
|
|
2019-02-14 07:59:03 -05:00
|
|
|
list = Gem::Specification.find_all do |s|
|
2011-05-31 23:45:05 -04:00
|
|
|
s.name == spec.name && s.version != spec.version
|
2019-02-14 07:59:03 -05:00
|
|
|
end
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
list.each do |s|
|
|
|
|
s.executables.each do |exe_name|
|
|
|
|
executables.delete exe_name
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
2011-01-28 18:46:47 -05:00
|
|
|
end
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
return if executables.empty?
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2011-07-26 23:33:45 -04:00
|
|
|
executables = executables.map { |exec| formatted_program_filename exec }
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
remove = if @force_executables.nil?
|
2012-12-06 00:26:46 -05:00
|
|
|
ask_yes_no("Remove executables:\n" +
|
|
|
|
"\t#{executables.join ', '}\n\n" +
|
2011-01-28 18:46:47 -05:00
|
|
|
"in addition to the gem?",
|
|
|
|
true)
|
|
|
|
else
|
|
|
|
@force_executables
|
|
|
|
end
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if remove
|
2011-07-26 23:33:45 -04:00
|
|
|
bin_dir = @bin_dir || Gem.bindir(spec.base_dir)
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2011-07-26 23:33:45 -04:00
|
|
|
raise Gem::FilePermissionError, bin_dir unless File.writable? bin_dir
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2011-07-26 23:33:45 -04:00
|
|
|
executables.each do |exe_name|
|
2011-01-28 18:46:47 -05:00
|
|
|
say "Removing #{exe_name}"
|
2011-07-26 23:33:45 -04:00
|
|
|
|
|
|
|
exe_file = File.join bin_dir, exe_name
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
safe_delete { FileUtils.rm exe_file }
|
|
|
|
safe_delete { FileUtils.rm "#{exe_file}.bat" }
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
else
|
|
|
|
say "Executables and scripts will remain installed."
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
end
|
2008-06-25 22:06:00 -04:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
##
|
|
|
|
# Removes all gems in +list+.
|
2007-11-10 03:05:00 -05:00
|
|
|
#
|
2008-03-31 18:40:06 -04:00
|
|
|
# NOTE: removes uninstalled gems from +list+.
|
|
|
|
|
2007-11-10 03:05:00 -05:00
|
|
|
def remove_all(list)
|
2011-05-31 23:45:05 -04:00
|
|
|
list.each { |spec| uninstall_gem spec }
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
##
|
2007-11-10 03:05:00 -05:00
|
|
|
# spec:: the spec of the gem to be uninstalled
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def remove(spec)
|
2009-06-09 17:38:59 -04:00
|
|
|
unless path_ok?(@gem_home, spec) or
|
2018-11-21 05:20:47 -05:00
|
|
|
(@user_install and path_ok?(Gem.user_dir, spec))
|
2008-03-31 18:40:06 -04:00
|
|
|
e = Gem::GemNotInHomeException.new \
|
2014-01-06 20:19:28 -05:00
|
|
|
"Gem '#{spec.full_name}' is not installed in directory #{@gem_home}"
|
2008-03-31 18:40:06 -04:00
|
|
|
e.spec = spec
|
|
|
|
|
|
|
|
raise e
|
2008-02-10 03:00:19 -05:00
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
raise Gem::FilePermissionError, spec.base_dir unless
|
|
|
|
File.writable?(spec.base_dir)
|
2007-11-10 03:05:00 -05:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
safe_delete { FileUtils.rm_r spec.full_gem_path }
|
|
|
|
safe_delete { FileUtils.rm_r spec.extension_dir }
|
2007-11-10 03:05:00 -05:00
|
|
|
|
2013-10-15 20:14:16 -04:00
|
|
|
old_platform_name = spec.original_name
|
2007-11-10 03:05:00 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
gem = spec.cache_file
|
|
|
|
gem = File.join(spec.cache_dir, "#{old_platform_name}.gem") unless
|
|
|
|
File.exist? gem
|
2007-11-10 03:05:00 -05:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
safe_delete { FileUtils.rm_r gem }
|
2007-11-10 03:05:00 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
Gem::RDoc.new(spec).remove
|
2007-11-10 03:05:00 -05:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
gemspec = spec.spec_file
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
unless File.exist? gemspec
|
2018-05-30 09:01:35 -04:00
|
|
|
gemspec = File.join(File.dirname(gemspec), "#{old_platform_name}.gemspec")
|
|
|
|
end
|
|
|
|
|
|
|
|
safe_delete { FileUtils.rm_r gemspec }
|
2007-11-10 03:05:00 -05:00
|
|
|
say "Successfully uninstalled #{spec.full_name}"
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
Gem::Specification.reset
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
##
|
|
|
|
# Remove any plugin wrappers for +spec+.
|
|
|
|
|
|
|
|
def remove_plugins(spec) # :nodoc:
|
|
|
|
return if spec.plugins.empty?
|
|
|
|
|
2020-03-24 02:39:24 -04:00
|
|
|
remove_plugins_for(spec, @plugins_dir)
|
2020-01-31 21:14:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Regenerates plugin wrappers after removal.
|
|
|
|
|
|
|
|
def regenerate_plugins
|
|
|
|
latest = Gem::Specification.latest_spec_for(@spec.name)
|
|
|
|
return if latest.nil?
|
|
|
|
|
2020-03-24 02:39:24 -04:00
|
|
|
regenerate_plugins_for(latest, @plugins_dir)
|
2020-01-31 21:14:04 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Is +spec+ in +gem_dir+?
|
|
|
|
|
|
|
|
def path_ok?(gem_dir, spec)
|
2011-05-31 23:45:05 -04:00
|
|
|
full_path = File.join gem_dir, 'gems', spec.full_name
|
2009-06-09 17:38:59 -04:00
|
|
|
original_path = File.join gem_dir, 'gems', spec.original_name
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
full_path == spec.full_gem_path || original_path == spec.full_gem_path
|
2008-02-10 03:00:19 -05:00
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Returns true if it is OK to remove +spec+ or this is a forced
|
|
|
|
# uninstallation.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def dependencies_ok?(spec) # :nodoc:
|
2007-11-10 03:05:00 -05:00
|
|
|
return true if @force_ignore
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
deplist = Gem::DependencyList.from_specs
|
2012-11-29 01:52:18 -05:00
|
|
|
deplist.ok_to_remove?(spec.full_name, @check_dev)
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Should the uninstallation abort if a dependency will go unsatisfied?
|
|
|
|
#
|
|
|
|
# See ::new.
|
|
|
|
|
|
|
|
def abort_on_dependent? # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
@abort_on_dependent
|
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Asks if it is OK to remove +spec+. Returns true if it is OK.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def ask_if_ok(spec) # :nodoc:
|
2007-11-10 03:05:00 -05:00
|
|
|
msg = ['']
|
|
|
|
msg << 'You have requested to uninstall the gem:'
|
|
|
|
msg << "\t#{spec.full_name}"
|
2012-11-29 01:52:18 -05:00
|
|
|
msg << ''
|
|
|
|
|
|
|
|
siblings = Gem::Specification.select do |s|
|
2018-10-21 20:27:02 -04:00
|
|
|
s.name == spec.name && s.full_name != spec.full_name
|
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
spec.dependent_gems(@check_dev).each do |dep_spec, dep, satlist|
|
2012-11-29 01:52:18 -05:00
|
|
|
unless siblings.any? { |s| s.satisfies_requirement? dep }
|
|
|
|
msg << "#{dep_spec.name}-#{dep_spec.version} depends on #{dep}"
|
|
|
|
end
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
msg << 'If you remove this gem, these dependencies will not be met.'
|
2007-11-10 03:05:00 -05:00
|
|
|
msg << 'Continue with Uninstall?'
|
2012-12-08 01:01:49 -05:00
|
|
|
return ask_yes_no(msg.join("\n"), false)
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Returns the formatted version of the executable +filename+
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def formatted_program_filename(filename) # :nodoc:
|
2012-11-29 01:52:18 -05:00
|
|
|
# TODO perhaps the installer should leave a small manifest
|
|
|
|
# of what it did for us to find rather than trying to recreate
|
|
|
|
# it again.
|
2018-11-21 05:20:47 -05:00
|
|
|
if @format_executable
|
2011-07-26 23:33:45 -04:00
|
|
|
require 'rubygems/installer'
|
2011-03-01 04:41:32 -05:00
|
|
|
Gem::Installer.exec_format % File.basename(filename)
|
|
|
|
else
|
|
|
|
filename
|
|
|
|
end
|
|
|
|
end
|
2018-05-30 09:01:35 -04:00
|
|
|
|
|
|
|
def safe_delete(&block)
|
|
|
|
block.call
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
nil
|
|
|
|
rescue Errno::EPERM
|
|
|
|
e = Gem::UninstallError.new
|
|
|
|
e.spec = @spec
|
|
|
|
|
|
|
|
raise e
|
|
|
|
end
|
2019-02-14 07:59:03 -05:00
|
|
|
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|