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?
|
2013-05-15 12:45:35 -05:00
|
|
|
cleanup_all
|
2010-09-11 20:22:54 +01:00
|
|
|
else
|
2013-05-15 12:45:35 -05:00
|
|
|
ARGV.formulae.each { |f| cleanup_formula(f) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup_all
|
2013-05-15 12:45:36 -05:00
|
|
|
return unless HOMEBREW_CELLAR.directory?
|
|
|
|
|
2013-05-15 12:45:36 -05:00
|
|
|
HOMEBREW_CELLAR.subdirs.each do |rack|
|
2013-05-15 12:45:36 -05:00
|
|
|
begin
|
2013-05-15 12:45:36 -05:00
|
|
|
cleanup_formula Formula.factory(rack.basename.to_s)
|
2013-05-15 12:45:36 -05:00
|
|
|
rescue FormulaUnavailableError
|
|
|
|
# Don't complain about directories from DIY installs
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
|
|
|
end
|
2013-05-15 12:45:36 -05:00
|
|
|
|
|
|
|
cleanup_cache
|
|
|
|
|
2013-05-15 12:45:35 -05:00
|
|
|
unless ARGV.dry_run?
|
|
|
|
Homebrew.prune
|
|
|
|
rm_DS_Store
|
|
|
|
end
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup_formula f
|
2013-05-15 12:45:36 -05:00
|
|
|
if f.installed?
|
2013-05-15 12:45:38 -05:00
|
|
|
eligible_kegs = f.rack.subdirs.map { |d| Keg.new(d) }.select { |k| f.version > k.version }
|
|
|
|
eligible_kegs.each do |keg|
|
|
|
|
if f.can_cleanup?
|
|
|
|
cleanup_keg(keg)
|
|
|
|
else
|
|
|
|
opoo "Skipping (old) keg-only: #{keg}"
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
|
|
|
end
|
2013-05-15 12:45:36 -05:00
|
|
|
elsif f.rack.subdirs.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
|
|
|
|
|
2013-05-15 12:45:38 -05:00
|
|
|
def cleanup_keg keg
|
|
|
|
if keg.linked?
|
|
|
|
opoo "Skipping (old) #{keg} due to it being linked"
|
|
|
|
elsif ARGV.dry_run?
|
|
|
|
puts "Would remove: #{keg}"
|
|
|
|
else
|
|
|
|
puts "Removing: #{keg}..."
|
|
|
|
keg.rmtree
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-15 12:45:36 -05:00
|
|
|
def cleanup_cache
|
2012-12-16 19:14:27 +00:00
|
|
|
return unless HOMEBREW_CACHE.directory?
|
2013-05-15 12:45:38 -05:00
|
|
|
HOMEBREW_CACHE.children.select(&:file?).each do |file|
|
|
|
|
version = file.version
|
|
|
|
name = file.basename.to_s.match(/(.*)-(#{version})/).captures.first rescue nil
|
2012-03-09 15:24:58 +00:00
|
|
|
if name and version
|
|
|
|
f = Formula.factory(name) rescue nil
|
2013-05-15 12:45:38 -05:00
|
|
|
old_bottle = bottle_file_outdated? f, file
|
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?
|
2013-05-15 12:45:38 -05:00
|
|
|
puts "Would remove: #{file}"
|
2012-09-22 00:29:17 -04:00
|
|
|
else
|
2013-05-15 12:45:38 -05:00
|
|
|
puts "Removing: #{file}..."
|
|
|
|
rm file
|
2012-09-22 00:29:17 -04:00
|
|
|
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
|
2013-01-26 14:11:03 +00:00
|
|
|
system "find #{HOMEBREW_PREFIX} -name .DS_Store -delete 2>/dev/null"
|
2012-08-10 16:06:24 -04:00
|
|
|
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 }.
|
2013-05-15 12:45:36 -05:00
|
|
|
map{ |ff| ff.rack.subdirs rescue [] }.
|
2012-08-10 16:33:47 -04:00
|
|
|
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
|