2011-01-18 19:08:49 -05:00
|
|
|
######################################################################
|
|
|
|
# This file is imported from the rubygems project.
|
|
|
|
# DO NOT make modifications in this repo. They _will_ be reverted!
|
|
|
|
# File a patch instead and assign it to Ryan Davis or Eric Hodel.
|
|
|
|
######################################################################
|
|
|
|
|
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'
|
|
|
|
require 'rubygems/doc_manager'
|
|
|
|
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 = {})
|
2011-05-31 23:45:05 -04:00
|
|
|
@gem = gem
|
|
|
|
@version = options[:version] || Gem::Requirement.default
|
|
|
|
@gem_home = File.expand_path(options[:install_dir] || Gem.dir)
|
2007-11-10 03:05:00 -05:00
|
|
|
@force_executables = options[:executables]
|
2011-05-31 23:45:05 -04:00
|
|
|
@force_all = options[:all]
|
|
|
|
@force_ignore = options[:ignore]
|
|
|
|
@bin_dir = options[:bin_dir]
|
2011-03-01 04:41:32 -05:00
|
|
|
@format_executable = options[:format_executable]
|
2008-09-25 06:13:50 -04:00
|
|
|
|
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]
|
|
|
|
|
|
|
|
if @user_install then
|
2011-05-31 23:45:05 -04:00
|
|
|
Gem.use_paths Gem.user_dir, @gem_home
|
|
|
|
else
|
|
|
|
Gem.use_paths @gem_home
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
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
|
2011-05-31 23:45:05 -04:00
|
|
|
list = Gem::Specification.find_all_by_name(@gem, @version)
|
2007-11-10 03:05:00 -05:00
|
|
|
|
|
|
|
if list.empty? then
|
2009-06-09 17:38:59 -04:00
|
|
|
raise Gem::InstallError, "cannot uninstall, check `gem list -d #{@gem}`"
|
2008-09-25 06:13:50 -04:00
|
|
|
|
|
|
|
elsif list.size > 1 and @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
|
|
|
|
unless ask_if_ok(spec)
|
|
|
|
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
|
|
|
|
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
|
|
|
executables = spec.executables.clone
|
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-01-28 18:46:47 -05:00
|
|
|
remove = if @force_executables.nil? then
|
|
|
|
ask_yes_no("Remove executables:\n" \
|
|
|
|
"\t#{spec.executables.join ', '}\n\n" \
|
|
|
|
"in addition to the gem?",
|
|
|
|
true)
|
|
|
|
else
|
|
|
|
@force_executables
|
|
|
|
end
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
unless remove then
|
|
|
|
say "Executables and scripts will remain installed."
|
|
|
|
else
|
2011-05-31 23:45:05 -04:00
|
|
|
bindir = @bin_dir || Gem.bindir(spec.base_dir)
|
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
raise Gem::FilePermissionError, bindir unless File.writable? bindir
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
spec.executables.each do |exe_name|
|
|
|
|
say "Removing #{exe_name}"
|
2011-03-01 04:41:32 -05:00
|
|
|
FileUtils.rm_f File.join(bindir, formatted_program_filename(exe_name))
|
|
|
|
FileUtils.rm_f File.join(bindir, "#{formatted_program_filename(exe_name)}.bat")
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
# TODO: should this be moved to spec?... I vote eww (also exists in docmgr)
|
|
|
|
old_platform_name = [spec.name,
|
|
|
|
spec.version,
|
|
|
|
spec.original_platform].join '-'
|
2007-11-10 03:05:00 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
Gem::DocManager.new(spec).uninstall_doc
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def dependencies_ok?(spec)
|
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
|
2011-03-01 04:41:32 -05:00
|
|
|
deplist.ok_to_remove?(spec.full_name)
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ask_if_ok(spec)
|
|
|
|
msg = ['']
|
|
|
|
msg << 'You have requested to uninstall the gem:'
|
|
|
|
msg << "\t#{spec.full_name}"
|
2011-05-31 23:45:05 -04:00
|
|
|
|
|
|
|
spec.dependent_gems.each do |dep_spec, dep, satlist|
|
2007-11-10 03:05:00 -05:00
|
|
|
msg <<
|
2011-05-31 23:45:05 -04:00
|
|
|
("#{dep_spec.name}-#{dep_spec.version} depends on " +
|
|
|
|
"[#{dep.name} (#{dep.requirement})]")
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2007-11-10 03:05:00 -05:00
|
|
|
msg << 'If you remove this gems, one or more dependencies will not be met.'
|
|
|
|
msg << 'Continue with Uninstall?'
|
|
|
|
return ask_yes_no(msg.join("\n"), true)
|
|
|
|
end
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
def formatted_program_filename(filename)
|
|
|
|
if @format_executable then
|
|
|
|
Gem::Installer.exec_format % File.basename(filename)
|
|
|
|
else
|
|
|
|
filename
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|