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

#command_sandbox, #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_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_incompatible, #pretty_install_status, #pretty_installed, #pretty_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#args ⇒ Homebrew::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

#list_pinned ⇒ 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.

This method returns an undefined value.



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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'cmd/list.rb', line 218

def list_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?
        display_name = package_name || name
        opoo "#{display_name} not pinned"
      elsif !package_found
        Homebrew.failed = true
      end
      entry
    end
  end

  puts pinned.sort(&Cask::List::TAP_AND_NAME_COMPARISON)
end

#run ⇒ 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.

This method returns an undefined value.



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
# File 'cmd/list.rb', line 94

def run
  args = self.args
  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

  one_per_line = args.public_send(:"1?")
  installed_as_dependency = args.no_installed_on_request?
  filter_flags = {
    "`--installed-on-request`"    => args.installed_on_request?,
    "`--no-installed-on-request`" => installed_as_dependency,
    "`--poured-from-bottle`"      => args.poured_from_bottle?,
    "`--built-from-source`"       => args.built_from_source?,
  }.select { |_, enabled| enabled }.keys

  if args.full_name? && filter_flags.empty?
    full_formula_names = if args.cask?
      []
    elsif args.no_named?
      Formula.racks.map do |rack|
        name = rack.basename.to_s
        tap = begin
          Keg.from_rack(rack)&.tab&.tap
        rescue JSON::ParserError, SystemCallError, Tap::InvalidNameError
          opoo "Could not identify the tap for #{name} from its installation receipt."
          nil
        end
        if tap.nil? || tap.core_tap?
          name
        else
          "#{tap}/#{name}"
        end
      end
    else
      args.named.to_resolved_formulae.map(&:full_name)
    end.sort(&Cask::List::TAP_AND_NAME_COMPARISON)
    full_formula_names = Formatter.columns(full_formula_names) unless one_per_line
    puts full_formula_names if full_formula_names.present?
    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 one_per_line
      puts full_cask_names if full_cask_names.present?
    end
  elsif args.pinned?
    list_pinned
  elsif args.versions? || args.multiple?
    filtered_list unless args.cask?
    list_casks if args.cask? || (!args.formula? && !args.multiple? && args.no_named?)
  elsif filter_flags.any?
    raise UsageError, "Cannot use #{filter_flags.join(", ")} with formula arguments." unless args.no_named?

    formulae = if args.t?
      Formula.installed.sort_by { |formula| formula.rack.mtime }.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
      statuses << "installed as dependency" if 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 filter_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 one_per_line
    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
    unless args.formula?
      if 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
      warn_about_broken_caskroom_symlinks
    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?
      casks.each { |cask| raise Cask::CaskNotInstalledError, cask unless cask.installed? }
      system_command! "find", args: casks.map(&:caskroom_path) + find_args, print_stdout: true if casks.present?
    else
      kegs.each { |keg| PrettyListing.new keg }
      Cask::List.list_casks(*casks, one: one_per_line) if casks.present?
    end
  end
end