diff --git a/Library/Homebrew/api/cask.rb b/Library/Homebrew/api/cask.rb index 8d518c4406bb1..082b2c67fd605 100644 --- a/Library/Homebrew/api/cask.rb +++ b/Library/Homebrew/api/cask.rb @@ -10,6 +10,8 @@ module API module Cask extend Cachable + DEFAULT_API_FILENAME = "cask.jws.json" + private_class_method :cache sig { params(token: String).returns(Hash) } @@ -38,9 +40,13 @@ def self.source_download(cask) .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| diff --git a/Library/Homebrew/api/formula.rb b/Library/Homebrew/api/formula.rb index e14866fefb4f9..94f9b004566b9 100644 --- a/Library/Homebrew/api/formula.rb +++ b/Library/Homebrew/api/formula.rb @@ -10,6 +10,9 @@ module API 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 @@ 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_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"] = {} diff --git a/Library/Homebrew/cask/cask_loader.rb b/Library/Homebrew/cask/cask_loader.rb index 812040637b4af..1c20582efe60b 100644 --- a/Library/Homebrew/cask/cask_loader.rb +++ b/Library/Homebrew/cask/cask_loader.rb @@ -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 diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index a9f864b3a265f..b2ad130a5b12a 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -249,7 +249,7 @@ def github_info(formula_or_cask) 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 diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 6bf5a1203f683..64d2ac954afe8 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -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? diff --git a/Library/Homebrew/test/cask/cask_loader/from_api_loader_spec.rb b/Library/Homebrew/test/cask/cask_loader/from_api_loader_spec.rb index 43f2314c221db..f066a098a93ac 100644 --- a/Library/Homebrew/test/cask/cask_loader/from_api_loader_spec.rb +++ b/Library/Homebrew/test/cask/cask_loader/from_api_loader_spec.rb @@ -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 diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 6fc08f61d1215..b558edb6cb272 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -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