brew/Library/Homebrew/cmd/unlinkapps.rb

52 lines
1.2 KiB
Ruby
Raw Normal View History

require "cmd/linkapps"
module Homebrew
def unlinkapps
target_dir = linkapps_target(:local => ARGV.include?("--local"))
unlinkapps_from_dir(target_dir)
end
private
def unlinkapps_from_dir(target_dir)
return unless target_dir.directory?
apps = Pathname.glob("#{target_dir}/*.app").select do |app|
unlinkapps_unlink?(app)
end
ObserverPathnameExtension.reset_counts!
apps.each do |app|
app.extend(ObserverPathnameExtension)
puts "Unlinking: #{app}"
app.unlink
end
if ObserverPathnameExtension.total.zero?
puts "No apps unlinked from #{target_dir}" if ARGV.verbose?
else
n = ObserverPathnameExtension.total
puts "Unlinked #{n} app#{plural(n)} from #{target_dir}"
end
end
UNLINKAPPS_PREFIXES = %W[
#{HOMEBREW_CELLAR}/
#{HOMEBREW_PREFIX}/opt/
].freeze
def unlinkapps_unlink?(target_app)
# Skip non-symlinks and symlinks that don't point into the Homebrew prefix.
app = "#{target_app.readlink}" if target_app.symlink?
return false unless app && app.start_with?(*UNLINKAPPS_PREFIXES)
if ARGV.named.empty?
true
else
ARGV.kegs.any? { |keg| app.start_with?("#{keg}/", "#{keg.opt_record}/") }
end
end
end