| 
									
										
										
										
											2023-02-22 09:11:29 -08:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-03 19:39:07 +01:00
										 |  |  | require "cask/artifact/abstract_artifact" | 
					
						
							| 
									
										
										
										
											2024-01-12 09:38:49 -08:00
										 |  |  | require "extend/hash/keys" | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 08:29:14 +02:00
										 |  |  | module Cask | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |   module Artifact | 
					
						
							| 
									
										
										
										
											2020-08-19 10:23:41 +02:00
										 |  |  |     # Artifact corresponding to the `installer` stanza. | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # @api private | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |     class Installer < AbstractArtifact | 
					
						
							| 
									
										
										
										
											2019-04-19 21:46:20 +09:00
										 |  |  |       VALID_KEYS = Set.new([ | 
					
						
							| 
									
										
										
										
											2021-02-11 13:24:19 +00:00
										 |  |  |         :manual, | 
					
						
							|  |  |  |         :script, | 
					
						
							|  |  |  |       ]).freeze | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 10:23:41 +02:00
										 |  |  |       # Extension module for manual installers. | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       module ManualInstaller | 
					
						
							|  |  |  |         def install_phase(**) | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |           puts <<~EOS | 
					
						
							| 
									
										
										
										
											2023-04-14 20:07:01 +02:00
										 |  |  |             Cask #{cask} only provides a manual installer. To run it and complete the installation: | 
					
						
							|  |  |  |               open #{cask.staged_path.join(path).to_s.shellescape} | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 10:23:41 +02:00
										 |  |  |       # Extension module for script installers. | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       module ScriptInstaller | 
					
						
							|  |  |  |         def install_phase(command: nil, **_) | 
					
						
							| 
									
										
										
										
											2023-02-22 09:11:29 -08:00
										 |  |  |           # TODO: The `T.unsafe` is a false positive that is unnecessary in newer releasese of Sorbet | 
					
						
							|  |  |  |           # (confirmend with sorbet v0.5.10672) | 
					
						
							|  |  |  |           ohai "Running #{T.unsafe(self.class).dsl_key} script '#{path}'" | 
					
						
							| 
									
										
										
										
											2018-07-11 16:39:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-25 21:05:35 +01:00
										 |  |  |           executable_path = staged_path_join_executable(path) | 
					
						
							| 
									
										
										
										
											2018-07-11 16:39:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |           command.run!( | 
					
						
							| 
									
										
										
										
											2019-02-25 21:05:35 +01:00
										 |  |  |             executable_path, | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |             **args, | 
					
						
							|  |  |  |             env: { "PATH" => PATH.new( | 
					
						
							| 
									
										
										
										
											2022-06-15 05:40:43 +01:00
										 |  |  |               HOMEBREW_PREFIX/"bin", HOMEBREW_PREFIX/"sbin", ENV.fetch("PATH") | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |             ) }, | 
					
						
							|  |  |  |           ) | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.from_args(cask, **args) | 
					
						
							|  |  |  |         raise CaskInvalidError.new(cask, "'installer' stanza requires an argument.") if args.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if args.key?(:script) && !args[:script].respond_to?(:key?) | 
					
						
							|  |  |  |           if args.key?(:executable) | 
					
						
							|  |  |  |             raise CaskInvalidError.new(cask, "'installer' stanza gave arguments for both :script and :executable.") | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |           args[:executable] = args[:script] | 
					
						
							|  |  |  |           args.delete(:script) | 
					
						
							|  |  |  |           args = { script: args } | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 15:06:50 -07:00
										 |  |  |         if args.keys.count != 1
 | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |           raise CaskInvalidError.new( | 
					
						
							|  |  |  |             cask, | 
					
						
							|  |  |  |             "invalid 'installer' stanza: Only one of #{VALID_KEYS.inspect} is permitted.", | 
					
						
							|  |  |  |           ) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-19 17:31:51 -07:00
										 |  |  |         args.assert_valid_keys(*VALID_KEYS) | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |         new(cask, **args) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       attr_reader :path, :args | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def initialize(cask, **args) | 
					
						
							| 
									
										
										
										
											2022-08-25 02:52:40 -04:00
										 |  |  |         super(cask, **args) | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if args.key?(:manual) | 
					
						
							| 
									
										
										
										
											2018-07-11 16:39:17 +02:00
										 |  |  |           @path = Pathname(args[:manual]) | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |           @args = [] | 
					
						
							|  |  |  |           extend(ManualInstaller) | 
					
						
							|  |  |  |           return | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         path, @args = self.class.read_script_arguments( | 
					
						
							|  |  |  |           args[:script], self.class.dsl_key.to_s, { must_succeed: true, sudo: false }, print_stdout: true | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         raise CaskInvalidError.new(cask, "#{self.class.dsl_key} missing executable") if path.nil? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 16:39:17 +02:00
										 |  |  |         @path = Pathname(path) | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |         extend(ScriptInstaller) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def summarize | 
					
						
							| 
									
										
										
										
											2018-07-11 16:39:17 +02:00
										 |  |  |         path.to_s | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-08-04 14:59:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       def to_h | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         { path: }.tap do |h| | 
					
						
							| 
									
										
										
										
											2017-08-04 14:59:18 +02:00
										 |  |  |           h[:args] = args unless is_a?(ManualInstaller) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | end |