Symbolize dependency hash in FormulaStruct

This commit is contained in:
Rylan Polster
2026-01-24 12:17:43 -05:00
parent 8582f2ac16
commit c8f6e04a57
3 changed files with 74 additions and 6 deletions
+29 -3
View File
@@ -202,7 +202,7 @@ module Homebrew
hash["disable_args"] = disable_args
end
hash["head_dependency_hash"] = hash["head_dependencies"]
hash["head_dependency_hash"] = symbolize_dependency_hash(hash["head_dependencies"])
hash["head_url_args"] = begin
# Fall back to "" to satisfy the type checker. If the head URL is missing, head_present will be false.
@@ -258,7 +258,7 @@ module Homebrew
hash["stable_checksum"] = hash.dig("urls", "stable", "checksum")
hash["stable_dependency_hash"] = {
hash["stable_dependency_hash"] = symbolize_dependency_hash({
"dependencies" => hash["dependencies"] || [],
"build_dependencies" => hash["build_dependencies"] || [],
"test_dependencies" => hash["test_dependencies"] || [],
@@ -266,7 +266,7 @@ module Homebrew
"optional_dependencies" => hash["optional_dependencies"] || [],
"uses_from_macos" => hash["uses_from_macos"] || [],
"uses_from_macos_bounds" => hash["uses_from_macos_bounds"] || [],
}
})
hash["stable_url_args"] = begin
url = hash.dig("urls", "stable", "url")
@@ -295,6 +295,32 @@ module Homebrew
FormulaStruct.from_hash(hash)
end
# Convert from { "dependencies" => ["foo", { "bar" => "build" }, { "baz" => ["build", "test"] }] }
# to { "dependencies" => ["foo", { "bar" => :build }, { "baz" => [:build, :test] }] }
sig { params(hash: T.nilable(T::Hash[String, T.untyped])).returns(T.nilable(T::Hash[String, T.untyped])) }
def self.symbolize_dependency_hash(hash)
hash = hash.dup
if hash && (uses_from_macos_bounds = hash["uses_from_macos_bounds"])
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
end
end
end
+4 -3
View File
@@ -74,11 +74,13 @@ module Homebrew
T.any(
# Formula name: "foo"
String,
# Hash like { "foo" => "build" } or { :foo => ["build", "test"] }
# Hash like { "foo" => :build } or { "foo" => [:build, :test] }
T::Hash[
String,
T.any(String, T::Array[String]),
T.any(Symbol, T::Array[Symbol]),
],
# Hash like { since: :catalina } for uses_from_macos_bounds
T::Hash[Symbol, Symbol],
),
],
]
@@ -97,7 +99,6 @@ module Homebrew
# Changes to this struct must be mirrored in Homebrew::API::Formula.generate_formula_struct_hash
const :aliases, T::Array[String], default: []
const :bottle, T::Hash[String, T.anything], default: {}
const :bottle_checksums, T::Array[T::Hash[Symbol, T.anything]], default: []
const :bottle_rebuild, Integer, default: 0
const :caveats, T.nilable(String)
+41
View File
@@ -63,4 +63,45 @@ RSpec.describe Homebrew::API::Formula do
expect(aliases_output).to eq formulae_aliases
end
end
specify "::symbolize_dependency_hash" do
input = {
"dependencies" => [
"foo",
{ "bar" => "build" },
{ "baz" => ["build", "test"] },
],
"uses_from_macos" => [
"abc",
{ "def" => "build" },
{ "ghi" => ["build", "test"] },
],
"uses_from_macos_bounds" => [
{},
{ "since" => "catalina" },
{},
],
}
expected_output = {
"dependencies" => [
"foo",
{ "bar" => :build },
{ "baz" => [:build, :test] },
],
"uses_from_macos" => [
"abc",
{ "def" => :build },
{ "ghi" => [:build, :test] },
],
"uses_from_macos_bounds" => [
{},
{ since: :catalina },
{},
],
}
output = described_class.symbolize_dependency_hash(input)
expect(output).to eq expected_output
end
end