| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  | # typed: true # rubocop:todo Sorbet/StrictSigil | 
					
						
							|  |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							|  |  |  |   module Bundle | 
					
						
							|  |  |  |     module CaskInstaller | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       def self.reset! | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         @installed_casks = nil | 
					
						
							|  |  |  |         @outdated_casks = nil | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       private_class_method def self.upgrading?(no_upgrade, name, options) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         return false if no_upgrade | 
					
						
							|  |  |  |         return true if outdated_casks.include?(name) | 
					
						
							|  |  |  |         return false unless options[:greedy] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-24 21:55:47 +08:00
										 |  |  |         require "bundle/cask_dumper" | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         Homebrew::Bundle::CaskDumper.cask_is_outdated_using_greedy?(name) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       def self.preinstall(name, no_upgrade: false, verbose: false, **options) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         if installed_casks.include?(name) && !upgrading?(no_upgrade, name, options) | 
					
						
							|  |  |  |           puts "Skipping install of #{name} cask. It is already installed." if verbose | 
					
						
							|  |  |  |           return false | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         true | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       def self.install(name, preinstall: true, no_upgrade: false, verbose: false, force: false, **options) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         return true unless preinstall | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         full_name = options.fetch(:full_name, name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         install_result = if installed_casks.include?(name) && upgrading?(no_upgrade, name, options) | 
					
						
							|  |  |  |           status = "#{options[:greedy] ? "may not be" : "not"} up-to-date" | 
					
						
							|  |  |  |           puts "Upgrading #{name} cask. It is installed but #{status}." if verbose | 
					
						
							|  |  |  |           Bundle.brew("upgrade", "--cask", full_name, verbose:) | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           args = options.fetch(:args, []).filter_map do |k, v| | 
					
						
							|  |  |  |             case v | 
					
						
							|  |  |  |             when TrueClass | 
					
						
							|  |  |  |               "--#{k}" | 
					
						
							|  |  |  |             when FalseClass | 
					
						
							|  |  |  |               nil | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |               "--#{k}=#{v}" | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           args << "--force" if force | 
					
						
							|  |  |  |           args << "--adopt" unless args.include?("--force") | 
					
						
							|  |  |  |           args.uniq! | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           with_args = " with #{args.join(" ")}" if args.present? | 
					
						
							|  |  |  |           puts "Installing #{name} cask#{with_args}. It is not currently installed." if verbose | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if Bundle.brew("install", "--cask", full_name, *args, verbose:) | 
					
						
							|  |  |  |             installed_casks << name | 
					
						
							|  |  |  |             true | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             false | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         result = install_result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if cask_installed?(name) | 
					
						
							|  |  |  |           postinstall_result = postinstall_change_state!(name:, options:, verbose:) | 
					
						
							|  |  |  |           result &&= postinstall_result | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         result | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       private_class_method def self.postinstall_change_state!(name:, options:, verbose:) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         postinstall = options.fetch(:postinstall, nil) | 
					
						
							|  |  |  |         return true if postinstall.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         puts "Running postinstall for #{@name}: #{postinstall}" if verbose | 
					
						
							|  |  |  |         Kernel.system(postinstall) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.cask_installed_and_up_to_date?(cask, no_upgrade: false) | 
					
						
							|  |  |  |         return false unless cask_installed?(cask) | 
					
						
							|  |  |  |         return true if no_upgrade | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         !cask_upgradable?(cask) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       def self.cask_installed?(cask) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         installed_casks.include? cask | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       def self.cask_upgradable?(cask) | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         outdated_casks.include? cask | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       def self.installed_casks | 
					
						
							| 
									
										
										
										
											2025-03-24 21:55:47 +08:00
										 |  |  |         require "bundle/cask_dumper" | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         @installed_casks ||= Homebrew::Bundle::CaskDumper.cask_names | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 04:24:55 +00:00
										 |  |  |       def self.outdated_casks | 
					
						
							| 
									
										
										
										
											2025-03-24 21:55:47 +08:00
										 |  |  |         require "bundle/cask_dumper" | 
					
						
							| 
									
										
										
										
											2025-03-18 17:38:37 +00:00
										 |  |  |         @outdated_casks ||= Homebrew::Bundle::CaskDumper.outdated_cask_names | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |