Enable strict typing in Cask::URL

This commit is contained in:
Douglas Eichelberger 2025-03-12 19:05:01 -07:00
parent e49a69679d
commit f183d0a398
No known key found for this signature in database
GPG Key ID: F90193CBD547EB81

View File

@ -1,15 +1,31 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "source_location" require "source_location"
require "utils/curl" require "utils/curl"
module Cask module Cask
BlockReturn = T.type_alias do
T.any(URI::Generic, String, [T.any(URI::Generic, String), T::Hash[Symbol, T.untyped]])
end
# Class corresponding to the `url` stanza. # Class corresponding to the `url` stanza.
class URL < SimpleDelegator class URL < SimpleDelegator
class DSL class DSL
attr_reader :uri, :tag, :branch, :revisions, :revision, sig { returns(T.any(URI::Generic, String)) }
:trust_cert, :cookies, :header, :data, :only_path attr_reader :uri
sig { returns(T.nilable(T::Array[String])) }
attr_reader :revisions
sig { returns(T.nilable(T::Boolean)) }
attr_reader :trust_cert
sig { returns(T.nilable(T::Hash[String, String])) }
attr_reader :cookies, :data
sig { returns(T.nilable(T.any(String, T::Array[String]))) }
attr_reader :header
sig { returns(T.nilable(T.any(URI::Generic, String))) } sig { returns(T.nilable(T.any(URI::Generic, String))) }
attr_reader :referer attr_reader :referer
@ -24,7 +40,7 @@ module Cask
attr_reader :using attr_reader :using
sig { returns(T.nilable(String)) } sig { returns(T.nilable(String)) }
attr_reader :verified attr_reader :tag, :branch, :revision, :only_path, :verified
extend Forwardable extend Forwardable
def_delegators :uri, :path, :scheme, :to_s def_delegators :uri, :path, :scheme, :to_s
@ -59,41 +75,29 @@ module Cask
).void ).void
} }
def initialize( def initialize(
uri, uri, verified: nil, using: nil, tag: nil, branch: nil, revisions: nil, revision: nil, trust_cert: nil,
verified: nil, cookies: nil, referer: nil, header: nil, user_agent: nil, data: nil, only_path: 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) @uri = T.let(URI(uri), T.any(URI::Generic, String))
header = Array(header) unless header.nil? header = Array(header) unless header.nil?
specs = {} specs = {}
specs[:verified] = @verified = verified specs[:verified] = @verified = T.let(verified, T.nilable(String))
specs[:using] = @using = using specs[:using] = @using = T.let(using, T.any(T::Class[AbstractDownloadStrategy], Symbol, NilClass))
specs[:tag] = @tag = tag specs[:tag] = @tag = T.let(tag, T.nilable(String))
specs[:branch] = @branch = branch specs[:branch] = @branch = T.let(branch, T.nilable(String))
specs[:revisions] = @revisions = revisions specs[:revisions] = @revisions = T.let(revisions, T.nilable(T::Array[String]))
specs[:revision] = @revision = revision specs[:revision] = @revision = T.let(revision, T.nilable(String))
specs[:trust_cert] = @trust_cert = trust_cert specs[:trust_cert] = @trust_cert = T.let(trust_cert, T.nilable(T::Boolean))
specs[:cookies] = @cookies = cookies specs[:cookies] = @cookies = T.let(cookies, T.nilable(T::Hash[String, String]))
specs[:referer] = @referer = referer specs[:referer] = @referer = T.let(referer, T.nilable(T.any(URI::Generic, String)))
specs[:headers] = @header = header specs[:headers] = @header = T.let(header, T.nilable(T.any(String, T::Array[String])))
specs[:user_agent] = @user_agent = user_agent || :default specs[:user_agent] = @user_agent = T.let(user_agent || :default, T.nilable(T.any(Symbol, String)))
specs[:data] = @data = data specs[:data] = @data = T.let(data, T.nilable(T::Hash[String, String]))
specs[:only_path] = @only_path = only_path specs[:only_path] = @only_path = T.let(only_path, T.nilable(String))
@specs = specs.compact @specs = T.let(specs.compact, T::Hash[Symbol, T.untyped])
end end
end end
@ -115,9 +119,10 @@ module Cask
sig { returns(URI::Generic) } sig { returns(URI::Generic) }
attr_accessor :url attr_accessor :url
sig { params(str: String, url: URI::Generic).void }
def initialize(str, url) def initialize(str, url)
super(str) super(str)
@url = url @url = T.let(url, URI::Generic)
end end
end end
@ -125,19 +130,18 @@ module Cask
params( params(
uri: T.nilable(T.any(URI::Generic, String)), uri: T.nilable(T.any(URI::Generic, String)),
dsl: ::Cask::DSL, dsl: ::Cask::DSL,
block: T.proc.params(arg0: T.all(String, PageWithURL)) block: T.proc.params(arg0: T.all(String, PageWithURL)).returns(BlockReturn),
.returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash])),
).void ).void
} }
def initialize(uri, dsl:, &block) def initialize(uri, dsl:, &block)
@uri = uri @uri = T.let(uri, T.nilable(T.any(URI::Generic, String)))
@dsl = dsl @dsl = T.let(dsl, ::Cask::DSL)
@block = block @block = T.let(block, T.proc.params(arg0: T.all(String, PageWithURL)).returns(BlockReturn))
odeprecated "cask `url do` blocks" if @block odeprecated "cask `url do` blocks" if @block
end end
sig { returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash])) } sig { returns(BlockReturn) }
def call def call
if @uri if @uri
result = ::Utils::Curl.curl_output("--fail", "--silent", "--location", @uri.to_s) result = ::Utils::Curl.curl_output("--fail", "--silent", "--location", @uri.to_s)
@ -158,9 +162,8 @@ module Cask
sig { sig {
params( params(
uri: T.any(URI::Generic, String), uri: T.any(URI::Generic, String),
block: T.proc.params(arg0: T.all(String, PageWithURL)) block: T.proc.params(arg0: T.all(String, PageWithURL)).returns(BlockReturn),
.returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash])), ).returns(BlockReturn)
).returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash]))
} }
def url(uri, &block) def url(uri, &block)
self.class.new(uri, dsl: @dsl, &block).call self.class.new(uri, dsl: @dsl, &block).call
@ -169,6 +172,11 @@ module Cask
# This allows calling DSL methods from inside a `url` block. # This allows calling DSL methods from inside a `url` block.
# #
# @api public # @api public
sig {
override
.params(method: Symbol, args: T.untyped, block: T.nilable(T.proc.returns(T.untyped)))
.returns(T.untyped)
}
def method_missing(method, *args, &block) def method_missing(method, *args, &block)
if @dsl.respond_to?(method) if @dsl.respond_to?(method)
@dsl.public_send(method, *args, &block) @dsl.public_send(method, *args, &block)
@ -177,8 +185,9 @@ module Cask
end end
end end
def respond_to_missing?(method, include_all) sig { override.params(method_name: T.any(Symbol, String), include_private: T::Boolean).returns(T::Boolean) }
@dsl.respond_to?(method, include_all) || super def respond_to_missing?(method_name, include_private = false)
@dsl.respond_to?(method_name, include_private) || super
end end
end end
@ -200,30 +209,13 @@ module Cask
only_path: T.nilable(String), only_path: T.nilable(String),
caller_location: Thread::Backtrace::Location, caller_location: Thread::Backtrace::Location,
dsl: T.nilable(::Cask::DSL), dsl: T.nilable(::Cask::DSL),
block: T.nilable( block: T.nilable(T.proc.params(arg0: T.all(String, BlockDSL::PageWithURL)).returns(BlockReturn)),
T.proc.params(arg0: T.all(String, BlockDSL::PageWithURL))
.returns(T.any(T.any(URI::Generic, String), [T.any(URI::Generic, String), Hash])),
),
).void ).void
} }
def initialize( def initialize(
uri = nil, uri = nil, verified: nil, using: nil, tag: nil, branch: nil, revisions: nil, revision: nil, trust_cert: nil,
verified: nil, cookies: nil, referer: nil, header: nil, user_agent: nil, data: nil, only_path: nil,
using: nil, caller_location: caller_locations.fetch(0), dsl: nil, &block
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,
caller_location: T.must(caller_locations).fetch(0),
dsl: nil,
&block
) )
super( super(
if block if block
@ -233,27 +225,13 @@ module Cask
DSL.new(uri2, **options) DSL.new(uri2, **options)
end end
else else
DSL.new( DSL.new(T.must(uri), verified:, using:, tag:, branch:, revisions:, revision:, trust_cert:, cookies:,
T.must(uri), referer:, header:, user_agent:, data:, only_path:)
verified:,
using:,
tag:,
branch:,
revisions:,
revision:,
trust_cert:,
cookies:,
referer:,
header:,
user_agent:,
data:,
only_path:,
)
end end
) )
@from_block = !block.nil? @from_block = T.let(!block.nil?, T::Boolean)
@caller_location = caller_location @caller_location = T.let(caller_location, Thread::Backtrace::Location)
end end
sig { returns(Homebrew::SourceLocation) } sig { returns(Homebrew::SourceLocation) }
@ -284,10 +262,10 @@ module Cask
def raw_url_line def raw_url_line
return @raw_url_line if defined?(@raw_url_line) return @raw_url_line if defined?(@raw_url_line)
@raw_url_line = Pathname(T.must(@caller_location.path)) @raw_url_line = T.let(Pathname(T.must(@caller_location.path))
.each_line .each_line
.drop(@caller_location.lineno - 1) .drop(@caller_location.lineno - 1)
.first .first, T.nilable(String))
end end
end end
end end