| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-18 00:23:23 +01:00
										 |  |  | require "cask/dsl" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 08:29:14 +02:00
										 |  |  | module Cask | 
					
						
							| 
									
										
										
										
											2018-09-04 08:45:48 +01:00
										 |  |  |   class Cmd | 
					
						
							| 
									
										
										
										
											2020-08-19 10:34:07 +02:00
										 |  |  |     # Implementation of the `brew cask _stanza` command. | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # @api private | 
					
						
							| 
									
										
										
										
											2017-05-20 19:08:03 +02:00
										 |  |  |     class InternalStanza < AbstractInternalCommand | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       # Syntax | 
					
						
							|  |  |  |       # | 
					
						
							| 
									
										
										
										
											2017-08-04 14:59:18 +02:00
										 |  |  |       #     brew cask _stanza <stanza_name> [ --quiet ] [ --table | --yaml ] [ <cask_token> ... ] | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       # | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |       # If no tokens are given, then data for all casks is returned. | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       # | 
					
						
							|  |  |  |       # The pseudo-stanza "artifacts" is available. | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # On failure, a blank line is returned on the standard output. | 
					
						
							|  |  |  |       # | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       ARTIFACTS = | 
					
						
							| 
									
										
										
										
											2019-04-19 21:46:20 +09:00
										 |  |  |         (DSL::ORDINARY_ARTIFACT_CLASSES.map(&:dsl_key) + | 
					
						
							|  |  |  |          DSL::ARTIFACT_BLOCK_CLASSES.map(&:dsl_key)).freeze | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { override.returns(T.nilable(T.any(Integer, Symbol))) } | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |       def self.min_named | 
					
						
							|  |  |  |         1
 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |       def self.banner_args | 
					
						
							|  |  |  |         " <stanza_name> [<cask>]" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |       def self.description | 
					
						
							|  |  |  |         <<~EOS | 
					
						
							|  |  |  |           Extract and render a specific stanza for the given <cask>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           Examples: | 
					
						
							|  |  |  |             `brew cask _stanza appcast   --table` | 
					
						
							|  |  |  |             `brew cask _stanza app       --table           alfred google-chrome vagrant` | 
					
						
							|  |  |  |             `brew cask _stanza url       --table           alfred google-chrome vagrant` | 
					
						
							|  |  |  |             `brew cask _stanza version   --table           alfred google-chrome vagrant` | 
					
						
							|  |  |  |             `brew cask _stanza artifacts --table           alfred google-chrome vagrant` | 
					
						
							|  |  |  |             `brew cask _stanza artifacts --table --yaml    alfred google-chrome vagrant` | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.parser | 
					
						
							|  |  |  |         super do | 
					
						
							|  |  |  |           switch "--table", | 
					
						
							|  |  |  |                  description: "Print stanza in table format." | 
					
						
							|  |  |  |           switch "--quiet", | 
					
						
							|  |  |  |                  description: "" | 
					
						
							|  |  |  |           switch "--yaml", | 
					
						
							|  |  |  |                  description: "" | 
					
						
							|  |  |  |           switch "--inspect", | 
					
						
							|  |  |  |                  description: "" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-05-21 00:15:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-27 12:57:29 +01:00
										 |  |  |       attr_accessor :format, :stanza | 
					
						
							| 
									
										
										
										
											2017-05-21 00:15:56 +02:00
										 |  |  |       private :format, :format= | 
					
						
							|  |  |  |       private :stanza, :stanza= | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def initialize(*) | 
					
						
							|  |  |  |         super | 
					
						
							| 
									
										
										
										
											2016-12-31 13:39:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |         named = args.named.dup | 
					
						
							|  |  |  |         @stanza = named.shift.to_sym | 
					
						
							|  |  |  |         args.freeze_named_args!(named) | 
					
						
							| 
									
										
										
										
											2017-05-21 00:15:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |         @format = :to_yaml if args.yaml? | 
					
						
							| 
									
										
										
										
											2017-10-03 19:35:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-04 06:03:13 +02:00
										 |  |  |         return if DSL::DSL_METHODS.include?(stanza) | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |         raise UsageError, <<~EOS | 
					
						
							|  |  |  |           Unknown/unsupported stanza: '#{stanza}' | 
					
						
							|  |  |  |           Check cask reference for supported stanzas. | 
					
						
							|  |  |  |         EOS | 
					
						
							| 
									
										
										
										
											2017-05-20 03:19:41 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { void } | 
					
						
							| 
									
										
										
										
											2017-05-20 03:19:41 +02:00
										 |  |  |       def run | 
					
						
							| 
									
										
										
										
											2017-05-21 00:15:56 +02:00
										 |  |  |         if ARTIFACTS.include?(stanza) | 
					
						
							|  |  |  |           artifact_name = stanza | 
					
						
							| 
									
										
										
										
											2017-05-20 03:19:41 +02:00
										 |  |  |           @stanza = :artifacts | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-14 11:32:29 +02:00
										 |  |  |         casks(alternative: -> { Cask.to_a }).each do |cask| | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |           print "#{cask}\t" if args.table? | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |           begin | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |             value = cask.send(stanza) | 
					
						
							| 
									
										
										
										
											2018-09-02 20:14:54 +01:00
										 |  |  |           rescue | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |             opoo "failure calling '#{stanza}' on Cask '#{cask}'" unless args.quiet? | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |             puts "" | 
					
						
							|  |  |  |             next | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 08:38:46 +02:00
										 |  |  |           if stanza == :artifacts | 
					
						
							| 
									
										
										
										
											2017-10-22 09:57:40 +02:00
										 |  |  |             value = Hash[value.map { |v| [v.class.dsl_key, v.to_s] }] | 
					
						
							| 
									
										
										
										
											2017-10-14 17:39:26 +02:00
										 |  |  |             value = value[artifact_name] if artifact_name | 
					
						
							| 
									
										
										
										
											2017-10-14 08:38:46 +02:00
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 19:39:49 +01:00
										 |  |  |           if value.nil? || (value.respond_to?(:empty?) && value.empty?) | 
					
						
							| 
									
										
										
										
											2018-06-06 23:34:19 -04:00
										 |  |  |             stanza_name = artifact_name || stanza | 
					
						
							| 
									
										
										
										
											2017-10-22 13:43:17 +02:00
										 |  |  |             raise CaskError, "no such stanza '#{stanza_name}' on Cask '#{cask}'" | 
					
						
							| 
									
										
										
										
											2017-08-04 14:59:18 +02:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |           if format | 
					
						
							|  |  |  |             puts value.send(format) | 
					
						
							| 
									
										
										
										
											2017-08-04 14:59:18 +02:00
										 |  |  |           elsif value.is_a?(Symbol) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |             puts value.inspect | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             puts value.to_s | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |