forked from Homebrew/brew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tap-info.rb
90 lines (78 loc) 路 2.47 KB
/
tap-info.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# typed: false
# frozen_string_literal: true
require "cli/parser"
module Homebrew
extend T::Sig
module_function
sig { returns(CLI::Parser) }
def tap_info_args
Homebrew::CLI::Parser.new do
description <<~EOS
Show detailed information about one or more <tap>s.
If no <tap> names are provided, display brief statistics for all installed taps.
EOS
switch "--installed",
description: "Show information on each installed tap."
flag "--json",
description: "Print a JSON representation of <tap>. Currently the default and only accepted " \
"value for <version> is `v1`. See the docs for examples of using the JSON " \
"output: <https://docs.brew.sh/Querying-Brew>"
named_args :tap
end
end
def tap_info
args = tap_info_args.parse
taps = if args.installed?
Tap
else
args.named.to_taps
end
if args.json
raise UsageError, "invalid JSON version: #{args.json}" unless ["v1", true].include? args.json
print_tap_json(taps.sort_by(&:to_s))
else
print_tap_info(taps.sort_by(&:to_s))
end
end
def print_tap_info(taps)
if taps.none?
tap_count = 0
formula_count = 0
command_count = 0
private_count = 0
Tap.each do |tap|
tap_count += 1
formula_count += tap.formula_files.size
command_count += tap.command_files.size
private_count += 1 if tap.private?
end
info = "#{tap_count} #{Utils.pluralize("tap", tap_count)}"
info += ", #{private_count} private"
info += ", #{formula_count} #{Utils.pluralize("formula", formula_count, plural: "e")}"
info += ", #{command_count} #{Utils.pluralize("command", command_count)}"
info += ", #{Tap::TAP_DIRECTORY.dup.abv}" if Tap::TAP_DIRECTORY.directory?
puts info
else
taps.each_with_index do |tap, i|
puts unless i.zero?
info = "#{tap}: "
if tap.installed?
info += if (contents = tap.contents).blank?
"no commands/casks/formulae"
else
contents.join(", ")
end
info += ", private" if tap.private?
info += "\n#{tap.path} (#{tap.path.abv})"
info += "\nFrom: #{tap.remote.presence || "N/A"}"
else
info += "Not installed"
end
puts info
end
end
end
def print_tap_json(taps)
puts JSON.pretty_generate(taps.map(&:to_hash))
end
end