Merge pull request #17615 from Homebrew/api-specified-paths

This commit is contained in:
Rylan Polster 2024-07-04 14:06:12 -04:00 committed by GitHub
commit 82a6fd2389
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 62 additions and 5 deletions

View File

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

View File

@ -10,6 +10,9 @@ module Homebrew
module Formula
extend Cachable
DEFAULT_API_FILENAME = "formula.jws.json"
INTERNAL_V3_API_FILENAME = "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_FILENAME
else
HOMEBREW_CACHE_API/DEFAULT_API_FILENAME
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_FILENAME
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_FILENAME
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

@ -249,7 +249,7 @@ module Homebrew
formula.path.relative_path_from(T.must(formula.tap).path)
when Cask::Cask
cask = formula_or_cask
if cask.sourcefile_path.blank?
if cask.sourcefile_path.blank? || cask.sourcefile_path.extname != ".rb"
return "#{cask.tap.default_remote}/blob/HEAD/#{cask.tap.relative_cask_path(cask.token)}"
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