2020-10-10 14:16:11 +02:00
|
|
|
# typed: false
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-06-07 17:49:07 -05:00
|
|
|
require "metafiles"
|
2015-06-20 22:48:45 +08:00
|
|
|
require "formula"
|
2019-04-17 18:25:08 +09:00
|
|
|
require "cli/parser"
|
2023-03-01 21:22:11 +09:00
|
|
|
require "cask/list"
|
2014-06-07 17:49:07 -05:00
|
|
|
|
2014-06-18 22:41:47 -05:00
|
|
|
module Homebrew
|
2020-10-20 12:03:48 +02:00
|
|
|
extend T::Sig
|
|
|
|
|
2016-09-26 01:44:51 +02:00
|
|
|
module_function
|
|
|
|
|
2020-10-20 12:03:48 +02:00
|
|
|
sig { returns(CLI::Parser) }
|
2018-11-11 19:56:53 +05:30
|
|
|
def list_args
|
|
|
|
Homebrew::CLI::Parser.new do
|
2021-01-15 15:04:02 -05:00
|
|
|
description <<~EOS
|
2020-11-12 10:40:48 -05:00
|
|
|
List all installed formulae and casks.
|
2019-08-19 22:44:50 -04:00
|
|
|
|
2019-10-30 18:52:02 +00:00
|
|
|
If <formula> is provided, summarise the paths within its current keg.
|
2020-11-23 20:21:03 -05:00
|
|
|
If <cask> is provided, list its artifacts.
|
2018-11-11 19:56:53 +05:30
|
|
|
EOS
|
2020-11-03 12:29:58 +00:00
|
|
|
switch "--formula", "--formulae",
|
2020-11-23 20:21:03 -05:00
|
|
|
description: "List only formulae, or treat all named arguments as formulae."
|
2020-11-03 12:29:58 +00:00
|
|
|
switch "--cask", "--casks",
|
2020-11-23 20:21:03 -05:00
|
|
|
description: "List only casks, or treat all named arguments as casks."
|
2018-11-11 19:56:53 +05:30
|
|
|
switch "--full-name",
|
2022-06-28 10:09:59 +01:00
|
|
|
description: "Print formulae with fully-qualified names. Unless `--full-name`, `--versions` " \
|
|
|
|
"or `--pinned` are passed, other options (i.e. `-1`, `-l`, `-r` and `-t`) are " \
|
2020-11-23 20:21:03 -05:00
|
|
|
"passed to `ls`(1) which produces the actual output."
|
2018-11-11 19:56:53 +05:30
|
|
|
switch "--versions",
|
2022-06-28 10:09:59 +01:00
|
|
|
description: "Show the version number for installed formulae, or only the specified " \
|
2019-08-20 00:04:14 -04:00
|
|
|
"formulae if <formula> are provided."
|
2018-11-11 19:56:53 +05:30
|
|
|
switch "--multiple",
|
2019-04-30 08:44:35 +01:00
|
|
|
depends_on: "--versions",
|
|
|
|
description: "Only show formulae with multiple versions installed."
|
2018-11-11 19:56:53 +05:30
|
|
|
switch "--pinned",
|
2022-06-28 10:09:59 +01:00
|
|
|
description: "List only pinned formulae, or only the specified (pinned) " \
|
2019-08-20 00:04:14 -04:00
|
|
|
"formulae if <formula> are provided. See also `pin`, `unpin`."
|
2018-05-29 18:45:46 +01:00
|
|
|
# passed through to ls
|
2018-11-11 19:56:53 +05:30
|
|
|
switch "-1",
|
2019-04-30 08:44:35 +01:00
|
|
|
description: "Force output to be one entry per line. " \
|
|
|
|
"This is the default when output is not to a terminal."
|
2018-11-11 19:56:53 +05:30
|
|
|
switch "-l",
|
2021-03-26 18:54:53 +00:00
|
|
|
description: "List formulae and/or casks in long format. " \
|
|
|
|
"Has no effect when a formula or cask name is passed as an argument."
|
2018-11-11 19:56:53 +05:30
|
|
|
switch "-r",
|
2021-03-26 18:54:53 +00:00
|
|
|
description: "Reverse the order of the formulae and/or casks sort to list the oldest entries first. " \
|
|
|
|
"Has no effect when a formula or cask name is passed as an argument."
|
2018-11-24 11:23:14 +00:00
|
|
|
switch "-t",
|
2021-03-26 18:54:53 +00:00
|
|
|
description: "Sort formulae and/or casks by time modified, listing most recently modified first. " \
|
|
|
|
"Has no effect when a formula or cask name is passed as an argument."
|
2020-11-03 12:29:58 +00:00
|
|
|
|
2020-11-23 20:21:03 -05:00
|
|
|
conflicts "--formula", "--cask"
|
2021-03-26 19:18:48 +00:00
|
|
|
conflicts "--pinned", "--cask"
|
2022-03-27 18:27:13 -04:00
|
|
|
conflicts "--multiple", "--cask"
|
|
|
|
conflicts "--pinned", "--multiple"
|
2020-11-03 12:29:58 +00:00
|
|
|
["-1", "-l", "-r", "-t"].each do |flag|
|
|
|
|
conflicts "--versions", flag
|
2020-11-23 20:21:03 -05:00
|
|
|
conflicts "--pinned", flag
|
2020-11-03 12:29:58 +00:00
|
|
|
end
|
2022-03-27 18:27:13 -04:00
|
|
|
["--versions", "--pinned", "-l", "-r", "-t"].each do |flag|
|
2020-11-23 20:21:03 -05:00
|
|
|
conflicts "--full-name", flag
|
2020-08-06 09:25:45 +01:00
|
|
|
end
|
2021-01-10 14:26:40 -05:00
|
|
|
|
|
|
|
named_args [:installed_formula, :installed_cask]
|
2018-04-13 03:02:47 -05:00
|
|
|
end
|
2018-11-11 19:56:53 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def list
|
2020-07-30 18:40:10 +02:00
|
|
|
args = list_args.parse
|
2018-04-13 03:02:47 -05:00
|
|
|
|
2020-11-23 20:21:03 -05:00
|
|
|
if args.full_name?
|
|
|
|
unless args.cask?
|
|
|
|
formula_names = args.no_named? ? Formula.installed : args.named.to_resolved_formulae
|
|
|
|
full_formula_names = formula_names.map(&:full_name).sort(&tap_and_name_comparison)
|
2021-06-02 11:43:16 +05:30
|
|
|
full_formula_names = Formatter.columns(full_formula_names) unless args.public_send(:"1?")
|
2020-12-01 17:04:59 +00:00
|
|
|
puts full_formula_names if full_formula_names.present?
|
2020-11-23 20:21:03 -05:00
|
|
|
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
|
|
|
|
full_cask_names = cask_names.map(&:full_name).sort(&tap_and_name_comparison)
|
2021-06-02 11:43:16 +05:30
|
|
|
full_cask_names = Formatter.columns(full_cask_names) unless args.public_send(:"1?")
|
2020-12-01 17:04:59 +00:00
|
|
|
puts full_cask_names if full_cask_names.present?
|
2020-11-23 20:21:03 -05:00
|
|
|
end
|
2022-03-27 18:27:13 -04:00
|
|
|
elsif args.pinned?
|
|
|
|
filtered_list(args: args)
|
|
|
|
elsif args.versions?
|
|
|
|
filtered_list(args: args) unless args.cask?
|
|
|
|
list_casks(args: args) if args.cask? || (!args.formula? && !args.multiple? && args.no_named?)
|
2020-03-04 17:28:15 +00:00
|
|
|
elsif args.no_named?
|
2020-11-23 20:21:03 -05:00
|
|
|
ENV["CLICOLOR"] = nil
|
2020-07-30 18:40:10 +02:00
|
|
|
|
2020-11-23 20:21:03 -05:00
|
|
|
ls_args = []
|
2021-06-02 11:43:16 +05:30
|
|
|
ls_args << "-1" if args.public_send(:"1?")
|
2020-11-23 20:21:03 -05:00
|
|
|
ls_args << "-l" if args.l?
|
|
|
|
ls_args << "-r" if args.r?
|
|
|
|
ls_args << "-t" if args.t?
|
2020-07-30 18:40:10 +02:00
|
|
|
|
2022-03-27 18:27:13 -04:00
|
|
|
if !args.cask? && HOMEBREW_CELLAR.exist? && HOMEBREW_CELLAR.children.any?
|
2021-03-22 17:15:56 +00:00
|
|
|
ohai "Formulae" if $stdout.tty? && !args.formula?
|
|
|
|
safe_system "ls", *ls_args, HOMEBREW_CELLAR
|
2022-08-13 12:21:14 -07:00
|
|
|
puts if $stdout.tty? && !args.formula?
|
2021-03-22 17:15:56 +00:00
|
|
|
end
|
2022-03-30 17:25:00 +01:00
|
|
|
if !args.formula? && Cask::Caskroom.any_casks_installed?
|
2022-08-13 12:21:14 -07:00
|
|
|
ohai "Casks" if $stdout.tty? && !args.cask?
|
2021-03-26 18:54:53 +00:00
|
|
|
safe_system "ls", *ls_args, Cask::Caskroom.path
|
2021-03-22 05:18:29 +00:00
|
|
|
end
|
2010-09-11 20:22:54 +01:00
|
|
|
else
|
2021-11-11 13:52:46 -05:00
|
|
|
kegs, casks = args.named.to_kegs_to_casks
|
2021-09-09 22:16:45 +09:00
|
|
|
|
2022-03-27 18:27:13 -04:00
|
|
|
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?
|
|
|
|
list_casks(args: args) if casks.present?
|
|
|
|
end
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
|
|
|
end
|
2012-08-28 10:59:46 -07:00
|
|
|
|
2020-08-01 10:56:21 -04:00
|
|
|
def filtered_list(args:)
|
2020-03-04 17:28:15 +00:00
|
|
|
names = if args.no_named?
|
2015-08-13 20:35:22 +08:00
|
|
|
Formula.racks
|
2012-08-28 10:59:46 -07:00
|
|
|
else
|
2020-03-04 17:28:15 +00:00
|
|
|
racks = args.named.map { |n| Formulary.to_rack(n) }
|
2016-10-02 12:18:07 +03:00
|
|
|
racks.select do |rack|
|
|
|
|
Homebrew.failed = true unless rack.exist?
|
|
|
|
rack.exist?
|
|
|
|
end
|
2012-08-28 10:59:46 -07:00
|
|
|
end
|
2018-05-05 18:40:01 +05:30
|
|
|
if args.pinned?
|
2014-09-06 19:12:03 -07:00
|
|
|
pinned_versions = {}
|
2017-11-07 12:35:40 +10:00
|
|
|
names.sort.each do |d|
|
2016-09-15 18:28:42 +01:00
|
|
|
keg_pin = (HOMEBREW_PINNED_KEGS/d.basename.to_s)
|
2019-02-19 13:11:32 +00:00
|
|
|
pinned_versions[d] = keg_pin.readlink.basename.to_s if keg_pin.exist? || keg_pin.symlink?
|
2014-09-06 19:12:03 -07:00
|
|
|
end
|
|
|
|
pinned_versions.each do |d, version|
|
2018-05-05 18:40:01 +05:30
|
|
|
puts d.basename.to_s.concat(args.versions? ? " #{version}" : "")
|
2014-09-06 19:12:03 -07:00
|
|
|
end
|
|
|
|
else # --versions without --pinned
|
2017-11-06 19:31:57 +10:00
|
|
|
names.sort.each do |d|
|
2014-09-06 19:12:03 -07:00
|
|
|
versions = d.subdirs.map { |pn| pn.basename.to_s }
|
2018-05-05 18:40:01 +05:30
|
|
|
next if args.multiple? && versions.length < 2
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-06-01 16:06:51 +02:00
|
|
|
puts "#{d.basename} #{versions * " "}"
|
2014-09-06 19:12:03 -07:00
|
|
|
end
|
2013-03-11 16:41:08 +01:00
|
|
|
end
|
|
|
|
end
|
2020-07-08 12:02:42 -04:00
|
|
|
|
2022-03-27 18:27:13 -04:00
|
|
|
def list_casks(args:)
|
|
|
|
casks = if args.no_named?
|
|
|
|
Cask::Caskroom.casks
|
|
|
|
else
|
|
|
|
args.named.dup.delete_if do |n|
|
|
|
|
Homebrew.failed = true unless Cask::Caskroom.path.join(n).exist?
|
|
|
|
!Cask::Caskroom.path.join(n).exist?
|
|
|
|
end.to_formulae_and_casks(only: :cask)
|
|
|
|
end
|
2022-04-13 10:25:21 -04:00
|
|
|
return if casks.blank?
|
|
|
|
|
2023-03-01 21:22:11 +09:00
|
|
|
Cask::List.list_casks(
|
2021-09-08 22:53:27 +09:00
|
|
|
*casks,
|
2021-06-02 11:43:16 +05:30
|
|
|
one: args.public_send(:"1?"),
|
2020-08-10 16:16:24 -04:00
|
|
|
full_name: args.full_name?,
|
|
|
|
versions: args.versions?,
|
|
|
|
)
|
2020-07-08 12:02:42 -04:00
|
|
|
end
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
class PrettyListing
|
2015-08-03 13:09:07 +01:00
|
|
|
def initialize(path)
|
2014-07-01 13:32:53 -05:00
|
|
|
Pathname.new(path).children.sort_by { |p| p.to_s.downcase }.each do |pn|
|
2010-09-11 20:22:54 +01:00
|
|
|
case pn.basename.to_s
|
2015-08-03 13:09:07 +01:00
|
|
|
when "bin", "sbin"
|
2010-09-11 20:22:54 +01:00
|
|
|
pn.find { |pnn| puts pnn unless pnn.directory? }
|
2015-08-03 13:09:07 +01:00
|
|
|
when "lib"
|
2010-09-11 20:22:54 +01:00
|
|
|
print_dir pn do |pnn|
|
|
|
|
# dylibs have multiple symlinks and we don't care about them
|
2015-08-03 13:09:07 +01:00
|
|
|
(pnn.extname == ".dylib" || pnn.extname == ".pc") && !pnn.symlink?
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
2016-09-20 14:42:29 -07:00
|
|
|
when ".brew"
|
2016-11-13 23:37:51 +01:00
|
|
|
next # Ignore .brew
|
2010-09-11 20:22:54 +01:00
|
|
|
else
|
|
|
|
if pn.directory?
|
|
|
|
if pn.symlink?
|
|
|
|
puts "#{pn} -> #{pn.readlink}"
|
|
|
|
else
|
|
|
|
print_dir pn
|
|
|
|
end
|
2014-06-07 17:49:07 -05:00
|
|
|
elsif Metafiles.list?(pn.basename.to_s)
|
2010-09-11 20:22:54 +01:00
|
|
|
puts pn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def print_dir(root)
|
2010-09-11 20:22:54 +01:00
|
|
|
dirs = []
|
|
|
|
remaining_root_files = []
|
2015-08-03 13:09:07 +01:00
|
|
|
other = ""
|
2010-09-11 20:22:54 +01:00
|
|
|
|
|
|
|
root.children.sort.each do |pn|
|
|
|
|
if pn.directory?
|
|
|
|
dirs << pn
|
2015-08-06 15:45:52 +08:00
|
|
|
elsif block_given? && yield(pn)
|
2010-09-11 20:22:54 +01:00
|
|
|
puts pn
|
2015-08-03 13:09:07 +01:00
|
|
|
other = "other "
|
2010-09-11 20:22:54 +01:00
|
|
|
else
|
2015-08-03 13:09:07 +01:00
|
|
|
remaining_root_files << pn unless pn.basename.to_s == ".DS_Store"
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
dirs.each do |d|
|
|
|
|
files = []
|
|
|
|
d.find { |pn| files << pn unless pn.directory? }
|
|
|
|
print_remaining_files files, d
|
|
|
|
end
|
|
|
|
|
|
|
|
print_remaining_files remaining_root_files, root, other
|
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def print_remaining_files(files, root, other = "")
|
2016-11-13 23:37:51 +01:00
|
|
|
if files.length == 1
|
2010-09-11 20:22:54 +01:00
|
|
|
puts files
|
2016-11-13 23:37:51 +01:00
|
|
|
elsif files.length > 1
|
2010-09-11 20:22:54 +01:00
|
|
|
puts "#{root}/ (#{files.length} #{other}files)"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|