Module: Homebrew::API::Formula::FormulaStructGenerator Private

Defined in:
api/formula/formula_struct_generator.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Methods for generating FormulaStruct instances from API data.

Constant Summary collapse

DependencyHash =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.type_alias do
  T::Hash[
    # Keys are strings of the dependency type (e.g. "dependencies", "build_dependencies")
    String,
    # Values are arrays of either:
    T::Array[
      T.any(
        # Formula name: "foo"
        String,
        # Hash like { "foo" => :build } or { "foo" => [:build, :test] }
        T::Hash[
          String,
          T.any(Symbol, T::Array[Symbol]),
        ],
        # Hash like { since: :catalina } for uses_from_macos_bounds
        T::Hash[Symbol, Symbol],
      ),
    ],
  ]
end
RequirementsArray =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.type_alias do
  T::Array[T::Hash[String, T.untyped]]
end

Class Method Summary collapse

Class Method Details

.generate_formula_struct_hash(hash, bottle_tag: Utils::Bottles.tag) ⇒ FormulaStruct

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'api/formula/formula_struct_generator.rb', line 41

def generate_formula_struct_hash(hash, bottle_tag: Utils::Bottles.tag)
  hash = Homebrew::API.merge_variations(hash, bottle_tag:).dup

  if (caveats = hash["caveats"])
    hash["caveats"] = Formulary.replace_placeholders(caveats)
  end

  hash["bottle_checksums"] = begin
    files = hash.dig("bottle", "stable", "files") || {}
    files.map do |tag, bottle_spec|
      {
        cellar: Utils.convert_to_string_or_symbol(bottle_spec.fetch("cellar")),
        tag.to_sym => bottle_spec.fetch("sha256"),
      }
    end
  end

  hash["bottle_rebuild"] = hash.dig("bottle", "stable", "rebuild")

  conflicts_with = hash["conflicts_with"] || []
  conflicts_with_reasons = hash["conflicts_with_reasons"] || []
  hash["conflicts"] = conflicts_with.zip(conflicts_with_reasons).map do |name, reason|
    if reason.present?
      [name, { because: reason }]
    else
      [name, {}]
    end
  end

  if (deprecate_args = hash["deprecate_args"])
    deprecate_args = deprecate_args.dup.transform_keys(&:to_sym)
    deprecate_args[:because] =
      DeprecateDisable.to_reason_string_or_symbol(deprecate_args[:because], type: :formula)
    hash["deprecate_args"] = deprecate_args
  end

  if (disable_args = hash["disable_args"])
    disable_args = disable_args.dup.transform_keys(&:to_sym)
    disable_args[:because] =
      DeprecateDisable.to_reason_string_or_symbol(disable_args[:because], type: :formula)
    hash["disable_args"] = disable_args
  end

  hash["head_url_args"] = begin
    # Fall back to "" to satisfy the type checker. If the head URL is missing, head_present will be false.
    url = hash.dig("urls", "head", "url") || ""
    specs = {
      branch: hash.dig("urls", "head", "branch"),
      using:  hash.dig("urls", "head", "using")&.to_sym,
    }.compact_blank
    [url, specs]
  end

  if (keg_only_hash = hash["keg_only_reason"])
    reason = Utils.convert_to_string_or_symbol(keg_only_hash.fetch("reason"))
    explanation = keg_only_hash["explanation"]
    hash["keg_only_args"] = [reason, explanation].compact
  end

  hash["license"] = SPDX.string_to_license_expression(hash["license"])

  hash["link_overwrite_paths"] = hash["link_overwrite"]

  if (reason = hash["no_autobump_message"])
    reason = reason.to_sym if NO_AUTOBUMP_REASONS_LIST.key?(reason.to_sym)
    hash["no_autobump_args"] = { because: reason }
  end

  if (condition = hash["pour_bottle_only_if"])
    hash["pour_bottle_args"] = { only_if: condition.to_sym }
  end

  hash["ruby_source_checksum"] = hash.dig("ruby_source_checksum", "sha256")

  if (service_hash = hash["service"])
    service_hash = Homebrew::Service.from_hash(service_hash)

    hash["service_run_args"], hash["service_run_kwargs"] = case (run = service_hash[:run])
    when Hash
      [[], run]
    when Array, String
      [[run], {}]
    else
      [[], {}]
    end

    hash["service_name_args"] = service_hash[:name]

    hash["service_args"] = service_hash.filter_map do |key, arg|
      [key.to_sym, arg] if key != :name && key != :run
    end
  end

  hash["stable_checksum"] = hash.dig("urls", "stable", "checksum")

  hash["stable_url_args"] = begin
    url = hash.dig("urls", "stable", "url")
    specs = {
      tag:      hash.dig("urls", "stable", "tag"),
      revision: hash.dig("urls", "stable", "revision"),
      using:    hash.dig("urls", "stable", "using")&.to_sym,
    }.compact_blank
    [url, specs]
  end

  hash["stable_version"] = hash.dig("versions", "stable")

  # Do dependency processing last because it's more involved and depends on other fields
  hash["requirements_array"] = hash["requirements"]

  stable_dependency_hash = {
    "dependencies"             => hash["dependencies"] || [],
    "build_dependencies"       => hash["build_dependencies"] || [],
    "test_dependencies"        => hash["test_dependencies"] || [],
    "recommended_dependencies" => hash["recommended_dependencies"] || [],
    "optional_dependencies"    => hash["optional_dependencies"] || [],
    "uses_from_macos"          => hash["uses_from_macos"] || [],
    "uses_from_macos_bounds"   => hash["uses_from_macos_bounds"] || [],
  }

  stable_dependencies, stable_uses_from_macos = process_dependencies_and_requirements(
    stable_dependency_hash,
    hash["requirements_array"],
    :stable,
  )

  head_dependencies, head_uses_from_macos = process_dependencies_and_requirements(
    hash["head_dependencies"],
    hash["requirements_array"],
    :head,
  )

  hash["stable_dependencies"] = stable_dependencies
  hash["stable_uses_from_macos"] = stable_uses_from_macos
  hash["head_dependencies"] = head_dependencies
  hash["head_uses_from_macos"] = head_uses_from_macos

  # Should match FormulaStruct::PREDICATES
  hash["bottle_present"] = hash["bottle"].present?
  hash["deprecate_present"] = hash["deprecate_args"].present?
  hash["disable_present"] = hash["disable_args"].present?
  hash["head_present"] = hash.dig("urls", "head").present?
  hash["keg_only_present"] = hash["keg_only_reason"].present?
  hash["no_autobump_present"] = hash["no_autobump_message"].present?
  hash["pour_bottle_present"] = hash["pour_bottle_only_if"].present?
  hash["service_present"] = hash["service"].present?
  hash["service_run_present"] = hash.dig("service", "run").present?
  hash["service_name_present"] = hash.dig("service", "name").present?
  hash["stable_present"] = hash.dig("urls", "stable").present?

  FormulaStruct.from_hash(hash)
end

.process_dependencies(deps_hash) ⇒ Array<FormulaStruct::DependsOnArgs>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



243
244
245
246
247
248
249
250
# File 'api/formula/formula_struct_generator.rb', line 243

def process_dependencies(deps_hash)
  dependencies = deps_hash.fetch("dependencies", [])
  dependencies + [:build, :test, :recommended, :optional].filter_map do |type|
    deps_hash["#{type}_dependencies"]&.map do |dep|
      { dep => type }
    end
  end.flatten(1)
end

.process_dependencies_and_requirements(deps_hash, requirements_array, spec) ⇒ Array<(Array<FormulaStruct::DependsOnArgs>, Array<FormulaStruct::UsesFromMacOSArgs>)>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'api/formula/formula_struct_generator.rb', line 198

def process_dependencies_and_requirements(deps_hash, requirements_array, spec)
  deps, uses_from_macos = if deps_hash.present?
    deps_hash = symbolize_dependency_hash(deps_hash)
    [process_dependencies(deps_hash), process_uses_from_macos(deps_hash)]
  else
    [[], []]
  end

  requirements = if requirements_array.present?
    process_requirements(requirements_array, spec)
  else
    []
  end

  [deps + requirements, uses_from_macos]
end

.process_requirements(requirements_array, spec) ⇒ Array<FormulaStruct::DependsOnArgs>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'api/formula/formula_struct_generator.rb', line 253

def process_requirements(requirements_array, spec)
  requirements_array.filter_map do |req|
    next unless req["specs"].include?(spec.to_s)

    req_name = req["name"].to_sym
    next if API_SUPPORTED_REQUIREMENTS.exclude?(req_name)

    req_version = case req_name
    when :arch
      req["version"]&.to_sym
    when :macos, :maximum_macos
      MacOSVersion::SYMBOLS.key(req["version"])
    else
      req["version"]
    end

    req_tags = []
    req_tags << req_version if req_version.present?
    req_tags += req.fetch("contexts", []).map do |tag|
      case tag
      when String
        tag.to_sym
      when Hash
        tag.deep_transform_keys(&:to_sym)
      else
        tag
      end
    end

    if req_tags.empty?
      req_name
    else
      { req_name => req_tags }
    end
  end
end

.process_uses_from_macos(deps_hash) ⇒ Array<FormulaStruct::UsesFromMacOSArgs>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'api/formula/formula_struct_generator.rb', line 291

def process_uses_from_macos(deps_hash)
  uses_from_macos = deps_hash.fetch("uses_from_macos", [])

  uses_from_macos_bounds = deps_hash.fetch("uses_from_macos_bounds", [])
  uses_from_macos_bounds = T.cast(uses_from_macos_bounds, T::Array[T::Hash[Symbol, Symbol]])

  uses_from_macos.zip(uses_from_macos_bounds).map do |entry, bounds|
    bounds ||= {}
    bounds = bounds.transform_keys(&:to_sym).transform_values(&:to_sym)

    if entry.is_a?(Hash)
      # The key is the dependency name, the value is the dep type. Only the type should be a symbol
      entry = entry.deep_transform_values(&:to_sym)
      # When passing both a dep type and bounds, uses_from_macos expects them both in the first argument
      entry = entry.merge(bounds)
      [entry, {}]
    else
      [entry, bounds]
    end
  end
end

.symbolize_dependency_hash(hash) ⇒ DependencyHash

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Convert from { "dependencies" => ["foo", { "bar" => "build" }, { "baz" => ["build", "test"] }] } to { "dependencies" => ["foo", { "bar" => :build }, { "baz" => [:build, :test] }] }

Parameters:

Returns:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'api/formula/formula_struct_generator.rb', line 218

def symbolize_dependency_hash(hash)
  hash = hash.dup

  if (uses_from_macos_bounds = hash["uses_from_macos_bounds"])
    uses_from_macos_bounds = T.cast(uses_from_macos_bounds, T::Array[T::Hash[Symbol, Symbol]])
    hash["uses_from_macos_bounds"] = uses_from_macos_bounds.map(&:deep_symbolize_keys)
  end

  hash.transform_values do |deps|
    deps.map do |dep|
      next dep unless dep.is_a?(Hash)

      dep.transform_values do |types|
        case types
        when Array
          types.map(&:to_sym)
        else
          types.to_sym
        end
      end
    end
  end
end