class Gem::Uninstaller
AnUninstaller.
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. SeeGem.pre_uninstall andGem.post_uninstall for details.
Attributes
The directory a gem’s executables will be installed into
The gem repository the gem will be uninstalled from
TheGem::Specification for the gem being uninstalled, only set duringuninstall_gem
Public Class Methods
Source
# File lib/rubygems/uninstaller.rb, line 63definitialize(gem,options = {})@gem =gem@version =options[:version]||Gem::Requirement.default@install_dir =options[:install_dir]@gem_home =File.realpath(@install_dir||Gem.dir)@user_dir =File.exist?(Gem.user_dir)?File.realpath(Gem.user_dir):Gem.user_dir@force_executables =options[:executables]@force_all =options[:all]@force_ignore =options[:ignore]@bin_dir =options[:bin_dir]@format_executable =options[:format_executable]@abort_on_dependent =options[:abort_on_dependent]@check_dev =options[:check_dev]ifoptions[:force]@force_all =true@force_ignore =trueend# only add user directory if install_dir is not set@user_install =false@user_install =options[:user_install]unless@install_dir# Optimization: populated during #uninstall@default_specs_matching_uninstall_params = []end
Constructs an uninstaller that will uninstall gem namedgem.options is aHash with the following keys:
- :version
Version requirement for the gem to uninstall. If not specified, uses
Gem::Requirement.default.- :install_dir
The directory where the gem is installed. If not specified, uses
Gem.dir.- :executables
Whether executables should be removed without confirmation or not. If nil, asks the user explicitly.
- :all
If more than one version matches the requirement, whether to forcefully remove all matching versions or ask the user to select specific matching versions that should be removed.
- :ignore
Ignore broken dependency checks when uninstalling.
- :bin_dir
Directory containing executables to remove. If not specified, uses
Gem.bindir.- :format_executable
In order to find executables to be removed, format executable names using
Gem::Installer.exec_format.- :abort_on_dependent
Directly abort uninstallation if dependencies would be broken, rather than asking the user for confirmation.
- :check_dev
When checking if uninstalling gem would leave broken dependencies around, also consider development dependencies.
- :force
Set both :all and :ignore to true for forced uninstallation.
- :user_install
Uninstall from user gem directory instead of system directory.
Public Instance Methods
Source
# File lib/rubygems/uninstaller.rb, line 320defpath_ok?(gem_dir,spec)full_path =File.joingem_dir,"gems",spec.full_nameoriginal_path =File.joingem_dir,"gems",spec.original_namefull_path==spec.full_gem_path||original_path==spec.full_gem_pathend
Isspec ingem_dir?
Source
# File lib/rubygems/uninstaller.rb, line 310defregenerate_pluginslatest =specification_record.latest_spec_for(@spec.name)returniflatest.nil?regenerate_plugins_for(latest,plugin_dir_for(@spec))end
Regenerates plugin wrappers after removal.
Source
# File lib/rubygems/uninstaller.rb, line 251defremove(spec)unlesspath_ok?(@gem_home,spec)|| (@user_install&&path_ok?(@user_dir,spec))e =Gem::GemNotInHomeException.new \"Gem '#{spec.full_name}' is not installed in directory #{@gem_home}"e.spec =specraiseeendraiseGem::FilePermissionError,spec.base_dirunlessFile.writable?(spec.base_dir)full_gem_path =spec.full_gem_pathexclusions = []ifdefault_spec_matches?(spec)&&spec.executables.any?exclusions =spec.executables.map {|exe|File.join(spec.bin_dir,exe) }exclusions<<File.dirname(exclusions.last)untilexclusions.last==full_gem_pathendsafe_delete {rm_rfull_gem_path,exclusions:exclusions }safe_delete {FileUtils.rm_rspec.extension_dir }old_platform_name =spec.original_namegem =spec.cache_filegem =File.join(spec.cache_dir,"#{old_platform_name}.gem")unlessFile.exist?gemsafe_delete {FileUtils.rm_rgem }beginGem::RDoc.new(spec).removerescueNameErrorendgemspec =spec.spec_fileunlessFile.exist?gemspecgemspec =File.join(File.dirname(gemspec),"#{old_platform_name}.gemspec")endsafe_delete {FileUtils.rm_rgemspec }announce_deletion_of(spec)end
- spec
the spec of the gem to be uninstalled
Source
# File lib/rubygems/uninstaller.rb, line 244defremove_all(list)list.each {|spec|uninstall_gemspec }end
Removes all gems inlist.
NOTE: removes uninstalled gems fromlist.
Source
# File lib/rubygems/uninstaller.rb, line 190defremove_executables(spec)returnifspec.executables.empty?||default_spec_matches?(spec)executables =spec.executables.clone# Leave any executables created by other installed versions# of this gem installed.list =Gem::Specification.find_alldo|s|s.name==spec.name&&s.version!=spec.versionendlist.eachdo|s|s.executables.eachdo|exe_name|executables.deleteexe_nameendendreturnifexecutables.empty?executables =executables.map {|exec|formatted_program_filenameexec }remove =if@force_executables.nil?ask_yes_no("Remove executables:\n" \"\t#{executables.join ", "}\n\n" \"in addition to the gem?",true)else@force_executablesendifremovebin_dir =@bin_dir||Gem.bindir(spec.base_dir)raiseGem::FilePermissionError,bin_dirunlessFile.writable?bin_direxecutables.eachdo|exe_name|say"Removing #{exe_name}"exe_file =File.joinbin_dir,exe_namesafe_delete {FileUtils.rmexe_file }safe_delete {FileUtils.rm"#{exe_file}.bat" }endelsesay"Executables and scripts will remain installed."endend
Removes installed executables and batch files (windows only) forspec.
Source
# File lib/rubygems/uninstaller.rb, line 386defsafe_delete(&block)block.callrescueErrno::ENOENTnilrescueErrno::EPERMe =Gem::UninstallError.newe.spec =@specraiseeend
Source
# File lib/rubygems/uninstaller.rb, line 94defuninstalldependency =Gem::Dependency.new@gem,@versionlist = []specification_record.stubs.eachdo|spec|nextunlessdependency.matches_spec?speclist<<specendiflist.empty?raiseGem::InstallError,"gem #{@gem.inspect} is not installed"enddefault_specs,list =list.partition(&:default_gem?)warn_cannot_uninstall_default_gems(default_specs-list)@default_specs_matching_uninstall_params =default_specs.map(&:to_spec)list,other_repo_specs =list.partitiondo|spec|@gem_home==spec.base_dir|| (@user_install&&spec.base_dir==@user_dir)endlist.sort!iflist.empty?returnunlessother_repo_specs.any?other_repos =other_repo_specs.map(&:base_dir).uniqmessage = ["#{@gem} is not installed in GEM_HOME, try:"]message.concatother_repos.map {|repo|"\tgem uninstall -i #{repo} #{@gem}" }raiseGem::InstallError,message.join("\n")elsif@force_allremove_alllistelsiflist.size>1gem_names =list.map(&:full_name_with_location)gem_names<<"All versions"say_,index =choose_from_list"Select gem to uninstall:",gem_namesifindex==list.sizeremove_alllistelsifindex&&index>=0&&index<list.sizeuninstall_gemlist[index]elsesay"Error: must enter a number [1-#{list.size + 1}]"endelseuninstall_gemlist.firstendend
Performs the uninstall of the gem. This removes the spec, theGem directory, and the cached .gem file.
Source
# File lib/rubygems/uninstaller.rb, line 156defuninstall_gem(stub)spec =stub.to_spec@spec =specunlessdependencies_ok?specifabort_on_dependent?||!ask_if_ok(spec)raiseGem::DependencyRemovalException,"Uninstallation aborted due to dependent gem(s)"endendGem.pre_uninstall_hooks.eachdo|hook|hook.callselfendremove_executables@specremove_plugins@specremove@specspecification_record.remove_spec(stub)regenerate_pluginsGem.post_uninstall_hooks.eachdo|hook|hook.callselfend@spec =nilend
Uninstalls gemspec
Private Instance Methods
Source
# File lib/rubygems/uninstaller.rb, line 409defannounce_deletion_of(spec)name =spec.full_namesay"Successfully uninstalled #{name}"ifdefault_spec_matches?(spec)say("There was both a regular copy and a default copy of #{name}. The " \"regular copy was successfully uninstalled, but the default copy " \"was left around because default gems can't be removed." )endend
Source
# File lib/rubygems/uninstaller.rb, line 422defdefault_spec_matches?(spec)!default_specs_that_match(spec).empty?end
@return true if the specs of any default gems are ‘==` to the given `spec`.
Source
# File lib/rubygems/uninstaller.rb, line 427defdefault_specs_that_match(spec)@default_specs_matching_uninstall_params.select {|default_spec|spec==default_spec }end
@return [Array] specs of default gems that are ‘==` to the given `spec`.
Source
# File lib/rubygems/uninstaller.rb, line 437defplugin_dir_for(spec)Gem.plugindir(spec.base_dir)end
Source
# File lib/rubygems/uninstaller.rb, line 399defrm_r(path,exclusions:)FileUtils::Entry_.new(path).postorder_traversedo|ent|ent.removeunlessexclusions.include?(ent.path)endend
Source
# File lib/rubygems/uninstaller.rb, line 405defspecification_record@specification_record||=@install_dir?Gem::SpecificationRecord.from_path(@install_dir):Gem::Specification.specification_recordend
Source
# File lib/rubygems/uninstaller.rb, line 431defwarn_cannot_uninstall_default_gems(specs)specs.eachdo|spec|say"Gem #{spec.full_name} cannot be uninstalled because it is a default gem"endend