| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 23:46:30 +02:00
										 |  |  | require "cli/args" | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							|  |  |  |   module CLI | 
					
						
							| 
									
										
										
										
											2020-08-17 19:10:06 +02:00
										 |  |  |     # Helper class for loading formulae/casks from named arguments. | 
					
						
							| 
									
										
										
										
											2020-11-16 01:52:57 +01:00
										 |  |  |     class NamedArgs < Array | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       extend T::Generic | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Elem = type_member(:out) { { fixed: String } } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:53:00 -08:00
										 |  |  |       sig { returns(Args) } | 
					
						
							|  |  |  |       attr_reader :parent | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  |       sig { | 
					
						
							|  |  |  |         params( | 
					
						
							|  |  |  |           args:          String, | 
					
						
							|  |  |  |           parent:        Args, | 
					
						
							| 
									
										
										
										
											2024-12-06 11:06:27 -08:00
										 |  |  |           override_spec: T.nilable(Symbol), | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  |           force_bottle:  T::Boolean, | 
					
						
							|  |  |  |           flags:         T::Array[String], | 
					
						
							|  |  |  |           cask_options:  T::Boolean, | 
					
						
							| 
									
										
										
										
											2023-06-19 03:57:52 +01:00
										 |  |  |           without_api:   T::Boolean, | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  |         ).void | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       def initialize( | 
					
						
							|  |  |  |         *args, | 
					
						
							|  |  |  |         parent: Args.new, | 
					
						
							| 
									
										
										
										
											2024-12-06 11:06:27 -08:00
										 |  |  |         override_spec: nil, | 
					
						
							|  |  |  |         force_bottle: false, | 
					
						
							|  |  |  |         flags: [], | 
					
						
							| 
									
										
										
										
											2023-06-19 03:57:52 +01:00
										 |  |  |         cask_options: false, | 
					
						
							|  |  |  |         without_api: false | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  |       ) | 
					
						
							| 
									
										
										
										
											2024-12-03 17:53:00 -08:00
										 |  |  |         super(args) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |         @override_spec = override_spec | 
					
						
							|  |  |  |         @force_bottle = force_bottle | 
					
						
							|  |  |  |         @flags = flags | 
					
						
							| 
									
										
										
										
											2021-03-18 14:46:48 +00:00
										 |  |  |         @cask_options = cask_options | 
					
						
							| 
									
										
										
										
											2023-06-19 03:57:52 +01:00
										 |  |  |         @without_api = without_api | 
					
						
							| 
									
										
										
										
											2020-09-29 23:46:30 +02:00
										 |  |  |         @parent = parent | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       sig { returns(T::Array[Cask::Cask]) } | 
					
						
							| 
									
										
										
										
											2020-09-28 02:10:17 +02:00
										 |  |  |       def to_casks | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         @to_casks ||= T.let( | 
					
						
							|  |  |  |           to_formulae_and_casks(only: :cask).freeze, T.nilable(T::Array[T.any(Formula, Keg, Cask::Cask)]) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         T.cast(@to_casks, T::Array[Cask::Cask]) | 
					
						
							| 
									
										
										
										
											2020-09-28 02:10:17 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       sig { returns(T::Array[Formula]) } | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |       def to_formulae | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         @to_formulae ||= T.let( | 
					
						
							|  |  |  |           to_formulae_and_casks(only: :formula).freeze, T.nilable(T::Array[T.any(Formula, Keg, Cask::Cask)]) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         T.cast(@to_formulae, T::Array[Formula]) | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 04:17:02 +01:00
										 |  |  |       # Convert named arguments to {Formula} or {Cask} objects. | 
					
						
							|  |  |  |       # If both a formula and cask with the same name exist, returns | 
					
						
							|  |  |  |       # the formula and prints a warning unless `only` is specified. | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |       sig { | 
					
						
							| 
									
										
										
										
											2021-04-08 22:05:02 +01:00
										 |  |  |         params( | 
					
						
							| 
									
										
										
										
											2022-06-14 17:19:29 -04:00
										 |  |  |           only:               T.nilable(Symbol), | 
					
						
							| 
									
										
										
										
											2024-11-22 18:06:49 -08:00
										 |  |  |           ignore_unavailable: T::Boolean, | 
					
						
							| 
									
										
										
										
											2022-06-14 17:19:29 -04:00
										 |  |  |           method:             T.nilable(Symbol), | 
					
						
							|  |  |  |           uniq:               T::Boolean, | 
					
						
							| 
									
										
										
										
											2023-04-08 14:10:58 +02:00
										 |  |  |           warn:               T::Boolean, | 
					
						
							| 
									
										
										
										
											2021-04-08 22:05:02 +01:00
										 |  |  |         ).returns(T::Array[T.any(Formula, Keg, Cask::Cask)]) | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  |       def to_formulae_and_casks( | 
					
						
							| 
									
										
										
										
											2024-12-06 11:06:27 -08:00
										 |  |  |         only: parent.only_formula_or_cask, ignore_unavailable: false, method: nil, uniq: true, warn: false | 
					
						
							| 
									
										
										
										
											2023-04-14 15:33:40 +02:00
										 |  |  |       ) | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         @to_formulae_and_casks ||= T.let( | 
					
						
							|  |  |  |           {}, T.nilable(T::Hash[T.nilable(Symbol), T::Array[T.any(Formula, Keg, Cask::Cask)]]) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-12-03 04:17:02 +01:00
										 |  |  |         @to_formulae_and_casks[only] ||= downcased_unique_named.flat_map do |name| | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           options = { warn: }.compact | 
					
						
							|  |  |  |           load_formula_or_cask(name, only:, method:, **options) | 
					
						
							| 
									
										
										
										
											2021-01-24 09:04:10 -08:00
										 |  |  |         rescue FormulaUnreadableError, FormulaClassUnavailableError, | 
					
						
							|  |  |  |                TapFormulaUnreadableError, TapFormulaClassUnavailableError, | 
					
						
							|  |  |  |                Cask::CaskUnreadableError | 
					
						
							|  |  |  |           # Need to rescue before `*UnavailableError` (superclass of this) | 
					
						
							|  |  |  |           # The formula/cask was found, but there's a problem with its implementation | 
					
						
							|  |  |  |           raise | 
					
						
							| 
									
										
										
										
											2021-03-25 19:34:34 +01:00
										 |  |  |         rescue NoSuchKegError, FormulaUnavailableError, Cask::CaskUnavailableError, FormulaOrCaskUnavailableError | 
					
						
							| 
									
										
										
										
											2020-12-05 13:57:41 -05:00
										 |  |  |           ignore_unavailable ? [] : raise | 
					
						
							| 
									
										
										
										
											2021-04-08 22:05:02 +01:00
										 |  |  |         end.freeze | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if uniq | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |           @to_formulae_and_casks.fetch(only).uniq.freeze | 
					
						
							| 
									
										
										
										
											2021-04-08 22:05:02 +01:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |           @to_formulae_and_casks.fetch(only) | 
					
						
							| 
									
										
										
										
											2021-04-08 22:05:02 +01:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-09-25 21:13:26 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-08-17 12:40:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       sig { | 
					
						
							|  |  |  |         params(only: T.nilable(Symbol), method: T.nilable(Symbol)) | 
					
						
							|  |  |  |           .returns([T::Array[T.any(Formula, Keg)], T::Array[Cask::Cask]]) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       def to_formulae_to_casks(only: parent.only_formula_or_cask, method: nil) | 
					
						
							|  |  |  |         @to_formulae_to_casks ||= T.let( | 
					
						
							|  |  |  |           {}, T.nilable(T::Hash[[T.nilable(Symbol), T.nilable(Symbol)], | 
					
						
							|  |  |  |                                 [T::Array[T.any(Formula, Keg)], T::Array[Cask::Cask]]]) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         @to_formulae_to_casks[[method, only]] = | 
					
						
							|  |  |  |           T.cast( | 
					
						
							|  |  |  |             to_formulae_and_casks(only:, method:).partition { |o| o.is_a?(Formula) || o.is_a?(Keg) } | 
					
						
							|  |  |  |                     .map(&:freeze).freeze, | 
					
						
							|  |  |  |             [T::Array[T.any(Formula, Keg)], T::Array[Cask::Cask]], | 
					
						
							|  |  |  |           ) | 
					
						
							| 
									
										
										
										
											2020-10-09 21:09:07 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 23:34:23 -07:00
										 |  |  |       # Returns formulae and casks after validating that a tap is present for each of them. | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       sig { returns(T::Array[T.any(Formula, Keg, Cask::Cask)]) } | 
					
						
							| 
									
										
										
										
											2024-05-06 23:34:23 -07:00
										 |  |  |       def to_formulae_and_casks_with_taps | 
					
						
							| 
									
										
										
										
											2024-05-07 21:17:57 -07:00
										 |  |  |         formulae_and_casks_with_taps, formulae_and_casks_without_taps = | 
					
						
							|  |  |  |           to_formulae_and_casks.partition do |formula_or_cask| | 
					
						
							| 
									
										
										
										
											2024-05-08 18:13:35 -07:00
										 |  |  |             T.cast(formula_or_cask, T.any(Formula, Cask::Cask)).tap&.installed? | 
					
						
							| 
									
										
										
										
											2024-05-06 23:34:23 -07:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-05-07 21:17:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-06 11:06:27 -08:00
										 |  |  |         return formulae_and_casks_with_taps if formulae_and_casks_without_taps.empty? | 
					
						
							| 
									
										
										
										
											2024-05-07 21:17:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         types = [] | 
					
						
							|  |  |  |         types << "formulae" if formulae_and_casks_without_taps.any?(Formula) | 
					
						
							|  |  |  |         types << "casks" if formulae_and_casks_without_taps.any?(Cask::Cask) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         odie <<~ERROR | 
					
						
							|  |  |  |           These #{types.join(" and ")} are not in any locally installed taps! | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 18:13:35 -07:00
										 |  |  |             #{formulae_and_casks_without_taps.sort_by(&:to_s).join("\n  ")} | 
					
						
							| 
									
										
										
										
											2024-05-07 21:17:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           You may need to run `brew tap` to install additional taps. | 
					
						
							|  |  |  |         ERROR | 
					
						
							| 
									
										
										
										
											2024-05-06 23:34:23 -07:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       sig { | 
					
						
							|  |  |  |         params(only: T.nilable(Symbol), method: T.nilable(Symbol)) | 
					
						
							|  |  |  |           .returns(T::Array[T.any(Formula, Keg, Cask::Cask, T::Array[Keg], FormulaOrCaskUnavailableError)]) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       def to_formulae_and_casks_and_unavailable(only: parent.only_formula_or_cask, method: nil) | 
					
						
							|  |  |  |         @to_formulae_casks_unknowns ||= T.let( | 
					
						
							|  |  |  |           {}, | 
					
						
							| 
									
										
										
										
											2024-12-03 17:53:00 -08:00
										 |  |  |           T.nilable(T::Hash[ | 
					
						
							|  |  |  |             T.nilable(Symbol), | 
					
						
							| 
									
										
										
										
											2025-05-05 14:35:08 -07:00
										 |  |  |             T::Array[T.any(Formula, Keg, Cask::Cask, T::Array[Keg], FormulaOrCaskUnavailableError)], | 
					
						
							| 
									
										
										
										
											2024-12-03 17:53:00 -08:00
										 |  |  |           ]), | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-09 21:09:07 -04:00
										 |  |  |         @to_formulae_casks_unknowns[method] = downcased_unique_named.map do |name| | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           load_formula_or_cask(name, only:, method:) | 
					
						
							| 
									
										
										
										
											2020-10-08 19:55:24 -04:00
										 |  |  |         rescue FormulaOrCaskUnavailableError => e | 
					
						
							|  |  |  |           e | 
					
						
							|  |  |  |         end.uniq.freeze | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-08 22:05:02 +01:00
										 |  |  |       sig { params(uniq: T::Boolean).returns(T::Array[Formula]) } | 
					
						
							|  |  |  |       def to_resolved_formulae(uniq: true) | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         @to_resolved_formulae ||= T.let( | 
					
						
							|  |  |  |           to_formulae_and_casks(only: :formula, method: :resolve, uniq:).freeze, | 
					
						
							|  |  |  |           T.nilable(T::Array[T.any(Formula, Keg, Cask::Cask)]), | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         T.cast(@to_resolved_formulae, T::Array[Formula]) | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       sig { params(only: T.nilable(Symbol)).returns([T::Array[Formula], T::Array[Cask::Cask]]) } | 
					
						
							|  |  |  |       def to_resolved_formulae_to_casks(only: parent.only_formula_or_cask) | 
					
						
							|  |  |  |         T.cast(to_formulae_to_casks(only:, method: :resolve), [T::Array[Formula], T::Array[Cask::Cask]]) | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-18 22:18:42 +00:00
										 |  |  |       LOCAL_PATH_REGEX = %r{^/|[.]|/$} | 
					
						
							|  |  |  |       TAP_NAME_REGEX = %r{^[^./]+/[^./]+$} | 
					
						
							| 
									
										
										
										
											2023-10-01 13:23:12 -07:00
										 |  |  |       private_constant :LOCAL_PATH_REGEX, :TAP_NAME_REGEX | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 18:39:58 +02:00
										 |  |  |       # Keep existing paths and try to convert others to tap, formula or cask paths. | 
					
						
							|  |  |  |       # If a cask and formula with the same name exist, includes both their paths | 
					
						
							|  |  |  |       # unless `only` is specified. | 
					
						
							| 
									
										
										
										
											2020-12-08 01:03:39 +01:00
										 |  |  |       sig { params(only: T.nilable(Symbol), recurse_tap: T::Boolean).returns(T::Array[Pathname]) } | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       def to_paths(only: parent.only_formula_or_cask, recurse_tap: false) | 
					
						
							|  |  |  |         @to_paths ||= T.let({}, T.nilable(T::Hash[T.nilable(Symbol), T::Array[Pathname]])) | 
					
						
							| 
									
										
										
										
											2023-08-04 16:21:31 +01:00
										 |  |  |         @to_paths[only] ||= Homebrew.with_no_api_env_if_needed(@without_api) do | 
					
						
							|  |  |  |           downcased_unique_named.flat_map do |name| | 
					
						
							| 
									
										
										
										
											2023-09-27 23:16:20 -07:00
										 |  |  |             path = Pathname(name).expand_path | 
					
						
							| 
									
										
										
										
											2023-10-01 13:23:12 -07:00
										 |  |  |             if only.nil? && name.match?(LOCAL_PATH_REGEX) && path.exist? | 
					
						
							| 
									
										
										
										
											2023-08-04 16:21:31 +01:00
										 |  |  |               path | 
					
						
							| 
									
										
										
										
											2023-10-01 13:23:12 -07:00
										 |  |  |             elsif name.match?(TAP_NAME_REGEX) | 
					
						
							| 
									
										
										
										
											2023-08-04 16:21:31 +01:00
										 |  |  |               tap = Tap.fetch(name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               if recurse_tap | 
					
						
							|  |  |  |                 next tap.formula_files if only == :formula | 
					
						
							|  |  |  |                 next tap.cask_files if only == :cask | 
					
						
							|  |  |  |               end | 
					
						
							| 
									
										
										
										
											2020-12-08 01:03:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 16:21:31 +01:00
										 |  |  |               tap.path | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |               next Formulary.path(name) if only == :formula | 
					
						
							|  |  |  |               next Cask::CaskLoader.path(name) if only == :cask | 
					
						
							| 
									
										
										
										
											2020-09-07 18:39:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 16:21:31 +01:00
										 |  |  |               formula_path = Formulary.path(name) | 
					
						
							|  |  |  |               cask_path = Cask::CaskLoader.path(name) | 
					
						
							| 
									
										
										
										
											2020-09-07 18:39:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 16:21:31 +01:00
										 |  |  |               paths = [] | 
					
						
							| 
									
										
										
										
											2020-09-07 18:39:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 16:21:31 +01:00
										 |  |  |               if formula_path.exist? || | 
					
						
							| 
									
										
										
										
											2024-03-16 13:17:54 -07:00
										 |  |  |                  (!Homebrew::EnvConfig.no_install_from_api? && | 
					
						
							|  |  |  |                  !CoreTap.instance.installed? && | 
					
						
							|  |  |  |                  Homebrew::API::Formula.all_formulae.key?(path.basename.to_s)) | 
					
						
							| 
									
										
										
										
											2023-08-04 16:21:31 +01:00
										 |  |  |                 paths << formula_path | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |               if cask_path.exist? || | 
					
						
							| 
									
										
										
										
											2024-03-16 13:17:54 -07:00
										 |  |  |                  (!Homebrew::EnvConfig.no_install_from_api? && | 
					
						
							|  |  |  |                  !CoreCaskTap.instance.installed? && | 
					
						
							|  |  |  |                  Homebrew::API::Cask.all_casks.key?(path.basename.to_s)) | 
					
						
							| 
									
										
										
										
											2023-08-04 16:21:31 +01:00
										 |  |  |                 paths << cask_path | 
					
						
							|  |  |  |               end | 
					
						
							| 
									
										
										
										
											2020-09-07 18:39:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 16:21:31 +01:00
										 |  |  |               paths.empty? ? path : paths | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end.uniq.freeze | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 16:39:07 +01:00
										 |  |  |       sig { returns(T::Array[Keg]) } | 
					
						
							| 
									
										
										
										
											2021-05-19 09:34:18 -04:00
										 |  |  |       def to_default_kegs | 
					
						
							| 
									
										
										
										
											2024-07-25 12:53:12 +01:00
										 |  |  |         require "missing_formula" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         @to_default_kegs ||= T.let(begin | 
					
						
							| 
									
										
										
										
											2021-05-19 10:07:03 -04:00
										 |  |  |           to_formulae_and_casks(only: :formula, method: :default_kegs).freeze | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |         rescue NoSuchKegError => e | 
					
						
							| 
									
										
										
										
											2020-11-30 00:54:27 +01:00
										 |  |  |           if (reason = MissingFormula.suggest_command(e.name, "uninstall")) | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |             $stderr.puts reason | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           raise e | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         end, T.nilable(T::Array[T.any(Formula, Keg, Cask::Cask)])) | 
					
						
							|  |  |  |         T.cast(@to_default_kegs, T::Array[Keg]) | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-20 12:58:23 -04:00
										 |  |  |       sig { returns(T::Array[Keg]) } | 
					
						
							|  |  |  |       def to_latest_kegs | 
					
						
							| 
									
										
										
										
											2024-07-25 12:53:12 +01:00
										 |  |  |         require "missing_formula" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         @to_latest_kegs ||= T.let(begin | 
					
						
							| 
									
										
										
										
											2021-05-20 12:58:23 -04:00
										 |  |  |           to_formulae_and_casks(only: :formula, method: :latest_kegs).freeze | 
					
						
							|  |  |  |         rescue NoSuchKegError => e | 
					
						
							|  |  |  |           if (reason = MissingFormula.suggest_command(e.name, "uninstall")) | 
					
						
							|  |  |  |             $stderr.puts reason | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           raise e | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         end, T.nilable(T::Array[T.any(Formula, Keg, Cask::Cask)])) | 
					
						
							|  |  |  |         T.cast(@to_latest_kegs, T::Array[Keg]) | 
					
						
							| 
									
										
										
										
											2021-05-20 12:58:23 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-18 10:07:35 -04:00
										 |  |  |       sig { returns(T::Array[Keg]) } | 
					
						
							|  |  |  |       def to_kegs | 
					
						
							| 
									
										
										
										
											2024-07-25 12:53:12 +01:00
										 |  |  |         require "missing_formula" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         @to_kegs ||= T.let(begin | 
					
						
							| 
									
										
										
										
											2021-05-18 10:07:35 -04:00
										 |  |  |           to_formulae_and_casks(only: :formula, method: :kegs).freeze | 
					
						
							|  |  |  |         rescue NoSuchKegError => e | 
					
						
							|  |  |  |           if (reason = MissingFormula.suggest_command(e.name, "uninstall")) | 
					
						
							|  |  |  |             $stderr.puts reason | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           raise e | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         end, T.nilable(T::Array[T.any(Formula, Keg, Cask::Cask)])) | 
					
						
							|  |  |  |         T.cast(@to_kegs, T::Array[Keg]) | 
					
						
							| 
									
										
										
										
											2021-05-18 10:07:35 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |       sig { | 
					
						
							| 
									
										
										
										
											2024-11-22 18:06:49 -08:00
										 |  |  |         params(only: T.nilable(Symbol), ignore_unavailable: T::Boolean, all_kegs: T.nilable(T::Boolean)) | 
					
						
							| 
									
										
										
										
											2020-11-19 12:57:10 +01:00
										 |  |  |           .returns([T::Array[Keg], T::Array[Cask::Cask]]) | 
					
						
							| 
									
										
										
										
											2021-01-17 22:45:55 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       def to_kegs_to_casks(only: parent.only_formula_or_cask, ignore_unavailable: false, all_kegs: nil) | 
					
						
							| 
									
										
										
										
											2021-05-19 10:07:03 -04:00
										 |  |  |         method = all_kegs ? :kegs : :default_kegs | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         @to_kegs_to_casks ||= T.let({}, T.nilable(T::Hash[T.nilable(Symbol), [T::Array[Keg], T::Array[Cask::Cask]]])) | 
					
						
							| 
									
										
										
										
											2020-11-19 12:57:10 +01:00
										 |  |  |         @to_kegs_to_casks[method] ||= | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |           T.cast(to_formulae_and_casks(only:, ignore_unavailable:, method:) | 
					
						
							| 
									
										
										
										
											2020-11-19 12:57:10 +01:00
										 |  |  |           .partition { |o| o.is_a?(Keg) } | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |           .map(&:freeze).freeze, [T::Array[Keg], T::Array[Cask::Cask]]) | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |       sig { returns(T::Array[Tap]) } | 
					
						
							|  |  |  |       def to_taps | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         @to_taps ||= T.let(downcased_unique_named.map { |name| Tap.fetch name }.uniq.freeze, T.nilable(T::Array[Tap])) | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { returns(T::Array[Tap]) } | 
					
						
							|  |  |  |       def to_installed_taps | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         @to_installed_taps ||= T.let(to_taps.each do |tap| | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |           raise TapUnavailableError, tap.name unless tap.installed? | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         end.uniq.freeze, T.nilable(T::Array[Tap])) | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 16:39:07 +01:00
										 |  |  |       sig { returns(T::Array[String]) } | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |       def homebrew_tap_cask_names | 
					
						
							|  |  |  |         downcased_unique_named.grep(HOMEBREW_CASK_TAP_CASK_REGEX) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 16:39:07 +01:00
										 |  |  |       sig { returns(T::Array[String]) } | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |       def downcased_unique_named | 
					
						
							|  |  |  |         # Only lowercase names, not paths, bottle filenames or URLs | 
					
						
							|  |  |  |         map do |arg| | 
					
						
							|  |  |  |           if arg.include?("/") || arg.end_with?(".tar.gz") || File.exist?(arg) | 
					
						
							|  |  |  |             arg | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             arg.downcase | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end.uniq | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:53:00 -08:00
										 |  |  |       sig { | 
					
						
							|  |  |  |         params(name: String, only: T.nilable(Symbol), method: T.nilable(Symbol), warn: T.nilable(T::Boolean)) | 
					
						
							|  |  |  |           .returns(T.any(Formula, Keg, Cask::Cask, T::Array[Keg])) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       def load_formula_or_cask(name, only: nil, method: nil, warn: nil) | 
					
						
							|  |  |  |         Homebrew.with_no_api_env_if_needed(@without_api) do | 
					
						
							|  |  |  |           unreadable_error = nil | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           formula_or_kegs = if only != :cask | 
					
						
							|  |  |  |             begin | 
					
						
							|  |  |  |               case method | 
					
						
							|  |  |  |               when nil, :factory | 
					
						
							|  |  |  |                 options = { warn:, force_bottle: @force_bottle, flags: @flags }.compact | 
					
						
							|  |  |  |                 Formulary.factory(name, *@override_spec, **options) | 
					
						
							|  |  |  |               when :resolve | 
					
						
							|  |  |  |                 resolve_formula(name) | 
					
						
							|  |  |  |               when :latest_kegs | 
					
						
							|  |  |  |                 resolve_latest_keg(name) | 
					
						
							|  |  |  |               when :default_kegs | 
					
						
							|  |  |  |                 resolve_default_keg(name) | 
					
						
							|  |  |  |               when :kegs | 
					
						
							|  |  |  |                 _, kegs = resolve_kegs(name) | 
					
						
							|  |  |  |                 kegs | 
					
						
							|  |  |  |               else | 
					
						
							|  |  |  |                 raise | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             rescue FormulaUnreadableError, FormulaClassUnavailableError, | 
					
						
							|  |  |  |                    TapFormulaUnreadableError, TapFormulaClassUnavailableError, | 
					
						
							|  |  |  |                    FormulaSpecificationError => e | 
					
						
							|  |  |  |               # Need to rescue before `FormulaUnavailableError` (superclass of this) | 
					
						
							|  |  |  |               # The formula was found, but there's a problem with its implementation | 
					
						
							|  |  |  |               unreadable_error ||= e | 
					
						
							|  |  |  |               nil | 
					
						
							|  |  |  |             rescue NoSuchKegError, FormulaUnavailableError => e | 
					
						
							|  |  |  |               raise e if only == :formula | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               nil | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if only == :formula | 
					
						
							|  |  |  |             return formula_or_kegs if formula_or_kegs | 
					
						
							|  |  |  |           elsif formula_or_kegs && (!formula_or_kegs.is_a?(Formula) || formula_or_kegs.tap&.core_tap?) | 
					
						
							|  |  |  |             warn_if_cask_conflicts(name, "formula") | 
					
						
							|  |  |  |             return formula_or_kegs | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             want_keg_like_cask = [:latest_kegs, :default_kegs, :kegs].include?(method) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             cask = begin | 
					
						
							|  |  |  |               config = Cask::Config.from_args(@parent) if @cask_options | 
					
						
							|  |  |  |               options = { warn: }.compact | 
					
						
							|  |  |  |               candidate_cask = Cask::CaskLoader.load(name, config:, **options) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-06 11:06:27 -08:00
										 |  |  |               if unreadable_error | 
					
						
							| 
									
										
										
										
											2024-12-03 17:53:00 -08:00
										 |  |  |                 onoe <<~EOS | 
					
						
							|  |  |  |                   Failed to load formula: #{name} | 
					
						
							|  |  |  |                   #{unreadable_error} | 
					
						
							|  |  |  |                 EOS | 
					
						
							|  |  |  |                 opoo "Treating #{name} as a cask." | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               # If we're trying to get a keg-like Cask, do our best to use the same cask | 
					
						
							|  |  |  |               # file that was used for installation, if possible. | 
					
						
							|  |  |  |               if want_keg_like_cask && | 
					
						
							|  |  |  |                  (installed_caskfile = candidate_cask.installed_caskfile) && | 
					
						
							|  |  |  |                  installed_caskfile.exist? | 
					
						
							|  |  |  |                 cask = Cask::CaskLoader.load_from_installed_caskfile(installed_caskfile) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 requested_tap, requested_token = Tap.with_cask_token(name) | 
					
						
							|  |  |  |                 if requested_tap && requested_token | 
					
						
							|  |  |  |                   installed_cask_tap = cask.tab.tap | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                   if installed_cask_tap && installed_cask_tap != requested_tap | 
					
						
							|  |  |  |                     raise Cask::TapCaskUnavailableError.new(requested_tap, requested_token) | 
					
						
							|  |  |  |                   end | 
					
						
							|  |  |  |                 end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 cask | 
					
						
							|  |  |  |               else | 
					
						
							|  |  |  |                 candidate_cask | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             rescue Cask::CaskUnreadableError, Cask::CaskInvalidError => e | 
					
						
							|  |  |  |               # If we're trying to get a keg-like Cask, do our best to handle it | 
					
						
							|  |  |  |               # not being readable and return something that can be used. | 
					
						
							|  |  |  |               if want_keg_like_cask | 
					
						
							|  |  |  |                 cask_version = Cask::Cask.new(name, config:).installed_version | 
					
						
							|  |  |  |                 Cask::Cask.new(name, config:) do | 
					
						
							|  |  |  |                   version cask_version if cask_version | 
					
						
							|  |  |  |                 end | 
					
						
							|  |  |  |               else | 
					
						
							|  |  |  |                 # Need to rescue before `CaskUnavailableError` (superclass of this) | 
					
						
							|  |  |  |                 # The cask was found, but there's a problem with its implementation | 
					
						
							|  |  |  |                 unreadable_error ||= e | 
					
						
							|  |  |  |                 nil | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             rescue Cask::CaskUnavailableError => e | 
					
						
							|  |  |  |               raise e if only == :cask | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               nil | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # Prioritise formulae unless it's a core tap cask (we already prioritised core tap formulae above) | 
					
						
							|  |  |  |             if formula_or_kegs && !cask&.tap&.core_cask_tap? | 
					
						
							|  |  |  |               if cask || unreadable_error | 
					
						
							|  |  |  |                 onoe <<~EOS if unreadable_error | 
					
						
							|  |  |  |                   Failed to load cask: #{name} | 
					
						
							|  |  |  |                   #{unreadable_error} | 
					
						
							|  |  |  |                 EOS | 
					
						
							|  |  |  |                 opoo package_conflicts_message(name, "formula", cask) unless Context.current.quiet? | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |               return formula_or_kegs | 
					
						
							|  |  |  |             elsif cask | 
					
						
							|  |  |  |               if formula_or_kegs && !Context.current.quiet? | 
					
						
							|  |  |  |                 opoo package_conflicts_message(name, "cask", formula_or_kegs) | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |               return cask | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-06 11:06:27 -08:00
										 |  |  |           raise unreadable_error if unreadable_error | 
					
						
							| 
									
										
										
										
											2024-12-03 17:53:00 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           user, repo, short_name = name.downcase.split("/", 3) | 
					
						
							|  |  |  |           if repo.present? && short_name.present? | 
					
						
							|  |  |  |             tap = Tap.fetch(T.must(user), repo) | 
					
						
							|  |  |  |             raise TapFormulaOrCaskUnavailableError.new(tap, short_name) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           raise NoSuchKegError, name if resolve_formula(name) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { params(name: String).returns(Formula) } | 
					
						
							|  |  |  |       def resolve_formula(name) | 
					
						
							|  |  |  |         Formulary.resolve(name, **{ spec: @override_spec, force_bottle: @force_bottle, flags: @flags }.compact) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       sig { params(name: String).returns([Pathname, T::Array[Keg]]) } | 
					
						
							| 
									
										
										
										
											2021-03-25 19:11:52 +01:00
										 |  |  |       def resolve_kegs(name) | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |         raise UsageError if name.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-18 00:23:23 +01:00
										 |  |  |         require "keg" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |         rack = Formulary.to_rack(name.downcase) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-25 19:11:52 +01:00
										 |  |  |         kegs = rack.directory? ? rack.subdirs.map { |d| Keg.new(d) } : [] | 
					
						
							| 
									
										
										
										
											2024-07-20 21:41:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         requested_tap, requested_formula = Tap.with_formula_name(name) | 
					
						
							|  |  |  |         if requested_tap && requested_formula | 
					
						
							|  |  |  |           kegs = kegs.select do |keg| | 
					
						
							|  |  |  |             keg.tab.tap == requested_tap | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 05:46:14 +01:00
										 |  |  |           raise NoSuchKegError.new(requested_formula, tap: requested_tap) if kegs.none? | 
					
						
							| 
									
										
										
										
											2024-07-20 21:41:34 -04:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 11:37:34 -08:00
										 |  |  |         raise NoSuchKegError, name if kegs.none? | 
					
						
							| 
									
										
										
										
											2021-03-25 19:11:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         [rack, kegs] | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       sig { params(name: String).returns(Keg) } | 
					
						
							| 
									
										
										
										
											2021-05-20 12:58:23 -04:00
										 |  |  |       def resolve_latest_keg(name) | 
					
						
							|  |  |  |         _, kegs = resolve_kegs(name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-21 11:55:11 -04:00
										 |  |  |         # Return keg if it is the only installed keg | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         return kegs.fetch(0) if kegs.length == 1
 | 
					
						
							| 
									
										
										
										
											2021-05-20 12:58:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-28 03:23:21 +02:00
										 |  |  |         stable_kegs = kegs.reject { |keg| keg.version.head? } | 
					
						
							| 
									
										
										
										
											2021-08-04 18:51:39 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         latest_keg = if stable_kegs.empty? | 
					
						
							|  |  |  |           kegs.max_by do |keg| | 
					
						
							| 
									
										
										
										
											2024-04-28 03:23:21 +02:00
										 |  |  |             [keg.tab.source_modified_time, keg.version.revision] | 
					
						
							| 
									
										
										
										
											2021-08-12 09:16:23 -04:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         else | 
					
						
							|  |  |  |           stable_kegs.max_by(&:scheme_and_version) | 
					
						
							| 
									
										
										
										
											2021-08-11 20:07:28 -04:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |         T.must(latest_keg) | 
					
						
							| 
									
										
										
										
											2021-05-20 12:58:23 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       sig { params(name: String).returns(Keg) } | 
					
						
							| 
									
										
										
										
											2021-05-19 10:29:40 -04:00
										 |  |  |       def resolve_default_keg(name) | 
					
						
							| 
									
										
										
										
											2021-03-25 19:11:52 +01:00
										 |  |  |         rack, kegs = resolve_kegs(name) | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         linked_keg_ref = HOMEBREW_LINKED_KEGS/rack.basename | 
					
						
							|  |  |  |         opt_prefix = HOMEBREW_PREFIX/"opt/#{rack.basename}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         begin | 
					
						
							| 
									
										
										
										
											2021-03-01 09:10:26 +09:00
										 |  |  |           return Keg.new(opt_prefix.resolved_path) if opt_prefix.symlink? && opt_prefix.directory? | 
					
						
							|  |  |  |           return Keg.new(linked_keg_ref.resolved_path) if linked_keg_ref.symlink? && linked_keg_ref.directory? | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |           return kegs.fetch(0) if kegs.length == 1
 | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-01 09:10:26 +09:00
										 |  |  |           f = if name.include?("/") || File.exist?(name) | 
					
						
							|  |  |  |             Formulary.factory(name) | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             Formulary.from_rack(rack) | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-01 09:10:26 +09:00
										 |  |  |           unless (prefix = f.latest_installed_prefix).directory? | 
					
						
							|  |  |  |             raise MultipleVersionsInstalledError, <<~EOS | 
					
						
							|  |  |  |               #{rack.basename} has multiple installed versions | 
					
						
							|  |  |  |               Run `brew uninstall --force #{rack.basename}` to remove all versions. | 
					
						
							|  |  |  |             EOS | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2021-03-01 09:10:26 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |           Keg.new(prefix) | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |         rescue FormulaUnavailableError | 
					
						
							|  |  |  |           raise MultipleVersionsInstalledError, <<~EOS | 
					
						
							|  |  |  |             Multiple kegs installed to #{rack} | 
					
						
							|  |  |  |             However we don't know which one you refer to. | 
					
						
							| 
									
										
										
										
											2023-02-10 23:15:40 -05:00
										 |  |  |             Please delete (with `rm -rf`!) all but one and then try again. | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-08-17 12:40:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       sig { | 
					
						
							|  |  |  |         params( | 
					
						
							|  |  |  |           ref: String, loaded_type: String, | 
					
						
							|  |  |  |           package: T.any(T::Array[T.any(Formula, Keg)], Cask::Cask, Formula, Keg, NilClass) | 
					
						
							|  |  |  |         ).returns(String) | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2024-07-12 04:20:54 +01:00
										 |  |  |       def package_conflicts_message(ref, loaded_type, package) | 
					
						
							| 
									
										
										
										
											2020-09-01 08:50:08 +01:00
										 |  |  |         message = "Treating #{ref} as a #{loaded_type}." | 
					
						
							| 
									
										
										
										
											2024-07-12 04:20:54 +01:00
										 |  |  |         case package | 
					
						
							|  |  |  |         when Formula, Keg, Array | 
					
						
							|  |  |  |           message += " For the formula, " | 
					
						
							|  |  |  |           if package.is_a?(Formula) && (tap = package.tap) | 
					
						
							|  |  |  |             message += "use #{tap.name}/#{package.name} or " | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-09-17 00:23:15 +08:00
										 |  |  |           message += "specify the `--formula` flag. To silence this message, use the `--cask` flag." | 
					
						
							| 
									
										
										
										
											2024-07-12 04:20:54 +01:00
										 |  |  |         when Cask::Cask | 
					
						
							| 
									
										
										
										
											2024-03-06 23:24:04 +01:00
										 |  |  |           message += " For the cask, " | 
					
						
							| 
									
										
										
										
											2024-07-12 04:20:54 +01:00
										 |  |  |           if (tap = package.tap) | 
					
						
							|  |  |  |             message += "use #{tap.name}/#{package.token} or " | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-09-17 00:23:15 +08:00
										 |  |  |           message += "specify the `--cask` flag. To silence this message, use the `--formula` flag." | 
					
						
							| 
									
										
										
										
											2024-07-12 04:20:54 +01:00
										 |  |  |         end | 
					
						
							|  |  |  |         message.freeze | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 17:43:22 -08:00
										 |  |  |       sig { params(ref: String, loaded_type: String).void } | 
					
						
							| 
									
										
										
										
											2024-07-12 04:20:54 +01:00
										 |  |  |       def warn_if_cask_conflicts(ref, loaded_type) | 
					
						
							|  |  |  |         available = true | 
					
						
							|  |  |  |         cask = begin | 
					
						
							|  |  |  |           Cask::CaskLoader.load(ref, warn: false) | 
					
						
							| 
									
										
										
										
											2021-01-27 10:03:44 -08:00
										 |  |  |         rescue Cask::CaskUnreadableError => e | 
					
						
							|  |  |  |           # Need to rescue before `CaskUnavailableError` (superclass of this) | 
					
						
							|  |  |  |           # The cask was found, but there's a problem with its implementation | 
					
						
							|  |  |  |           onoe <<~EOS | 
					
						
							|  |  |  |             Failed to load cask: #{ref} | 
					
						
							|  |  |  |             #{e} | 
					
						
							|  |  |  |           EOS | 
					
						
							| 
									
										
										
										
											2024-07-12 04:20:54 +01:00
										 |  |  |           nil | 
					
						
							| 
									
										
										
										
											2021-01-27 10:03:44 -08:00
										 |  |  |         rescue Cask::CaskUnavailableError | 
					
						
							|  |  |  |           # No ref conflict with a cask, do nothing | 
					
						
							| 
									
										
										
										
											2024-07-12 04:20:54 +01:00
										 |  |  |           available = false | 
					
						
							|  |  |  |           nil | 
					
						
							| 
									
										
										
										
											2021-01-27 10:03:44 -08:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2024-07-12 04:20:54 +01:00
										 |  |  |         return unless available | 
					
						
							| 
									
										
										
										
											2024-09-15 18:51:06 +08:00
										 |  |  |         return if Context.current.quiet? | 
					
						
							| 
									
										
										
										
											2025-06-25 17:24:35 +01:00
										 |  |  |         return if cask&.old_tokens&.include?(ref) | 
					
						
							| 
									
										
										
										
											2024-07-12 04:20:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         opoo package_conflicts_message(ref, loaded_type, cask) | 
					
						
							| 
									
										
										
										
											2020-08-17 12:40:23 -04:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-08-13 08:55:55 -04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |