2020-10-10 14:16:11 +02:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-05-19 19:43:15 +02:00
|
|
|
require "source_location"
|
2023-09-06 09:55:17 -04:00
|
|
|
require "utils/curl"
|
2023-05-19 19:43:15 +02:00
|
|
|
|
2023-04-18 00:22:13 +01:00
|
|
|
module Cask
|
|
|
|
# Class corresponding to the `url` stanza.
|
|
|
|
class URL < Delegator
|
|
|
|
class DSL
|
|
|
|
attr_reader :uri, :specs,
|
|
|
|
:verified, :using,
|
|
|
|
:tag, :branch, :revisions, :revision,
|
|
|
|
:trust_cert, :cookies, :referer, :header, :user_agent,
|
|
|
|
:data, :only_path
|
|
|
|
|
|
|
|
extend Forwardable
|
|
|
|
def_delegators :uri, :path, :scheme, :to_s
|
2021-03-31 06:14:41 +02:00
|
|
|
|
2023-04-18 00:22:13 +01:00
|
|
|
# @api public
|
|
|
|
sig {
|
|
|
|
params(
|
|
|
|
uri: T.any(URI::Generic, String),
|
|
|
|
verified: T.nilable(String),
|
2023-07-25 15:50:49 +01:00
|
|
|
using: T.any(Class, Symbol, NilClass),
|
2023-04-18 00:22:13 +01:00
|
|
|
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)),
|
2023-06-27 09:37:22 -04:00
|
|
|
header: T.nilable(T.any(String, T::Array[String])),
|
2023-04-18 00:22:13 +01:00
|
|
|
user_agent: T.nilable(T.any(Symbol, String)),
|
|
|
|
data: T.nilable(T::Hash[String, String]),
|
|
|
|
only_path: T.nilable(String),
|
|
|
|
).void
|
|
|
|
}
|
|
|
|
def initialize(
|
|
|
|
uri,
|
|
|
|
verified: nil,
|
|
|
|
using: nil,
|
|
|
|
tag: nil,
|
|
|
|
branch: nil,
|
|
|
|
revisions: nil,
|
|
|
|
revision: nil,
|
|
|
|
trust_cert: nil,
|
|
|
|
cookies: nil,
|
|
|
|
referer: nil,
|
|
|
|
header: nil,
|
|
|
|
user_agent: nil,
|
|
|
|
data: nil,
|
|
|
|
only_path: nil
|
|
|
|
)
|
|
|
|
|
|
|
|
@uri = URI(uri)
|
|
|
|
|
2023-06-28 09:11:14 -04:00
|
|
|
header = Array(header) unless header.nil?
|
|
|
|
|
2023-04-18 00:22:13 +01:00
|
|
|
specs = {}
|
|
|
|
specs[:verified] = @verified = verified
|
|
|
|
specs[:using] = @using = using
|
|
|
|
specs[:tag] = @tag = tag
|
|
|
|
specs[:branch] = @branch = branch
|
|
|
|
specs[:revisions] = @revisions = revisions
|
|
|
|
specs[:revision] = @revision = revision
|
|
|
|
specs[:trust_cert] = @trust_cert = trust_cert
|
|
|
|
specs[:cookies] = @cookies = cookies
|
|
|
|
specs[:referer] = @referer = referer
|
2023-06-28 09:11:14 -04:00
|
|
|
specs[:headers] = @header = header
|
2023-04-18 00:22:13 +01:00
|
|
|
specs[:user_agent] = @user_agent = user_agent || :default
|
|
|
|
specs[:data] = @data = data
|
|
|
|
specs[:only_path] = @only_path = only_path
|
|
|
|
|
|
|
|
@specs = specs.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class BlockDSL
|
|
|
|
# To access URL associated with page contents.
|
|
|
|
module PageWithURL
|
|
|
|
# @api public
|
|
|
|
sig { returns(URI::Generic) }
|
|
|
|
attr_accessor :url
|
|
|
|
end
|
|
|
|
|
|
|
|
sig {
|
|
|
|
params(
|
|
|
|
uri: T.nilable(T.any(URI::Generic, String)),
|
|
|
|
dsl: T.nilable(::Cask::DSL),
|
2023-04-29 03:09:50 +02:00
|
|
|
block: T.proc.params(arg0: T.all(String, PageWithURL))
|
|
|
|
.returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash])),
|
2023-04-18 00:22:13 +01:00
|
|
|
).void
|
|
|
|
}
|
|
|
|
def initialize(uri, dsl: nil, &block)
|
|
|
|
@uri = uri
|
|
|
|
@dsl = dsl
|
|
|
|
@block = block
|
|
|
|
end
|
|
|
|
|
2023-04-29 03:09:50 +02:00
|
|
|
sig { returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash])) }
|
2023-04-18 00:22:13 +01:00
|
|
|
def call
|
|
|
|
if @uri
|
2023-09-06 09:55:17 -04:00
|
|
|
result = ::Utils::Curl.curl_output("--fail", "--silent", "--location", @uri)
|
2023-04-18 00:22:13 +01:00
|
|
|
result.assert_success!
|
|
|
|
|
|
|
|
page = result.stdout
|
|
|
|
page.extend PageWithURL
|
|
|
|
page.url = URI(@uri)
|
|
|
|
|
|
|
|
instance_exec(page, &@block)
|
|
|
|
else
|
|
|
|
instance_exec(&@block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# @api public
|
|
|
|
sig {
|
|
|
|
params(
|
|
|
|
uri: T.any(URI::Generic, String),
|
2023-04-29 03:09:50 +02:00
|
|
|
block: T.proc.params(arg0: T.all(String, PageWithURL))
|
|
|
|
.returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash])),
|
|
|
|
).returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash]))
|
2023-04-18 00:22:13 +01:00
|
|
|
}
|
|
|
|
def url(uri, &block)
|
|
|
|
self.class.new(uri, &block).call
|
|
|
|
end
|
|
|
|
private :url
|
|
|
|
|
|
|
|
# @api public
|
|
|
|
def method_missing(method, *args, &block)
|
|
|
|
if @dsl.respond_to?(method)
|
|
|
|
T.unsafe(@dsl).public_send(method, *args, &block)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond_to_missing?(method, include_all)
|
|
|
|
@dsl.respond_to?(method, include_all) || super
|
|
|
|
end
|
|
|
|
end
|
2021-03-31 06:14:41 +02:00
|
|
|
|
|
|
|
sig {
|
|
|
|
params(
|
2023-04-18 00:22:13 +01:00
|
|
|
uri: T.nilable(T.any(URI::Generic, String)),
|
|
|
|
verified: T.nilable(String),
|
2023-09-04 08:46:22 +01:00
|
|
|
using: T.any(Class, Symbol, NilClass),
|
2023-04-18 00:22:13 +01:00
|
|
|
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)),
|
2023-11-09 10:23:13 +11:00
|
|
|
header: T.nilable(T.any(String, T::Array[String])),
|
2023-04-18 00:22:13 +01:00
|
|
|
user_agent: T.nilable(T.any(Symbol, String)),
|
|
|
|
data: T.nilable(T::Hash[String, String]),
|
|
|
|
only_path: T.nilable(String),
|
|
|
|
caller_location: Thread::Backtrace::Location,
|
|
|
|
dsl: T.nilable(::Cask::DSL),
|
2023-04-29 03:09:50 +02:00
|
|
|
block: T.nilable(
|
|
|
|
T.proc.params(arg0: T.all(String, BlockDSL::PageWithURL))
|
|
|
|
.returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash])),
|
|
|
|
),
|
2021-03-31 06:14:41 +02:00
|
|
|
).void
|
|
|
|
}
|
|
|
|
def initialize(
|
2023-04-18 00:22:13 +01:00
|
|
|
uri = nil,
|
2021-03-31 06:14:41 +02:00
|
|
|
verified: nil,
|
|
|
|
using: nil,
|
|
|
|
tag: nil,
|
|
|
|
branch: nil,
|
|
|
|
revisions: nil,
|
|
|
|
revision: nil,
|
|
|
|
trust_cert: nil,
|
|
|
|
cookies: nil,
|
|
|
|
referer: nil,
|
|
|
|
header: nil,
|
|
|
|
user_agent: nil,
|
2022-05-02 19:53:44 -04:00
|
|
|
data: nil,
|
2023-04-18 00:22:13 +01:00
|
|
|
only_path: nil,
|
|
|
|
caller_location: T.must(caller_locations).fetch(0),
|
|
|
|
dsl: nil,
|
|
|
|
&block
|
2021-03-31 06:14:41 +02:00
|
|
|
)
|
2023-04-18 00:22:13 +01:00
|
|
|
super(
|
|
|
|
if block
|
|
|
|
LazyObject.new do
|
2024-03-07 16:20:20 +00:00
|
|
|
uri2, options = *BlockDSL.new(uri, dsl:, &block).call
|
2023-04-29 03:09:50 +02:00
|
|
|
options ||= {}
|
|
|
|
DSL.new(uri2, **options)
|
2023-04-18 00:22:13 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
DSL.new(
|
|
|
|
T.must(uri),
|
2024-03-07 16:20:20 +00:00
|
|
|
verified:,
|
|
|
|
using:,
|
|
|
|
tag:,
|
|
|
|
branch:,
|
|
|
|
revisions:,
|
|
|
|
revision:,
|
|
|
|
trust_cert:,
|
|
|
|
cookies:,
|
|
|
|
referer:,
|
|
|
|
header:,
|
|
|
|
user_agent:,
|
|
|
|
data:,
|
|
|
|
only_path:,
|
2023-04-18 00:22:13 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
)
|
2021-03-31 06:14:41 +02:00
|
|
|
|
2023-04-18 00:22:13 +01:00
|
|
|
@from_block = !block.nil?
|
|
|
|
@caller_location = caller_location
|
2021-03-31 06:14:41 +02:00
|
|
|
end
|
|
|
|
|
2023-04-18 00:22:13 +01:00
|
|
|
def __getobj__
|
|
|
|
@dsl
|
2021-03-31 06:14:41 +02:00
|
|
|
end
|
|
|
|
|
2023-04-18 00:22:13 +01:00
|
|
|
def __setobj__(dsl)
|
2021-03-31 06:14:41 +02:00
|
|
|
@dsl = dsl
|
|
|
|
end
|
|
|
|
|
2023-05-19 19:43:15 +02:00
|
|
|
sig { returns(Homebrew::SourceLocation) }
|
|
|
|
def location
|
|
|
|
Homebrew::SourceLocation.new(@caller_location.lineno, raw_url_line&.index("url"))
|
|
|
|
end
|
|
|
|
|
2023-04-18 00:22:13 +01:00
|
|
|
sig { returns(T.nilable(String)) }
|
2023-05-19 19:43:15 +02:00
|
|
|
def raw_url_line
|
|
|
|
return @raw_url_line if defined?(@raw_url_line)
|
2021-03-31 06:14:41 +02:00
|
|
|
|
2023-11-05 08:55:58 -08:00
|
|
|
@raw_url_line = Pathname(T.must(@caller_location.path))
|
2023-05-19 19:43:15 +02:00
|
|
|
.each_line
|
|
|
|
.drop(@caller_location.lineno - 1)
|
|
|
|
.first
|
2021-03-31 06:14:41 +02:00
|
|
|
end
|
2023-05-19 19:43:15 +02:00
|
|
|
private :raw_url_line
|
2021-03-31 06:14:41 +02:00
|
|
|
|
2023-04-18 00:22:13 +01:00
|
|
|
sig { params(ignore_major_version: T::Boolean).returns(T::Boolean) }
|
|
|
|
def unversioned?(ignore_major_version: false)
|
2023-05-19 19:43:15 +02:00
|
|
|
interpolated_url = raw_url_line&.then { |line| line[/url\s+"([^"]+)"/, 1] }
|
2021-03-31 06:14:41 +02:00
|
|
|
|
2023-04-18 00:22:13 +01:00
|
|
|
return false unless interpolated_url
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2023-04-18 00:22:13 +01:00
|
|
|
interpolated_url = interpolated_url.gsub(/\#{\s*version\s*\.major\s*}/, "") if ignore_major_version
|
2020-12-07 18:46:35 +01:00
|
|
|
|
2023-04-18 00:22:13 +01:00
|
|
|
interpolated_url.exclude?('#{')
|
|
|
|
end
|
2020-12-12 06:01:26 +01:00
|
|
|
|
2023-04-18 00:22:13 +01:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def from_block?
|
|
|
|
@from_block
|
|
|
|
end
|
2020-12-12 06:01:26 +01:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|