Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New formula internal json v3 dependencies format #17153

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 31 additions & 8 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2361,13 +2361,6 @@
"ruby_source_sha256" => ruby_source_checksum&.hexdigest,
}

dep_hash = dependencies_hash
.except("recommended_dependencies", "optional_dependencies")
.transform_values(&:presence)
.compact

api_hash.merge!(dep_hash)

# Exclude default values.
api_hash["revision"] = revision unless revision.zero?
api_hash["version_scheme"] = version_scheme unless version_scheme.zero?
Expand All @@ -2389,6 +2382,14 @@
api_hash["versioned_formulae"] = versioned_formulae_list.map(&:name)
end

if (dependencies = internal_dependencies_hash(:stable).presence)
api_hash["dependencies"] = dependencies
end

if (head_dependencies = internal_dependencies_hash(:head).presence)
api_hash["head_dependencies"] = head_dependencies
end

if (requirements_array = serialized_requirements.presence)
api_hash["requirements"] = requirements_array
end
Expand Down Expand Up @@ -2553,7 +2554,11 @@
dependencies = self.class.spec_syms.to_h do |sym|
[sym, send(sym)&.declared_deps]
end
dependencies.transform_values! { |deps| deps&.reject(&:implicit?) } # Remove all implicit deps from all lists

# Implicit dependencies are only needed when installing from source
# since they are only used to download and unpack source files.
# @see DependencyCollector
dependencies.transform_values! { |deps| deps&.reject(&:implicit?) }

hash = {}

Expand Down Expand Up @@ -2608,6 +2613,24 @@
hash
end

def internal_dependencies_hash(spec_symbol)
raise ArgumentError, "Unsupported spec: #{spec_symbol}" unless [:stable, :head].include?(spec_symbol)
return unless (spec = public_send(spec_symbol))

spec.declared_deps.each_with_object({}) do |dep, dep_hash|
# Implicit dependencies are only needed when installing from source
# since they are only used to download and unpack source files.
# @see DependencyCollector
next if dep.implicit?

Check warning on line 2625 in Library/Homebrew/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula.rb#L2625

Added line #L2625 was not covered by tests
metadata_hash = {}
metadata_hash[:tags] = dep.tags if dep.tags.present?
metadata_hash[:uses_from_macos] = dep.bounds.presence if dep.uses_from_macos?

dep_hash[dep.name] = metadata_hash.presence
end

Check warning on line 2631 in Library/Homebrew/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula.rb#L2631

Added line #L2631 was not covered by tests
end

def on_system_blocks_exist?
self.class.on_system_blocks_exist? || @on_system_blocks_exist
end
Expand Down
87 changes: 64 additions & 23 deletions Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,38 +211,71 @@ def self.load_formula_from_api(name, flags:)
end
end

add_deps = lambda do |spec|
T.bind(self, SoftwareSpec)

dep_json = json_formula.fetch("#{spec}_dependencies", json_formula)
add_deps = if Homebrew::API.internal_json_v3?
lambda do |deps|
T.bind(self, SoftwareSpec)

deps&.each do |name, info|
tags = case info&.dig("tags")
in Array => tag_list
tag_list.map(&:to_sym)
in String => tag
tag.to_sym
else
nil
end

dep_json["dependencies"]&.each do |dep|
# Backwards compatibility check - uses_from_macos used to be a part of dependencies on Linux
next if !json_formula.key?("uses_from_macos_bounds") && uses_from_macos_names.include?(dep) &&
!Homebrew::SimulateSystem.simulating_or_running_on_macos?
if info&.key?("uses_from_macos")
bounds = info["uses_from_macos"] || {}
bounds.deep_transform_keys!(&:to_sym)
bounds.deep_transform_values!(&:to_sym)

depends_on dep
if tags
uses_from_macos name => tags, **bounds
else
uses_from_macos name, **bounds
end
elsif tags
depends_on name => tags
else
depends_on name
end
end
end
else
lambda do |spec|
T.bind(self, SoftwareSpec)

dep_json = json_formula.fetch("#{spec}_dependencies", json_formula)

[:build, :test, :recommended, :optional].each do |type|
dep_json["#{type}_dependencies"]&.each do |dep|
dep_json["dependencies"]&.each do |dep|
# Backwards compatibility check - uses_from_macos used to be a part of dependencies on Linux
next if !json_formula.key?("uses_from_macos_bounds") && uses_from_macos_names.include?(dep) &&
!Homebrew::SimulateSystem.simulating_or_running_on_macos?

depends_on dep => type
depends_on dep
end
end

dep_json["uses_from_macos"]&.each_with_index do |dep, index|
bounds = dep_json.fetch("uses_from_macos_bounds", [])[index].dup || {}
bounds.deep_transform_keys!(&:to_sym)
bounds.deep_transform_values!(&:to_sym)
[:build, :test, :recommended, :optional].each do |type|
dep_json["#{type}_dependencies"]&.each do |dep|
# Backwards compatibility check - uses_from_macos used to be a part of dependencies on Linux
next if !json_formula.key?("uses_from_macos_bounds") && uses_from_macos_names.include?(dep) &&
!Homebrew::SimulateSystem.simulating_or_running_on_macos?

if dep.is_a?(Hash)
uses_from_macos dep.deep_transform_values(&:to_sym).merge(bounds)
else
uses_from_macos dep, bounds
depends_on dep => type
end
end

dep_json["uses_from_macos"]&.each_with_index do |dep, index|
bounds = dep_json.fetch("uses_from_macos_bounds", [])[index].dup || {}
bounds.deep_transform_keys!(&:to_sym)
bounds.deep_transform_values!(&:to_sym)

if dep.is_a?(Hash)
uses_from_macos dep.deep_transform_values(&:to_sym).merge(bounds)
else
uses_from_macos dep, bounds
end
end
end
end
Expand All @@ -267,7 +300,11 @@ def self.load_formula_from_api(name, flags:)
version Homebrew::API.internal_json_v3? ? json_formula["version"] : json_formula["versions"]["stable"]
sha256 urls_stable["checksum"] if urls_stable["checksum"].present?

instance_exec(:stable, &add_deps)
if Homebrew::API.internal_json_v3?
instance_exec(json_formula["dependencies"], &add_deps)
else
instance_exec(:stable, &add_deps)
end

requirements[:stable]&.each do |req|
depends_on req
Expand All @@ -283,7 +320,11 @@ def self.load_formula_from_api(name, flags:)
}.compact
url urls_head["url"], **url_spec

instance_exec(:head, &add_deps)
if Homebrew::API.internal_json_v3?
instance_exec(json_formula["head_dependencies"], &add_deps)
else
instance_exec(:head, &add_deps)
end

requirements[:head]&.each do |req|
depends_on req
Expand Down
26 changes: 25 additions & 1 deletion Library/Homebrew/test/api/internal_tap_json/formula_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
"ruby_source_path" => "Formula/p/ponyc.rb",
"tap" => "homebrew/core",
"tap_git_head" => tap_git_head,
# TODO: improve this API before we ship internal API v3 to users
"uses_from_macos" => [{ "llvm"=>[:build, :test] }, "zlib"],
"uses_from_macos_bounds" => [{}, {}],
"versions" => { "bottle"=>true, "head"=>nil, "stable"=>"0.58.1" },
Expand All @@ -117,6 +116,26 @@
}
end

let(:inko_metadata) do
{
"desc" => "Safe and concurrent object-oriented programming language",
"full_name" => "inko",
"homepage" => "https://inko-lang.org/",
"license" => "MPL-2.0",
"name" => "inko",
"ruby_source_path" => "Formula/i/inko.rb",
"tap" => "homebrew/core",
"tap_git_head" => tap_git_head,
"dependencies" => ["llvm@15", "zstd"],
"uses_from_macos" => ["libffi", "ruby"],
"uses_from_macos_bounds" => [{ since: :catalina }, { since: :sierra }],
"versions" => { "bottle"=>true, "head"=>"HEAD", "stable"=>"0.14.0" },
"ruby_source_checksum" => {
"sha256" => "843f6b5652483b971c83876201d68c95d5f32e67e55a75ac7c95d68c4350aa1c",
},
}
end

it "loads fennel" do
fennel = Formulary.factory("fennel")
expect(fennel.to_hash).to include(**fennel_metadata)
Expand All @@ -141,6 +160,11 @@
ponyc = Formulary.factory("ponyc-lang")
expect(ponyc.to_hash).to include(**ponyc_metadata)
end

it "loads ink" do
inko = Formulary.factory("inko")
expect(inko.to_hash).to include(**inko_metadata)
end
end
end
end