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'
|
|
|
|
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
|
|
|
|
|
|
|
|
##
|
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
|
|
|
|
@gem_home = File.expand_path(options[:install_dir] || Gem.dir)
|
|
|
|
@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)
|
|
|
|
#
|
|
|
|
@check_dev = options[:check_dev]
|
|
|
|
|
|
|
|
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 +
|
|
|
|
[Gem::Specification.default_specifications_dir]
|
|
|
|
|
|
|
|
Gem::Specification.each_spec dirs do |spec|
|
|
|
|
next unless dependency.matches_spec? spec
|
|
|
|
|
|
|
|
list << spec
|
|
|
|
end
|
2007-11-10 03:05:00 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
default_specs, list = list.partition do |spec|
|
|
|
|
spec.default_gem?
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2007-11-10 03:05:00 -05:00
|
|
|
if list.empty? then
|
2012-11-29 01:52:18 -05:00
|
|
|
if other_repo_specs.empty?
|
|
|
|
if default_specs.empty?
|
|
|
|
raise Gem::InstallError, "gem #{@gem.inspect} is not installed"
|
|
|
|
else
|
|
|
|
message =
|
|
|
|
"gem #{@gem.inspect} cannot be uninstalled " +
|
|
|
|
"because it is a default gem"
|
|
|
|
raise Gem::InstallError, message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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")
|
|
|
|
elsif @force_all then
|
2011-05-31 23:45:05 -04:00
|
|
|
remove_all list
|
2008-09-25 06:13:50 -04:00
|
|
|
|
|
|
|
elsif list.size > 1 then
|
2007-11-10 03:05:00 -05:00
|
|
|
gem_names = list.collect {|gem| gem.full_name} + ["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
|
|
|
|
|
|
|
if index == list.size then
|
2011-05-31 23:45:05 -04:00
|
|
|
remove_all list
|
2008-09-25 06:13:50 -04:00
|
|
|
elsif index >= 0 && index < list.size then
|
2011-05-31 23:45:05 -04:00
|
|
|
uninstall_gem list[index]
|
2007-11-10 03:05:00 -05:00
|
|
|
else
|
|
|
|
say "Error: must enter a number [1-#{list.size+1}]"
|
|
|
|
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
|
2011-05-31 23:45:05 -04:00
|
|
|
remove @spec
|
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
|
|
|
##
|
2008-03-31 18:40:06 -04:00
|
|
|
# Removes installed executables and batch files (windows only) for
|
|
|
|
# +gemspec+.
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def remove_executables(spec)
|
2011-01-28 18:46:47 -05:00
|
|
|
return if spec.nil? or 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.
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
list = Gem::Specification.find_all { |s|
|
|
|
|
s.name == spec.name && s.version != spec.version
|
2011-01-28 18:46:47 -05:00
|
|
|
}
|
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 }
|
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
remove = if @force_executables.nil? then
|
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
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
if remove then
|
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
|
|
|
|
|
|
|
|
FileUtils.rm_f exe_file
|
|
|
|
FileUtils.rm_f "#{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
|
|
|
|
# list:: the list of all such gems
|
|
|
|
#
|
|
|
|
# Warning: this method modifies the +list+ parameter. Once it has
|
|
|
|
# uninstalled a gem, it is removed from that list.
|
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
|
|
|
|
(@user_install and path_ok?(Gem.user_dir, spec)) then
|
2008-03-31 18:40:06 -04:00
|
|
|
e = Gem::GemNotInHomeException.new \
|
2008-02-10 03:00:19 -05:00
|
|
|
"Gem 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
|
|
|
|
|
|
|
FileUtils.rm_rf spec.full_gem_path
|
2013-10-15 20:14:16 -04:00
|
|
|
FileUtils.rm_rf spec.extension_install_dir
|
2007-11-10 03:05:00 -05:00
|
|
|
|
2013-10-15 20:14:16 -04:00
|
|
|
old_platform_name = spec.original_name
|
|
|
|
gemspec = spec.spec_file
|
2007-11-10 03:05:00 -05:00
|
|
|
|
|
|
|
unless File.exist? gemspec then
|
2011-05-31 23:45:05 -04:00
|
|
|
gemspec = File.join(File.dirname(gemspec), "#{old_platform_name}.gemspec")
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.rm_rf gemspec
|
|
|
|
|
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
|
|
|
|
|
|
|
FileUtils.rm_rf gem
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
Gem::RDoc.new(spec).remove
|
2007-11-10 03:05:00 -05:00
|
|
|
|
|
|
|
say "Successfully uninstalled #{spec.full_name}"
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
Gem::Specification.remove_spec spec
|
2007-11-10 03:05:00 -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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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|
|
|
|
|
s.name == spec.name && s.full_name != spec.full_name
|
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
|
|
|
|
spec.dependent_gems.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+
|
|
|
|
|
|
|
|
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.
|
2011-03-01 04:41:32 -05:00
|
|
|
if @format_executable then
|
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
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|