79 lines
2.0 KiB
Ruby
Raw Normal View History

2016-04-08 16:28:43 +02:00
#: * `prune` [`--dry-run`]:
#: Remove dead symlinks from the Homebrew prefix. This is generally not
#: needed, but can be useful when doing DIY installations.
2016-04-08 16:28:43 +02:00
#:
#: If `--dry-run` or `-n` is passed, show what would be removed, but do not
#: actually remove anything.
require "keg"
2018-11-05 17:07:33 +05:30
require "cli_parser"
module Homebrew
2016-09-26 01:44:51 +02:00
module_function
2018-11-05 17:07:33 +05:30
def prune_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
`prune` [<options>]
Remove dead symlinks from the Homebrew prefix. This is generally not
needed, but can be useful when doing DIY installations.
EOS
switch "-n", "--dry-run",
description: "Show what would be removed, but do not actually remove anything."
switch :verbose
switch :debug
end
end
def prune
2018-11-05 17:07:33 +05:30
prune_args.parse
ObserverPathnameExtension.reset_counts!
dirs = []
Keg::MUST_EXIST_SUBDIRECTORIES.each do |dir|
next unless dir.directory?
2018-09-17 02:45:00 +02:00
2013-02-17 22:54:43 -06:00
dir.find do |path|
2013-05-15 12:45:39 -05:00
path.extend(ObserverPathnameExtension)
if path.symlink?
unless path.resolved_path_exists?
if path.to_s =~ Keg::INFOFILE_RX
2013-05-15 12:45:39 -05:00
path.uninstall_info unless ARGV.dry_run?
end
2018-11-05 17:07:33 +05:30
if args.dry_run?
2013-05-15 12:45:39 -05:00
puts "Would remove (broken link): #{path}"
else
path.unlink
end
end
elsif path.directory? && !Keg::MUST_EXIST_SUBDIRECTORIES.include?(path)
dirs << path
end
end
end
2013-12-21 21:37:26 -06:00
dirs.reverse_each do |d|
2013-05-15 12:45:39 -05:00
if ARGV.dry_run? && d.children.empty?
puts "Would remove (empty directory): #{d}"
else
d.rmdir_if_possible
end
end
2018-11-05 17:07:33 +05:30
return if args.dry_run?
if ObserverPathnameExtension.total.zero?
2018-11-05 17:07:33 +05:30
puts "Nothing pruned" if args.verbose?
else
n, d = ObserverPathnameExtension.counts
print "Pruned #{n} symbolic links "
print "and #{d} directories " if d.positive?
puts "from #{HOMEBREW_PREFIX}"
end
end
end