Use cached json API file for formulae and cask specified paths
This commit is contained in:
parent
7aa2b15965
commit
c16a9b33b2
@ -10,6 +10,8 @@ module Homebrew
|
|||||||
module Cask
|
module Cask
|
||||||
extend Cachable
|
extend Cachable
|
||||||
|
|
||||||
|
DEFAULT_API_ENDPOINT = "cask.jws.json"
|
||||||
|
|
||||||
private_class_method :cache
|
private_class_method :cache
|
||||||
|
|
||||||
sig { params(token: String).returns(Hash) }
|
sig { params(token: String).returns(Hash) }
|
||||||
@ -38,6 +40,10 @@ module Homebrew
|
|||||||
.load(config: cask.config)
|
.load(config: cask.config)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.cached_json_file_path
|
||||||
|
HOMEBREW_CACHE_API/DEFAULT_API_ENDPOINT
|
||||||
|
end
|
||||||
|
|
||||||
sig { returns(T::Boolean) }
|
sig { returns(T::Boolean) }
|
||||||
def self.download_and_cache_data!
|
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 "cask.jws.json"
|
||||||
|
|||||||
@ -10,6 +10,9 @@ module Homebrew
|
|||||||
module Formula
|
module Formula
|
||||||
extend Cachable
|
extend Cachable
|
||||||
|
|
||||||
|
DEFAULT_API_ENDPOINT = "formula.jws.json"
|
||||||
|
INTERNAL_V3_API_ENDPOINT = "internal/v3/homebrew-core.jws.json"
|
||||||
|
|
||||||
private_class_method :cache
|
private_class_method :cache
|
||||||
|
|
||||||
sig { params(name: String).returns(Hash) }
|
sig { params(name: String).returns(Hash) }
|
||||||
@ -35,13 +38,21 @@ module Homebrew
|
|||||||
flags: formula.class.build_flags)
|
flags: formula.class.build_flags)
|
||||||
end
|
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) }
|
sig { returns(T::Boolean) }
|
||||||
def self.download_and_cache_data!
|
def self.download_and_cache_data!
|
||||||
if Homebrew::API.internal_json_v3?
|
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])
|
overwrite_cache! T.cast(json_formulae, T::Hash[String, T.untyped])
|
||||||
else
|
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["aliases"] = {}
|
||||||
cache["renames"] = {}
|
cache["renames"] = {}
|
||||||
|
|||||||
@ -289,7 +289,7 @@ module Cask
|
|||||||
sig { params(token: String, from_json: Hash, path: T.nilable(Pathname)).void }
|
sig { params(token: String, from_json: Hash, path: T.nilable(Pathname)).void }
|
||||||
def initialize(token, from_json: T.unsafe(nil), path: nil)
|
def initialize(token, from_json: T.unsafe(nil), path: nil)
|
||||||
@token = token.sub(%r{^homebrew/(?:homebrew-)?cask/}i, "")
|
@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)
|
@path = path || CaskLoader.default_path(@token)
|
||||||
@from_json = from_json
|
@from_json = from_json
|
||||||
end
|
end
|
||||||
|
|||||||
@ -358,6 +358,7 @@ class Formula
|
|||||||
|
|
||||||
# The path that was specified to find this formula.
|
# The path that was specified to find this formula.
|
||||||
def specified_path
|
def specified_path
|
||||||
|
return Homebrew::API::Formula.cached_json_file_path if loaded_from_api?
|
||||||
return alias_path if alias_path&.exist?
|
return alias_path if alias_path&.exist?
|
||||||
|
|
||||||
return @unresolved_path if @unresolved_path.exist?
|
return @unresolved_path if @unresolved_path.exist?
|
||||||
|
|||||||
@ -99,6 +99,7 @@ RSpec.describe Cask::CaskLoader::FromAPILoader, :cask do
|
|||||||
expect(cask_from_api.token).to eq(api_token)
|
expect(cask_from_api.token).to eq(api_token)
|
||||||
expect(cask_from_api.loaded_from_api?).to be(true)
|
expect(cask_from_api.loaded_from_api?).to be(true)
|
||||||
expect(cask_from_api.caskfile_only?).to be(caskfile_only)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1932,4 +1932,42 @@ RSpec.describe Formula do
|
|||||||
expect { f.network_access_allowed?(:foo) }.to raise_error(ArgumentError)
|
expect { f.network_access_allowed?(:foo) }.to raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user