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 = {})
|
2007-11-10 03:05:00 -05:00
|
|
|
@gem = gem
|
|
|
|
@version = options[:version] || Gem::Requirement.default
|
2008-02-10 03:00:19 -05:00
|
|
|
gem_home = options[:install_dir] || Gem.dir
|
|
|
|
@gem_home = File.expand_path gem_home
|
2007-11-10 03:05:00 -05:00
|
|
|
@force_executables = options[:executables]
|
|
|
|
@force_all = options[:all]
|
|
|
|
@force_ignore = options[:ignore]
|
2008-06-25 22:06:00 -04:00
|
|
|
@bin_dir = options[:bin_dir]
|
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]
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
spec_dir = File.join @gem_home, 'specifications'
|
|
|
|
@source_index = Gem::SourceIndex.from_gems_in spec_dir
|
2009-06-09 17: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 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
|
2008-09-25 06:13:50 -04:00
|
|
|
list = @source_index.find_name @gem, @version
|
2009-06-09 17:38:59 -04:00
|
|
|
list += @user_index.find_name @gem, @version if @user_install
|
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
|
|
|
|
remove_all list.dup
|
|
|
|
|
|
|
|
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
|
|
|
|
gem_name, index = choose_from_list "Select gem to uninstall:", gem_names
|
|
|
|
|
|
|
|
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 03:05:00 -05:00
|
|
|
else
|
|
|
|
say "Error: must enter a number [1-#{list.size+1}]"
|
|
|
|
end
|
|
|
|
else
|
2008-09-25 06:13:50 -04:00
|
|
|
uninstall_gem list.first, list.dup
|
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+
|
|
|
|
|
|
|
|
def uninstall_gem(spec, specs)
|
|
|
|
@spec = spec
|
|
|
|
|
|
|
|
Gem.pre_uninstall_hooks.each do |hook|
|
|
|
|
hook.call self
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
remove_executables @spec
|
|
|
|
remove @spec, specs
|
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)
|
|
|
|
return if spec.nil?
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
unless spec.executables.empty? then
|
|
|
|
bindir = @bin_dir ? @bin_dir : Gem.bindir(spec.installation_path)
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
list = @source_index.find_name(spec.name).delete_if { |s|
|
|
|
|
s.version == spec.version
|
2007-11-10 03:05:00 -05:00
|
|
|
}
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
executables = spec.executables.clone
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
list.each do |s|
|
|
|
|
s.executables.each do |exe_name|
|
|
|
|
executables.delete exe_name
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
end
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
return if executables.empty?
|
2008-02-10 03:00:19 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
answer = if @force_executables.nil? then
|
|
|
|
ask_yes_no("Remove executables:\n" \
|
2009-06-09 17:38:59 -04:00
|
|
|
"\t#{spec.executables.join(", ")}\n\nin addition to the gem?",
|
2008-03-31 18:40:06 -04:00
|
|
|
true) # " # appease ruby-mode - don't ask
|
|
|
|
else
|
|
|
|
@force_executables
|
|
|
|
end
|
2008-02-10 03:00:19 -05:00
|
|
|
|
|
|
|
unless answer then
|
2007-11-10 03:05:00 -05:00
|
|
|
say "Executables and scripts will remain installed."
|
|
|
|
else
|
2008-03-31 18:40:06 -04:00
|
|
|
raise Gem::FilePermissionError, bindir unless File.writable? bindir
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
spec.executables.each do |exe_name|
|
2007-11-10 03:05:00 -05:00
|
|
|
say "Removing #{exe_name}"
|
2008-02-10 03:00:19 -05:00
|
|
|
FileUtils.rm_f File.join(bindir, exe_name)
|
|
|
|
FileUtils.rm_f File.join(bindir, "#{exe_name}.bat")
|
2007-11-10 03:05:00 -05:00
|
|
|
end
|
|
|
|
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)
|
2008-09-25 06:13:50 -04:00
|
|
|
list.dup.each { |spec| uninstall_gem spec, list }
|
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
|
|
|
|
2007-11-10 03:05:00 -05:00
|
|
|
def remove(spec, list)
|
2008-02-10 03:00:19 -05:00
|
|
|
unless dependencies_ok? spec then
|
2007-11-10 03:05:00 -05:00
|
|
|
raise Gem::DependencyRemovalException,
|
|
|
|
"Uninstallation aborted due to dependent gem(s)"
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2007-11-10 03:05:00 -05: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'
|
|
|
|
gemspec = File.join spec_dir, "#{spec.full_name}.gemspec"
|
|
|
|
|
|
|
|
unless File.exist? gemspec then
|
|
|
|
gemspec = File.join spec_dir, "#{original_platform_name}.gemspec"
|
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.rm_rf gemspec
|
|
|
|
|
|
|
|
cache_dir = File.join spec.installation_path, 'cache'
|
|
|
|
gem = File.join cache_dir, "#{spec.full_name}.gem"
|
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
unless File.exist? gem then
|
2007-11-10 03:05:00 -05:00
|
|
|
gem = File.join cache_dir, "#{original_platform_name}.gem"
|
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.rm_rf gem
|
|
|
|
|
|
|
|
Gem::DocManager.new(spec).uninstall_doc
|
|
|
|
|
|
|
|
say "Successfully uninstalled #{spec.full_name}"
|
|
|
|
|
|
|
|
list.delete spec
|
|
|
|
end
|
|
|
|
|
2009-06-09 17: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 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
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
deplist = Gem::DependencyList.from_source_index @source_index
|
2009-06-09 17:38:59 -04:00
|
|
|
deplist.add(*@user_index.gems.values) if @user_install
|
2007-11-10 03:05:00 -05:00
|
|
|
deplist.ok_to_remove?(spec.full_name) || ask_if_ok(spec)
|
|
|
|
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 " +
|
|
|
|
"[#{dep.name} (#{dep.version_requirements})]")
|
|
|
|
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
|
|
|
|
|
|
|
|
end
|
|
|
|
|