aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cmd/linkapps.rb
blob: 1bec67a047f962d83d61e36062fcadc88b0923a5 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#: @hide_from_man_page
#:  * `linkapps` [`--local`] [<formulae>]:
#:    Find installed formulae that provide `.app`-style macOS apps and symlink them
#:    into `/Applications`, allowing for easier access (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.

require "keg"
require "formula"

module Homebrew
  module_function

  def linkapps
    odeprecated "'brew linkapps'"

    target_dir = linkapps_target(local: ARGV.include?("--local"))

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

    if ARGV.named.empty?
      kegs = Formula.racks.map do |rack|
        keg = rack.subdirs.map { |d| Keg.new(d) }
        next if keg.empty?
        keg.detect(&:linked?) || keg.max_by(&:version)
      end
    else
      kegs = ARGV.kegs
    end

    link_count = 0
    kegs.each do |keg|
      keg.apps.each do |app|
        puts "Linking: #{app}"
        target_app = target_dir/app.basename

        if target_app.exist? && !target_app.symlink?
          onoe "#{target_app} already exists, skipping."
          next
        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
        link_count += 1
      end
    end

    if link_count.zero?
      puts "No apps linked to #{target_dir}" if ARGV.verbose?
    else
      puts "Linked #{Formatter.pluralize(link_count, "app")} to #{target_dir}"
    end
  end

  def linkapps_target(opts = {})
    local = opts.fetch(:local, false)
    Pathname.new(local ? "~/Applications" : "/Applications").expand_path
  end
end