| 
									
										
										
										
											2024-08-12 10:30:59 +01:00
										 |  |  | # typed: true # rubocop:todo Sorbet/StrictSigil | 
					
						
							| 
									
										
										
										
											2020-07-31 11:35:45 -04:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "cask_dependent" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 04:03:15 +02:00
										 |  |  | # Helper functions for dependencies. | 
					
						
							| 
									
										
										
										
											2020-07-31 11:35:45 -04:00
										 |  |  | module DependenciesHelpers | 
					
						
							| 
									
										
										
										
											2024-08-20 19:10:14 +01:00
										 |  |  |   extend T::Helpers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   requires_ancestor { Kernel } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 11:55:40 -04:00
										 |  |  |   def args_includes_ignores(args) | 
					
						
							| 
									
										
										
										
											2023-08-24 21:18:24 -07:00
										 |  |  |     includes = [:required?, :recommended?] # included by default | 
					
						
							| 
									
										
										
										
											2023-08-19 15:49:16 -07:00
										 |  |  |     includes << :build? if args.include_build? | 
					
						
							|  |  |  |     includes << :test? if args.include_test? | 
					
						
							|  |  |  |     includes << :optional? if args.include_optional? | 
					
						
							| 
									
										
										
										
											2020-07-31 11:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-19 15:49:16 -07:00
										 |  |  |     ignores = [] | 
					
						
							|  |  |  |     ignores << :recommended? if args.skip_recommended? | 
					
						
							|  |  |  |     ignores << :satisfied? if args.missing? | 
					
						
							| 
									
										
										
										
											2020-07-31 11:35:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     [includes, ignores] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def recursive_includes(klass, root_dependent, includes, ignores) | 
					
						
							| 
									
										
										
										
											2021-03-19 03:21:27 +00:00
										 |  |  |     raise ArgumentError, "Invalid class argument: #{klass}" if klass != Dependency && klass != Requirement | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cache_key = "recursive_includes_#{includes}_#{ignores}" | 
					
						
							| 
									
										
										
										
											2020-07-31 11:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |     klass.expand(root_dependent, cache_key:) do |dependent, dep| | 
					
						
							| 
									
										
										
										
											2023-08-24 21:18:24 -07:00
										 |  |  |       klass.prune if ignores.any? { |ignore| dep.public_send(ignore) } | 
					
						
							|  |  |  |       klass.prune if includes.none? do |include| | 
					
						
							| 
									
										
										
										
											2023-08-19 15:49:16 -07:00
										 |  |  |         # Ignore indirect test dependencies | 
					
						
							|  |  |  |         next if include == :test? && dependent != root_dependent | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:18:24 -07:00
										 |  |  |         dep.public_send(include) | 
					
						
							| 
									
										
										
										
											2020-07-31 11:35:45 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # If a tap isn't installed, we can't find the dependencies of one of | 
					
						
							| 
									
										
										
										
											2024-04-30 11:10:23 +02:00
										 |  |  |       # its formulae and an exception will be thrown if we try. | 
					
						
							| 
									
										
										
										
											2023-06-19 04:37:55 +01:00
										 |  |  |       Dependency.keep_but_prune_recursive_deps if klass == Dependency && dep.tap && !dep.tap.installed? | 
					
						
							| 
									
										
										
										
											2020-07-31 11:35:45 -04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:18:24 -07:00
										 |  |  |   def select_includes(dependables, ignores, includes) | 
					
						
							|  |  |  |     dependables.select do |dep| | 
					
						
							|  |  |  |       next false if ignores.any? { |ignore| dep.public_send(ignore) } | 
					
						
							| 
									
										
										
										
											2020-07-31 11:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:18:24 -07:00
										 |  |  |       includes.any? { |include| dep.public_send(include) } | 
					
						
							| 
									
										
										
										
											2020-07-31 11:35:45 -04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def dependents(formulae_or_casks) | 
					
						
							|  |  |  |     formulae_or_casks.map do |formula_or_cask| | 
					
						
							|  |  |  |       if formula_or_cask.is_a?(Formula) | 
					
						
							|  |  |  |         formula_or_cask | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         CaskDependent.new(formula_or_cask) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |   module_function :dependents | 
					
						
							|  |  |  | end |