| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-09 15:40:44 +02:00
										 |  |  | require "search" | 
					
						
							| 
									
										
										
										
											2017-05-21 00:15:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 08:29:14 +02:00
										 |  |  | module Cask | 
					
						
							| 
									
										
										
										
											2018-09-04 08:45:48 +01:00
										 |  |  |   class Cmd | 
					
						
							| 
									
										
										
										
											2021-01-25 09:18:10 +00:00
										 |  |  |     # Abstract superclass for all Cask implementations of commands. | 
					
						
							| 
									
										
										
										
											2020-08-19 10:34:07 +02:00
										 |  |  |     # | 
					
						
							|  |  |  |     # @api private | 
					
						
							| 
									
										
										
										
											2017-05-20 19:08:03 +02:00
										 |  |  |     class AbstractCommand | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       extend T::Sig | 
					
						
							|  |  |  |       extend T::Helpers | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-09 15:40:44 +02:00
										 |  |  |       include Homebrew::Search | 
					
						
							| 
									
										
										
										
											2017-05-21 00:15:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-27 22:53:01 +02:00
										 |  |  |       OPTIONS = [ | 
					
						
							|  |  |  |         [:switch, "--[no-]binaries", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |           description: "Disable/enable linking of helper executables (default: enabled).", | 
					
						
							| 
									
										
										
										
											2020-09-27 22:53:01 +02:00
										 |  |  |           env:         :cask_opts_binaries, | 
					
						
							|  |  |  |         }], | 
					
						
							|  |  |  |         [:switch, "--require-sha",  { | 
					
						
							|  |  |  |           description: "Require all casks to have a checksum.", | 
					
						
							|  |  |  |           env:         :cask_opts_require_sha, | 
					
						
							|  |  |  |         }], | 
					
						
							|  |  |  |         [:switch, "--[no-]quarantine", { | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |           description: "Disable/enable quarantining of downloads (default: enabled).", | 
					
						
							| 
									
										
										
										
											2020-09-27 22:53:01 +02:00
										 |  |  |           env:         :cask_opts_quarantine, | 
					
						
							|  |  |  |         }], | 
					
						
							|  |  |  |       ].freeze | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |       def self.parser(&block) | 
					
						
							|  |  |  |         Cmd.parser do | 
					
						
							| 
									
										
										
										
											2020-11-16 22:18:56 +01:00
										 |  |  |           instance_eval(&block) if block | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-08 01:08:15 +01:00
										 |  |  |           OPTIONS.map(&:dup).each do |option| | 
					
						
							|  |  |  |             kwargs = option.pop | 
					
						
							|  |  |  |             send(*option, **kwargs) | 
					
						
							| 
									
										
										
										
											2020-09-27 22:53:01 +02:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-05-21 00:15:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.command_name | 
					
						
							| 
									
										
										
										
											2016-10-14 20:03:34 +02:00
										 |  |  |         @command_name ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2017-05-20 19:08:03 +02:00
										 |  |  |       def self.abstract? | 
					
						
							| 
									
										
										
										
											2017-05-29 16:36:01 -07:00
										 |  |  |         name.split("::").last.match?(/^Abstract[^a-z]/) | 
					
						
							| 
									
										
										
										
											2017-05-20 19:08:03 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2020-04-07 08:32:30 +02:00
										 |  |  |       def self.visible? | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         true | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.help | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |         parser.generate_help_text | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |       def self.short_description | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |         description[/\A[^.]*\./] | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-20 19:08:03 +02:00
										 |  |  |       def self.run(*args) | 
					
						
							|  |  |  |         new(*args).run | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |       attr_reader :args | 
					
						
							| 
									
										
										
										
											2017-05-21 00:15:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-19 21:13:08 +02:00
										 |  |  |       def initialize(*args) | 
					
						
							| 
									
										
										
										
											2020-08-01 02:30:46 +02:00
										 |  |  |         @args = self.class.parser.parse(args) | 
					
						
							| 
									
										
										
										
											2017-05-19 21:13:08 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-06-13 17:14:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def casks(alternative: -> { [] }) | 
					
						
							| 
									
										
										
										
											2017-09-11 08:37:15 +02:00
										 |  |  |         return @casks if defined?(@casks) | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 23:46:30 +02:00
										 |  |  |         @casks = args.named.empty? ? alternative.call : args.named.to_casks | 
					
						
							| 
									
										
										
										
											2017-09-11 08:37:15 +02:00
										 |  |  |       rescue CaskUnavailableError => e | 
					
						
							| 
									
										
										
										
											2018-06-02 02:47:23 +02:00
										 |  |  |         reason = [e.reason, *suggestion_message(e.token)].join(" ") | 
					
						
							| 
									
										
										
										
											2017-09-11 08:37:15 +02:00
										 |  |  |         raise e.class.new(e.token, reason) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def suggestion_message(cask_token) | 
					
						
							| 
									
										
										
										
											2018-06-09 15:40:44 +02:00
										 |  |  |         matches = search_casks(cask_token) | 
					
						
							| 
									
										
										
										
											2017-06-13 17:14:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-02 02:47:23 +02:00
										 |  |  |         if matches.one? | 
					
						
							| 
									
										
										
										
											2021-01-26 15:21:24 -05:00
										 |  |  |           "Did you mean '#{matches.first}'?" | 
					
						
							| 
									
										
										
										
											2018-06-02 02:47:23 +02:00
										 |  |  |         elsif !matches.empty? | 
					
						
							| 
									
										
										
										
											2019-04-20 14:07:29 +09:00
										 |  |  |           "Did you mean one of these?\n#{Formatter.columns(matches.take(20))}" | 
					
						
							| 
									
										
										
										
											2017-09-11 08:37:15 +02:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2017-06-13 17:14:01 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | end |