Add source paths to API.

This commit is contained in:
Markus Reiter 2023-04-07 14:06:47 +02:00
parent d15f571eb6
commit 844db75361
No known key found for this signature in database
GPG Key ID: 245293B51702655B
10 changed files with 44 additions and 18 deletions

View File

@ -116,11 +116,13 @@ module Homebrew
end end
end end
sig { params(name: String, git_head: T.nilable(String), sha256: T.nilable(String)).returns(String) } sig {
def self.fetch_homebrew_cask_source(name, git_head: nil, sha256: nil) params(name: String, path: T.any(Pathname, String), git_head: String,
sha256: T.nilable(String)).returns(String)
}
def self.fetch_homebrew_cask_source(name, path:, git_head:, sha256: nil)
# TODO: unify with formula logic (https://github.com/Homebrew/brew/issues/14746) # TODO: unify with formula logic (https://github.com/Homebrew/brew/issues/14746)
git_head = "master" if git_head.blank? raw_endpoint = "#{git_head}/#{path}"
raw_endpoint = "#{git_head}/Casks/#{name}.rb"
return cache[raw_endpoint] if cache.present? && cache.key?(raw_endpoint) return cache[raw_endpoint] if cache.present? && cache.key?(raw_endpoint)
# This API sometimes returns random 404s so needs a fallback at formulae.brew.sh. # This API sometimes returns random 404s so needs a fallback at formulae.brew.sh.

View File

@ -20,9 +20,12 @@ module Homebrew
Homebrew::API.fetch "cask/#{token}.json" Homebrew::API.fetch "cask/#{token}.json"
end end
sig { params(token: String, git_head: T.nilable(String), sha256: T.nilable(String)).returns(String) } sig {
def fetch_source(token, git_head: nil, sha256: nil) params(token: String, path: T.any(String, Pathname), git_head: String,
Homebrew::API.fetch_homebrew_cask_source token, git_head: git_head, sha256: sha256 sha256: T.nilable(String)).returns(String)
}
def fetch_source(token, path:, git_head:, sha256: nil)
Homebrew::API.fetch_homebrew_cask_source token, path: path, git_head: git_head, sha256: sha256
end end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }

View File

@ -241,6 +241,15 @@ module Cask
end end
end end
def ruby_source_path
return @ruby_source_path if defined?(@ruby_source_path)
return unless sourcefile_path
return unless tap
@ruby_source_path = sourcefile_path.relative_path_from(tap.path)
end
def ruby_source_checksum def ruby_source_checksum
@ruby_source_checksum ||= { @ruby_source_checksum ||= {
"sha256" => Digest::SHA256.file(sourcefile_path).hexdigest, "sha256" => Digest::SHA256.file(sourcefile_path).hexdigest,
@ -259,7 +268,9 @@ module Cask
raise ArgumentError, "Expected cask to be loaded from the API" unless loaded_from_api? raise ArgumentError, "Expected cask to be loaded from the API" unless loaded_from_api?
@languages = json_cask[:languages] @languages = json_cask[:languages]
@tap_git_head = json_cask[:tap_git_head] @tap_git_head = json_cask.fetch(:tap_git_head, "HEAD")
@ruby_source_path = json_cask[:ruby_source_path]
@ruby_source_checksum = json_cask[:ruby_source_checksum].freeze @ruby_source_checksum = json_cask[:ruby_source_checksum].freeze
end end
@ -308,6 +319,7 @@ module Cask
"auto_updates" => auto_updates, "auto_updates" => auto_updates,
"tap_git_head" => tap_git_head, "tap_git_head" => tap_git_head,
"languages" => languages, "languages" => languages,
"ruby_source_path" => ruby_source_path,
"ruby_source_checksum" => ruby_source_checksum, "ruby_source_checksum" => ruby_source_checksum,
} }
end end

View File

@ -564,8 +564,12 @@ on_request: true)
end end
def load_cask_from_source_api! def load_cask_from_source_api!
options = { git_head: @cask.tap_git_head, sha256: @cask.ruby_source_checksum["sha256"] } cask_source = Homebrew::API::Cask.fetch_source(
cask_source = Homebrew::API::Cask.fetch_source(@cask.token, **options) @cask.token,
path: @cask.ruby_source_path || "Casks/#{@cask.token}.rb",
git_head: @cask.tap_git_head,
sha256: @cask.ruby_source_checksum["sha256"],
)
@cask = CaskLoader::FromContentLoader.new(cask_source, tap: @cask.tap).load(config: @cask.config) @cask = CaskLoader::FromContentLoader.new(cask_source, tap: @cask.tap).load(config: @cask.config)
end end
end end

View File

@ -2199,6 +2199,7 @@ class Formula
"sha256" => resource("ruby-source").checksum.hexdigest, "sha256" => resource("ruby-source").checksum.hexdigest,
} }
elsif !self.class.loaded_from_api && path.exist? elsif !self.class.loaded_from_api && path.exist?
hsh["ruby_source_path"] = path.relative_path_from(tap.path).to_s
hsh["ruby_source_checksum"] = { hsh["ruby_source_checksum"] = {
"sha256" => Digest::SHA256.file(path).hexdigest, "sha256" => Digest::SHA256.file(path).hexdigest,
} }

View File

@ -255,10 +255,12 @@ module Formulary
end end
resource "ruby-source" do resource "ruby-source" do
url "https://raw.githubusercontent.com/Homebrew/homebrew-core/#{json_formula["tap_git_head"]}/Formula/#{name}.rb" tap_git_head = json_formula.fetch("tap_git_head", "HEAD")
if (ruby_source_sha256 = json_formula.dig("ruby_source_checksum", "sha256")).present? ruby_source_path = json_formula.fetch("ruby_source_path", "Formula/#{name}.rb")
sha256 ruby_source_sha256 ruby_source_sha256 = json_formula.dig("ruby_source_checksum", "sha256")
end
url "https://raw.githubusercontent.com/Homebrew/homebrew-core/#{tap_git_head}/#{ruby_source_path}"
sha256 ruby_source_sha256 if ruby_source_sha256
end end
def install def install

View File

@ -51,9 +51,9 @@ describe Homebrew::API::Cask do
it "fetches the source of a cask (defaulting to master when no `git_head` is passed)" do it "fetches the source of a cask (defaulting to master when no `git_head` is passed)" do
curl_output = instance_double(SystemCommand::Result, stdout: "foo", success?: true) curl_output = instance_double(SystemCommand::Result, stdout: "foo", success?: true)
expect(Utils::Curl).to receive(:curl_output) expect(Utils::Curl).to receive(:curl_output)
.with("--fail", "https://raw.githubusercontent.com/Homebrew/homebrew-cask/master/Casks/foo.rb") .with("--fail", "https://raw.githubusercontent.com/Homebrew/homebrew-cask/HEAD/Casks/foo.rb")
.and_return(curl_output) .and_return(curl_output)
described_class.fetch_source("foo", git_head: nil) described_class.fetch_source("foo", path: "Casks/foo.rb", git_head: "HEAD")
end end
end end
end end

View File

@ -72,14 +72,14 @@ describe Homebrew::API do
describe "::fetch_file_source" do describe "::fetch_file_source" do
it "fetches a file" do it "fetches a file" do
mock_curl_output stdout: json mock_curl_output stdout: json
fetched_json = described_class.fetch_homebrew_cask_source("foo", git_head: "master") fetched_json = described_class.fetch_homebrew_cask_source("foo", path: "Casks/foo.rb", git_head: "HEAD")
expect(fetched_json).to eq json expect(fetched_json).to eq json
end end
it "raises an error if the file does not exist" do it "raises an error if the file does not exist" do
mock_curl_output success: false mock_curl_output success: false
expect do expect do
described_class.fetch_homebrew_cask_source("bar", git_head: "master") described_class.fetch_homebrew_cask_source("bar", path: "Casks/bar.rb", git_head: "HEAD")
end.to raise_error(ArgumentError, /No valid file found/) end.to raise_error(ArgumentError, /No valid file found/)
end end
end end

View File

@ -93,6 +93,7 @@
"en", "en",
"eo" "eo"
], ],
"ruby_source_path": "Formula/everything.rb",
"ruby_source_checksum": { "ruby_source_checksum": {
"sha256": "b2707d1952f02c3fa566b7ad2a707a847a959d36f51d3dee642dbe5deec12f27" "sha256": "b2707d1952f02c3fa566b7ad2a707a847a959d36f51d3dee642dbe5deec12f27"
} }

View File

@ -93,6 +93,7 @@
"en", "en",
"eo" "eo"
], ],
"ruby_source_path": "Casks/everything.rb",
"ruby_source_checksum": { "ruby_source_checksum": {
"sha256": "b2707d1952f02c3fa566b7ad2a707a847a959d36f51d3dee642dbe5deec12f27" "sha256": "b2707d1952f02c3fa566b7ad2a707a847a959d36f51d3dee642dbe5deec12f27"
} }