|
| 1 | +# typed: strict |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "service" |
| 5 | +require "utils/spdx" |
| 6 | + |
| 7 | +module Homebrew |
| 8 | + module API |
| 9 | + class FormulaStruct < T::Struct |
| 10 | + PREDICATES = [ |
| 11 | + :bottle, |
| 12 | + :deprecated, |
| 13 | + :disabled, |
| 14 | + :head, |
| 15 | + :keg_only, |
| 16 | + :no_autobump_message, |
| 17 | + :pour_bottle, |
| 18 | + :service, |
| 19 | + :service_run, |
| 20 | + :service_name, |
| 21 | + :stable, |
| 22 | + ].freeze |
| 23 | + |
| 24 | + # `:codesign` and custom requirement classes are not supported. |
| 25 | + API_SUPPORTED_REQUIREMENTS = [:arch, :linux, :macos, :maximum_macos, :xcode].freeze |
| 26 | + private_constant :API_SUPPORTED_REQUIREMENTS |
| 27 | + |
| 28 | + DependencyArgs = T.type_alias do |
| 29 | + T.any( |
| 30 | + # Formula name: "foo" |
| 31 | + String, |
| 32 | + # Formula name and dependency type: { "foo" => :build } |
| 33 | + T::Hash[String, Symbol], |
| 34 | + ) |
| 35 | + end |
| 36 | + |
| 37 | + RequirementArgs = T.type_alias do |
| 38 | + T.any( |
| 39 | + # Requirement name: :macos |
| 40 | + Symbol, |
| 41 | + # Requirement name and other info: { macos: :build } |
| 42 | + T::Hash[Symbol, T::Array[T.anything]], |
| 43 | + ) |
| 44 | + end |
| 45 | + |
| 46 | + UsesFromMacOSArgs = T.type_alias do |
| 47 | + [ |
| 48 | + T.any( |
| 49 | + # Formula name: "foo" |
| 50 | + String, |
| 51 | + # Formula name and dependency type: { "foo" => :build } |
| 52 | + # Formula name, dependency type, and version bounds: { "foo" => :build, since: :catalina } |
| 53 | + T::Hash[T.any(String, Symbol), T.any(Symbol, T::Array[Symbol])], |
| 54 | + ), |
| 55 | + # If the first argument is only a name, this argument contains the version bounds: { since: :catalina } |
| 56 | + T::Hash[Symbol, Symbol], |
| 57 | + ] |
| 58 | + end |
| 59 | + |
| 60 | + PREDICATES.each do |predicate_name| |
| 61 | + present_method_name = :"#{predicate_name}_present" |
| 62 | + predicate_method_name = :"#{predicate_name}?" |
| 63 | + |
| 64 | + const present_method_name, T::Boolean, default: false |
| 65 | + |
| 66 | + define_method(predicate_method_name) do |
| 67 | + send(present_method_name) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + # Changes to this struct must be mirrored in Homebrew::API::Formula.generate_formula_struct_hash |
| 72 | + const :aliases, T::Array[String], default: [] |
| 73 | + const :bottle, T::Hash[String, T.anything], default: {} |
| 74 | + const :bottle_checksums, T::Array[T::Hash[String, T.anything]], default: [] |
| 75 | + const :bottle_rebuild, Integer, default: 0 |
| 76 | + const :caveats, T.nilable(String) |
| 77 | + const :conflicts, T::Array[[String, T::Hash[Symbol, String]]], default: [] |
| 78 | + const :deprecate_args, T::Hash[Symbol, T.nilable(T.any(String, Symbol))], default: {} |
| 79 | + const :desc, String |
| 80 | + const :disable_args, T::Hash[Symbol, T.nilable(T.any(String, Symbol))], default: {} |
| 81 | + const :head_url_args, [String, T::Hash[Symbol, T.anything]] |
| 82 | + const :homepage, String |
| 83 | + const :keg_only_args, T::Array[T.any(String, Symbol)], default: [] |
| 84 | + const :license, SPDX::LicenseExpression |
| 85 | + const :link_overwrite_paths, T::Array[String], default: [] |
| 86 | + const :no_autobump_args, T::Hash[Symbol, T.any(String, Symbol)], default: {} |
| 87 | + const :oldnames, T::Array[String], default: [] |
| 88 | + const :post_install_defined, T::Boolean, default: true |
| 89 | + const :pour_bottle_args, T::Hash[Symbol, Symbol], default: {} |
| 90 | + const :revision, Integer, default: 0 |
| 91 | + const :ruby_source_checksum, String |
| 92 | + const :ruby_source_path, String |
| 93 | + const :service_args, T::Array[[Symbol, BasicObject]], default: [] |
| 94 | + const :service_name_args, T::Hash[Symbol, String], default: {} |
| 95 | + const :service_run_args, T::Array[Homebrew::Service::RunParam], default: [] |
| 96 | + const :service_run_kwargs, T::Hash[Symbol, Homebrew::Service::RunParam], default: {} |
| 97 | + const :stable_checksum, T.nilable(String) |
| 98 | + const :stable_url_args, [String, T::Hash[Symbol, T.anything]] |
| 99 | + const :stable_version, String |
| 100 | + const :tap_git_head, String |
| 101 | + const :version_scheme, Integer, default: 0 |
| 102 | + const :versioned_formulae, T::Array[String], default: [] |
| 103 | + |
| 104 | + sig { returns(T::Array[T.any(DependencyArgs, RequirementArgs)]) } |
| 105 | + def head_dependencies |
| 106 | + spec_dependencies(:head) + spec_requirements(:head) |
| 107 | + end |
| 108 | + |
| 109 | + sig { returns(T::Array[T.any(DependencyArgs, RequirementArgs)]) } |
| 110 | + def stable_dependencies |
| 111 | + spec_dependencies(:stable) + spec_requirements(:stable) |
| 112 | + end |
| 113 | + |
| 114 | + sig { returns(T::Array[UsesFromMacOSArgs]) } |
| 115 | + def head_uses_from_macos |
| 116 | + spec_uses_from_macos(:head) |
| 117 | + end |
| 118 | + |
| 119 | + sig { returns(T::Array[UsesFromMacOSArgs]) } |
| 120 | + def stable_uses_from_macos |
| 121 | + spec_uses_from_macos(:stable) |
| 122 | + end |
| 123 | + |
| 124 | + private |
| 125 | + |
| 126 | + const :stable_dependency_hash, T::Hash[String, T::Array[String]], default: {} |
| 127 | + const :head_dependency_hash, T::Hash[String, T::Array[String]], default: {} |
| 128 | + const :requirements_array, T::Array[T::Hash[String, T.untyped]], default: [] |
| 129 | + |
| 130 | + sig { params(spec: Symbol).returns(T::Array[DependencyArgs]) } |
| 131 | + def spec_dependencies(spec) |
| 132 | + deps_hash = send("#{spec}_dependency_hash") |
| 133 | + dependencies = deps_hash.fetch("dependencies", []) |
| 134 | + dependencies + [:build, :test, :recommended, :optional].filter_map do |type| |
| 135 | + deps_hash["#{type}_dependencies"]&.map do |dep| |
| 136 | + { dep => type } |
| 137 | + end |
| 138 | + end.flatten(1) |
| 139 | + end |
| 140 | + |
| 141 | + sig { params(spec: Symbol).returns(T::Array[UsesFromMacOSArgs]) } |
| 142 | + def spec_uses_from_macos(spec) |
| 143 | + deps_hash = send("#{spec}_dependency_hash") |
| 144 | + zipped_array = deps_hash["uses_from_macos"]&.zip(deps_hash["uses_from_macos_bounds"]) |
| 145 | + return [] unless zipped_array |
| 146 | + |
| 147 | + zipped_array.map do |entry, bounds| |
| 148 | + bounds ||= {} |
| 149 | + bounds = bounds.transform_keys(&:to_sym).transform_values(&:to_sym) |
| 150 | + |
| 151 | + if entry.is_a?(Hash) |
| 152 | + # The key is the dependency name, the value is the dep type. Only the type should be a symbol |
| 153 | + entry = entry.deep_transform_values(&:to_sym) |
| 154 | + # When passing both a dep type and bounds, uses_from_macos expects them both in the first argument |
| 155 | + entry = entry.merge(bounds) |
| 156 | + [entry, {}] |
| 157 | + else |
| 158 | + [entry, bounds] |
| 159 | + end |
| 160 | + end |
| 161 | + end |
| 162 | + |
| 163 | + sig { params(spec: Symbol).returns(T::Array[RequirementArgs]) } |
| 164 | + def spec_requirements(spec) |
| 165 | + requirements_array.filter_map do |req| |
| 166 | + next unless req["specs"].include?(spec.to_s) |
| 167 | + |
| 168 | + req_name = req["name"].to_sym |
| 169 | + next if API_SUPPORTED_REQUIREMENTS.exclude?(req_name) |
| 170 | + |
| 171 | + req_version = case req_name |
| 172 | + when :arch |
| 173 | + req["version"]&.to_sym |
| 174 | + when :macos, :maximum_macos |
| 175 | + MacOSVersion::SYMBOLS.key(req["version"]) |
| 176 | + else |
| 177 | + req["version"] |
| 178 | + end |
| 179 | + |
| 180 | + req_tags = [] |
| 181 | + req_tags << req_version if req_version.present? |
| 182 | + req_tags += req["contexts"]&.map do |tag| |
| 183 | + case tag |
| 184 | + when String |
| 185 | + tag.to_sym |
| 186 | + when Hash |
| 187 | + tag.deep_transform_keys(&:to_sym) |
| 188 | + else |
| 189 | + tag |
| 190 | + end |
| 191 | + end |
| 192 | + |
| 193 | + if req_tags.empty? |
| 194 | + req_name |
| 195 | + else |
| 196 | + { req_name => req_tags } |
| 197 | + end |
| 198 | + end |
| 199 | + end |
| 200 | + end |
| 201 | + end |
| 202 | +end |
0 commit comments