| Class | Gem::Uninstaller |
| In: |
lib/rubygems/uninstaller.rb
|
| Parent: | Object |
An Uninstaller.
| 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 39
39: def initialize(gem, options = {})
40: @gem = gem
41: @version = options[:version] || Gem::Requirement.default
42: gem_home = options[:install_dir] || Gem.dir
43: @gem_home = File.expand_path gem_home
44: @force_executables = options[:executables]
45: @force_all = options[:all]
46: @force_ignore = options[:ignore]
47: @bin_dir = options[:bin_dir]
48:
49: spec_dir = File.join @gem_home, 'specifications'
50: @source_index = Gem::SourceIndex.from_gems_in spec_dir
51: end
# File lib/rubygems/uninstaller.rb, line 227
227: def ask_if_ok(spec)
228: msg = ['']
229: msg << 'You have requested to uninstall the gem:'
230: msg << "\t#{spec.full_name}"
231: spec.dependent_gems.each do |gem,dep,satlist|
232: msg <<
233: ("#{gem.name}-#{gem.version} depends on " +
234: "[#{dep.name} (#{dep.version_requirements})]")
235: end
236: msg << 'If you remove this gems, one or more dependencies will not be met.'
237: msg << 'Continue with Uninstall?'
238: return ask_yes_no(msg.join("\n"), true)
239: end
# File lib/rubygems/uninstaller.rb, line 220
220: def dependencies_ok?(spec)
221: return true if @force_ignore
222:
223: deplist = Gem::DependencyList.from_source_index @source_index
224: deplist.ok_to_remove?(spec.full_name) || ask_if_ok(spec)
225: end
# File lib/rubygems/uninstaller.rb, line 213
213: def path_ok?(spec)
214: full_path = File.join @gem_home, 'gems', spec.full_name
215: original_path = File.join @gem_home, 'gems', spec.original_name
216:
217: full_path == spec.full_gem_path || original_path == spec.full_gem_path
218: 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 166
166: def remove(spec, list)
167: unless dependencies_ok? spec then
168: raise Gem::DependencyRemovalException,
169: "Uninstallation aborted due to dependent gem(s)"
170: end
171:
172: unless path_ok? spec then
173: e = Gem::GemNotInHomeException.new \
174: "Gem is not installed in directory #{@gem_home}"
175: e.spec = spec
176:
177: raise e
178: end
179:
180: raise Gem::FilePermissionError, spec.installation_path unless
181: File.writable?(spec.installation_path)
182:
183: FileUtils.rm_rf spec.full_gem_path
184:
185: original_platform_name = [
186: spec.name, spec.version, spec.original_platform].join '-'
187:
188: spec_dir = File.join spec.installation_path, 'specifications'
189: gemspec = File.join spec_dir, "#{spec.full_name}.gemspec"
190:
191: unless File.exist? gemspec then
192: gemspec = File.join spec_dir, "#{original_platform_name}.gemspec"
193: end
194:
195: FileUtils.rm_rf gemspec
196:
197: cache_dir = File.join spec.installation_path, 'cache'
198: gem = File.join cache_dir, "#{spec.full_name}.gem"
199:
200: unless File.exist? gem then
201: gem = File.join cache_dir, "#{original_platform_name}.gem"
202: end
203:
204: FileUtils.rm_rf gem
205:
206: Gem::DocManager.new(spec).uninstall_doc
207:
208: say "Successfully uninstalled #{spec.full_name}"
209:
210: list.delete spec
211: end
Removes all gems in list.
NOTE: removes uninstalled gems from list.
# File lib/rubygems/uninstaller.rb, line 155
155: def remove_all(list)
156: list.dup.each { |spec| uninstall_gem spec, list }
157: end
Removes installed executables and batch files (windows only) for gemspec.
# File lib/rubygems/uninstaller.rb, line 108
108: def remove_executables(gemspec)
109: return if gemspec.nil?
110:
111: if gemspec.executables.size > 0 then
112: bindir = @bin_dir ? @bin_dir : (Gem.bindir @gem_home)
113:
114: list = @source_index.find_name(gemspec.name).delete_if { |spec|
115: spec.version == gemspec.version
116: }
117:
118: executables = gemspec.executables.clone
119:
120: list.each do |spec|
121: spec.executables.each do |exe_name|
122: executables.delete(exe_name)
123: end
124: end
125:
126: return if executables.size == 0
127:
128: answer = if @force_executables.nil? then
129: ask_yes_no("Remove executables:\n" \
130: "\t#{gemspec.executables.join(", ")}\n\nin addition to the gem?",
131: true) # " # appease ruby-mode - don't ask
132: else
133: @force_executables
134: end
135:
136: unless answer then
137: say "Executables and scripts will remain installed."
138: else
139: raise Gem::FilePermissionError, bindir unless File.writable? bindir
140:
141: gemspec.executables.each do |exe_name|
142: say "Removing #{exe_name}"
143: FileUtils.rm_f File.join(bindir, exe_name)
144: FileUtils.rm_f File.join(bindir, "#{exe_name}.bat")
145: end
146: end
147: end
148: 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 57
57: def uninstall
58: list = @source_index.find_name @gem, @version
59:
60: if list.empty? then
61: raise Gem::InstallError, "Unknown gem #{@gem} #{@version}"
62:
63: elsif list.size > 1 and @force_all then
64: remove_all list.dup
65:
66: elsif list.size > 1 then
67: gem_names = list.collect {|gem| gem.full_name} + ["All versions"]
68:
69: say
70: gem_name, index = choose_from_list "Select gem to uninstall:", gem_names
71:
72: if index == list.size then
73: remove_all list.dup
74: elsif index >= 0 && index < list.size then
75: uninstall_gem list[index], list.dup
76: else
77: say "Error: must enter a number [1-#{list.size+1}]"
78: end
79: else
80: uninstall_gem list.first, list.dup
81: end
82: end
Uninstalls gem spec
# File lib/rubygems/uninstaller.rb, line 87
87: def uninstall_gem(spec, specs)
88: @spec = spec
89:
90: Gem.pre_uninstall_hooks.each do |hook|
91: hook.call self
92: end
93:
94: specs.each { |s| remove_executables s }
95: remove spec, specs
96:
97: Gem.post_uninstall_hooks.each do |hook|
98: hook.call self
99: end
100:
101: @spec = nil
102: end