Class: Homebrew::CLI::NamedArgs Private

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
brew/Library/Homebrew/cli/named_args.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Helper class for loading formulae/casks from named arguments.

Instance Method Summary collapse

Constructor Details

#initialize(*args, override_spec: nil, force_bottle: false, flags: []) ⇒ NamedArgs

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of NamedArgs.



14
15
16
17
18
19
20
21
# File 'brew/Library/Homebrew/cli/named_args.rb', line 14

def initialize(*args, override_spec: nil, force_bottle: false, flags: [])
  @args = args
  @override_spec = override_spec
  @force_bottle = force_bottle
  @flags = flags

  super(@args)
end

Instance Method Details

#homebrew_tap_cask_namesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



168
169
170
# File 'brew/Library/Homebrew/cli/named_args.rb', line 168

def homebrew_tap_cask_names
  downcased_unique_named.grep(HOMEBREW_CASK_TAP_CASK_REGEX)
end

#to_casksObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



132
133
134
# File 'brew/Library/Homebrew/cli/named_args.rb', line 132

def to_casks
  @to_casks ||= downcased_unique_named.map(&Cask::CaskLoader.method(:load)).freeze
end

#to_formulaeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
# File 'brew/Library/Homebrew/cli/named_args.rb', line 23

def to_formulae
  @to_formulae ||= to_formulae_and_casks(only: :formula).freeze
end

#to_formulae_and_casks(only: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
30
31
32
# File 'brew/Library/Homebrew/cli/named_args.rb', line 27

def to_formulae_and_casks(only: nil)
  @to_formulae_and_casks ||= {}
  @to_formulae_and_casks[only] ||= begin
    to_objects(only: only).reject { |o| o.is_a?(Tap) }.freeze
  end
end

#to_formulae_pathsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



101
102
103
# File 'brew/Library/Homebrew/cli/named_args.rb', line 101

def to_formulae_paths
  to_paths(only: :formulae)
end

#to_kegsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



136
137
138
139
140
141
142
143
144
145
# File 'brew/Library/Homebrew/cli/named_args.rb', line 136

def to_kegs
  @to_kegs ||= downcased_unique_named.map do |name|
    resolve_keg name
  rescue NoSuchKegError => e
    if (reason = Homebrew::MissingFormula.suggest_command(name, "uninstall"))
      $stderr.puts reason
    end
    raise e
  end.freeze
end

#to_kegs_to_casksObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'brew/Library/Homebrew/cli/named_args.rb', line 147

def to_kegs_to_casks
  @to_kegs_to_casks ||= begin
    kegs = []
    casks = []

    downcased_unique_named.each do |name|
      kegs << resolve_keg(name)

      warn_if_cask_conflicts(name, "keg")
    rescue NoSuchKegError, FormulaUnavailableError
      begin
        casks << Cask::CaskLoader.load(name)
      rescue Cask::CaskUnavailableError
        raise "No installed keg or cask with the name \"#{name}\""
      end
    end

    [kegs.freeze, casks.freeze].freeze
  end
end

#to_objects(only: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



92
93
94
95
96
97
98
99
# File 'brew/Library/Homebrew/cli/named_args.rb', line 92

def to_objects(only: nil)
  @to_objects ||= {}
  @to_objects[only] ||= downcased_unique_named.flat_map do |name|
    next Tap.fetch(name) if only == :tap || (only.nil? && name.count("/") == 1 && !name.start_with?("./", "/"))

    load_formula_or_cask(name, only: only)
  end.uniq.freeze
end

#to_paths(only: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'brew/Library/Homebrew/cli/named_args.rb', line 108

def to_paths(only: nil)
  @to_paths ||= {}
  @to_paths[only] ||= downcased_unique_named.flat_map do |name|
    if File.exist?(name)
      Pathname(name)
    elsif name.count("/") == 1
      Tap.fetch(name).path
    else
      next Formulary.path(name) if only == :formulae
      next Cask::CaskLoader.path(name) if only == :casks

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

      paths = []

      paths << formula_path if formula_path.exist?
      paths << cask_path if cask_path.exist?

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

#to_resolved_formulaeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



62
63
64
65
66
# File 'brew/Library/Homebrew/cli/named_args.rb', line 62

def to_resolved_formulae
  @to_resolved_formulae ||= (downcased_unique_named - homebrew_tap_cask_names).map do |name|
    resolve_formula(name)
  end.uniq(&:name).freeze
end

#to_resolved_formulae_to_casksObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'brew/Library/Homebrew/cli/named_args.rb', line 68

def to_resolved_formulae_to_casks
  @to_resolved_formulae_to_casks ||= begin
    resolved_formulae = []
    casks = []

    downcased_unique_named.each do |name|
      resolved_formulae << resolve_formula(name)

      warn_if_cask_conflicts(name, "formula")
    rescue FormulaUnavailableError
      begin
        casks << Cask::CaskLoader.load(name)
      rescue Cask::CaskUnavailableError
        raise "No available formula or cask with the name \"#{name}\""
      end
    end

    [resolved_formulae.freeze, casks.freeze].freeze
  end
end