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?

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

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.



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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'cmd/link.rb', line 42

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

  kegs, casks = if args.HEAD?
    args.named.to_kegs_to_casks(only: :formula, method: :kegs)
  else
    args.named.to_kegs_to_casks(method: :latest_kegs)
  end
  if args.HEAD?
    kegs = 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
  end

  kegs.freeze.each do |keg|
    keg_only = Formulary.keg_only?(keg.rack)
    formula = begin
      keg.to_formula
    rescue FormulaUnavailableError
      # Not all kegs may belong to current formulae
      nil
    end
    versioned_keg_only_formula = formula.present? && formula.keg_only_reason&.versioned_formula?

    if keg.linked?
      opoo "Already linked: #{keg}"
      name_and_flag = +""
      name_and_flag << "--HEAD " if args.HEAD?
      name_and_flag << "--force " if keg_only && !versioned_keg_only_formula
      name_and_flag << 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 && !versioned_keg_only_formula
      next
    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}
          #{caveats.keg_only_text(skip_reason: true)&.strip}
        EOS
        next
      end

      if !args.force? && (formula.nil? || !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_link_overwrite_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

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

  casks.each do |cask|
    raise Cask::CaskNotInstalledError, cask unless cask.installed?

    artifacts = cask.artifacts.grep(Cask::Artifact::Symlinked)
    conflict = artifacts.find do |artifact|
      artifact.link_action(force: args.force?, overwrite: args.overwrite?) == :conflict
    end
    if conflict
      raise Cask::CaskError, <<~EOS
        Could not link #{cask}: #{conflict.target} already exists.
        To force the link and overwrite all conflicting files:
          brew link --cask --overwrite #{cask}
      EOS
    end

    puts(args.overwrite? ? "Would remove:" : "Would link:") if args.dry_run?
    artifacts.each { |artifact| artifact.install_phase(force: args.force?, **options) }
  end
end