aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cmd/tap-info.rb
blob: 26daa1a8ae0621e034eaaab18bc8eb845b9caf6e (plain)
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
#:  * `tap-info`:
#:    Display a brief summary of all installed taps.
#:
#:  * `tap-info` (`--installed`|<taps>):
#:    Display detailed information about one or more <taps>.
#:
#:    Pass `--installed` to display information on all installed taps.
#:
#:  * `tap-info` `--json=`<version> (`--installed`|<taps>):
#:    Print a JSON representation of <taps>. Currently the only accepted value
#:    for <version> is `v1`.
#:
#:    Pass `--installed` to get information on installed taps.
#:
#:    See the docs for examples of using the JSON output:
#:    <https://docs.brew.sh/Querying-Brew>

require "tap"

module Homebrew
  module_function

  def tap_info
    if ARGV.include? "--installed"
      taps = Tap
    else
      taps = ARGV.named.sort.map do |name|
        Tap.fetch(name)
      end
    end

    if ARGV.json == "v1"
      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
      pinned_count = 0
      private_count = 0
      Tap.each do |tap|
        tap_count += 1
        formula_count += tap.formula_files.size
        command_count += tap.command_files.size
        pinned_count += 1 if tap.pinned?
        private_count += 1 if tap.private?
      end
      info = Formatter.pluralize(tap_count, "tap").to_s
      info += ", #{pinned_count} pinned"
      info += ", #{private_count} private"
      info += ", #{Formatter.pluralize(formula_count, "formula")}"
      info += ", #{Formatter.pluralize(command_count, "command")}"
      info += ", #{Tap::TAP_DIRECTORY.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 += tap.pinned? ? "pinned" : "unpinned"
          info += ", private" if tap.private?
          if (formula_count = tap.formula_files.size).positive?
            info += ", #{Formatter.pluralize(formula_count, "formula")}"
          end
          if (command_count = tap.command_files.size).positive?
            info += ", #{Formatter.pluralize(command_count, "command")}"
          end
          info += ", no formulae/commands" if (formula_count + command_count).zero?
          info += "\n#{tap.path} (#{tap.path.abv})"
          info += "\nFrom: #{tap.remote.nil? ? "N/A" : tap.remote}"
        else
          info += "Not installed"
        end
        puts info
      end
    end
  end

  def print_tap_json(taps)
    puts JSON.generate(taps.map(&:to_hash))
  end
end