Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cached json API file for formulae and cask specified paths #17615

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Library/Homebrew/api/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module API
module Cask
extend Cachable

DEFAULT_API_ENDPOINT = "cask.jws.json"
Rylan12 marked this conversation as resolved.
Show resolved Hide resolved

private_class_method :cache

sig { params(token: String).returns(Hash) }
Expand Down Expand Up @@ -38,6 +40,10 @@ def self.source_download(cask)
.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"
Rylan12 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
15 changes: 13 additions & 2 deletions Library/Homebrew/api/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module API
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) }
Expand All @@ -35,13 +38,21 @@ def self.source_download(formula)
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"] = {}
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/cask_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def self.try_new(ref, warn: false)
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
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ def full_installed_alias_name

# 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?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
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

Expand Down
38 changes: 38 additions & 0 deletions Library/Homebrew/test/formula_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1932,4 +1932,42 @@ def install
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