diff --git a/Library/Homebrew/api.rb b/Library/Homebrew/api.rb index df733d69dd..13a411987d 100644 --- a/Library/Homebrew/api.rb +++ b/Library/Homebrew/api.rb @@ -184,6 +184,13 @@ module Homebrew Tap.fetch(org, repo) end + + sig { returns(T::Boolean) } + def self.internal_json_v3? + return @internal_json_v3 if defined?(@internal_json_v3) + + @internal_json_v3 = ENV["HOMEBREW_INTERNAL_JSON_V3"].present? + end end # @api private diff --git a/Library/Homebrew/api/formula.rb b/Library/Homebrew/api/formula.rb index e9747c1ba4..d27e963eff 100644 --- a/Library/Homebrew/api/formula.rb +++ b/Library/Homebrew/api/formula.rb @@ -39,19 +39,24 @@ module Homebrew sig { returns(T::Boolean) } def self.download_and_cache_data! - json_formulae, updated = Homebrew::API.fetch_json_api_file "formula.jws.json" + if Homebrew::API.internal_json_v3? + json_formulae, updated = Homebrew::API.fetch_json_api_file "internal/v3/homebrew-core.jws.json" + overwrite_cache! T.cast(json_formulae, T::Hash[String, T.untyped]) + else + json_formulae, updated = Homebrew::API.fetch_json_api_file "formula.jws.json" - cache["aliases"] = {} - cache["renames"] = {} - cache["formulae"] = json_formulae.to_h do |json_formula| - json_formula["aliases"].each do |alias_name| - cache["aliases"][alias_name] = json_formula["name"] - end - (json_formula["oldnames"] || [json_formula["oldname"]].compact).each do |oldname| - cache["renames"][oldname] = json_formula["name"] - end + cache["aliases"] = {} + cache["renames"] = {} + cache["formulae"] = json_formulae.to_h do |json_formula| + json_formula["aliases"].each do |alias_name| + cache["aliases"][alias_name] = json_formula["name"] + end + (json_formula["oldnames"] || [json_formula["oldname"]].compact).each do |oldname| + cache["renames"][oldname] = json_formula["name"] + end - [json_formula["name"], json_formula.except("name")] + [json_formula["name"], json_formula.except("name")] + end end updated @@ -88,6 +93,28 @@ module Homebrew cache["renames"] end + sig { returns(Hash) } + def self.tap_migrations + # Not sure that we need to reload here. + unless cache.key?("tap_migrations") + json_updated = download_and_cache_data! + write_names_and_aliases(regenerate: json_updated) + end + + cache["tap_migrations"] + end + + sig { returns(String) } + def self.tap_git_head + # Note sure we need to reload here. + unless cache.key?("tap_git_head") + json_updated = download_and_cache_data! + write_names_and_aliases(regenerate: json_updated) + end + + cache["tap_git_head"] + end + sig { params(regenerate: T::Boolean).void } def self.write_names_and_aliases(regenerate: false) download_and_cache_data! unless cache.key?("formulae") diff --git a/Library/Homebrew/extend/cachable.rb b/Library/Homebrew/extend/cachable.rb index b124c638e6..785dff4c57 100644 --- a/Library/Homebrew/extend/cachable.rb +++ b/Library/Homebrew/extend/cachable.rb @@ -7,6 +7,11 @@ module Cachable @cache ||= T.let({}, T.nilable(T::Hash[T.untyped, T.untyped])) end + sig { params(hash: T.nilable(T::Hash[T.untyped, T.untyped])).void } + def overwrite_cache!(hash) + @cache = hash + end + sig { void } def clear_cache cache.clear diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index 60df907f12..568e155706 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -1101,6 +1101,8 @@ class CoreTap < AbstractCoreTap @tap_migrations ||= if Homebrew::EnvConfig.no_install_from_api? ensure_installed! super + elsif Homebrew::API.internal_json_v3? + Homebrew::API::Formula.tap_migrations else migrations, = Homebrew::API.fetch_json_api_file "formula_tap_migrations.jws.json", stale_seconds: TAP_MIGRATIONS_STALE_SECONDS