2016-04-08 16:28:43 +02:00
|
|
|
#: * `unlinkapps` [`--local`] [`--dry-run`] [<formulae>]:
|
2017-01-09 12:31:00 +00:00
|
|
|
#: Remove symlinks created by `brew linkapps` from `/Applications` (deprecated).
|
|
|
|
#:
|
|
|
|
#: Unfortunately `brew linkapps` cannot behave nicely with e.g. Spotlight using
|
|
|
|
#: either aliases or symlinks and Homebrew formulae do not build "proper" `.app`
|
|
|
|
#: bundles that can be relocated. Instead, please consider using `brew cask` and
|
|
|
|
#: migrate formulae using `.app`s to casks.
|
2016-04-08 16:28:43 +02:00
|
|
|
#:
|
|
|
|
#: If no <formulae> are provided, all linked apps will be removed.
|
|
|
|
#:
|
|
|
|
#: If provided, `--local` will remove symlinks from the user's `~/Applications`
|
|
|
|
#: directory instead of the system directory.
|
|
|
|
#:
|
|
|
|
#: If `--dry-run` or `-n` is passed, Homebrew will list all symlinks which
|
|
|
|
#: would be removed, but will not actually delete any files.
|
|
|
|
|
2015-12-08 07:59:14 +01:00
|
|
|
require "cmd/linkapps"
|
2013-09-21 17:11:25 +02:00
|
|
|
|
2014-06-18 22:41:47 -05:00
|
|
|
module Homebrew
|
2016-09-26 01:44:51 +02:00
|
|
|
module_function
|
|
|
|
|
2013-09-21 17:11:25 +02:00
|
|
|
def unlinkapps
|
2017-01-09 12:31:00 +00:00
|
|
|
opoo <<-EOS.undent
|
|
|
|
`brew unlinkapps` has been deprecated and will eventually be removed!
|
|
|
|
|
|
|
|
Unfortunately `brew linkapps` cannot behave nicely with e.g. Spotlight using either aliases or symlinks and Homebrew formulae do not build "proper" `.app` bundles that can be relocated. Instead, please consider using `brew cask` and migrate formulae using `.app`s to casks.
|
|
|
|
EOS
|
|
|
|
|
2016-09-17 15:32:44 +01:00
|
|
|
target_dir = linkapps_target(local: ARGV.include?("--local"))
|
2013-09-21 17:11:25 +02:00
|
|
|
|
2016-09-17 15:32:44 +01:00
|
|
|
unlinkapps_from_dir(target_dir, dry_run: ARGV.dry_run?)
|
2015-12-08 07:59:14 +01:00
|
|
|
end
|
|
|
|
|
2015-12-08 09:11:33 +01:00
|
|
|
def unlinkapps_prune(opts = {})
|
2016-09-17 15:32:44 +01:00
|
|
|
opts = opts.merge(prune: true)
|
|
|
|
unlinkapps_from_dir(linkapps_target(local: false), opts)
|
|
|
|
unlinkapps_from_dir(linkapps_target(local: true), opts)
|
2015-12-08 09:11:33 +01:00
|
|
|
end
|
|
|
|
|
2015-12-08 08:40:44 +01:00
|
|
|
def unlinkapps_from_dir(target_dir, opts = {})
|
2015-12-08 07:59:14 +01:00
|
|
|
return unless target_dir.directory?
|
2015-12-08 08:40:44 +01:00
|
|
|
dry_run = opts.fetch(:dry_run, false)
|
2015-12-08 09:11:33 +01:00
|
|
|
quiet = opts.fetch(:quiet, false)
|
2015-12-08 07:59:14 +01:00
|
|
|
|
|
|
|
apps = Pathname.glob("#{target_dir}/*.app").select do |app|
|
2015-12-08 09:11:33 +01:00
|
|
|
unlinkapps_unlink?(app, opts)
|
2013-09-21 17:11:25 +02:00
|
|
|
end
|
|
|
|
|
2015-12-08 07:59:14 +01:00
|
|
|
ObserverPathnameExtension.reset_counts!
|
|
|
|
|
2015-12-08 09:11:33 +01:00
|
|
|
app_kind = opts.fetch(:prune, false) ? " (broken link)" : ""
|
2015-12-08 07:59:14 +01:00
|
|
|
apps.each do |app|
|
|
|
|
app.extend(ObserverPathnameExtension)
|
2015-12-08 08:40:44 +01:00
|
|
|
if dry_run
|
2015-12-08 09:11:33 +01:00
|
|
|
puts "Would unlink#{app_kind}: #{app}"
|
2015-12-08 08:40:44 +01:00
|
|
|
else
|
2015-12-08 09:11:33 +01:00
|
|
|
puts "Unlinking#{app_kind}: #{app}" unless quiet
|
2015-12-08 08:40:44 +01:00
|
|
|
app.unlink
|
|
|
|
end
|
2013-09-21 17:11:25 +02:00
|
|
|
end
|
|
|
|
|
2015-12-08 08:40:44 +01:00
|
|
|
return if dry_run
|
|
|
|
|
2015-12-08 07:59:14 +01:00
|
|
|
if ObserverPathnameExtension.total.zero?
|
|
|
|
puts "No apps unlinked from #{target_dir}" if ARGV.verbose?
|
|
|
|
else
|
|
|
|
n = ObserverPathnameExtension.total
|
2017-03-11 11:33:12 +01:00
|
|
|
puts "Unlinked #{Formatter.pluralize(n, "app")} from #{target_dir}"
|
2015-12-08 07:59:14 +01:00
|
|
|
end
|
2013-09-21 17:11:25 +02:00
|
|
|
end
|
2014-12-18 15:13:04 +08:00
|
|
|
|
2015-12-08 07:59:14 +01:00
|
|
|
UNLINKAPPS_PREFIXES = %W[
|
|
|
|
#{HOMEBREW_CELLAR}/
|
|
|
|
#{HOMEBREW_PREFIX}/opt/
|
|
|
|
].freeze
|
|
|
|
|
2015-12-08 09:11:33 +01:00
|
|
|
def unlinkapps_unlink?(target_app, opts = {})
|
2015-12-08 07:59:14 +01:00
|
|
|
# Skip non-symlinks and symlinks that don't point into the Homebrew prefix.
|
2016-09-10 10:24:57 +01:00
|
|
|
app = target_app.readlink.to_s if target_app.symlink?
|
2015-12-08 07:59:14 +01:00
|
|
|
return false unless app && app.start_with?(*UNLINKAPPS_PREFIXES)
|
2014-12-18 15:13:04 +08:00
|
|
|
|
2015-12-08 09:11:33 +01:00
|
|
|
if opts.fetch(:prune, false)
|
|
|
|
!File.exist?(app) # Remove only broken symlinks in prune mode.
|
|
|
|
elsif ARGV.named.empty?
|
2015-12-08 07:59:14 +01:00
|
|
|
true
|
2014-12-18 15:13:04 +08:00
|
|
|
else
|
2015-12-08 07:59:14 +01:00
|
|
|
ARGV.kegs.any? { |keg| app.start_with?("#{keg}/", "#{keg.opt_record}/") }
|
2014-12-18 15:13:04 +08:00
|
|
|
end
|
|
|
|
end
|
2013-09-21 17:11:25 +02:00
|
|
|
end
|