| 
									
										
										
										
											2024-08-12 10:30:59 +01:00
										 |  |  | # typed: true # rubocop:todo Sorbet/StrictSigil | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-28 11:45:18 -08:00
										 |  |  | require "attrable" | 
					
						
							| 
									
										
										
										
											2024-01-12 09:53:53 -08:00
										 |  |  | require "extend/object/deep_dup" | 
					
						
							| 
									
										
										
										
											2022-08-25 02:52:40 -04: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
										 |  |  |     # Abstract superclass for all artifacts. | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |     class AbstractArtifact | 
					
						
							| 
									
										
										
										
											2023-02-22 09:11:29 -08:00
										 |  |  |       extend T::Helpers | 
					
						
							|  |  |  |       abstract! | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-04 17:08:35 +02:00
										 |  |  |       include Comparable | 
					
						
							| 
									
										
										
										
											2023-12-28 11:45:18 -08:00
										 |  |  |       extend Attrable | 
					
						
							| 
									
										
										
										
											2017-06-26 07:30:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       def self.english_name | 
					
						
							| 
									
										
										
										
											2023-02-22 09:11:29 -08:00
										 |  |  |         @english_name ||= T.must(name).sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1 \2') | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       def self.english_article | 
					
						
							| 
									
										
										
										
											2023-12-27 13:00:48 -08:00
										 |  |  |         @english_article ||= /^[aeiou]/i.match?(english_name) ? "an" : "a" | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       def self.dsl_key | 
					
						
							| 
									
										
										
										
											2023-02-22 09:11:29 -08:00
										 |  |  |         @dsl_key ||= T.must(name).sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase.to_sym | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       def self.dirmethod | 
					
						
							| 
									
										
										
										
											2023-12-14 02:52:30 +00:00
										 |  |  |         @dirmethod ||= :"#{dsl_key}dir" | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 09:11:29 -08:00
										 |  |  |       sig { abstract.returns(String) } | 
					
						
							|  |  |  |       def summarize; end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-25 21:05:35 +01:00
										 |  |  |       def staged_path_join_executable(path) | 
					
						
							|  |  |  |         path = Pathname(path) | 
					
						
							| 
									
										
										
										
											2021-03-21 19:32:15 -07:00
										 |  |  |         path = path.expand_path if path.to_s.start_with?("~") | 
					
						
							| 
									
										
										
										
											2019-02-25 21:05:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         absolute_path = if path.absolute? | 
					
						
							|  |  |  |           path | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           cask.staged_path.join(path) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         FileUtils.chmod "+x", absolute_path if absolute_path.exist? && !absolute_path.executable? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if absolute_path.exist? | 
					
						
							|  |  |  |           absolute_path | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           path | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-04 17:08:35 +02:00
										 |  |  |       def <=>(other) | 
					
						
							| 
									
										
										
										
											2017-10-04 17:46:29 +02:00
										 |  |  |         return unless other.class < AbstractArtifact | 
					
						
							| 
									
										
										
										
											2020-10-09 11:12:06 +02:00
										 |  |  |         return 0 if instance_of?(other.class) | 
					
						
							| 
									
										
										
										
											2017-10-04 17:46:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-04 17:08:35 +02:00
										 |  |  |         @@sort_order ||= [ # rubocop:disable Style/ClassVars | 
					
						
							|  |  |  |           PreflightBlock, | 
					
						
							| 
									
										
										
										
											2017-10-04 17:46:29 +02:00
										 |  |  |           # The `uninstall` stanza should be run first, as it may | 
					
						
							|  |  |  |           # depend on other artifacts still being installed. | 
					
						
							| 
									
										
										
										
											2017-10-04 17:08:35 +02:00
										 |  |  |           Uninstall, | 
					
						
							|  |  |  |           Installer, | 
					
						
							| 
									
										
										
										
											2019-10-24 14:55:38 +02:00
										 |  |  |           # `pkg` should be run before `binary`, so | 
					
						
							|  |  |  |           # targets are created prior to linking. | 
					
						
							|  |  |  |           # `pkg` should be run before `app`, since an `app` could | 
					
						
							|  |  |  |           # contain a nested installer (e.g. `wireshark`). | 
					
						
							|  |  |  |           Pkg, | 
					
						
							| 
									
										
										
										
											2017-10-04 17:46:29 +02:00
										 |  |  |           [ | 
					
						
							|  |  |  |             App, | 
					
						
							|  |  |  |             Suite, | 
					
						
							|  |  |  |             Artifact, | 
					
						
							|  |  |  |             Colorpicker, | 
					
						
							|  |  |  |             Prefpane, | 
					
						
							|  |  |  |             Qlplugin, | 
					
						
							| 
									
										
										
										
											2020-04-05 15:30:37 +02:00
										 |  |  |             Mdimporter, | 
					
						
							| 
									
										
										
										
											2017-10-04 17:46:29 +02:00
										 |  |  |             Dictionary, | 
					
						
							|  |  |  |             Font, | 
					
						
							|  |  |  |             Service, | 
					
						
							|  |  |  |             InputMethod, | 
					
						
							|  |  |  |             InternetPlugin, | 
					
						
							| 
									
										
										
										
											2023-04-14 15:47:33 -04:00
										 |  |  |             KeyboardLayout, | 
					
						
							| 
									
										
										
										
											2017-10-04 17:46:29 +02:00
										 |  |  |             AudioUnitPlugin, | 
					
						
							|  |  |  |             VstPlugin, | 
					
						
							|  |  |  |             Vst3Plugin, | 
					
						
							|  |  |  |             ScreenSaver, | 
					
						
							|  |  |  |           ], | 
					
						
							| 
									
										
										
										
											2017-10-07 22:06:39 +10:00
										 |  |  |           Binary, | 
					
						
							| 
									
										
										
										
											2019-10-24 09:43:59 +03:00
										 |  |  |           Manpage, | 
					
						
							| 
									
										
										
										
											2017-10-04 17:08:35 +02:00
										 |  |  |           PostflightBlock, | 
					
						
							|  |  |  |           Zap, | 
					
						
							| 
									
										
										
										
											2020-07-13 22:48:53 +10:00
										 |  |  |         ].each_with_index.flat_map { |classes, i| Array(classes).map { |c| [c, i] } }.to_h | 
					
						
							| 
									
										
										
										
											2017-10-04 17:08:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-04 17:46:29 +02:00
										 |  |  |         (@@sort_order[self.class] <=> @@sort_order[other.class]).to_i | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # TODO: this sort of logic would make more sense in dsl.rb, or a | 
					
						
							|  |  |  |       #       constructor called from dsl.rb, so long as that isn't slow. | 
					
						
							|  |  |  |       def self.read_script_arguments(arguments, stanza, default_arguments = {}, override_arguments = {}, key = nil) | 
					
						
							|  |  |  |         # TODO: when stanza names are harmonized with class names, | 
					
						
							|  |  |  |         #       stanza may not be needed as an explicit argument | 
					
						
							| 
									
										
										
										
											2017-03-10 09:33:48 +01:00
										 |  |  |         description = key ? "#{stanza} #{key.inspect}" : stanza.to_s | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # backward-compatible string value | 
					
						
							| 
									
										
										
										
											2023-01-25 00:08:15 +01:00
										 |  |  |         arguments = if arguments.is_a?(String) | 
					
						
							|  |  |  |           { executable: arguments } | 
					
						
							| 
									
										
										
										
											2023-01-24 23:34:47 +01:00
										 |  |  |         else | 
					
						
							|  |  |  |           # Avoid mutating the original argument | 
					
						
							| 
									
										
										
										
											2023-01-25 00:08:15 +01:00
										 |  |  |           arguments.dup | 
					
						
							| 
									
										
										
										
											2023-01-24 23:34:47 +01:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # key sanity | 
					
						
							| 
									
										
										
										
											2016-12-31 18:02:42 +01:00
										 |  |  |         permitted_keys = [:args, :input, :executable, :must_succeed, :sudo, :print_stdout, :print_stderr] | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         unknown_keys = arguments.keys - permitted_keys | 
					
						
							|  |  |  |         unless unknown_keys.empty? | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |           opoo "Unknown arguments to #{description} -- " \ | 
					
						
							|  |  |  |                "#{unknown_keys.inspect} (ignored). Running " \ | 
					
						
							| 
									
										
										
										
											2019-04-05 12:24:10 -04:00
										 |  |  |                "`brew update; brew cleanup` will likely fix it." | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2017-05-29 18:24:52 +01:00
										 |  |  |         arguments.select! { |k| permitted_keys.include?(k) } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # key warnings | 
					
						
							|  |  |  |         override_keys = override_arguments.keys | 
					
						
							|  |  |  |         ignored_keys = arguments.keys & override_keys | 
					
						
							|  |  |  |         unless ignored_keys.empty? | 
					
						
							|  |  |  |           onoe "Some arguments to #{description} will be ignored -- :#{unknown_keys.inspect} (overridden)." | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # extract executable | 
					
						
							|  |  |  |         executable = arguments.key?(:executable) ? arguments.delete(:executable) : nil | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         arguments = default_arguments.merge arguments | 
					
						
							|  |  |  |         arguments.merge! override_arguments | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         [executable, arguments] | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-03 13:03:16 +01:00
										 |  |  |       attr_reader :cask | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-25 02:52:40 -04:00
										 |  |  |       def initialize(cask, *dsl_args) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         @cask = cask | 
					
						
							| 
									
										
										
										
											2022-08-25 02:52:40 -04:00
										 |  |  |         @dsl_args = dsl_args.deep_dup | 
					
						
							| 
									
										
										
										
											2019-02-03 13:03:16 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def config | 
					
						
							|  |  |  |         cask.config | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       def to_s | 
					
						
							|  |  |  |         "#{summarize} (#{self.class.english_name})" | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2022-08-25 02:52:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |       def to_args | 
					
						
							| 
									
										
										
										
											2024-01-26 13:24:32 -08:00
										 |  |  |         @dsl_args.compact_blank | 
					
						
							| 
									
										
										
										
											2022-08-25 02:52:40 -04:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |