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`.
This commit is contained in:
parent
f63180927c
commit
dcf406f1e4
@ -1,33 +1,51 @@
|
|||||||
# Unlinks any Applications (.app) found in installed prefixes from /Applications
|
require "cmd/linkapps"
|
||||||
require "keg"
|
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
def unlinkapps
|
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)
|
||||||
|
|
||||||
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def should_unlink?(file)
|
def unlinkapps_from_dir(target_dir)
|
||||||
if ARGV.named.empty?
|
return unless target_dir.directory?
|
||||||
file.start_with?("#{HOMEBREW_CELLAR}/", "#{HOMEBREW_PREFIX}/opt/")
|
|
||||||
|
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
|
else
|
||||||
ARGV.kegs.any? { |keg| file.start_with?("#{keg}/", "#{keg.opt_record}/") }
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user