diff options
Diffstat (limited to 'lib/rubygems/uninstaller.rb')
-rw-r--r-- | lib/rubygems/uninstaller.rb | 86 |
1 files changed, 60 insertions, 26 deletions
diff --git a/lib/rubygems/uninstaller.rb b/lib/rubygems/uninstaller.rb index 2ad961972b..5f19da5e82 100644 --- a/lib/rubygems/uninstaller.rb +++ b/lib/rubygems/uninstaller.rb @@ -18,9 +18,23 @@ class Gem::Uninstaller include Gem::UserInteraction ## - # Constructs an Uninstaller instance - # - # gem:: [String] The Gem name to uninstall + # 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+ def initialize(gem, options = {}) @gem = gem @@ -31,42 +45,63 @@ class Gem::Uninstaller @force_all = options[:all] @force_ignore = options[:ignore] @bin_dir = options[:bin_dir] + + spec_dir = File.join @gem_home, 'specifications' + @source_index = Gem::SourceIndex.from_gems_in spec_dir end ## - # Performs the uninstall of the Gem. This removes the spec, the - # Gem directory, and the cached .gem file, + # Performs the uninstall of the gem. This removes the spec, the Gem + # directory, and the cached .gem file. def uninstall - list = Gem.source_index.search(/^#{@gem}$/, @version) + list = @source_index.find_name @gem, @version if list.empty? then raise Gem::InstallError, "Unknown gem #{@gem} #{@version}" - elsif list.size > 1 && @force_all - remove_all(list.dup) - remove_executables(list.last) - elsif list.size > 1 - say + + elsif list.size > 1 and @force_all then + remove_all list.dup + + elsif list.size > 1 then gem_names = list.collect {|gem| gem.full_name} + ["All versions"] - gem_name, index = - choose_from_list("Select gem to uninstall:", gem_names) - if index == list.size - remove_all(list.dup) - remove_executables(list.last) - elsif index >= 0 && index < list.size - to_remove = list[index] - remove(to_remove, list) - remove_executables(to_remove) + + 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 else say "Error: must enter a number [1-#{list.size+1}]" end else - remove(list[0], list.dup) - remove_executables(list.last) + uninstall_gem list.first, list.dup end end ## + # Uninstalls gem +spec+ + + def uninstall_gem(spec, specs) + @spec = spec + + Gem.pre_uninstall_hooks.each do |hook| + hook.call self + end + + specs.each { |s| remove_executables s } + remove spec, specs + + Gem.post_uninstall_hooks.each do |hook| + hook.call self + end + + @spec = nil + end + + ## # Removes installed executables and batch files (windows only) for # +gemspec+. @@ -76,7 +111,7 @@ class Gem::Uninstaller if gemspec.executables.size > 0 then bindir = @bin_dir ? @bin_dir : (Gem.bindir @gem_home) - list = Gem.source_index.search(gemspec.name).delete_if { |spec| + list = @source_index.find_name(gemspec.name).delete_if { |spec| spec.version == gemspec.version } @@ -118,7 +153,7 @@ class Gem::Uninstaller # NOTE: removes uninstalled gems from +list+. def remove_all(list) - list.dup.each { |spec| remove spec, list } + list.dup.each { |spec| uninstall_gem spec, list } end ## @@ -185,8 +220,7 @@ class Gem::Uninstaller def dependencies_ok?(spec) return true if @force_ignore - source_index = Gem::SourceIndex.from_installed_gems - deplist = Gem::DependencyList.from_source_index source_index + deplist = Gem::DependencyList.from_source_index @source_index deplist.ok_to_remove?(spec.full_name) || ask_if_ok(spec) end |