From 76a3c0338b513c87f3e33588f89b45b17f6e472e Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 7 Dec 2020 18:46:35 +0100 Subject: [PATCH] Add `unversioned?` method to `Cask::URL`. --- Library/Homebrew/cask/dsl.rb | 10 ++++--- Library/Homebrew/cask/url.rb | 55 +++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/Library/Homebrew/cask/dsl.rb b/Library/Homebrew/cask/dsl.rb index 25e753ec2f..e8922c811b 100644 --- a/Library/Homebrew/cask/dsl.rb +++ b/Library/Homebrew/cask/dsl.rb @@ -173,12 +173,14 @@ module Cask @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 diff --git a/Library/Homebrew/cask/url.rb b/Library/Homebrew/cask/url.rb index 7bb6d5adf7..9c6d518479 100644 --- a/Library/Homebrew/cask/url.rb +++ b/Library/Homebrew/cask/url.rb @@ -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( @@ -46,8 +47,10 @@ class URL referer: nil, header: nil, user_agent: nil, - data: nil + data: nil, + caller_location: T.must(caller_locations).fetch(0) ) + @uri = URI(uri) specs = {} @@ -65,5 +68,29 @@ class URL 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