linkapps: modernize
Simplify code by using `Pathname` methods as much as possible. Also avoid calling external commands for basic functionality like symlink creation, refactor code that can be shared with `brew unlinkapps`, and print a summary line at the end (if symlinks were created).
This commit is contained in:
parent
15b42301e5
commit
f63180927c
@ -1,12 +1,11 @@
|
|||||||
# Links any Applications (.app) found in installed prefixes to /Applications
|
|
||||||
require "keg"
|
require "keg"
|
||||||
require "formula"
|
require "formula"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
def linkapps
|
def linkapps
|
||||||
target_dir = ARGV.include?("--local") ? File.expand_path("~/Applications") : "/Applications"
|
target_dir = linkapps_target(:local => ARGV.include?("--local"))
|
||||||
|
|
||||||
unless File.exist? target_dir
|
unless target_dir.directory?
|
||||||
opoo "#{target_dir} does not exist, stopping."
|
opoo "#{target_dir} does not exist, stopping."
|
||||||
puts "Run `mkdir #{target_dir}` first."
|
puts "Run `mkdir #{target_dir}` first."
|
||||||
exit 1
|
exit 1
|
||||||
@ -16,24 +15,41 @@ module Homebrew
|
|||||||
kegs = Formula.racks.map do |rack|
|
kegs = Formula.racks.map do |rack|
|
||||||
keg = rack.subdirs.map { |d| Keg.new(d) }
|
keg = rack.subdirs.map { |d| Keg.new(d) }
|
||||||
next if keg.empty?
|
next if keg.empty?
|
||||||
keg.detect(&:linked?) || keg.max { |a, b| a.version <=> b.version }
|
keg.detect(&:linked?) || keg.max_by(&:version)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
kegs = ARGV.kegs
|
kegs = ARGV.kegs
|
||||||
end
|
end
|
||||||
|
|
||||||
|
link_count = 0
|
||||||
kegs.each do |keg|
|
kegs.each do |keg|
|
||||||
keg.apps.each do |app|
|
keg.apps.each do |app|
|
||||||
puts "Linking #{app} to #{target_dir}."
|
puts "Linking: #{app}"
|
||||||
target = "#{target_dir}/#{app.basename}"
|
target_app = target_dir/app.basename
|
||||||
|
|
||||||
if File.exist?(target) && !File.symlink?(target)
|
if target_app.exist? && !target_app.symlink?
|
||||||
onoe "#{target} already exists, skipping."
|
onoe "#{target_app} already exists, skipping."
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# We prefer system `ln` over `FileUtils.ln_sf` because the latter seems
|
||||||
|
# to have weird failure conditions (that were observed in the past).
|
||||||
system "ln", "-sf", app, target_dir
|
system "ln", "-sf", app, target_dir
|
||||||
|
link_count += 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if link_count.zero?
|
||||||
|
puts "No apps linked to #{target_dir}" if ARGV.verbose?
|
||||||
|
else
|
||||||
|
puts "Linked #{link_count} app#{plural(link_count)} to #{target_dir}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def linkapps_target(opts = {})
|
||||||
|
local = opts.fetch(:local, false)
|
||||||
|
Pathname.new(local ? "~/Applications" : "/Applications").expand_path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user