| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  | module Hbc | 
					
						
							|  |  |  |   class CLI | 
					
						
							| 
									
										
										
										
											2017-05-20 19:08:03 +02:00
										 |  |  |     class InternalStanza < AbstractInternalCommand | 
					
						
							| 
									
										
										
										
											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
										 |  |  |       # | 
					
						
							|  |  |  |       # 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 | 
					
						
							| 
									
										
										
										
											2017-08-04 14:59:18 +02:00
										 |  |  |       #     brew cask _stanza app       --table           alfred google-chrome adium vagrant | 
					
						
							|  |  |  |       #     brew cask _stanza url       --table           alfred google-chrome adium vagrant | 
					
						
							|  |  |  |       #     brew cask _stanza version   --table           alfred google-chrome adium vagrant | 
					
						
							|  |  |  |       #     brew cask _stanza artifacts --table           alfred google-chrome adium vagrant | 
					
						
							|  |  |  |       #     brew cask _stanza artifacts --table --yaml    alfred google-chrome adium vagrant | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       # | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       ARTIFACTS = | 
					
						
							|  |  |  |         DSL::ORDINARY_ARTIFACT_CLASSES.map(&:dsl_key) + | 
					
						
							|  |  |  |         DSL::ARTIFACT_BLOCK_CLASSES.map(&:dsl_key) | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-21 00:15:56 +02:00
										 |  |  |       option "--table",   :table,   false | 
					
						
							|  |  |  |       option "--quiet",   :quiet,   false | 
					
						
							|  |  |  |       option "--yaml",    :yaml,    false | 
					
						
							|  |  |  |       option "--inspect", :inspect, false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       attr_accessor :format | 
					
						
							|  |  |  |       private :format, :format= | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       attr_accessor :stanza | 
					
						
							|  |  |  |       private :stanza, :stanza= | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def initialize(*) | 
					
						
							|  |  |  |         super | 
					
						
							| 
									
										
										
										
											2016-12-31 13:39:30 +01:00
										 |  |  |         raise ArgumentError, "No stanza given." if args.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-21 00:15:56 +02:00
										 |  |  |         @stanza = args.shift.to_sym | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @format = :to_yaml if yaml? | 
					
						
							| 
									
										
										
										
											2017-05-20 03:19:41 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 17:14:01 +02:00
										 |  |  |         casks(alternative: -> { Hbc.all }).each do |cask| | 
					
						
							|  |  |  |           print "#{cask}\t" if table? | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-21 00:15:56 +02:00
										 |  |  |           unless cask.respond_to?(stanza) | 
					
						
							| 
									
										
										
										
											2017-06-13 17:14:01 +02:00
										 |  |  |             opoo "no such stanza '#{stanza}' on Cask '#{cask}'" unless quiet? | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |             puts "" | 
					
						
							|  |  |  |             next | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |           rescue StandardError | 
					
						
							| 
									
										
										
										
											2017-06-13 17:14:01 +02:00
										 |  |  |             opoo "failure calling '#{stanza}' on Cask '#{cask}'" unless quiet? | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |             puts "" | 
					
						
							|  |  |  |             next | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |           if artifact_name && !value.key?(artifact_name) | 
					
						
							| 
									
										
										
										
											2017-06-13 17:14:01 +02:00
										 |  |  |             opoo "no such stanza '#{artifact_name}' on Cask '#{cask}'" unless quiet? | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |             puts "" | 
					
						
							|  |  |  |             next | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 14:59:18 +02:00
										 |  |  |           if stanza == :artifacts | 
					
						
							|  |  |  |             value = Hash[ | 
					
						
							|  |  |  |               value.map do |k, v| | 
					
						
							|  |  |  |                 v = v.map do |a| | 
					
						
							|  |  |  |                   next a.to_a if a.respond_to?(:to_a) | 
					
						
							|  |  |  |                   next a.to_h if a.respond_to?(:to_h) | 
					
						
							|  |  |  |                   a | 
					
						
							|  |  |  |                 end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 [k, v] | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             value = value.fetch(artifact_name) if artifact_name | 
					
						
							|  |  |  |           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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.help | 
					
						
							| 
									
										
										
										
											2017-01-23 01:16:07 +00:00
										 |  |  |         "extract and render a specific stanza for the given Casks" | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |