link.rb 3.36 KB
Newer Older
Markus Reiter's avatar
Markus Reiter committed
1
# typed: true
2
3
# frozen_string_literal: true

BrewTestBot's avatar
BrewTestBot committed
4
require "ostruct"
5
require "caveats"
6
require "cli/parser"
7
require "unlink"
8

9
module Homebrew
Markus Reiter's avatar
Markus Reiter committed
10
11
  extend T::Sig

12
13
  module_function

Markus Reiter's avatar
Markus Reiter committed
14
  sig { returns(CLI::Parser) }
15
16
17
  def link_args
    Homebrew::CLI::Parser.new do
      usage_banner <<~EOS
18
        `link`, `ln` [<options>] <formula>
19

20
        Symlink all of <formula>'s installed files into Homebrew's prefix. This
21
22
23
24
        is done automatically when you install formulae but can be useful for DIY
        installations.
      EOS
      switch "--overwrite",
25
             description: "Delete files that already exist in the prefix while linking."
26
      switch "-n", "--dry-run",
27
28
             description: "List files which would be linked or deleted by "\
                          "`brew link --overwrite` without actually linking or deleting any files."
29
      switch "-f", "--force",
Jonathan Chang's avatar
Jonathan Chang committed
30
             description: "Allow keg-only formulae to be linked."
Markus Reiter's avatar
Markus Reiter committed
31

32
      min_named :keg
33
34
35
    end
  end

36
  def link
Markus Reiter's avatar
Markus Reiter committed
37
    args = link_args.parse
38

39
40
41
42
43
    options = {
      overwrite: args.overwrite?,
      dry_run:   args.dry_run?,
      verbose:   args.verbose?,
    }
44

45
    args.named.to_kegs.each do |keg|
46
      keg_only = Formulary.keg_only?(keg.rack)
47
48

      if keg.linked?
49
        opoo "Already linked: #{keg}"
50
51
52
53
54
        name_and_flag = if keg_only
          "--force #{keg.name}"
        else
          keg.name
        end
55
56
57
58
        puts <<~EOS
          To relink:
            brew unlink #{keg.name} && brew link #{name_and_flag}
        EOS
59
        next
60
      end
61

62
63
      if args.dry_run?
        if args.overwrite?
64
65
66
67
          puts "Would remove:"
        else
          puts "Would link:"
        end
68
        keg.link(**options)
69
        puts_keg_only_path_message(keg) if keg_only
70
71
72
        next
      end

73
74
75
76
77
78
      formula = begin
        keg.to_formula
      rescue FormulaUnavailableError
        # Not all kegs may belong to formulae e.g. with `brew diy`
        nil
      end
79

80
      if keg_only
81
        if Homebrew.default_prefix? && formula.present? && formula.keg_only_reason.by_macos?
82
83
84
85
86
87
          caveats = Caveats.new(formula)
          opoo <<~EOS
            Refusing to link macOS provided/shadowed software: #{keg.name}
            #{caveats.keg_only_text(skip_reason: true).strip}
          EOS
          next
88
89
        end

90
        if !args.force? && (formula.blank? || !formula.keg_only_reason.versioned_formula?)
91
92
93
94
95
96
          opoo "#{keg.name} is keg-only and must be linked with --force"
          puts_keg_only_path_message(keg)
          next
        end
      end

97
      Unlink.unlink_versioned_formulae(formula, verbose: args.verbose?) if formula
98

99
      keg.lock do
100
        print "Linking #{keg}... "
101
        puts if args.verbose?
102
103

        begin
104
          n = keg.link(**options)
105
106
107
108
109
        rescue Keg::LinkError
          puts
          raise
        else
          puts "#{n} symlinks created"
110
        end
111

Mike McQuaid's avatar
Mike McQuaid committed
112
        puts_keg_only_path_message(keg) if keg_only && !Homebrew::EnvConfig.developer?
Max Howell's avatar
Max Howell committed
113
114
115
116
      end
    end
  end

117
118
119
120
121
122
123
  def puts_keg_only_path_message(keg)
    bin = keg/"bin"
    sbin = keg/"sbin"
    return if !bin.directory? && !sbin.directory?

    opt = HOMEBREW_PREFIX/"opt/#{keg.name}"
    puts "\nIf you need to have this software first in your PATH instead consider running:"
Mike McQuaid's avatar
Mike McQuaid committed
124
125
    puts "  #{Utils::Shell.prepend_path_in_profile(opt/"bin")}"  if bin.directory?
    puts "  #{Utils::Shell.prepend_path_in_profile(opt/"sbin")}" if sbin.directory?
126
  end
127
end