Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unversioned? method to Cask::URL. #9450

Merged
merged 1 commit into from Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions Library/Homebrew/cask/dsl.rb
Expand Up @@ -173,12 +173,14 @@ def languages
@language_blocks.keys.flatten
end

def url(*args)
set_unique_stanza(:url, args.empty? && !block_given?) do
def url(*args, **options)
caller_location = caller_locations[0]

set_unique_stanza(:url, args.empty? && options.empty? && !block_given?) do
if block_given?
LazyObject.new { URL.new(*yield) }
LazyObject.new { URL.new(*yield, caller_location: caller_location) }
else
URL.new(*args)
URL.new(*args, **options, caller_location: caller_location)
end
end
end
Expand Down
55 changes: 41 additions & 14 deletions Library/Homebrew/cask/url.rb
Expand Up @@ -18,19 +18,20 @@ class URL

sig do
params(
uri: T.any(URI::Generic, String),
verified: T.nilable(String),
using: T.nilable(Symbol),
tag: T.nilable(String),
branch: T.nilable(String),
revisions: T.nilable(T::Array[String]),
revision: T.nilable(String),
trust_cert: T.nilable(T::Boolean),
cookies: T.nilable(T::Hash[String, String]),
referer: T.nilable(T.any(URI::Generic, String)),
header: T.nilable(String),
user_agent: T.nilable(T.any(Symbol, String)),
data: T.nilable(T::Hash[String, String]),
uri: T.any(URI::Generic, String),
verified: T.nilable(String),
using: T.nilable(Symbol),
tag: T.nilable(String),
branch: T.nilable(String),
revisions: T.nilable(T::Array[String]),
revision: T.nilable(String),
trust_cert: T.nilable(T::Boolean),
cookies: T.nilable(T::Hash[String, String]),
referer: T.nilable(T.any(URI::Generic, String)),
header: T.nilable(String),
user_agent: T.nilable(T.any(Symbol, String)),
data: T.nilable(T::Hash[String, String]),
caller_location: Thread::Backtrace::Location,
).returns(T.untyped)
end
def initialize(
Expand All @@ -46,8 +47,10 @@ def initialize(
referer: nil,
header: nil,
user_agent: nil,
data: nil
data: nil,
caller_location: T.must(caller_locations).fetch(0)
)

@uri = URI(uri)

specs = {}
Expand All @@ -65,5 +68,29 @@ def initialize(
specs[:data] = @data = data

@specs = specs.compact

@caller_location = caller_location
end

sig { returns(T.nilable(String)) }
def raw_interpolated_url
return @raw_interpolated_url if defined?(@raw_interpolated_url)

@raw_interpolated_url =
Pathname(@caller_location.absolute_path)
.each_line.drop(@caller_location.lineno - 1)
.first&.yield_self { |line| line[/url\s+"([^"]+)"/, 1] }
end
private :raw_interpolated_url

sig { params(ignore_major_version: T::Boolean).returns(T::Boolean) }
def unversioned?(ignore_major_version: false)
interpolated_url = raw_interpolated_url

return false unless interpolated_url

interpolated_url = interpolated_url.gsub(/\#{\s*version\s*\.major\s*}/, "") if ignore_major_version

interpolated_url.exclude?('#{')
end
end