Class: Homebrew::CLI::NamedArgs Private

Inherits:
Array show all
Extended by:
T::Generic
Defined in:
cli/named_args.rb

Overview

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.

Helper class for loading formulae/casks from named arguments.

Constant Summary collapse

Elem =

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

type_member(:out) { { fixed: String } }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#blank?, #deep_dup, #fifth, #fourth, #present?, #second, #third, #to_sentence

Constructor Details

#initialize(*args, parent: Args.new, override_spec: nil, force_bottle: false, flags: [], cask_options: false, without_api: false) ⇒ void

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.

Parameters:

  • args (String)
  • parent (Args) (defaults to: Args.new)
  • override_spec (Symbol, nil) (defaults to: nil)
  • force_bottle (Boolean) (defaults to: false)
  • flags (Array<String>) (defaults to: [])
  • cask_options (Boolean) (defaults to: false)
  • without_api (Boolean) (defaults to: false)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'cli/named_args.rb', line 28

def initialize(
  *args,
  parent: Args.new,
  override_spec: nil,
  force_bottle: false,
  flags: [],
  cask_options: false,
  without_api: false
)
  super(args)

  @override_spec = override_spec
  @force_bottle = force_bottle
  @flags = flags
  @cask_options = cask_options
  @without_api = without_api
  @parent = parent
end

Instance Attribute Details

#parentArgs (readonly)

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.

Returns:



15
16
17
# File 'cli/named_args.rb', line 15

def parent
  @parent
end

Instance Method Details

#homebrew_tap_cask_namesArray<String>

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.

Returns:



297
298
299
# File 'cli/named_args.rb', line 297

def homebrew_tap_cask_names
  downcased_unique_named.grep(HOMEBREW_CASK_TAP_CASK_REGEX)
end

#to_casksArray<Cask::Cask>

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.

Returns:



48
49
50
51
52
53
# File 'cli/named_args.rb', line 48

def to_casks
  @to_casks ||= T.let(
    to_formulae_and_casks(only: :cask).freeze, T.nilable(T::Array[T.any(Formula, Keg, Cask::Cask)])
  )
  T.cast(@to_casks, T::Array[Cask::Cask])
end

#to_default_kegsArray<Keg>

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.

Returns:



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'cli/named_args.rb', line 227

def to_default_kegs
  require "missing_formula"

  @to_default_kegs ||= T.let(begin
    to_formulae_and_casks(only: :formula, method: :default_kegs).freeze
  rescue NoSuchKegError => e
    if (reason = MissingFormula.suggest_command(e.name, "uninstall"))
      $stderr.puts reason
    end
    raise e
  end, T.nilable(T::Array[T.any(Formula, Keg, Cask::Cask)]))
  T.cast(@to_default_kegs, T::Array[Keg])
end

#to_formulaeArray<Formula>

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.

Returns:



56
57
58
59
60
61
# File 'cli/named_args.rb', line 56

def to_formulae
  @to_formulae ||= T.let(
    to_formulae_and_casks(only: :formula).freeze, T.nilable(T::Array[T.any(Formula, Keg, Cask::Cask)])
  )
  T.cast(@to_formulae, T::Array[Formula])
end

#to_formulae_and_casks(only: parent.only_formula_or_cask, ignore_unavailable: false, method: nil, uniq: true, warn: false) ⇒ Array<Formula, Keg, Cask::Cask>

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.

Convert named arguments to Formula or Cask objects. If both a formula and cask with the same name exist, returns the formula and prints a warning unless only is specified.

Parameters:

  • only (Symbol, nil) (defaults to: parent.only_formula_or_cask)
  • ignore_unavailable (Boolean) (defaults to: false)
  • method (Symbol, nil) (defaults to: nil)
  • uniq (Boolean) (defaults to: true)
  • warn (Boolean) (defaults to: false)

Returns:



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
# File 'cli/named_args.rb', line 75

def to_formulae_and_casks(
  only: parent.only_formula_or_cask, ignore_unavailable: false, method: nil, uniq: true, warn: false
)
  @to_formulae_and_casks ||= T.let(
    {}, T.nilable(T::Hash[T.nilable(Symbol), T::Array[T.any(Formula, Keg, Cask::Cask)]])
  )
  @to_formulae_and_casks[only] ||= downcased_unique_named.flat_map do |name|
    options = { warn: }.compact
    load_formula_or_cask(name, only:, method:, **options)
  rescue FormulaUnreadableError, FormulaClassUnavailableError,
         TapFormulaUnreadableError, TapFormulaClassUnavailableError,
         Cask::CaskUnreadableError
    # Need to rescue before `*UnavailableError` (superclass of this)
    # The formula/cask was found, but there's a problem with its implementation
    raise
  rescue NoSuchKegError, FormulaUnavailableError, Cask::CaskUnavailableError, FormulaOrCaskUnavailableError
    ignore_unavailable ? [] : raise
  end.freeze

  if uniq
    @to_formulae_and_casks.fetch(only).uniq.freeze
  else
    @to_formulae_and_casks.fetch(only)
  end
end

#to_formulae_and_casks_and_unavailable(only: parent.only_formula_or_cask, method: nil) ⇒ Array<Formula, Keg, Cask::Cask, Array<Keg>, FormulaOrCaskUnavailableError>

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.

Parameters:

  • only (Symbol, nil) (defaults to: parent.only_formula_or_cask)
  • method (Symbol, nil) (defaults to: nil)

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'cli/named_args.rb', line 145

def to_formulae_and_casks_and_unavailable(only: parent.only_formula_or_cask, method: nil)
  @to_formulae_casks_unknowns ||= T.let(
    {},
    T.nilable(T::Hash[
      T.nilable(Symbol),
      T::Array[T.any(Formula, Keg, Cask::Cask, T::Array[Keg], FormulaOrCaskUnavailableError)]
    ]),
  )
  @to_formulae_casks_unknowns[method] = downcased_unique_named.map do |name|
    load_formula_or_cask(name, only:, method:)
  rescue FormulaOrCaskUnavailableError => e
    e
  end.uniq.freeze
end

#to_formulae_and_casks_with_tapsArray<Formula, Keg, Cask::Cask>

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.

Returns formulae and casks after validating that a tap is present for each of them.

Returns:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'cli/named_args.rb', line 120

def to_formulae_and_casks_with_taps
  formulae_and_casks_with_taps, formulae_and_casks_without_taps =
    to_formulae_and_casks.partition do |formula_or_cask|
      T.cast(formula_or_cask, T.any(Formula, Cask::Cask)).tap&.installed?
    end

  return formulae_and_casks_with_taps if formulae_and_casks_without_taps.empty?

  types = []
  types << "formulae" if formulae_and_casks_without_taps.any?(Formula)
  types << "casks" if formulae_and_casks_without_taps.any?(Cask::Cask)

  odie <<~ERROR
    These #{types.join(" and ")} are not in any locally installed taps!

      #{formulae_and_casks_without_taps.sort_by(&:to_s).join("\n  ")}

    You may need to run `brew tap` to install additional taps.
  ERROR
end

#to_formulae_to_casks(only: parent.only_formula_or_cask, method: nil) ⇒ Array<(Array<Formula, Keg>, Array<Cask::Cask>)>

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.

Parameters:

  • only (Symbol, nil) (defaults to: parent.only_formula_or_cask)
  • method (Symbol, nil) (defaults to: nil)

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
# File 'cli/named_args.rb', line 105

def to_formulae_to_casks(only: parent.only_formula_or_cask, method: nil)
  @to_formulae_to_casks ||= T.let(
    {}, T.nilable(T::Hash[[T.nilable(Symbol), T.nilable(Symbol)],
                          [T::Array[T.any(Formula, Keg)], T::Array[Cask::Cask]]])
  )
  @to_formulae_to_casks[[method, only]] =
    T.cast(
      to_formulae_and_casks(only:, method:).partition { |o| o.is_a?(Formula) || o.is_a?(Keg) }
              .map(&:freeze).freeze,
      [T::Array[T.any(Formula, Keg)], T::Array[Cask::Cask]],
    )
end

#to_installed_tapsArray<Tap>

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.

Returns:



290
291
292
293
294
# File 'cli/named_args.rb', line 290

def to_installed_taps
  @to_installed_taps ||= T.let(to_taps.each do |tap|
    raise TapUnavailableError, tap.name unless tap.installed?
  end.uniq.freeze, T.nilable(T::Array[Tap]))
end

#to_kegsArray<Keg>

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.

Returns:



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'cli/named_args.rb', line 257

def to_kegs
  require "missing_formula"

  @to_kegs ||= T.let(begin
    to_formulae_and_casks(only: :formula, method: :kegs).freeze
  rescue NoSuchKegError => e
    if (reason = MissingFormula.suggest_command(e.name, "uninstall"))
      $stderr.puts reason
    end
    raise e
  end, T.nilable(T::Array[T.any(Formula, Keg, Cask::Cask)]))
  T.cast(@to_kegs, T::Array[Keg])
end

#to_kegs_to_casks(only: parent.only_formula_or_cask, ignore_unavailable: false, all_kegs: nil) ⇒ Array<(Array<Keg>, Array<Cask::Cask>)>

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.

Parameters:

  • only (Symbol, nil) (defaults to: parent.only_formula_or_cask)
  • ignore_unavailable (Boolean) (defaults to: false)
  • all_kegs (Boolean, nil) (defaults to: nil)

Returns:



275
276
277
278
279
280
281
282
# File 'cli/named_args.rb', line 275

def to_kegs_to_casks(only: parent.only_formula_or_cask, ignore_unavailable: false, all_kegs: nil)
  method = all_kegs ? :kegs : :default_kegs
  @to_kegs_to_casks ||= T.let({}, T.nilable(T::Hash[T.nilable(Symbol), [T::Array[Keg], T::Array[Cask::Cask]]]))
  @to_kegs_to_casks[method] ||=
    T.cast(to_formulae_and_casks(only:, ignore_unavailable:, method:)
    .partition { |o| o.is_a?(Keg) }
    .map(&:freeze).freeze, [T::Array[Keg], T::Array[Cask::Cask]])
end

#to_latest_kegsArray<Keg>

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.

Returns:



242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'cli/named_args.rb', line 242

def to_latest_kegs
  require "missing_formula"

  @to_latest_kegs ||= T.let(begin
    to_formulae_and_casks(only: :formula, method: :latest_kegs).freeze
  rescue NoSuchKegError => e
    if (reason = MissingFormula.suggest_command(e.name, "uninstall"))
      $stderr.puts reason
    end
    raise e
  end, T.nilable(T::Array[T.any(Formula, Keg, Cask::Cask)]))
  T.cast(@to_latest_kegs, T::Array[Keg])
end

#to_paths(only: parent.only_formula_or_cask, recurse_tap: false) ⇒ Array<Pathname>

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.

Keep existing paths and try to convert others to tap, formula or cask paths. If a cask and formula with the same name exist, includes both their paths unless only is specified.

Parameters:

  • only (Symbol, nil) (defaults to: parent.only_formula_or_cask)
  • recurse_tap (Boolean) (defaults to: false)

Returns:



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'cli/named_args.rb', line 182

def to_paths(only: parent.only_formula_or_cask, recurse_tap: false)
  @to_paths ||= T.let({}, T.nilable(T::Hash[T.nilable(Symbol), T::Array[Pathname]]))
  @to_paths[only] ||= Homebrew.with_no_api_env_if_needed(@without_api) do
    downcased_unique_named.flat_map do |name|
      path = Pathname(name).expand_path
      if only.nil? && name.match?(LOCAL_PATH_REGEX) && path.exist?
        path
      elsif name.match?(TAP_NAME_REGEX)
        tap = Tap.fetch(name)

        if recurse_tap
          next tap.formula_files if only == :formula
          next tap.cask_files if only == :cask
        end

        tap.path
      else
        next Formulary.path(name) if only == :formula
        next Cask::CaskLoader.path(name) if only == :cask

        formula_path = Formulary.path(name)
        cask_path = Cask::CaskLoader.path(name)

        paths = []

        if formula_path.exist? ||
           (!Homebrew::EnvConfig.no_install_from_api? &&
           !CoreTap.instance.installed? &&
           Homebrew::API::Formula.all_formulae.key?(path.basename.to_s))
          paths << formula_path
        end
        if cask_path.exist? ||
           (!Homebrew::EnvConfig.no_install_from_api? &&
           !CoreCaskTap.instance.installed? &&
           Homebrew::API::Cask.all_casks.key?(path.basename.to_s))
          paths << cask_path
        end

        paths.empty? ? path : paths
      end
    end.uniq.freeze
  end
end

#to_resolved_formulae(uniq: true) ⇒ Array<Formula>

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.

Parameters:

  • uniq (Boolean) (defaults to: true)

Returns:



161
162
163
164
165
166
167
# File 'cli/named_args.rb', line 161

def to_resolved_formulae(uniq: true)
  @to_resolved_formulae ||= T.let(
    to_formulae_and_casks(only: :formula, method: :resolve, uniq:).freeze,
    T.nilable(T::Array[T.any(Formula, Keg, Cask::Cask)]),
  )
  T.cast(@to_resolved_formulae, T::Array[Formula])
end

#to_resolved_formulae_to_casks(only: parent.only_formula_or_cask) ⇒ Array<(Array<Formula>, Array<Cask::Cask>)>

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.

Parameters:

  • only (Symbol, nil) (defaults to: parent.only_formula_or_cask)

Returns:



170
171
172
# File 'cli/named_args.rb', line 170

def to_resolved_formulae_to_casks(only: parent.only_formula_or_cask)
  T.cast(to_formulae_to_casks(only:, method: :resolve), [T::Array[Formula], T::Array[Cask::Cask]])
end

#to_tapsArray<Tap>

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.

Returns:



285
286
287
# File 'cli/named_args.rb', line 285

def to_taps
  @to_taps ||= T.let(downcased_unique_named.map { |name| Tap.fetch name }.uniq.freeze, T.nilable(T::Array[Tap]))
end