diff options
| author | Martin Afanasjew | 2015-12-08 07:59:14 +0100 |
|---|---|---|
| committer | Martin Afanasjew | 2016-04-06 10:31:54 +0200 |
| commit | dcf406f1e42ac15718e2dcbee98fcb7e22f4f27b (patch) | |
| tree | 19cc89ec0f5f59039e2336b78e1e527a19b50756 | |
| parent | f63180927cc1f1920dc5a920ad423bb6806509e1 (diff) | |
| download | brew-dcf406f1e42ac15718e2dcbee98fcb7e22f4f27b.tar.bz2 | |
unlinkapps: modernize
Simplify code by using `Pathname` methods as much as possible. Also
avoid calling external commands for basic functionality like unlinking,
reduce code duplication by using a method from `cmd/linkapps.rb`, count
unlinked symlinks with `ObserverPathnameExtension`, and adjust output
for consistency with `brew linkapps`.
| -rw-r--r-- | Library/Homebrew/cmd/unlinkapps.rb | 50 |
1 files changed, 34 insertions, 16 deletions
diff --git a/Library/Homebrew/cmd/unlinkapps.rb b/Library/Homebrew/cmd/unlinkapps.rb index 86db7a932..86a816050 100644 --- a/Library/Homebrew/cmd/unlinkapps.rb +++ b/Library/Homebrew/cmd/unlinkapps.rb @@ -1,33 +1,51 @@ -# Unlinks any Applications (.app) found in installed prefixes from /Applications -require "keg" +require "cmd/linkapps" module Homebrew def unlinkapps - target_dir = ARGV.include?("--local") ? File.expand_path("~/Applications") : "/Applications" + target_dir = linkapps_target(:local => ARGV.include?("--local")) - return unless File.exist? target_dir + unlinkapps_from_dir(target_dir) + end + + private - cellar_apps = Dir[target_dir + "/*.app"].select do |app| - if File.symlink?(app) - should_unlink? File.readlink(app) - end + def unlinkapps_from_dir(target_dir) + return unless target_dir.directory? + + apps = Pathname.glob("#{target_dir}/*.app").select do |app| + unlinkapps_unlink?(app) end - cellar_apps.each do |app| - puts "Unlinking #{app}" - system "unlink", app + ObserverPathnameExtension.reset_counts! + + apps.each do |app| + app.extend(ObserverPathnameExtension) + puts "Unlinking: #{app}" + app.unlink end - puts "Finished unlinking from #{target_dir}" if cellar_apps + 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 - private + 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) - def should_unlink?(file) if ARGV.named.empty? - file.start_with?("#{HOMEBREW_CELLAR}/", "#{HOMEBREW_PREFIX}/opt/") + true else - ARGV.kegs.any? { |keg| file.start_with?("#{keg}/", "#{keg.opt_record}/") } + ARGV.kegs.any? { |keg| app.start_with?("#{keg}/", "#{keg.opt_record}/") } end end end |
