brew/Library/Homebrew/cmd/unlinkapps.rb
Martin Afanasjew eb5d90c1df unlinkapps: avoid deleting too many symlinks
Use `start_with?` to make sure the symlink actually points into one of
the Homebrew directories (depending on given arguments). Previously,
only a substring match was used, which would also remove a symlink to a
hypothetical `/opt/unrelated/usr/local/opt/Unrelated.app`. Even if
unlikely to occur, altering stuff unrelated to Homebrew is bad.

Furthermore, make sure to always use a trailing slash with directories.
Otherwise, e.g., `brew unlinkapps qt` will unlink .app bundles of both
`qt` and `qt5` if both are installed and `brew linkapps qt qt5` was
issued before. (Please ignore that `qt` and `qt5` offer a conflicting
set of .app bundles. This will have to be addressed elsewhere.)

Closes Homebrew/homebrew#45174.

Signed-off-by: Mike McQuaid <mike@mikemcquaid.com>
2015-10-20 08:38:54 +01:00

34 lines
830 B
Ruby

# Unlinks any Applications (.app) found in installed prefixes from /Applications
require "keg"
module Homebrew
def unlinkapps
target_dir = ARGV.include?("--local") ? File.expand_path("~/Applications") : "/Applications"
return unless File.exist? target_dir
cellar_apps = Dir[target_dir + "/*.app"].select do |app|
if File.symlink?(app)
should_unlink? File.readlink(app)
end
end
cellar_apps.each do |app|
puts "Unlinking #{app}"
system "unlink", app
end
puts "Finished unlinking from #{target_dir}" if cellar_apps
end
private
def should_unlink?(file)
if ARGV.named.empty?
file.start_with?("#{HOMEBREW_CELLAR}/", "#{HOMEBREW_PREFIX}/opt/")
else
ARGV.kegs.any? { |keg| file.start_with?("#{keg}/", "#{keg.opt_record}/") }
end
end
end