fix: Allow Formula.url with no args to get url

This commit is contained in:
Douglas Eichelberger 2025-03-26 11:35:26 -07:00
parent e532dea382
commit e58486cd4c
No known key found for this signature in database
GPG Key ID: F90193CBD547EB81
9 changed files with 26 additions and 13 deletions

View File

@ -67,7 +67,7 @@ class DependencyCollector
def cache_key(spec) def cache_key(spec)
if spec.is_a?(Resource) if spec.is_a?(Resource)
if spec.download_strategy <= CurlDownloadStrategy if spec.download_strategy <= CurlDownloadStrategy
return "#{spec.download_strategy}#{File.extname(spec.url).split("?").first}" return "#{spec.download_strategy}#{File.extname(T.must(spec.url)).split("?").first}"
end end
return spec.download_strategy return spec.download_strategy

View File

@ -156,7 +156,7 @@ module Homebrew
new_hash = args.sha256 new_hash = args.sha256
new_tag = args.tag new_tag = args.tag
new_revision = args.revision new_revision = args.revision
old_url = formula_spec.url old_url = T.must(formula_spec.url)
old_tag = formula_spec.specs[:tag] old_tag = formula_spec.specs[:tag]
old_formula_version = formula_version(formula) old_formula_version = formula_version(formula)
old_version = old_formula_version.to_s old_version = old_formula_version.to_s
@ -234,7 +234,7 @@ module Homebrew
replacement_pairs += if new_url_hash.present? replacement_pairs += if new_url_hash.present?
[ [
[ [
/#{Regexp.escape(formula_spec.url)}/, /#{Regexp.escape(T.must(formula_spec.url))}/,
new_url, new_url,
], ],
[ [
@ -256,7 +256,7 @@ module Homebrew
elsif new_url.present? elsif new_url.present?
[ [
[ [
/#{Regexp.escape(formula_spec.url)}/, /#{Regexp.escape(T.must(formula_spec.url))}/,
new_url, new_url,
], ],
[ [

View File

@ -12,7 +12,7 @@ module Downloadable
abstract! abstract!
requires_ancestor { Kernel } requires_ancestor { Kernel }
sig { overridable.returns(T.nilable(URL)) } sig { overridable.returns(T.any(NilClass, String, URL)) }
attr_reader :url attr_reader :url
sig { overridable.returns(T.nilable(Checksum)) } sig { overridable.returns(T.nilable(Checksum)) }

View File

@ -3650,8 +3650,8 @@ class Formula
# ``` # ```
# #
# @api public # @api public
sig { params(val: String, specs: T::Hash[Symbol, T.any(String, Symbol)]).void } sig { params(val: String, specs: T::Hash[Symbol, T.anything]).returns(String) }
def url(val, specs = {}) = stable.url(val, specs) def url(val = T.unsafe(nil), specs = {}) = stable.url(val, specs)
# The version string for the {.stable} version of the formula. # The version string for the {.stable} version of the formula.
# The version is autodetected from the URL and/or tag so only needs to be # The version is autodetected from the URL and/or tag so only needs to be

View File

@ -186,6 +186,7 @@ class Resource
@checksum = Checksum.new(val) @checksum = Checksum.new(val)
end end
sig { override.params(val: T.nilable(String), specs: T.anything).returns(T.nilable(String)) }
def url(val = nil, **specs) def url(val = nil, **specs)
return @url&.to_s if val.nil? return @url&.to_s if val.nil?
@ -198,6 +199,7 @@ class Resource
@url = URL.new(val, specs) @url = URL.new(val, specs)
@downloader = nil @downloader = nil
@download_strategy = @url.download_strategy @download_strategy = @url.download_strategy
@url.to_s
end end
sig { override.params(val: T.nilable(T.any(String, Version))).returns(T.nilable(Version)) } sig { override.params(val: T.nilable(T.any(String, Version))).returns(T.nilable(Version)) }
@ -239,6 +241,7 @@ class Resource
def determine_url_mirrors def determine_url_mirrors
extra_urls = [] extra_urls = []
url = T.must(self.url)
# glibc-bootstrap # glibc-bootstrap
if url.start_with?("https://github.com/Homebrew/glibc-bootstrap/releases/download") if url.start_with?("https://github.com/Homebrew/glibc-bootstrap/releases/download")

View File

@ -9,7 +9,7 @@ module Homebrew
attr_reader :downloadable attr_reader :downloadable
private :downloadable private :downloadable
sig { override.returns(T.nilable(URL)) } sig { override.returns(T.any(NilClass, String, URL)) }
def url = downloadable.url def url = downloadable.url
sig { override.returns(T.nilable(Checksum)) } sig { override.returns(T.nilable(Checksum)) }

View File

@ -102,11 +102,13 @@ class SoftwareSpec
patches.each { |p| p.owner = self } patches.each { |p| p.owner = self }
end end
sig { override.params(val: T.nilable(String), specs: T::Hash[Symbol, T.anything]).returns(T.nilable(String)) }
def url(val = nil, specs = {}) def url(val = nil, specs = {})
return @resource.url if val.nil? if val
@resource.url(val, **specs)
@resource.url(val, **specs) dependency_collector.add(@resource)
dependency_collector.add(@resource) end
@resource.url
end end
def bottle_defined? def bottle_defined?

View File

@ -535,6 +535,14 @@ RSpec.describe Formula do
end end
end end
specify ".url" do
f = formula do
url "foo-1.0"
end
expect(f.class.url).to eq("foo-1.0")
end
specify "spec integration" do specify "spec integration" do
f = formula do f = formula do
homepage "https://brew.sh" homepage "https://brew.sh"

View File

@ -285,7 +285,7 @@ module PyPI
url = if stable.specs[:tag].present? url = if stable.specs[:tag].present?
"git+#{stable.url}@#{stable.specs[:tag]}" "git+#{stable.url}@#{stable.specs[:tag]}"
else else
stable.url T.must(stable.url)
end end
Package.new(url, is_url: true, python_name:) Package.new(url, is_url: true, python_name:)
end end