2025-01-06 00:12:03 +00:00
|
|
|
# typed: strict
|
2021-08-06 02:30:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2025-06-09 19:06:16 +01:00
|
|
|
require "cachable"
|
2025-07-18 15:01:34 +01:00
|
|
|
require "api"
|
2025-07-22 17:48:32 +01:00
|
|
|
require "api/source_download"
|
2025-07-18 15:01:34 +01:00
|
|
|
require "download_queue"
|
2023-02-16 21:49:03 +00:00
|
|
|
|
2021-08-06 02:30:44 -04:00
|
|
|
module Homebrew
|
|
|
|
module API
|
|
|
|
# Helper functions for using the formula JSON API.
|
|
|
|
module Formula
|
2024-02-25 15:14:22 -08:00
|
|
|
extend Cachable
|
2021-08-06 02:30:44 -04:00
|
|
|
|
2024-07-04 12:08:01 -04:00
|
|
|
DEFAULT_API_FILENAME = "formula.jws.json"
|
2024-07-03 13:41:52 -04:00
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
private_class_method :cache
|
2023-02-16 21:49:03 +00:00
|
|
|
|
2025-01-06 00:12:03 +00:00
|
|
|
sig { params(name: String).returns(T::Hash[String, T.untyped]) }
|
2025-08-10 22:45:59 -04:00
|
|
|
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
|
2024-02-26 21:21:38 -08:00
|
|
|
end
|
2021-12-07 00:13:56 +00:00
|
|
|
|
2025-07-29 13:48:43 +01:00
|
|
|
sig { params(formula: ::Formula, download_queue: T.nilable(Homebrew::DownloadQueue)).returns(Homebrew::API::SourceDownload) }
|
|
|
|
def self.source_download(formula, download_queue: nil)
|
2024-02-26 21:21:38 -08:00
|
|
|
path = formula.ruby_source_path || "Formula/#{formula.name}.rb"
|
|
|
|
git_head = formula.tap_git_head || "HEAD"
|
|
|
|
tap = formula.tap&.full_name || "Homebrew/homebrew-core"
|
|
|
|
|
2025-07-22 17:48:32 +01:00
|
|
|
download = Homebrew::API::SourceDownload.new(
|
2024-02-26 21:21:38 -08:00
|
|
|
"https://raw.githubusercontent.com/#{tap}/#{git_head}/#{path}",
|
|
|
|
formula.ruby_source_checksum,
|
|
|
|
cache: HOMEBREW_CACHE_API_SOURCE/"#{tap}/#{git_head}/Formula",
|
|
|
|
)
|
2025-07-29 13:48:43 +01:00
|
|
|
|
|
|
|
if download_queue
|
|
|
|
download_queue.enqueue(download)
|
2025-07-31 05:40:40 +01:00
|
|
|
elsif !download.symlink_location.exist?
|
2025-07-29 13:48:43 +01:00
|
|
|
download.fetch
|
|
|
|
end
|
|
|
|
|
|
|
|
download
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { params(formula: ::Formula).returns(::Formula) }
|
|
|
|
def self.source_download_formula(formula)
|
|
|
|
download = source_download(formula)
|
2024-07-24 05:39:06 +01:00
|
|
|
|
2025-08-19 03:59:08 +01:00
|
|
|
with_env(HOMEBREW_INTERNAL_ALLOW_PACKAGES_FROM_PATHS: "1") do
|
2024-07-24 05:39:06 +01:00
|
|
|
Formulary.factory(download.symlink_location,
|
|
|
|
formula.active_spec_sym,
|
|
|
|
alias_path: formula.alias_path,
|
|
|
|
flags: formula.class.build_flags)
|
|
|
|
end
|
2024-02-26 21:21:38 -08:00
|
|
|
end
|
2023-04-18 00:22:13 +01:00
|
|
|
|
2025-01-06 00:12:03 +00:00
|
|
|
sig { returns(Pathname) }
|
2024-07-03 13:41:52 -04:00
|
|
|
def self.cached_json_file_path
|
2025-08-11 00:36:10 -04:00
|
|
|
HOMEBREW_CACHE_API/DEFAULT_API_FILENAME
|
2024-07-03 13:41:52 -04:00
|
|
|
end
|
|
|
|
|
2025-07-18 15:01:34 +01:00
|
|
|
sig {
|
2025-09-03 15:30:31 -04:00
|
|
|
params(download_queue: T.nilable(Homebrew::DownloadQueue), stale_seconds: T.nilable(Integer))
|
2025-07-18 15:01:34 +01:00
|
|
|
.returns([T.any(T::Array[T.untyped], T::Hash[String, T.untyped]), T::Boolean])
|
|
|
|
}
|
2025-09-03 15:30:31 -04:00
|
|
|
def self.fetch_api!(download_queue: nil, stale_seconds: nil)
|
2025-08-11 00:36:10 -04:00
|
|
|
Homebrew::API.fetch_json_api_file DEFAULT_API_FILENAME, stale_seconds:, download_queue:
|
2025-07-18 15:01:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
sig {
|
2025-09-03 15:30:31 -04:00
|
|
|
params(download_queue: T.nilable(Homebrew::DownloadQueue), stale_seconds: T.nilable(Integer))
|
2025-07-18 15:01:34 +01:00
|
|
|
.returns([T.any(T::Array[T.untyped], T::Hash[String, T.untyped]), T::Boolean])
|
|
|
|
}
|
2025-09-03 15:30:31 -04:00
|
|
|
def self.fetch_tap_migrations!(download_queue: nil, stale_seconds: nil)
|
2025-07-18 15:01:34 +01:00
|
|
|
Homebrew::API.fetch_json_api_file "formula_tap_migrations.jws.json", stale_seconds:, download_queue:
|
|
|
|
end
|
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def self.download_and_cache_data!
|
2025-07-18 15:01:34 +01:00
|
|
|
json_formulae, updated = fetch_api!
|
2024-02-10 22:24:36 -08:00
|
|
|
|
2025-02-06 21:04:08 -08:00
|
|
|
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"]
|
2023-02-16 21:49:03 +00:00
|
|
|
end
|
2025-02-06 21:04:08 -08:00
|
|
|
(json_formula["oldnames"] || [json_formula["oldname"]].compact).each do |oldname|
|
|
|
|
cache["renames"][oldname] = json_formula["name"]
|
|
|
|
end
|
|
|
|
|
|
|
|
[json_formula["name"], json_formula.except("name")]
|
2023-02-16 21:49:03 +00:00
|
|
|
end
|
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
updated
|
|
|
|
end
|
|
|
|
private_class_method :download_and_cache_data!
|
2023-02-16 21:49:03 +00:00
|
|
|
|
2025-01-06 00:12:03 +00:00
|
|
|
sig { returns(T::Hash[String, T.untyped]) }
|
2024-02-26 21:21:38 -08:00
|
|
|
def self.all_formulae
|
|
|
|
unless cache.key?("formulae")
|
|
|
|
json_updated = download_and_cache_data!
|
|
|
|
write_names_and_aliases(regenerate: json_updated)
|
2021-12-07 00:13:56 +00:00
|
|
|
end
|
2022-09-14 23:54:14 -04:00
|
|
|
|
2025-07-18 15:01:34 +01:00
|
|
|
cache.fetch("formulae")
|
2024-02-26 21:21:38 -08:00
|
|
|
end
|
2023-02-16 21:49:03 +00:00
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
sig { returns(T::Hash[String, String]) }
|
|
|
|
def self.all_aliases
|
|
|
|
unless cache.key?("aliases")
|
|
|
|
json_updated = download_and_cache_data!
|
|
|
|
write_names_and_aliases(regenerate: json_updated)
|
2023-02-16 21:49:03 +00:00
|
|
|
end
|
|
|
|
|
2025-07-18 15:01:34 +01:00
|
|
|
cache.fetch("aliases")
|
2024-02-26 21:21:38 -08:00
|
|
|
end
|
2023-04-27 04:09:41 +01:00
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
sig { returns(T::Hash[String, String]) }
|
|
|
|
def self.all_renames
|
|
|
|
unless cache.key?("renames")
|
|
|
|
json_updated = download_and_cache_data!
|
|
|
|
write_names_and_aliases(regenerate: json_updated)
|
2023-04-27 04:09:41 +01:00
|
|
|
end
|
|
|
|
|
2025-07-18 15:01:34 +01:00
|
|
|
cache.fetch("renames")
|
2024-02-26 21:21:38 -08:00
|
|
|
end
|
|
|
|
|
2025-01-06 00:12:03 +00:00
|
|
|
sig { returns(T::Hash[String, T.untyped]) }
|
2024-02-10 22:24:36 -08:00
|
|
|
def self.tap_migrations
|
|
|
|
unless cache.key?("tap_migrations")
|
2025-07-18 15:01:34 +01:00
|
|
|
json_migrations, = fetch_tap_migrations!
|
|
|
|
cache["tap_migrations"] = json_migrations
|
2024-02-10 22:24:36 -08:00
|
|
|
end
|
|
|
|
|
2025-07-18 15:01:34 +01:00
|
|
|
cache.fetch("tap_migrations")
|
2024-02-10 22:24:36 -08:00
|
|
|
end
|
|
|
|
|
2024-02-26 21:21:38 -08:00
|
|
|
sig { params(regenerate: T::Boolean).void }
|
|
|
|
def self.write_names_and_aliases(regenerate: false)
|
|
|
|
download_and_cache_data! unless cache.key?("formulae")
|
2023-02-16 21:49:03 +00:00
|
|
|
|
2025-08-11 00:36:10 -04:00
|
|
|
Homebrew::API.write_names_file!(all_formulae.keys, "formula", regenerate:)
|
|
|
|
Homebrew::API.write_aliases_file!(all_aliases, "formula", regenerate:)
|
2021-08-06 02:30:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|