Class: Homebrew::Cmd::List Private

Inherits:
AbstractCommand show all
Includes:
SystemCommand::Mixin
Defined in:
cmd/list.rb,
sorbet/rbi/dsl/homebrew/cmd/list.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 included from SystemCommand::Mixin

#system_command, #system_command!

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_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_outdated, #pretty_uninstalled, #pretty_upgradable

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#argsHomebrew::Cmd::List::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/list.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.



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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'cmd/list.rb', line 91

def run
  if args.json?
    raise UsageError, "`brew list --json` requires `--versions`." unless args.versions?
    raise UsageError, "`brew list --versions --json` does not support named arguments." unless args.no_named?

    raise UsageError, "`brew list --versions --json` is only supported by the fast Bash path with `jq`."
  end

  if args.full_name? &&
     !(args.installed_on_request? || args.installed_as_dependency? ||
       args.poured_from_bottle? || args.built_from_source?)
    unless args.cask?
      formula_names = args.no_named? ? Formula.installed : args.named.to_resolved_formulae
      full_formula_names = formula_names.map(&:full_name).sort(&Cask::List::TAP_AND_NAME_COMPARISON)
      full_formula_names = Formatter.columns(full_formula_names) unless args.public_send(:"1?")
      puts full_formula_names if full_formula_names.present?
    end
    if args.cask? || (!args.formula? && args.no_named?)
      cask_names = if args.no_named?
        Cask::Caskroom.casks
      else
        args.named.to_formulae_and_casks(only: :cask, method: :resolve)
      end
      # The cast is because `Keg`` does not define `full_name`
      full_cask_names = T.cast(cask_names, T::Array[T.any(Formula, Cask::Cask)])
                         .map(&:full_name).sort(&Cask::List::TAP_AND_NAME_COMPARISON)
      full_cask_names = Formatter.columns(full_cask_names) unless args.public_send(:"1?")
      puts full_cask_names if full_cask_names.present?
    end
  elsif args.pinned?
    pinned = if args.no_named?
      entries = T.let([], T::Array[String])
      unless args.cask?
        Formula.racks.each do |rack|
          entry = pinned_formula_entry(rack.basename.to_s)
          entries << entry if entry
        end
      end

      if !args.formula? && Cask::Caskroom.path.directory?
        Cask::Caskroom.path.children.reject(&:file?).each do |path|
          entry = pinned_cask_entry(path.basename.to_s)
          entries << entry if entry
        end
      end
      entries
    else
      args.named.filter_map do |name|
        entry = T.let(nil, T.nilable(String))
        package_found = T.let(false, T::Boolean)
        package_name = T.let(nil, T.nilable(String))

        unless args.cask?
          rack = Formulary.to_rack(name)
          if rack.exist?
            package_found = true
            package_name = rack.basename.to_s
            entry ||= pinned_formula_entry(rack.basename.to_s)
          end
        end

        unless args.formula?
          token = ::Utils.name_from_full_name(name).to_s
          caskroom_path = Cask::Caskroom.path/token
          if caskroom_path.exist? || caskroom_path.symlink?
            package_found = true
            package_name ||= token
            entry ||= pinned_cask_entry(token)
          end
        end

        if package_found && entry.nil?
          opoo "#{package_name || name} not pinned"
        elsif !package_found
          Homebrew.failed = true
        end
        entry
      end
    end

    puts pinned.sort(&Cask::List::TAP_AND_NAME_COMPARISON)
  elsif args.versions? || args.multiple?
    filtered_list unless args.cask?
    list_casks if args.cask? || (!args.formula? && !args.multiple? && args.no_named?)
  elsif args.installed_on_request? ||
        args.installed_as_dependency? ||
        args.poured_from_bottle? ||
        args.built_from_source?
    flags = []
    flags << "`--installed-on-request`" if args.installed_on_request?
    flags << "`--installed-as-dependency`" if args.installed_as_dependency?
    flags << "`--poured-from-bottle`" if args.poured_from_bottle?
    flags << "`--built-from-source`" if args.built_from_source?

    raise UsageError, "Cannot use #{flags.join(", ")} with formula arguments." unless args.no_named?

    formulae = if args.t?
      # See https://ruby-doc.org/3.2/Kernel.html#method-i-test
      Formula.installed.sort_by { |formula| T.cast(test("M", formula.rack.to_s), Time) }.reverse!
    elsif args.full_name?
      Formula.installed.sort { |a, b| Cask::List::TAP_AND_NAME_COMPARISON.call(a.full_name, b.full_name) }
    else
      Formula.installed.sort
    end
    formulae.reverse! if args.r?
    formulae.each do |formula|
      tab = Tab.for_formula(formula)

      statuses = []
      statuses << "installed on request" if args.installed_on_request? && tab.installed_on_request
      # TODO: Uncomment to deprecate in a future release:
      # odeprecated "brew list --installed-as-dependency", "brew list --no-installed-on-request"
      statuses << "installed as dependency" if args.installed_as_dependency? && !tab.installed_on_request
      statuses << "poured from bottle" if args.poured_from_bottle? && tab.poured_from_bottle
      statuses << "built from source" if args.built_from_source? && !tab.poured_from_bottle
      next if statuses.empty?

      name = args.full_name? ? formula.full_name : formula.name
      if flags.count > 1
        puts "#{name}: #{statuses.join(", ")}"
      else
        puts name
      end
    end
  elsif args.no_named?
    ENV["CLICOLOR"] = nil

    ls_args = []
    ls_args << "-1" if args.public_send(:"1?")
    ls_args << "-l" if args.l?
    ls_args << "-r" if args.r?
    ls_args << "-t" if args.t?

    if !args.cask? && HOMEBREW_CELLAR.exist? && HOMEBREW_CELLAR.children.any?
      ohai "Formulae" if $stdout.tty? && !args.formula?
      system_command! "ls", args: [*ls_args, HOMEBREW_CELLAR], print_stdout: true
      puts if $stdout.tty? && !args.formula?
    end
    if !args.formula? && Cask::Caskroom.any_casks_installed?
      ohai "Casks" if $stdout.tty? && !args.cask?
      system_command! "ls", args: [*ls_args, Cask::Caskroom.path], print_stdout: true
    end
  else
    kegs, casks = args.named.to_kegs_to_casks

    if args.verbose? || !$stdout.tty?
      find_args = %w[-not -type d -not -name .DS_Store -print]
      system_command! "find", args: kegs.map(&:to_s) + find_args, print_stdout: true if kegs.present?
      system_command! "find", args: casks.map(&:caskroom_path) + find_args, print_stdout: true if casks.present?
    else
      kegs.each { |keg| PrettyListing.new keg } if kegs.present?
      Cask::List.list_casks(*casks, one: args.public_send(:"1?")) if casks.present?
    end
  end
end