Merge pull request #20406 from Homebrew/alias-rename-migrations-internal-api

Cleanup internal API handling
This commit is contained in:
Mike McQuaid 2025-08-11 13:58:38 +00:00 committed by GitHub
commit 04b47bc1aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 89 additions and 38 deletions

View File

@ -162,6 +162,20 @@ module Homebrew
false
end
sig { params(aliases: T::Hash[String, String], type: String, regenerate: T::Boolean).returns(T::Boolean) }
def self.write_aliases_file!(aliases, type, regenerate:)
aliases_path = HOMEBREW_CACHE_API/"#{type}_aliases.txt"
if !aliases_path.exist? || regenerate
aliases_text = aliases.map do |alias_name, real_name|
"#{alias_name}|#{real_name}"
end
aliases_path.write(aliases_text.join("\n"))
return true
end
false
end
sig {
params(json_data: T::Hash[String, T.untyped])
.returns([T::Boolean, T.any(String, T::Array[T.untyped], T::Hash[String, T.untyped])])

View File

@ -14,18 +14,25 @@ module Homebrew
DEFAULT_API_FILENAME = "cask.jws.json"
sig { returns(String) }
def self.api_filename
return DEFAULT_API_FILENAME unless ENV.fetch("HOMEBREW_USE_INTERNAL_API", false)
"cask.#{SimulateSystem.current_tag}.jws.json"
end
private_class_method :cache
sig { params(token: String).returns(T::Hash[String, T.untyped]) }
def self.fetch(token)
Homebrew::API.fetch "cask/#{token}.json"
sig { params(name: String).returns(T::Hash[String, T.untyped]) }
def self.cask_json(name)
fetch_cask_json! name if !cache.key?("cask_json") || !cache.fetch("cask_json").key?(name)
cache.fetch("cask_json").fetch(name)
end
sig { params(name: String, download_queue: T.nilable(DownloadQueue)).void }
def self.fetch_cask_json!(name, download_queue: nil)
endpoint = "cask/#{name}.json"
json_cask, updated = Homebrew::API.fetch_json_api_file endpoint, download_queue: download_queue
return if download_queue
json_cask = JSON.parse((HOMEBREW_CACHE_API/endpoint).read) unless updated
cache["cask_json"] ||= {}
cache["cask_json"][name] = json_cask
end
sig { params(cask: ::Cask::Cask, download_queue: T.nilable(Homebrew::DownloadQueue)).returns(Homebrew::API::SourceDownload) }
@ -64,7 +71,7 @@ module Homebrew
sig { returns(Pathname) }
def self.cached_json_file_path
HOMEBREW_CACHE_API/api_filename
HOMEBREW_CACHE_API/DEFAULT_API_FILENAME
end
sig {
@ -72,7 +79,7 @@ module Homebrew
.returns([T.any(T::Array[T.untyped], T::Hash[String, T.untyped]), T::Boolean])
}
def self.fetch_api!(download_queue: nil, stale_seconds: Homebrew::EnvConfig.api_auto_update_secs.to_i)
Homebrew::API.fetch_json_api_file api_filename, stale_seconds:, download_queue:
Homebrew::API.fetch_json_api_file DEFAULT_API_FILENAME, stale_seconds:, download_queue:
end
sig {

View File

@ -14,18 +14,25 @@ module Homebrew
DEFAULT_API_FILENAME = "formula.jws.json"
sig { returns(String) }
def self.api_filename
return DEFAULT_API_FILENAME unless ENV.fetch("HOMEBREW_USE_INTERNAL_API", false)
"internal/formula.#{SimulateSystem.current_tag}.jws.json"
end
private_class_method :cache
sig { params(name: String).returns(T::Hash[String, T.untyped]) }
def self.fetch(name)
Homebrew::API.fetch "formula/#{name}.json"
def self.formula_json(name)
fetch_formula_json! name if !cache.key?("formula_json") || !cache.fetch("formula_json").key?(name)
cache.fetch("formula_json").fetch(name)
end
sig { params(name: String, download_queue: T.nilable(DownloadQueue)).void }
def self.fetch_formula_json!(name, download_queue: nil)
endpoint = "formula/#{name}.json"
json_formula, updated = Homebrew::API.fetch_json_api_file endpoint, download_queue: download_queue
return if download_queue
json_formula = JSON.parse((HOMEBREW_CACHE_API/endpoint).read) unless updated
cache["formula_json"] ||= {}
cache["formula_json"][name] = json_formula
end
sig { params(formula: ::Formula, download_queue: T.nilable(Homebrew::DownloadQueue)).returns(Homebrew::API::SourceDownload) }
@ -63,7 +70,7 @@ module Homebrew
sig { returns(Pathname) }
def self.cached_json_file_path
HOMEBREW_CACHE_API/api_filename
HOMEBREW_CACHE_API/DEFAULT_API_FILENAME
end
sig {
@ -71,7 +78,7 @@ module Homebrew
.returns([T.any(T::Array[T.untyped], T::Hash[String, T.untyped]), T::Boolean])
}
def self.fetch_api!(download_queue: nil, stale_seconds: Homebrew::EnvConfig.api_auto_update_secs.to_i)
Homebrew::API.fetch_json_api_file api_filename, stale_seconds:, download_queue:
Homebrew::API.fetch_json_api_file DEFAULT_API_FILENAME, stale_seconds:, download_queue:
end
sig {
@ -147,13 +154,8 @@ module Homebrew
def self.write_names_and_aliases(regenerate: false)
download_and_cache_data! unless cache.key?("formulae")
return unless Homebrew::API.write_names_file!(all_formulae.keys, "formula", regenerate:)
(HOMEBREW_CACHE_API/"formula_aliases.txt").open("w") do |file|
all_aliases.each do |alias_name, real_name|
file.puts "#{alias_name}|#{real_name}"
end
end
Homebrew::API.write_names_file!(all_formulae.keys, "formula", regenerate:)
Homebrew::API.write_aliases_file!(all_aliases, "formula", regenerate:)
end
end
end

View File

@ -71,11 +71,24 @@ module Homebrew
File.write("_data/cask_canonical.json", "#{canonical_json}\n") unless args.dry_run?
OnSystem::VALID_OS_ARCH_TAGS.each do |bottle_tag|
variation_casks = all_casks.map do |_, cask|
Homebrew::API.merge_variations(cask, bottle_tag:)
renames = {}
variation_casks = all_casks.map do |token, cask|
cask = Homebrew::API.merge_variations(cask, bottle_tag:)
cask["old_tokens"]&.each do |old_token|
renames[old_token] = token
end
cask
end
File.write("api/internal/cask.#{bottle_tag}.json", JSON.generate(variation_casks)) unless args.dry_run?
json_contents = {
casks: variation_casks,
renames: renames,
tap_migrations: CoreCaskTap.instance.tap_migrations,
}
File.write("api/internal/cask.#{bottle_tag}.json", JSON.generate(json_contents)) unless args.dry_run?
end
end
end

View File

@ -69,9 +69,19 @@ module Homebrew
File.write("_data/formula_canonical.json", "#{canonical_json}\n") unless args.dry_run?
OnSystem::VALID_OS_ARCH_TAGS.each do |bottle_tag|
aliases = {}
renames = {}
variation_formulae = all_formulae.to_h do |name, formula|
formula = Homebrew::API.merge_variations(formula, bottle_tag:)
formula["aliases"]&.each do |alias_name|
aliases[alias_name] = name
end
formula["oldnames"]&.each do |oldname|
renames[oldname] = name
end
version = Version.new(formula.dig("versions", "stable"))
pkg_version = PkgVersion.new(version, formula["revision"])
rebuild = formula.dig("bottle", "stable", "rebuild") || 0
@ -87,9 +97,14 @@ module Homebrew
[name, [pkg_version.to_s, rebuild, sha256]]
end
unless args.dry_run?
File.write("api/internal/formula.#{bottle_tag}.json", JSON.generate(variation_formulae))
end
json_contents = {
formulae: variation_formulae,
aliases: aliases,
renames: renames,
tap_migrations: CoreTap.instance.tap_migrations,
}
File.write("api/internal/formula.#{bottle_tag}.json", JSON.generate(json_contents)) unless args.dry_run?
end
end
end

View File

@ -329,7 +329,7 @@ module Utils
require "api"
json = Homebrew::API::Formula.fetch formula.name
json = Homebrew::API::Formula.formula_json formula.name
return if json.blank? || json["analytics"].blank?
output_analytics(json, args:)
@ -345,7 +345,7 @@ module Utils
require "api"
json = Homebrew::API::Cask.fetch cask.token
json = Homebrew::API::Cask.cask_json cask.token
return if json.blank? || json["analytics"].blank?
output_analytics(json, args:)