Skip to content

Commit 7f0c605

Browse files
committed
Extract FormulaStruct generator
1 parent c330d07 commit 7f0c605

File tree

7 files changed

+476
-342
lines changed

7 files changed

+476
-342
lines changed

Library/Homebrew/api/formula.rb

Lines changed: 1 addition & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require "api/source_download"
77
require "download_queue"
88
require "autobump_constants"
9+
require "api/formula/formula_struct_generator"
910

1011
module Homebrew
1112
module API
@@ -158,169 +159,6 @@ def self.write_names_and_aliases(regenerate: false)
158159
Homebrew::API.write_names_file!(all_formulae.keys, "formula", regenerate:)
159160
Homebrew::API.write_aliases_file!(all_aliases, "formula", regenerate:)
160161
end
161-
162-
sig { params(hash: T::Hash[String, T.untyped]).returns(FormulaStruct) }
163-
def self.generate_formula_struct_hash(hash)
164-
hash = Homebrew::API.merge_variations(hash).dup
165-
166-
if (caveats = hash["caveats"])
167-
hash["caveats"] = Formulary.replace_placeholders(caveats)
168-
end
169-
170-
hash["bottle_checksums"] = begin
171-
files = hash.dig("bottle", "stable", "files") || {}
172-
files.map do |tag, bottle_spec|
173-
{
174-
cellar: Utils.convert_to_string_or_symbol(bottle_spec.fetch("cellar")),
175-
tag.to_sym => bottle_spec.fetch("sha256"),
176-
}
177-
end
178-
end
179-
180-
hash["bottle_rebuild"] = hash.dig("bottle", "stable", "rebuild")
181-
182-
conflicts_with = hash["conflicts_with"] || []
183-
conflicts_with_reasons = hash["conflicts_with_reasons"] || []
184-
hash["conflicts"] = conflicts_with.zip(conflicts_with_reasons).map do |name, reason|
185-
if reason.present?
186-
[name, { because: reason }]
187-
else
188-
[name, {}]
189-
end
190-
end
191-
192-
if (deprecate_args = hash["deprecate_args"])
193-
deprecate_args = deprecate_args.dup.transform_keys(&:to_sym)
194-
deprecate_args[:because] =
195-
DeprecateDisable.to_reason_string_or_symbol(deprecate_args[:because], type: :formula)
196-
hash["deprecate_args"] = deprecate_args
197-
end
198-
199-
if (disable_args = hash["disable_args"])
200-
disable_args = disable_args.dup.transform_keys(&:to_sym)
201-
disable_args[:because] = DeprecateDisable.to_reason_string_or_symbol(disable_args[:because], type: :formula)
202-
hash["disable_args"] = disable_args
203-
end
204-
205-
hash["head_dependency_hash"] = symbolize_dependency_hash(hash["head_dependencies"])
206-
207-
hash["head_url_args"] = begin
208-
# Fall back to "" to satisfy the type checker. If the head URL is missing, head_present will be false.
209-
url = hash.dig("urls", "head", "url") || ""
210-
specs = {
211-
branch: hash.dig("urls", "head", "branch"),
212-
using: hash.dig("urls", "head", "using")&.to_sym,
213-
}.compact_blank
214-
[url, specs]
215-
end
216-
217-
if (keg_only_hash = hash["keg_only_reason"])
218-
reason = Utils.convert_to_string_or_symbol(keg_only_hash.fetch("reason"))
219-
explanation = keg_only_hash["explanation"]
220-
hash["keg_only_args"] = [reason, explanation].compact
221-
end
222-
223-
hash["license"] = SPDX.string_to_license_expression(hash["license"])
224-
225-
hash["link_overwrite_paths"] = hash["link_overwrite"]
226-
227-
if (reason = hash["no_autobump_message"])
228-
reason = reason.to_sym if NO_AUTOBUMP_REASONS_LIST.key?(reason.to_sym)
229-
hash["no_autobump_args"] = { because: reason }
230-
end
231-
232-
if (condition = hash["pour_bottle_only_if"])
233-
hash["pour_bottle_args"] = { only_if: condition.to_sym }
234-
end
235-
236-
hash["requirements_array"] = hash["requirements"]
237-
238-
hash["ruby_source_checksum"] = hash.dig("ruby_source_checksum", "sha256")
239-
240-
if (service_hash = hash["service"])
241-
service_hash = Homebrew::Service.from_hash(service_hash)
242-
243-
hash["service_run_args"], hash["service_run_kwargs"] = case (run = service_hash[:run])
244-
when Hash
245-
[[], run]
246-
when Array, String
247-
[[run], {}]
248-
else
249-
[[], {}]
250-
end
251-
252-
hash["service_name_args"] = service_hash[:name]
253-
254-
hash["service_args"] = service_hash.filter_map do |key, arg|
255-
[key.to_sym, arg] if key != :name && key != :run
256-
end
257-
end
258-
259-
hash["stable_checksum"] = hash.dig("urls", "stable", "checksum")
260-
261-
hash["stable_dependency_hash"] = symbolize_dependency_hash({
262-
"dependencies" => hash["dependencies"] || [],
263-
"build_dependencies" => hash["build_dependencies"] || [],
264-
"test_dependencies" => hash["test_dependencies"] || [],
265-
"recommended_dependencies" => hash["recommended_dependencies"] || [],
266-
"optional_dependencies" => hash["optional_dependencies"] || [],
267-
"uses_from_macos" => hash["uses_from_macos"] || [],
268-
"uses_from_macos_bounds" => hash["uses_from_macos_bounds"] || [],
269-
})
270-
271-
hash["stable_url_args"] = begin
272-
url = hash.dig("urls", "stable", "url")
273-
specs = {
274-
tag: hash.dig("urls", "stable", "tag"),
275-
revision: hash.dig("urls", "stable", "revision"),
276-
using: hash.dig("urls", "stable", "using")&.to_sym,
277-
}.compact_blank
278-
[url, specs]
279-
end
280-
281-
hash["stable_version"] = hash.dig("versions", "stable")
282-
283-
# Should match FormulaStruct::PREDICATES
284-
hash["bottle_present"] = hash["bottle"].present?
285-
hash["deprecate_present"] = hash["deprecate_args"].present?
286-
hash["disable_present"] = hash["disable_args"].present?
287-
hash["head_present"] = hash.dig("urls", "head").present?
288-
hash["keg_only_present"] = hash["keg_only_reason"].present?
289-
hash["no_autobump_message_present"] = hash["no_autobump_message"].present?
290-
hash["pour_bottle_present"] = hash["pour_bottle_only_if"].present?
291-
hash["service_present"] = hash["service"].present?
292-
hash["service_run_present"] = hash.dig("service", "run").present?
293-
hash["service_name_present"] = hash.dig("service", "name").present?
294-
hash["stable_present"] = hash.dig("urls", "stable").present?
295-
296-
FormulaStruct.from_hash(hash)
297-
end
298-
299-
# Convert from { "dependencies" => ["foo", { "bar" => "build" }, { "baz" => ["build", "test"] }] }
300-
# to { "dependencies" => ["foo", { "bar" => :build }, { "baz" => [:build, :test] }] }
301-
sig { params(hash: T.nilable(T::Hash[String, T.untyped])).returns(T.nilable(T::Hash[String, T.untyped])) }
302-
def self.symbolize_dependency_hash(hash)
303-
hash = hash.dup
304-
305-
if hash && (uses_from_macos_bounds = hash["uses_from_macos_bounds"])
306-
hash["uses_from_macos_bounds"] = uses_from_macos_bounds.map(&:deep_symbolize_keys)
307-
end
308-
309-
hash&.transform_values do |deps|
310-
deps.map do |dep|
311-
next dep unless dep.is_a?(Hash)
312-
313-
dep.transform_values do |types|
314-
case types
315-
when Array
316-
types.map(&:to_sym)
317-
else
318-
types.to_sym
319-
end
320-
end
321-
end
322-
end
323-
end
324162
end
325163
end
326164
end

0 commit comments

Comments
 (0)