2011-01-19 03:08:49 +03: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 11:05:00 +03: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-10 01: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-04-01 02:40:06 +04:00
|
|
|
|
2007-11-10 11:05:00 +03:00
|
|
|
class Gem::Uninstaller
|
|
|
|
|
|
|
|
include Gem::UserInteraction
|
|
|
|
|
|
|
|
##
|
2008-09-25 14: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-04-01 02:40:06 +04:00
|
|
|
|
|
|
|
def initialize(gem, options = {})
|
2007-11-10 11:05:00 +03:00
|
|
|
@gem = gem
|
|
|
|
@version = options[:version] || Gem::Requirement.default
|
2008-02-10 11:00:19 +03:00
|
|
|
gem_home = options[:install_dir] || Gem.dir
|
|
|
|
@gem_home = File.expand_path gem_home
|
2007-11-10 11:05:00 +03:00
|
|
|
@force_executables = options[:executables]
|
|
|
|
@force_all = options[:all]
|
|
|
|
@force_ignore = options[:ignore]
|
2008-06-26 06:06:00 +04:00
|
|
|
@bin_dir = options[:bin_dir]
|
2011-03-01 12:41:32 +03:00
|
|
|
@format_executable = options[:format_executable]
|
2008-09-25 14:13:50 +04:00
|
|
|
|
2009-06-10 01: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]
|
|
|
|
|
2008-09-25 14:13:50 +04:00
|
|
|
spec_dir = File.join @gem_home, 'specifications'
|
|
|
|
@source_index = Gem::SourceIndex.from_gems_in spec_dir
|
2009-06-10 01:38:59 +04:00
|
|
|
|
|
|
|
if @user_install then
|
|
|
|
user_dir = File.join Gem.user_dir, 'specifications'
|
|
|
|
@user_index = Gem::SourceIndex.from_gems_in user_dir
|
|
|
|
end
|
2007-11-10 11:05:00 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2008-09-25 14:13:50 +04:00
|
|
|
# Performs the uninstall of the gem. This removes the spec, the Gem
|
|
|
|
# directory, and the cached .gem file.
|
2008-04-01 02:40:06 +04:00
|
|
|
|
2007-11-10 11:05:00 +03:00
|
|
|
def uninstall
|
2008-09-25 14:13:50 +04:00
|
|
|
list = @source_index.find_name @gem, @version
|
2009-06-10 01:38:59 +04:00
|
|
|
list += @user_index.find_name @gem, @version if @user_install
|
2007-11-10 11:05:00 +03:00
|
|
|
|
|
|
|
if list.empty? then
|
2009-06-10 01:38:59 +04:00
|
|
|
raise Gem::InstallError, "cannot uninstall, check `gem list -d #{@gem}`"
|
2008-09-25 14:13:50 +04:00
|
|
|
|
|
|
|
elsif list.size > 1 and @force_all then
|
|
|
|
remove_all list.dup
|
|
|
|
|
|
|
|
elsif list.size > 1 then
|
2007-11-10 11:05:00 +03:00
|
|
|
gem_names = list.collect {|gem| gem.full_name} + ["All versions"]
|
2008-09-25 14:13:50 +04:00
|
|
|
|
|
|
|
say
|
2010-11-08 23:58:42 +03:00
|
|
|
_, index = choose_from_list "Select gem to uninstall:", gem_names
|
2008-09-25 14:13:50 +04:00
|
|
|
|
|
|
|
if index == list.size then
|
|
|
|
remove_all list.dup
|
|
|
|
elsif index >= 0 && index < list.size then
|
|
|
|
uninstall_gem list[index], list.dup
|
2007-11-10 11:05:00 +03:00
|
|
|
else
|
|
|
|
say "Error: must enter a number [1-#{list.size+1}]"
|
|
|
|
end
|
|
|
|
else
|
2008-09-25 14:13:50 +04:00
|
|
|
uninstall_gem list.first, list.dup
|
2007-11-10 11:05:00 +03:00
|
|
|
end
|
|
|
|
end
|
2008-06-26 06:06:00 +04:00
|
|
|
|
2008-09-25 14:13:50 +04:00
|
|
|
##
|
|
|
|
# Uninstalls gem +spec+
|
|
|
|
|
|
|
|
def uninstall_gem(spec, specs)
|
|
|
|
@spec = spec
|
|
|
|
|
2011-03-01 12:41:32 +03: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 14:13:50 +04:00
|
|
|
Gem.pre_uninstall_hooks.each do |hook|
|
|
|
|
hook.call self
|
|
|
|
end
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
remove_executables @spec
|
|
|
|
remove @spec, specs
|
2008-09-25 14:13:50 +04:00
|
|
|
|
|
|
|
Gem.post_uninstall_hooks.each do |hook|
|
|
|
|
hook.call self
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec = nil
|
|
|
|
end
|
|
|
|
|
2007-11-10 11:05:00 +03:00
|
|
|
##
|
2008-04-01 02:40:06 +04:00
|
|
|
# Removes installed executables and batch files (windows only) for
|
|
|
|
# +gemspec+.
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
def remove_executables(spec)
|
2011-01-29 02:46:47 +03:00
|
|
|
return if spec.nil? or spec.executables.empty?
|
2008-02-10 11:00:19 +03:00
|
|
|
|
2011-01-29 02:46:47 +03:00
|
|
|
bindir = @bin_dir ? @bin_dir : Gem.bindir(spec.installation_path)
|
2008-02-10 11:00:19 +03:00
|
|
|
|
2011-01-29 02:46:47 +03:00
|
|
|
list = @source_index.find_name(spec.name).delete_if { |s|
|
|
|
|
s.version == spec.version
|
|
|
|
}
|
2008-02-10 11:00:19 +03:00
|
|
|
|
2011-01-29 02:46:47 +03:00
|
|
|
executables = spec.executables.clone
|
2008-02-10 11:00:19 +03:00
|
|
|
|
2011-01-29 02:46:47 +03:00
|
|
|
list.each do |s|
|
|
|
|
s.executables.each do |exe_name|
|
|
|
|
executables.delete exe_name
|
2007-11-10 11:05:00 +03:00
|
|
|
end
|
2011-01-29 02:46:47 +03:00
|
|
|
end
|
2008-02-10 11:00:19 +03:00
|
|
|
|
2011-01-29 02:46:47 +03:00
|
|
|
return if executables.empty?
|
2008-02-10 11:00:19 +03:00
|
|
|
|
2011-01-29 02:46:47 +03: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 11:00:19 +03:00
|
|
|
|
2011-01-29 02:46:47 +03:00
|
|
|
unless remove then
|
|
|
|
say "Executables and scripts will remain installed."
|
|
|
|
else
|
|
|
|
raise Gem::FilePermissionError, bindir unless File.writable? bindir
|
2008-04-01 02:40:06 +04:00
|
|
|
|
2011-01-29 02:46:47 +03:00
|
|
|
spec.executables.each do |exe_name|
|
|
|
|
say "Removing #{exe_name}"
|
2011-03-01 12:41:32 +03: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 11:05:00 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-06-26 06:06:00 +04:00
|
|
|
|
2008-04-01 02:40:06 +04:00
|
|
|
##
|
|
|
|
# Removes all gems in +list+.
|
2007-11-10 11:05:00 +03:00
|
|
|
#
|
2008-04-01 02:40:06 +04:00
|
|
|
# NOTE: removes uninstalled gems from +list+.
|
|
|
|
|
2007-11-10 11:05:00 +03:00
|
|
|
def remove_all(list)
|
2008-09-25 14:13:50 +04:00
|
|
|
list.dup.each { |spec| uninstall_gem spec, list }
|
2007-11-10 11:05:00 +03:00
|
|
|
end
|
|
|
|
|
2008-04-01 02:40:06 +04:00
|
|
|
##
|
2007-11-10 11:05:00 +03: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-04-01 02:40:06 +04:00
|
|
|
|
2007-11-10 11:05:00 +03:00
|
|
|
def remove(spec, list)
|
2009-06-10 01:38:59 +04:00
|
|
|
unless path_ok?(@gem_home, spec) or
|
|
|
|
(@user_install and path_ok?(Gem.user_dir, spec)) then
|
2008-04-01 02:40:06 +04:00
|
|
|
e = Gem::GemNotInHomeException.new \
|
2008-02-10 11:00:19 +03:00
|
|
|
"Gem is not installed in directory #{@gem_home}"
|
2008-04-01 02:40:06 +04:00
|
|
|
e.spec = spec
|
|
|
|
|
|
|
|
raise e
|
2008-02-10 11:00:19 +03:00
|
|
|
end
|
|
|
|
|
2007-11-10 11:05:00 +03:00
|
|
|
raise Gem::FilePermissionError, spec.installation_path unless
|
|
|
|
File.writable?(spec.installation_path)
|
|
|
|
|
|
|
|
FileUtils.rm_rf spec.full_gem_path
|
|
|
|
|
|
|
|
original_platform_name = [
|
|
|
|
spec.name, spec.version, spec.original_platform].join '-'
|
|
|
|
|
|
|
|
spec_dir = File.join spec.installation_path, 'specifications'
|
2010-02-22 05:52:35 +03:00
|
|
|
gemspec = File.join spec_dir, spec.spec_name
|
2007-11-10 11:05:00 +03:00
|
|
|
|
|
|
|
unless File.exist? gemspec then
|
|
|
|
gemspec = File.join spec_dir, "#{original_platform_name}.gemspec"
|
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.rm_rf gemspec
|
|
|
|
|
2011-03-01 12:41:32 +03:00
|
|
|
gem = Gem.cache_gem(spec.file_name, spec.installation_path)
|
2007-11-10 11:05:00 +03:00
|
|
|
|
2007-12-20 11:39:12 +03:00
|
|
|
unless File.exist? gem then
|
2011-03-01 12:41:32 +03:00
|
|
|
gem = Gem.cache_gem("#{original_platform_name}.gem", spec.installation_path)
|
2007-11-10 11:05:00 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.rm_rf gem
|
|
|
|
|
|
|
|
Gem::DocManager.new(spec).uninstall_doc
|
|
|
|
|
|
|
|
say "Successfully uninstalled #{spec.full_name}"
|
|
|
|
|
|
|
|
list.delete spec
|
|
|
|
end
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
|
|
|
# Is +spec+ in +gem_dir+?
|
|
|
|
|
|
|
|
def path_ok?(gem_dir, spec)
|
|
|
|
full_path = File.join gem_dir, 'gems', spec.full_name
|
|
|
|
original_path = File.join gem_dir, 'gems', spec.original_name
|
2008-02-10 11:00:19 +03:00
|
|
|
|
2008-06-18 02:04:18 +04:00
|
|
|
full_path == spec.full_gem_path || original_path == spec.full_gem_path
|
2008-02-10 11:00:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def dependencies_ok?(spec)
|
2007-11-10 11:05:00 +03:00
|
|
|
return true if @force_ignore
|
|
|
|
|
2008-09-25 14:13:50 +04:00
|
|
|
deplist = Gem::DependencyList.from_source_index @source_index
|
2009-06-10 01:38:59 +04:00
|
|
|
deplist.add(*@user_index.gems.values) if @user_install
|
2011-03-01 12:41:32 +03:00
|
|
|
deplist.ok_to_remove?(spec.full_name)
|
2007-11-10 11:05:00 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def ask_if_ok(spec)
|
|
|
|
msg = ['']
|
|
|
|
msg << 'You have requested to uninstall the gem:'
|
|
|
|
msg << "\t#{spec.full_name}"
|
|
|
|
spec.dependent_gems.each do |gem,dep,satlist|
|
|
|
|
msg <<
|
|
|
|
("#{gem.name}-#{gem.version} depends on " +
|
2010-02-22 05:52:35 +03:00
|
|
|
"[#{dep.name} (#{dep.requirement})]")
|
2007-11-10 11:05:00 +03:00
|
|
|
end
|
|
|
|
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 12:41:32 +03:00
|
|
|
def formatted_program_filename(filename)
|
|
|
|
if @format_executable then
|
|
|
|
Gem::Installer.exec_format % File.basename(filename)
|
|
|
|
else
|
|
|
|
filename
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2007-11-10 11:05:00 +03:00
|
|
|
end
|
|
|
|
|