aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Contributions/cmd/brew-linkapps.rb
blob: ce3f4960ad50fa36e1fc174a766da5d9558a482c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Links any Applications (.app) found in installed prefixes to ~/Applications
require 'keg'

TARGET_DIR = ARGV.include?("--system") ? "/Applications" : File.expand_path("~/Applications")

unless File.exist? TARGET_DIR
  opoo "#{TARGET_DIR} does not exist, stopping."
  puts "Run `mkdir #{TARGET_DIR}` first."
  exit 1
end

HOMEBREW_CELLAR.subdirs.each do |rack|
  kegs = rack.subdirs.map { |d| Keg.new(d) }
  next if kegs.empty?

  keg = kegs.detect(&:linked?) || kegs.max {|a,b| a.version <=> b.version}

  Dir["#{keg}/*.app", "#{keg}/bin/*.app", "#{keg}/libexec/*.app"].each do |app|
    puts "Linking #{app}"
    app_name = File.basename(app)
    target = "#{TARGET_DIR}/#{app_name}"

    if File.exist?(target) && File.symlink?(target)
      system "rm", target
    elsif File.exist?(target)
      onoe "#{target} already exists, skipping."
      next
    end
    system "ln", "-s", app, TARGET_DIR
  end
end

puts "Finished linking. Find the links under #{TARGET_DIR}."