| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | require 'formula' | 
					
						
							| 
									
										
										
										
											2012-08-10 16:33:47 -04:00
										 |  |  | require 'keg' | 
					
						
							| 
									
										
										
										
											2012-04-30 15:10:51 -05:00
										 |  |  | require 'bottles' | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | require 'cmd/prune' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Homebrew extend self | 
					
						
							| 
									
										
										
										
											2012-03-06 20:12:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   def cleanup | 
					
						
							|  |  |  |     if ARGV.named.empty? | 
					
						
							| 
									
										
										
										
											2012-12-16 19:14:27 +00:00
										 |  |  |       if HOMEBREW_CELLAR.directory? | 
					
						
							|  |  |  |         HOMEBREW_CELLAR.children.each do |rack| | 
					
						
							|  |  |  |           begin | 
					
						
							|  |  |  |             cleanup_formula rack.basename.to_s if rack.directory? | 
					
						
							|  |  |  |           rescue FormulaUnavailableError => e | 
					
						
							|  |  |  |             # Don't complain about Cellar folders that are from DIY installs | 
					
						
							|  |  |  |             # instead of core formulae. | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2012-03-06 20:12:42 +00:00
										 |  |  |       clean_cache | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |       # seems like a good time to do some additional cleanup | 
					
						
							| 
									
										
										
										
											2012-09-11 14:41:01 -05:00
										 |  |  |       unless ARGV.dry_run? | 
					
						
							|  |  |  |         Homebrew.prune | 
					
						
							|  |  |  |         rm_DS_Store | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |     else | 
					
						
							|  |  |  |       ARGV.formulae.each do |f| | 
					
						
							|  |  |  |         cleanup_formula f | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def cleanup_formula f | 
					
						
							|  |  |  |     f = Formula.factory f | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-11 12:57:53 -07:00
										 |  |  |     if f.installed? and f.rack.directory? | 
					
						
							|  |  |  |       f.rack.children.each do |keg| | 
					
						
							| 
									
										
										
										
											2012-12-04 00:37:14 -08:00
										 |  |  |         if File.directory? keg and f.version > Keg.new(keg).version | 
					
						
							| 
									
										
										
										
											2012-08-10 16:33:47 -04:00
										 |  |  |           if f.can_cleanup? | 
					
						
							| 
									
										
										
										
											2012-09-22 00:29:17 -04:00
										 |  |  |             if ARGV.dry_run? | 
					
						
							|  |  |  |               puts "Would remove: #{keg}" | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |               puts "Removing: #{keg}..." | 
					
						
							|  |  |  |               rm_rf keg | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2012-08-10 16:33:47 -04:00
										 |  |  |           else | 
					
						
							|  |  |  |             opoo "Skipping (old) keg-only: #{keg}" | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-11 12:57:53 -07:00
										 |  |  |     elsif f.rack.children.length > 1
 | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |       # If the cellar only has one version installed, don't complain | 
					
						
							|  |  |  |       # that we can't tell which one to keep. | 
					
						
							| 
									
										
										
										
											2011-03-11 13:36:26 -08:00
										 |  |  |       opoo "Skipping #{f.name}: most recent version #{f.version} not installed" | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-06 20:12:42 +00:00
										 |  |  |   def clean_cache | 
					
						
							| 
									
										
										
										
											2012-12-16 19:14:27 +00:00
										 |  |  |     return unless HOMEBREW_CACHE.directory? | 
					
						
							| 
									
										
										
										
											2012-03-06 20:12:42 +00:00
										 |  |  |     HOMEBREW_CACHE.children.each do |pn| | 
					
						
							| 
									
										
										
										
											2012-03-07 17:13:37 +00:00
										 |  |  |       next unless pn.file? | 
					
						
							| 
									
										
										
										
											2012-03-09 15:24:58 +00:00
										 |  |  |       version = pn.version | 
					
						
							|  |  |  |       name = pn.basename.to_s.match(/(.*)-(#{version})/).captures.first rescue nil | 
					
						
							|  |  |  |       if name and version | 
					
						
							|  |  |  |         f = Formula.factory(name) rescue nil | 
					
						
							| 
									
										
										
										
											2012-04-30 15:10:51 -05:00
										 |  |  |         old_bottle = bottle_file_outdated? f, pn | 
					
						
							| 
									
										
										
										
											2012-11-08 02:19:05 -08:00
										 |  |  |         if (f and f.version > version) or (ARGV.switch? "s" and (f and (not f.installed?))) or old_bottle | 
					
						
							| 
									
										
										
										
											2012-09-22 00:29:17 -04:00
										 |  |  |           if ARGV.dry_run? | 
					
						
							|  |  |  |             puts "Would remove: #{pn}" | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             puts "Removing: #{pn}..." | 
					
						
							|  |  |  |             rm pn | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2012-03-06 20:12:42 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-10 16:06:24 -04:00
										 |  |  |   def rm_DS_Store | 
					
						
							|  |  |  |     system "find #{HOMEBREW_PREFIX} -name .DS_Store -delete" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2012-08-10 16:33:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Formula | 
					
						
							|  |  |  |   def can_cleanup? | 
					
						
							|  |  |  |     # It used to be the case that keg-only kegs could not be cleaned up, because | 
					
						
							|  |  |  |     # older brews were built against the full path to the keg-only keg. Then we | 
					
						
							|  |  |  |     # introduced the opt symlink, and built against that instead. So provided | 
					
						
							|  |  |  |     # no brew exists that was built against an old-style keg-only keg, we can | 
					
						
							|  |  |  |     # remove it. | 
					
						
							| 
									
										
										
										
											2012-09-11 14:22:56 -05:00
										 |  |  |     if not keg_only? or ARGV.force? | 
					
						
							| 
									
										
										
										
											2012-08-10 16:33:47 -04:00
										 |  |  |       true | 
					
						
							|  |  |  |     elsif opt_prefix.directory? | 
					
						
							|  |  |  |       # SHA records were added to INSTALL_RECEIPTS the same day as opt symlinks | 
					
						
							|  |  |  |       !Formula.installed. | 
					
						
							| 
									
										
										
										
											2012-12-19 12:00:05 -06:00
										 |  |  |         select{ |ff| ff.deps.map{ |d| d.to_s }.include? name }. | 
					
						
							| 
									
										
										
										
											2012-08-10 16:33:47 -04:00
										 |  |  |         map{ |ff| ff.rack.children rescue [] }. | 
					
						
							|  |  |  |         flatten. | 
					
						
							| 
									
										
										
										
											2012-09-20 16:47:00 -05:00
										 |  |  |         map{ |keg_path| Tab.for_keg(keg_path).send("HEAD") }. | 
					
						
							| 
									
										
										
										
											2012-08-10 16:33:47 -04:00
										 |  |  |         include? nil | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |