| Class | Gem::Uninstaller |
| In: |
lib/rubygems/uninstaller.rb
|
| Parent: | Object |
An Uninstaller.
Constructs an Uninstaller instance
| gem: | [String] The Gem name to uninstall |
# File lib/rubygems/uninstaller.rb, line 25
25: def initialize(gem, options = {})
26: @gem = gem
27: @version = options[:version] || Gem::Requirement.default
28: gem_home = options[:install_dir] || Gem.dir
29: @gem_home = File.expand_path gem_home
30: @force_executables = options[:executables]
31: @force_all = options[:all]
32: @force_ignore = options[:ignore]
33: @bin_dir = options[:bin_dir]
34: end
# File lib/rubygems/uninstaller.rb, line 193
193: def ask_if_ok(spec)
194: msg = ['']
195: msg << 'You have requested to uninstall the gem:'
196: msg << "\t#{spec.full_name}"
197: spec.dependent_gems.each do |gem,dep,satlist|
198: msg <<
199: ("#{gem.name}-#{gem.version} depends on " +
200: "[#{dep.name} (#{dep.version_requirements})]")
201: end
202: msg << 'If you remove this gems, one or more dependencies will not be met.'
203: msg << 'Continue with Uninstall?'
204: return ask_yes_no(msg.join("\n"), true)
205: end
# File lib/rubygems/uninstaller.rb, line 185
185: def dependencies_ok?(spec)
186: return true if @force_ignore
187:
188: source_index = Gem::SourceIndex.from_installed_gems
189: deplist = Gem::DependencyList.from_source_index source_index
190: deplist.ok_to_remove?(spec.full_name) || ask_if_ok(spec)
191: end
# File lib/rubygems/uninstaller.rb, line 178
178: def path_ok?(spec)
179: full_path = File.join @gem_home, 'gems', spec.full_name
180: original_path = File.join @gem_home, 'gems', spec.original_name
181:
182: full_path == spec.full_gem_path || original_path == spec.full_gem_path
183: 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 131
131: def remove(spec, list)
132: unless dependencies_ok? spec then
133: raise Gem::DependencyRemovalException,
134: "Uninstallation aborted due to dependent gem(s)"
135: end
136:
137: unless path_ok? spec then
138: e = Gem::GemNotInHomeException.new \
139: "Gem is not installed in directory #{@gem_home}"
140: e.spec = spec
141:
142: raise e
143: end
144:
145: raise Gem::FilePermissionError, spec.installation_path unless
146: File.writable?(spec.installation_path)
147:
148: FileUtils.rm_rf spec.full_gem_path
149:
150: original_platform_name = [
151: spec.name, spec.version, spec.original_platform].join '-'
152:
153: spec_dir = File.join spec.installation_path, 'specifications'
154: gemspec = File.join spec_dir, "#{spec.full_name}.gemspec"
155:
156: unless File.exist? gemspec then
157: gemspec = File.join spec_dir, "#{original_platform_name}.gemspec"
158: end
159:
160: FileUtils.rm_rf gemspec
161:
162: cache_dir = File.join spec.installation_path, 'cache'
163: gem = File.join cache_dir, "#{spec.full_name}.gem"
164:
165: unless File.exist? gem then
166: gem = File.join cache_dir, "#{original_platform_name}.gem"
167: end
168:
169: FileUtils.rm_rf gem
170:
171: Gem::DocManager.new(spec).uninstall_doc
172:
173: say "Successfully uninstalled #{spec.full_name}"
174:
175: list.delete spec
176: end
Removes all gems in list.
NOTE: removes uninstalled gems from list.
# File lib/rubygems/uninstaller.rb, line 120
120: def remove_all(list)
121: list.dup.each { |spec| remove spec, list }
122: end
Removes installed executables and batch files (windows only) for gemspec.
# File lib/rubygems/uninstaller.rb, line 73
73: def remove_executables(gemspec)
74: return if gemspec.nil?
75:
76: if gemspec.executables.size > 0 then
77: bindir = @bin_dir ? @bin_dir : (Gem.bindir @gem_home)
78:
79: list = Gem.source_index.search(gemspec.name).delete_if { |spec|
80: spec.version == gemspec.version
81: }
82:
83: executables = gemspec.executables.clone
84:
85: list.each do |spec|
86: spec.executables.each do |exe_name|
87: executables.delete(exe_name)
88: end
89: end
90:
91: return if executables.size == 0
92:
93: answer = if @force_executables.nil? then
94: ask_yes_no("Remove executables:\n" \
95: "\t#{gemspec.executables.join(", ")}\n\nin addition to the gem?",
96: true) # " # appease ruby-mode - don't ask
97: else
98: @force_executables
99: end
100:
101: unless answer then
102: say "Executables and scripts will remain installed."
103: else
104: raise Gem::FilePermissionError, bindir unless File.writable? bindir
105:
106: gemspec.executables.each do |exe_name|
107: say "Removing #{exe_name}"
108: FileUtils.rm_f File.join(bindir, exe_name)
109: FileUtils.rm_f File.join(bindir, "#{exe_name}.bat")
110: end
111: end
112: end
113: 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 40
40: def uninstall
41: list = Gem.source_index.search(/^#{@gem}$/, @version)
42:
43: if list.empty? then
44: raise Gem::InstallError, "Unknown gem #{@gem} #{@version}"
45: elsif list.size > 1 && @force_all
46: remove_all(list.dup)
47: remove_executables(list.last)
48: elsif list.size > 1
49: say
50: gem_names = list.collect {|gem| gem.full_name} + ["All versions"]
51: gem_name, index =
52: choose_from_list("Select gem to uninstall:", gem_names)
53: if index == list.size
54: remove_all(list.dup)
55: remove_executables(list.last)
56: elsif index >= 0 && index < list.size
57: to_remove = list[index]
58: remove(to_remove, list)
59: remove_executables(to_remove)
60: else
61: say "Error: must enter a number [1-#{list.size+1}]"
62: end
63: else
64: remove(list[0], list.dup)
65: remove_executables(list.last)
66: end
67: end