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

WIP: create lightweight internal JSON API #19241

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Library/Homebrew/dev-cmd/generate-cask-api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def run
raise TapUnavailableError, tap.name unless tap.installed?

unless args.dry_run?
directories = ["_data/cask", "api/cask", "api/cask-source", "cask", "api/internal/v3"].freeze
directories = ["_data/cask", "api/cask", "api/cask-source", "cask", "api/internal"].freeze
FileUtils.rm_rf directories
FileUtils.mkdir_p directories
end
Expand Down Expand Up @@ -63,7 +63,7 @@ def run
end

homebrew_cask_tap_json = JSON.generate(tap.to_internal_api_hash)
File.write("api/internal/v3/homebrew-cask.json", homebrew_cask_tap_json) unless args.dry_run?
File.write("api/internal/homebrew-cask.json", homebrew_cask_tap_json) unless args.dry_run?
canonical_json = JSON.pretty_generate(tap.cask_renames)
File.write("_data/cask_canonical.json", "#{canonical_json}\n") unless args.dry_run?
end
Expand Down
8 changes: 5 additions & 3 deletions Library/Homebrew/dev-cmd/generate-formula-api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
raise TapUnavailableError, tap.name unless tap.installed?

unless args.dry_run?
directories = ["_data/formula", "api/formula", "formula", "api/internal/v3"]
directories = ["_data/formula", "api/formula", "formula", "api/internal"]
FileUtils.rm_rf directories + ["_data/formula_canonical.json"]
FileUtils.mkdir_p directories
end
Expand Down Expand Up @@ -60,8 +60,10 @@
raise
end

homebrew_core_tap_json = JSON.generate(tap.to_internal_api_hash)
File.write("api/internal/v3/homebrew-core.json", homebrew_core_tap_json) unless args.dry_run?
tap.to_internal_api_hashes.each do |os_arch, hash|

Check warning on line 63 in Library/Homebrew/dev-cmd/generate-formula-api.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/generate-formula-api.rb#L63

Added line #L63 was not covered by tests
File.write("api/internal/homebrew-core.#{os_arch}.json", JSON.generate(hash)) unless args.dry_run?
end

canonical_json = JSON.pretty_generate(tap.formula_renames.merge(tap.alias_table))
File.write("_data/formula_canonical.json", "#{canonical_json}\n") unless args.dry_run?
end
Expand Down
15 changes: 15 additions & 0 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,21 @@
hsh
end

def to_bottle_manifest_hashes
OnSystem::ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch|
bottle_tag = Utils::Bottles::Tag.new(system: os, arch:)

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula.rb#L2576-L2577

Added lines #L2576 - L2577 were not covered by tests
next unless bottle_tag.valid_combination?

hash = {
"name" => name,

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula.rb#L2581

Added line #L2581 was not covered by tests
"pkg_version" => pkg_version.to_s,
"sha256" => bottle_hash["files"][bottle_tag.to_sym]&.fetch("sha256"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could slim things even more and consider not setting this key at all unless we have a non-nil sha256 (pulling this into an if below)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, makes sense! I figured I might do some testing to see if doing an array instead of a hash is significantly better, too. I wasn't sure whether, once gzipped, that would make a huge difference or not

}

[bottle_tag.to_s, hash]

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula.rb#L2586

Added line #L2586 was not covered by tests
end.to_h
end

def to_internal_api_hash
api_hash = {
"desc" => desc,
Expand Down
31 changes: 21 additions & 10 deletions Library/Homebrew/tap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1402,20 +1402,31 @@
end

sig { returns(T::Hash[String, T.untyped]) }
def to_internal_api_hash
formulae_api_hash = formula_names.to_h do |name|
def to_internal_api_hashes
formula_hashes = formula_names.to_h do |name|

Check warning on line 1406 in Library/Homebrew/tap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/tap.rb#L1406

Added line #L1406 was not covered by tests
formula = Formulary.factory(name)
formula_hash = formula.to_hash_with_variations(hash_method: :to_internal_api_hash)
formula_hash = formula.to_bottle_manifest_hashes

Check warning on line 1408 in Library/Homebrew/tap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/tap.rb#L1408

Added line #L1408 was not covered by tests
[name, formula_hash]
end

{
"tap_git_head" => git_head,
"aliases" => alias_table,
"renames" => formula_renames,
"tap_migrations" => tap_migrations,
"formulae" => formulae_api_hash,
}
OnSystem::ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch|
bottle_tag = Utils::Bottles::Tag.new(system: os, arch:)

Check warning on line 1413 in Library/Homebrew/tap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/tap.rb#L1412-L1413

Added lines #L1412 - L1413 were not covered by tests
next unless bottle_tag.valid_combination?

formulae_hash = formula_hashes.transform_values do |formula_hash|
formula_hash[bottle_tag.to_s]

Check warning on line 1417 in Library/Homebrew/tap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/tap.rb#L1416-L1417

Added lines #L1416 - L1417 were not covered by tests
end

api_hash = {
"tap_git_head" => git_head,

Check warning on line 1421 in Library/Homebrew/tap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/tap.rb#L1421

Added line #L1421 was not covered by tests
"aliases" => alias_table,
"renames" => formula_renames,
"tap_migrations" => tap_migrations,
"formulae" => formulae_hash,
}

[bottle_tag.to_s, api_hash]

Check warning on line 1428 in Library/Homebrew/tap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/tap.rb#L1428

Added line #L1428 was not covered by tests
end.to_h
end
end

Expand Down
Loading