From 6182a6575a0b4a4636eecf996b58ed6e3c27806a Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Tue, 4 Feb 2025 22:28:30 -0500 Subject: [PATCH] WIP: create lightweight internal JSON API --- Library/Homebrew/dev-cmd/generate-cask-api.rb | 4 +-- .../Homebrew/dev-cmd/generate-formula-api.rb | 8 +++-- Library/Homebrew/formula.rb | 15 +++++++++ Library/Homebrew/tap.rb | 31 +++++++++++++------ 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/Library/Homebrew/dev-cmd/generate-cask-api.rb b/Library/Homebrew/dev-cmd/generate-cask-api.rb index ac35895e3d75d..5ebeec1d92845 100644 --- a/Library/Homebrew/dev-cmd/generate-cask-api.rb +++ b/Library/Homebrew/dev-cmd/generate-cask-api.rb @@ -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 @@ -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 diff --git a/Library/Homebrew/dev-cmd/generate-formula-api.rb b/Library/Homebrew/dev-cmd/generate-formula-api.rb index a3fe69de0d31a..3afb572ea39a2 100644 --- a/Library/Homebrew/dev-cmd/generate-formula-api.rb +++ b/Library/Homebrew/dev-cmd/generate-formula-api.rb @@ -32,7 +32,7 @@ def run 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 @@ -60,8 +60,10 @@ def run 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| + 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 diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 4dbd9124f140d..182504c4bd578 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -2572,6 +2572,21 @@ def to_hash 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:) + next unless bottle_tag.valid_combination? + + hash = { + "name" => name, + "pkg_version" => pkg_version.to_s, + "sha256" => bottle_hash["files"][bottle_tag.to_sym]&.fetch("sha256"), + } + + [bottle_tag.to_s, hash] + end.to_h + end + def to_internal_api_hash api_hash = { "desc" => desc, diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index 52794cb99b57c..02ebbae5281b9 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -1402,20 +1402,31 @@ def formula_files_by_name 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| formula = Formulary.factory(name) - formula_hash = formula.to_hash_with_variations(hash_method: :to_internal_api_hash) + formula_hash = formula.to_bottle_manifest_hashes [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:) + next unless bottle_tag.valid_combination? + + formulae_hash = formula_hashes.transform_values do |formula_hash| + formula_hash[bottle_tag.to_s] + end + + api_hash = { + "tap_git_head" => git_head, + "aliases" => alias_table, + "renames" => formula_renames, + "tap_migrations" => tap_migrations, + "formulae" => formulae_hash, + } + + [bottle_tag.to_s, api_hash] + end.to_h end end