| 
									
										
										
										
											2025-02-15 23:04:04 -08:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2024-12-24 12:50:31 -05:00
										 |  |  |     includes << :implicit? if args.include_implicit? | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-15 23:04:04 -08:00
										 |  |  |   sig { | 
					
						
							|  |  |  |     params(root_dependent: T.any(Formula, CaskDependent), includes: T::Array[Symbol], ignores: T::Array[Symbol]) | 
					
						
							|  |  |  |       .returns(T::Array[Dependency]) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   def recursive_dep_includes(root_dependent, includes, ignores) | 
					
						
							|  |  |  |     # The use of T.unsafe is recommended by the Sorbet docs: | 
					
						
							|  |  |  |     #   https://sorbet.org/docs/overloads#multiple-methods-but-sharing-a-common-implementation | 
					
						
							|  |  |  |     T.unsafe(recursive_includes(Dependency, root_dependent, includes, ignores)) | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2021-03-19 03:21:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-15 23:04:04 -08:00
										 |  |  |   sig { | 
					
						
							|  |  |  |     params(root_dependent: T.any(Formula, CaskDependent), includes: T::Array[Symbol], ignores: T::Array[Symbol]) | 
					
						
							|  |  |  |       .returns(Requirements) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   def recursive_req_includes(root_dependent, includes, ignores) | 
					
						
							|  |  |  |     # The use of T.unsafe is recommended by the Sorbet docs: | 
					
						
							|  |  |  |     #   https://sorbet.org/docs/overloads#multiple-methods-but-sharing-a-common-implementation | 
					
						
							|  |  |  |     T.unsafe(recursive_includes(Requirement, root_dependent, includes, ignores)) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   sig { | 
					
						
							|  |  |  |     params( | 
					
						
							|  |  |  |       klass:          T.any(T.class_of(Dependency), T.class_of(Requirement)), | 
					
						
							|  |  |  |       root_dependent: T.any(Formula, CaskDependent), | 
					
						
							|  |  |  |       includes:       T::Array[Symbol], | 
					
						
							|  |  |  |       ignores:        T::Array[Symbol], | 
					
						
							|  |  |  |     ).returns(T.any(T::Array[Dependency], Requirements)) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   def recursive_includes(klass, root_dependent, includes, ignores) | 
					
						
							| 
									
										
										
										
											2021-03-19 03:21:27 +00:00
										 |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-15 23:04:04 -08:00
										 |  |  |   sig { | 
					
						
							| 
									
										
										
										
											2025-02-18 20:48:56 -08:00
										 |  |  |     params( | 
					
						
							|  |  |  |       dependables: T.any(Dependencies, Requirements, T::Array[Dependency], T::Array[Requirement]), | 
					
						
							|  |  |  |       ignores:     T::Array[Symbol], | 
					
						
							|  |  |  |       includes:    T::Array[Symbol], | 
					
						
							|  |  |  |     ).returns(T::Array[T.any(Dependency, Requirement)]) | 
					
						
							| 
									
										
										
										
											2025-02-15 23:04:04 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-15 23:04:04 -08:00
										 |  |  |   sig { | 
					
						
							| 
									
										
										
										
											2025-02-16 15:41:57 -08:00
										 |  |  |     params(formulae_or_casks: T::Array[T.any(Formula, Keg, Cask::Cask)]) | 
					
						
							|  |  |  |       .returns(T::Array[T.any(Formula, CaskDependent)]) | 
					
						
							| 
									
										
										
										
											2025-02-15 23:04:04 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-07-31 11:35:45 -04:00
										 |  |  |   def dependents(formulae_or_casks) | 
					
						
							|  |  |  |     formulae_or_casks.map do |formula_or_cask| | 
					
						
							| 
									
										
										
										
											2025-02-16 15:41:57 -08:00
										 |  |  |       case formula_or_cask | 
					
						
							|  |  |  |       when Formula then formula_or_cask | 
					
						
							|  |  |  |       when Cask::Cask then CaskDependent.new(formula_or_cask) | 
					
						
							| 
									
										
										
										
											2020-07-31 11:35:45 -04:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2025-02-16 15:41:57 -08:00
										 |  |  |         raise TypeError, "Unsupported type: #{formula_or_cask.class}" | 
					
						
							| 
									
										
										
										
											2020-07-31 11:35:45 -04:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |