Class: Homebrew::Cmd::Link Private

Inherits:
AbstractCommand show all
Defined in:
cmd/link.rb,
sorbet/rbi/dsl/homebrew/cmd/link.rbi

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: Args

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#argsHomebrew::Cmd::Link::Args

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



10
# File 'sorbet/rbi/dsl/homebrew/cmd/link.rbi', line 10

def args; end

#runvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'cmd/link.rb', line 31

def run
  options = {
    overwrite: args.overwrite?,
    dry_run:   args.dry_run?,
    verbose:   args.verbose?,
  }

  kegs = if args.HEAD?
    args.named.to_kegs.group_by(&:name).filter_map do |name, resolved_kegs|
      head_keg = resolved_kegs.find { |keg| keg.version.head? }
      next head_keg if head_keg.present?

      opoo <<~EOS
        No HEAD keg installed for #{name}
        To install, run:
          brew install --HEAD #{name}
      EOS

      nil
    end
  else
    args.named.to_latest_kegs
  end

  kegs.freeze.each do |keg|
    keg_only = Formulary.keg_only?(keg.rack)

    if keg.linked?
      opoo "Already linked: #{keg}"
      name_and_flag = "#{"--HEAD " if args.HEAD?}#{"--force " if keg_only}#{keg.name}"
      puts <<~EOS
        To relink, run:
          brew unlink #{keg.name} && brew link #{name_and_flag}
      EOS
      next
    end

    if args.dry_run?
      if args.overwrite?
        puts "Would remove:"
      else
        puts "Would link:"
      end
      keg.link(**options)
      puts_keg_only_path_message(keg) if keg_only
      next
    end

    formula = begin
      keg.to_formula
    rescue FormulaUnavailableError
      # Not all kegs may belong to formulae
      nil
    end

    if keg_only
      if HOMEBREW_PREFIX.to_s == HOMEBREW_DEFAULT_PREFIX && formula.present? &&
         formula.keg_only_reason.by_macos?
        caveats = Caveats.new(formula)
        opoo <<~EOS
          Refusing to link macOS provided/shadowed software: #{keg.name}
          #{T.must(caveats.keg_only_text(skip_reason: true)).strip}
        EOS
        next
      end

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

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

    keg.lock do
      print "Linking #{keg}... "
      puts if args.verbose?

      begin
        n = keg.link(**options)
      rescue Keg::LinkError
        puts
        raise
      else
        puts "#{n} symlinks created."
      end

      puts_keg_only_path_message(keg) if keg_only && !Homebrew::EnvConfig.developer?
    end
  end
end