Class | Gem::Uninstaller |
In: |
lib/rubygems/uninstaller.rb
|
Parent: | Object |
An Uninstaller.
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.
bin_dir | [R] | The directory a gem‘s executables will be installed into |
gem_home | [R] | The gem repository the gem will be installed into |
spec | [R] | The Gem::Specification for the gem being uninstalled, only set during uninstall_gem |
Constructs an uninstaller that will uninstall gem
# File lib/rubygems/uninstaller.rb, line 44 44: def initialize(gem, options = {}) 45: @gem = gem 46: @version = options[:version] || Gem::Requirement.default 47: gem_home = options[:install_dir] || Gem.dir 48: @gem_home = File.expand_path gem_home 49: @force_executables = options[:executables] 50: @force_all = options[:all] 51: @force_ignore = options[:ignore] 52: @bin_dir = options[:bin_dir] 53: @format_executable = options[:format_executable] 54: 55: # only add user directory if install_dir is not set 56: @user_install = false 57: @user_install = options[:user_install] unless options[:install_dir] 58: 59: spec_dir = File.join @gem_home, 'specifications' 60: @source_index = Gem::SourceIndex.new [spec_dir] 61: 62: if @user_install then 63: user_dir = File.join Gem.user_dir, 'specifications' 64: @user_index = Gem::SourceIndex.new [user_dir] 65: end 66: end
# File lib/rubygems/uninstaller.rb, line 248 248: def ask_if_ok(spec) 249: msg = [''] 250: msg << 'You have requested to uninstall the gem:' 251: msg << "\t#{spec.full_name}" 252: spec.dependent_gems.each do |gem,dep,satlist| 253: msg << 254: ("#{gem.name}-#{gem.version} depends on " + 255: "[#{dep.name} (#{dep.requirement})]") 256: end 257: msg << 'If you remove this gems, one or more dependencies will not be met.' 258: msg << 'Continue with Uninstall?' 259: return ask_yes_no(msg.join("\n"), true) 260: end
# File lib/rubygems/uninstaller.rb, line 240 240: def dependencies_ok?(spec) 241: return true if @force_ignore 242: 243: deplist = Gem::DependencyList.from_source_index @source_index 244: deplist.add(*@user_index.gems.values) if @user_install 245: deplist.ok_to_remove?(spec.full_name) 246: end
# File lib/rubygems/uninstaller.rb, line 262 262: def formatted_program_filename(filename) 263: if @format_executable then 264: Gem::Installer.exec_format % File.basename(filename) 265: else 266: filename 267: end 268: end
Is spec in gem_dir?
# File lib/rubygems/uninstaller.rb, line 233 233: def path_ok?(gem_dir, spec) 234: full_path = File.join gem_dir, 'gems', spec.full_name 235: original_path = File.join gem_dir, 'gems', spec.original_name 236: 237: full_path == spec.full_gem_path || original_path == spec.full_gem_path 238: end
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.
# File lib/rubygems/uninstaller.rb, line 188 188: def remove(spec, list) 189: unless path_ok?(@gem_home, spec) or 190: (@user_install and path_ok?(Gem.user_dir, spec)) then 191: e = Gem::GemNotInHomeException.new \ 192: "Gem is not installed in directory #{@gem_home}" 193: e.spec = spec 194: 195: raise e 196: end 197: 198: raise Gem::FilePermissionError, spec.installation_path unless 199: File.writable?(spec.installation_path) 200: 201: FileUtils.rm_rf spec.full_gem_path 202: 203: original_platform_name = [ 204: spec.name, spec.version, spec.original_platform].join '-' 205: 206: spec_dir = File.join spec.installation_path, 'specifications' 207: gemspec = File.join spec_dir, spec.spec_name 208: 209: unless File.exist? gemspec then 210: gemspec = File.join spec_dir, "#{original_platform_name}.gemspec" 211: end 212: 213: FileUtils.rm_rf gemspec 214: 215: gem = Gem.cache_gem(spec.file_name, spec.installation_path) 216: 217: unless File.exist? gem then 218: gem = Gem.cache_gem("#{original_platform_name}.gem", spec.installation_path) 219: end 220: 221: FileUtils.rm_rf gem 222: 223: Gem::DocManager.new(spec).uninstall_doc 224: 225: say "Successfully uninstalled #{spec.full_name}" 226: 227: list.delete spec 228: end
Removes all gems in list.
NOTE: removes uninstalled gems from list.
# File lib/rubygems/uninstaller.rb, line 177 177: def remove_all(list) 178: list.dup.each { |spec| uninstall_gem spec, list } 179: end
Removes installed executables and batch files (windows only) for gemspec.
# File lib/rubygems/uninstaller.rb, line 131 131: def remove_executables(spec) 132: return if spec.nil? or spec.executables.empty? 133: 134: bindir = @bin_dir ? @bin_dir : Gem.bindir(spec.installation_path) 135: 136: list = @source_index.find_name(spec.name).delete_if { |s| 137: s.version == spec.version 138: } 139: 140: executables = spec.executables.clone 141: 142: list.each do |s| 143: s.executables.each do |exe_name| 144: executables.delete exe_name 145: end 146: end 147: 148: return if executables.empty? 149: 150: remove = if @force_executables.nil? then 151: ask_yes_no("Remove executables:\n" \ 152: "\t#{spec.executables.join ', '}\n\n" \ 153: "in addition to the gem?", 154: true) 155: else 156: @force_executables 157: end 158: 159: unless remove then 160: say "Executables and scripts will remain installed." 161: else 162: raise Gem::FilePermissionError, bindir unless File.writable? bindir 163: 164: spec.executables.each do |exe_name| 165: say "Removing #{exe_name}" 166: FileUtils.rm_f File.join(bindir, formatted_program_filename(exe_name)) 167: FileUtils.rm_f File.join(bindir, "#{formatted_program_filename(exe_name)}.bat") 168: end 169: end 170: end
Performs the uninstall of the gem. This removes the spec, the Gem directory, and the cached .gem file.
# File lib/rubygems/uninstaller.rb, line 72 72: def uninstall 73: list = @source_index.find_name @gem, @version 74: list += @user_index.find_name @gem, @version if @user_install 75: 76: if list.empty? then 77: raise Gem::InstallError, "cannot uninstall, check `gem list -d #{@gem}`" 78: 79: elsif list.size > 1 and @force_all then 80: remove_all list.dup 81: 82: elsif list.size > 1 then 83: gem_names = list.collect {|gem| gem.full_name} + ["All versions"] 84: 85: say 86: _, index = choose_from_list "Select gem to uninstall:", gem_names 87: 88: if index == list.size then 89: remove_all list.dup 90: elsif index >= 0 && index < list.size then 91: uninstall_gem list[index], list.dup 92: else 93: say "Error: must enter a number [1-#{list.size+1}]" 94: end 95: else 96: uninstall_gem list.first, list.dup 97: end 98: end
Uninstalls gem spec
# File lib/rubygems/uninstaller.rb, line 103 103: def uninstall_gem(spec, specs) 104: @spec = spec 105: 106: unless dependencies_ok? spec 107: unless ask_if_ok(spec) 108: raise Gem::DependencyRemovalException, 109: "Uninstallation aborted due to dependent gem(s)" 110: end 111: end 112: 113: Gem.pre_uninstall_hooks.each do |hook| 114: hook.call self 115: end 116: 117: remove_executables @spec 118: remove @spec, specs 119: 120: Gem.post_uninstall_hooks.each do |hook| 121: hook.call self 122: end 123: 124: @spec = nil 125: end