Use cached json API file for formulae and cask specified paths

This commit is contained in:
Rylan Polster 2024-07-03 13:41:52 -04:00
parent 7aa2b15965
commit c16a9b33b2
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
6 changed files with 60 additions and 3 deletions

View File

@ -10,6 +10,8 @@ module Homebrew
module Cask
extend Cachable
DEFAULT_API_ENDPOINT = "cask.jws.json"
private_class_method :cache
sig { params(token: String).returns(Hash) }
@ -38,6 +40,10 @@ module Homebrew
.load(config: cask.config)
end
def self.cached_json_file_path
HOMEBREW_CACHE_API/DEFAULT_API_ENDPOINT
end
sig { returns(T::Boolean) }
def self.download_and_cache_data!
json_casks, updated = Homebrew::API.fetch_json_api_file "cask.jws.json"

View File

@ -10,6 +10,9 @@ module Homebrew
module Formula
extend Cachable
DEFAULT_API_ENDPOINT = "formula.jws.json"
INTERNAL_V3_API_ENDPOINT = "internal/v3/homebrew-core.jws.json"
private_class_method :cache
sig { params(name: String).returns(Hash) }
@ -35,13 +38,21 @@ module Homebrew
flags: formula.class.build_flags)
end
def self.cached_json_file_path
if Homebrew::API.internal_json_v3?
HOMEBREW_CACHE_API/INTERNAL_V3_API_ENDPOINT
else
HOMEBREW_CACHE_API/DEFAULT_API_ENDPOINT
end
end
sig { returns(T::Boolean) }
def self.download_and_cache_data!
if Homebrew::API.internal_json_v3?
json_formulae, updated = Homebrew::API.fetch_json_api_file "internal/v3/homebrew-core.jws.json"
json_formulae, updated = Homebrew::API.fetch_json_api_file INTERNAL_V3_API_ENDPOINT
overwrite_cache! T.cast(json_formulae, T::Hash[String, T.untyped])
else
json_formulae, updated = Homebrew::API.fetch_json_api_file "formula.jws.json"
json_formulae, updated = Homebrew::API.fetch_json_api_file DEFAULT_API_ENDPOINT
cache["aliases"] = {}
cache["renames"] = {}

View File

@ -289,7 +289,7 @@ module Cask
sig { params(token: String, from_json: Hash, path: T.nilable(Pathname)).void }
def initialize(token, from_json: T.unsafe(nil), path: nil)
@token = token.sub(%r{^homebrew/(?:homebrew-)?cask/}i, "")
@sourcefile_path = path
@sourcefile_path = path || Homebrew::API::Cask.cached_json_file_path
@path = path || CaskLoader.default_path(@token)
@from_json = from_json
end

View File

@ -358,6 +358,7 @@ class Formula
# The path that was specified to find this formula.
def specified_path
return Homebrew::API::Formula.cached_json_file_path if loaded_from_api?
return alias_path if alias_path&.exist?
return @unresolved_path if @unresolved_path.exist?

View File

@ -99,6 +99,7 @@ RSpec.describe Cask::CaskLoader::FromAPILoader, :cask do
expect(cask_from_api.token).to eq(api_token)
expect(cask_from_api.loaded_from_api?).to be(true)
expect(cask_from_api.caskfile_only?).to be(caskfile_only)
expect(cask_from_api.sourcefile_path).to eq(Homebrew::API::Cask.cached_json_file_path)
end
end

View File

@ -1932,4 +1932,42 @@ RSpec.describe Formula do
expect { f.network_access_allowed?(:foo) }.to raise_error(ArgumentError)
end
end
describe "#specified_path" do
let(:klass) do
Class.new(described_class) do
url "https://brew.sh/foo-1.0.tar.gz"
end
end
let(:name) { "formula_name" }
let(:path) { Formulary.core_path(name) }
let(:spec) { :stable }
let(:alias_name) { "baz@1" }
let(:alias_path) { CoreTap.instance.alias_dir/alias_name }
let(:f) { klass.new(name, path, spec) }
let(:f_alias) { klass.new(name, path, spec, alias_path:) }
context "when loading from a formula file" do
it "returns the formula file path" do
expect(f.specified_path).to eq(path)
end
end
context "when loaded from an alias" do
it "returns the alias path" do
expect(f_alias.specified_path).to eq(alias_path)
end
end
context "when loaded from the API" do
before do
allow(f).to receive(:loaded_from_api?).and_return(true)
end
it "returns the API path" do
expect(f.specified_path).to eq(Homebrew::API::Formula.cached_json_file_path)
end
end
end
end