From cefd3273cc5d37540e4789ffbf98fb54bdb182e2 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Sat, 17 Feb 2024 14:53:26 -0800 Subject: [PATCH] tap: Add helper to generate core formula API hash This abstracts away this helper to make it easier to test and reason about. --- .../Homebrew/dev-cmd/generate-formula-api.rb | 13 +------------ Library/Homebrew/tap.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Library/Homebrew/dev-cmd/generate-formula-api.rb b/Library/Homebrew/dev-cmd/generate-formula-api.rb index bf149c8c31..9926fd04f5 100644 --- a/Library/Homebrew/dev-cmd/generate-formula-api.rb +++ b/Library/Homebrew/dev-cmd/generate-formula-api.rb @@ -58,14 +58,6 @@ module Homebrew Formulary.enable_factory_cache! Formula.generating_hash! - homebrew_core_tap_hash = { - "tap_git_head" => tap.git_head, - "aliases" => tap.alias_table, - "renames" => tap.formula_renames, - "tap_migrations" => tap.tap_migrations, - "formulae" => {}, - } - tap.formula_names.each do |name| formula = Formulary.factory(name) name = formula.name @@ -77,15 +69,12 @@ module Homebrew File.write("api/formula/#{name}.json", FORMULA_JSON_TEMPLATE) File.write("formula/#{name}.html", html_template_name) end - - homebrew_core_tap_hash["formulae"][formula.name] = - formula.to_hash_with_variations(hash_method: :to_api_hash) rescue onoe "Error while generating data for formula '#{name}'." raise end - homebrew_core_tap_json = JSON.generate(homebrew_core_tap_hash) + homebrew_core_tap_json = JSON.generate(tap.to_api_hash) File.write("api/internal/v3/homebrew-core.json", homebrew_core_tap_json) unless args.dry_run? 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? diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index 568e155706..0f444b1153 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -1190,6 +1190,23 @@ class CoreTap < AbstractCoreTap hash[name] = Pathname(new_path) if existing_path.nil? || existing_path.to_s.length < new_path.length end end + + sig { returns(T::Hash[String, T.untyped]) } + def to_api_hash + formulae_api_hash = formula_names.to_h do |name| + formula = Formulary.factory(name) + formula_hash = formula.to_hash_with_variations(hash_method: :to_api_hash) + [name, formula_hash] + end + + { + "tap_git_head" => git_head, + "aliases" => alias_table, + "renames" => formula_renames, + "tap_migrations" => tap_migrations, + "formulae" => formulae_api_hash, + } + end end # A specialized {Tap} class for homebrew-cask.