| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "keg" | 
					
						
							|  |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2015-08-09 14:57:15 +03:00
										 |  |  | require "migrator" | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   def uninstall | 
					
						
							| 
									
										
										
										
											2012-02-04 00:01:29 -06:00
										 |  |  |     raise KegUnspecifiedError if ARGV.named.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-09 14:57:15 +03:00
										 |  |  |     # Find symlinks that can point to keg.rack | 
					
						
							|  |  |  |     links = HOMEBREW_CELLAR.subdirs.select(&:symlink?) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     if !ARGV.force? | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |       ARGV.kegs.each do |keg| | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:25 -06:00
										 |  |  |         keg.lock do | 
					
						
							| 
									
										
										
										
											2015-04-09 15:18:25 +08:00
										 |  |  |           puts "Uninstalling #{keg}... (#{keg.abv})" | 
					
						
							| 
									
										
										
										
											2015-08-09 14:57:15 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |           old_cellars = [] | 
					
						
							|  |  |  |           # Remove every symlink that links to keg, because it can | 
					
						
							|  |  |  |           # be left by migrator | 
					
						
							|  |  |  |           links.each do |link| | 
					
						
							|  |  |  |             old_opt = HOMEBREW_PREFIX/"opt/#{link.basename}" | 
					
						
							|  |  |  |             if link.exist? && link.realpath == keg.rack.realpath | 
					
						
							|  |  |  |               old_cellars << link | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if old_opt.symlink? && old_opt.realpath.to_s == keg.to_s | 
					
						
							|  |  |  |               old_opt.unlink | 
					
						
							|  |  |  |               old_opt.parent.rmdir_if_possible | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:25 -06:00
										 |  |  |           keg.unlink | 
					
						
							|  |  |  |           keg.uninstall | 
					
						
							| 
									
										
										
										
											2015-05-17 21:22:29 +08:00
										 |  |  |           rack = keg.rack | 
					
						
							|  |  |  |           rm_pin rack | 
					
						
							| 
									
										
										
										
											2015-08-09 14:57:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-27 11:35:58 +00:00
										 |  |  |           if rack.directory? | 
					
						
							| 
									
										
										
										
											2014-12-23 00:49:49 -08:00
										 |  |  |             versions = rack.subdirs.map(&:basename) | 
					
						
							|  |  |  |             verb = versions.length == 1 ? "is" : "are" | 
					
						
							|  |  |  |             puts "#{keg.name} #{versions.join(", ")} #{verb} still installed." | 
					
						
							| 
									
										
										
										
											2014-11-27 11:35:58 +00:00
										 |  |  |             puts "Remove them all with `brew uninstall --force #{keg.name}`." | 
					
						
							| 
									
										
										
										
											2015-08-09 14:57:15 +03:00
										 |  |  |           else | 
					
						
							|  |  |  |             # If we delete Cellar/newname, then Cellar/oldname symlink | 
					
						
							|  |  |  |             # can become broken and we have to remove it. | 
					
						
							|  |  |  |             old_cellars.each(&:unlink) | 
					
						
							| 
									
										
										
										
											2014-11-27 11:35:58 +00:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2013-01-23 00:26:25 -06:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2012-03-06 13:43:41 +00:00
										 |  |  |       ARGV.named.each do |name| | 
					
						
							| 
									
										
										
										
											2015-08-09 14:57:15 +03:00
										 |  |  |         rack = Formulary.to_rack(name) | 
					
						
							|  |  |  |         name = rack.basename | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         links.each do |link| | 
					
						
							|  |  |  |           old_opt = HOMEBREW_PREFIX/"opt/#{link.basename}" | 
					
						
							|  |  |  |           if old_opt.symlink? && old_opt.exist? \ | 
					
						
							|  |  |  |               && old_opt.realpath.parent == rack.realpath | 
					
						
							|  |  |  |             old_opt.unlink | 
					
						
							|  |  |  |             old_opt.parent.rmdir_if_possible | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 19:26:16 +03:00
										 |  |  |           link.unlink if link.exist? && link.realpath == rack.realpath | 
					
						
							| 
									
										
										
										
											2015-08-09 14:57:15 +03:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2013-05-15 22:13:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-06 13:43:41 +00:00
										 |  |  |         if rack.directory? | 
					
						
							| 
									
										
										
										
											2015-04-09 15:18:25 +08:00
										 |  |  |           puts "Uninstalling #{name}... (#{rack.abv})" | 
					
						
							| 
									
										
										
										
											2014-06-23 22:00:33 -05:00
										 |  |  |           rack.subdirs.each do |d| | 
					
						
							|  |  |  |             keg = Keg.new(d) | 
					
						
							|  |  |  |             keg.unlink | 
					
						
							|  |  |  |             keg.uninstall | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2012-08-10 16:33:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-17 21:22:29 +08:00
										 |  |  |         rm_pin rack | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   rescue MultipleVersionsInstalledError => e | 
					
						
							| 
									
										
										
										
											2012-04-30 14:08:59 +10:00
										 |  |  |     ofail e | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |     puts "Use `brew remove --force #{e.name}` to remove all versions." | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2012-08-10 16:33:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def rm_pin(rack) | 
					
						
							| 
									
										
										
										
											2015-05-17 21:22:29 +08:00
										 |  |  |     Formulary.from_rack(rack).unpin rescue nil | 
					
						
							| 
									
										
										
										
											2013-03-31 21:28:20 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | end |