aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb
blob: e5c04ee514e6ae5992578b927d4a5c08b63787ee (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
88
89
90
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
module Hbc
  class CLI
    class InternalStanza < InternalUseBase
      # Syntax
      #
      #     brew cask _stanza <stanza_name> [ --table | --yaml | --inspect | --quiet ] [ <cask_token> ... ]
      #
      # If no tokens are given, then data for all Casks is returned.
      #
      # The pseudo-stanza "artifacts" is available.
      #
      # On failure, a blank line is returned on the standard output.
      #
      # Examples
      #
      #     brew cask _stanza appcast   --table
      #     brew cask _stanza app       --table alfred google-chrome adium voicemac logisim vagrant
      #     brew cask _stanza url       --table alfred google-chrome adium voicemac logisim vagrant
      #     brew cask _stanza version   --table alfred google-chrome adium voicemac logisim vagrant
      #     brew cask _stanza artifacts --table --inspect alfred google-chrome adium voicemac logisim vagrant
      #     brew cask _stanza artifacts --table --yaml    alfred google-chrome adium voicemac logisim vagrant
      #

      # TODO: this should be retrievable from Hbc::DSL
      ARTIFACTS = Set.new [
        :app,
        :suite,
        :artifact,
        :prefpane,
        :qlplugin,
        :dictionary,
        :font,
        :service,
        :colorpicker,
        :binary,
        :input_method,
        :internet_plugin,
        :audio_unit_plugin,
        :vst_plugin,
        :vst3_plugin,
        :screen_saver,
        :pkg,
        :installer,
        :stage_only,
        :nested_container,
        :uninstall,
        :postflight,
        :uninstall_postflight,
        :preflight,
        :uninstall_postflight,
      ]

      def self.run(*arguments)
        table = arguments.include? "--table"
        quiet = arguments.include? "--quiet"
        format = :to_yaml if arguments.include? "--yaml"
        format = :inspect if arguments.include? "--inspect"
        cask_tokens = arguments.reject { |arg| arg.chars.first == "-" }
        stanza = cask_tokens.shift.to_sym
        cask_tokens = Hbc.all_tokens if cask_tokens.empty?

        retval = print_stanzas(stanza, format, table, quiet, *cask_tokens)

        # retval is ternary: true/false/nil
        if retval.nil?
          exit 1 if quiet
          raise CaskError, "nothing to print"
        elsif !retval
          exit 1 if quiet
          raise CaskError, "print incomplete"
        end
      end

      def self.print_stanzas(stanza, format = nil, table = nil, quiet = nil, *cask_tokens)
        count = 0
        if ARTIFACTS.include?(stanza)
          artifact_name = stanza
          stanza = :artifacts
        end

        cask_tokens.each do |cask_token|
          print "#{cask_token}\t" if table

          begin
            cask = Hbc.load(cask_token)
          rescue StandardError
            opoo "Cask '#{cask_token}' was not found" unless quiet
            puts ""
            next
          end

          unless cask.respond_to?(stanza)
            opoo "no such stanza '#{stanza}' on Cask '#{cask_token}'" unless quiet
            puts ""
            next
          end

          begin
            value = cask.send(stanza)
          rescue StandardError
            opoo "failure calling '#{stanza}' on Cask '#{cask_token}'" unless quiet
            puts ""
            next
          end

          if artifact_name && !value.key?(artifact_name)
            opoo "no such stanza '#{artifact_name}' on Cask '#{cask_token}'" unless quiet
            puts ""
            next
          end

          value = value.fetch(artifact_name).to_a.flatten if artifact_name

          if format
            puts value.send(format)
          elsif artifact_name || value.is_a?(Symbol)
            puts value.inspect
          else
            puts value.to_s
          end

          count += 1
        end
        count.zero? ? nil : count == cask_tokens.length
      end

      def self.help
        "Extract and render a specific stanza for the given Casks"
      end
    end
  end
end