Add type annotations for cask loaders.

This commit is contained in:
Markus Reiter 2024-02-06 16:04:08 +01:00
parent 5cab0c8a6e
commit ed00dc0b95
No known key found for this signature in database
GPG Key ID: 245293B51702655B
2 changed files with 47 additions and 13 deletions

View File

@ -18,6 +18,11 @@ module Cask
extend T::Helpers extend T::Helpers
interface! interface!
sig { overridable.params(_ref: T.any(String, Pathname, Cask, URI::Generic)).returns(T::Boolean) }
def self.can_load?(_ref)
false
end
sig { abstract.params(config: Config).returns(Cask) } sig { abstract.params(config: Config).returns(Cask) }
def load(config:); end def load(config:); end
end end
@ -50,10 +55,11 @@ module Cask
# Loads a cask from a string. # Loads a cask from a string.
class FromContentLoader < AbstractContentLoader class FromContentLoader < AbstractContentLoader
sig { params(ref: T.any(String, Pathname, Cask, URI::Generic)).returns(T::Boolean) }
def self.can_load?(ref) def self.can_load?(ref)
return false unless ref.respond_to?(:to_str) return false unless ref.respond_to?(:to_str)
content = ref.to_str content = T.unsafe(ref).to_str
# Cache compiled regex # Cache compiled regex
@regex ||= begin @regex ||= begin
@ -66,7 +72,8 @@ module Cask
content.match?(@regex) content.match?(@regex)
end end
def initialize(content, tap: nil) sig { params(content: String, tap: Tap).void }
def initialize(content, tap: T.unsafe(nil))
super() super()
@content = content.force_encoding("UTF-8") @content = content.force_encoding("UTF-8")
@ -82,14 +89,19 @@ module Cask
# Loads a cask from a path. # Loads a cask from a path.
class FromPathLoader < AbstractContentLoader class FromPathLoader < AbstractContentLoader
sig { params(ref: T.any(String, Pathname, Cask, URI::Generic)).returns(T::Boolean) }
def self.can_load?(ref) def self.can_load?(ref)
path = Pathname(ref) ref = Pathname(ref) if ref.is_a?(String)
return false unless ref.is_a?(Pathname)
path = ref
%w[.rb .json].include?(path.extname) && path.expand_path.exist? %w[.rb .json].include?(path.extname) && path.expand_path.exist?
end end
attr_reader :token, :path attr_reader :token, :path
def initialize(path, token: nil) sig { params(path: T.any(Pathname, String), token: String).void }
def initialize(path, token: T.unsafe(nil))
super() super()
path = Pathname(path).expand_path path = Pathname(path).expand_path
@ -134,6 +146,7 @@ module Cask
# Loads a cask from a URI. # Loads a cask from a URI.
class FromURILoader < FromPathLoader class FromURILoader < FromPathLoader
sig { params(ref: T.any(String, Pathname, Cask, URI::Generic)).returns(T::Boolean) }
def self.can_load?(ref) def self.can_load?(ref)
# Cache compiled regex # Cache compiled regex
@uri_regex ||= begin @uri_regex ||= begin
@ -141,9 +154,10 @@ module Cask
Regexp.new("\\A#{uri_regex.source}\\Z", uri_regex.options) Regexp.new("\\A#{uri_regex.source}\\Z", uri_regex.options)
end end
return false unless ref.to_s.match?(@uri_regex) uri = ref.to_s
return false unless uri.match?(@uri_regex)
uri = URI(ref) uri = URI(uri)
return false unless uri.path return false unless uri.path
true true
@ -173,10 +187,12 @@ module Cask
# Loads a cask from a tap path. # Loads a cask from a tap path.
class FromTapPathLoader < FromPathLoader class FromTapPathLoader < FromPathLoader
sig { params(ref: T.any(String, Pathname, Cask, URI::Generic)).returns(T::Boolean) }
def self.can_load?(ref) def self.can_load?(ref)
super && !Tap.from_path(ref).nil? super && !Tap.from_path(ref).nil?
end end
sig { params(path: T.any(Pathname, String)).void }
def initialize(path) def initialize(path)
super(path) super(path)
@tap = Tap.from_path(path) @tap = Tap.from_path(path)
@ -185,10 +201,12 @@ module Cask
# Loads a cask from a specific tap. # Loads a cask from a specific tap.
class FromTapLoader < FromTapPathLoader class FromTapLoader < FromTapPathLoader
sig { params(ref: T.any(String, Pathname, Cask, URI::Generic)).returns(T::Boolean) }
def self.can_load?(ref) def self.can_load?(ref)
ref.to_s.match?(HOMEBREW_TAP_CASK_REGEX) ref.to_s.match?(HOMEBREW_TAP_CASK_REGEX)
end end
sig { params(tapped_name: String).void }
def initialize(tapped_name) def initialize(tapped_name)
user, repo, token = tapped_name.split("/", 3) user, repo, token = tapped_name.split("/", 3)
tap = Tap.fetch(user, repo) tap = Tap.fetch(user, repo)
@ -205,10 +223,12 @@ module Cask
# Loads a cask from the default tap path. # Loads a cask from the default tap path.
class FromDefaultTapPathLoader < FromTapPathLoader class FromDefaultTapPathLoader < FromTapPathLoader
sig { params(ref: T.any(String, Pathname, Cask, URI::Generic)).returns(T::Boolean) }
def self.can_load?(ref) def self.can_load?(ref)
super CaskLoader.default_path(ref) super CaskLoader.default_path(ref)
end end
sig { params(ref: String).void }
def initialize(ref) def initialize(ref)
super CaskLoader.default_path(ref) super CaskLoader.default_path(ref)
end end
@ -217,10 +237,13 @@ module Cask
# Loads a cask from an existing {Cask} instance. # Loads a cask from an existing {Cask} instance.
class FromInstanceLoader class FromInstanceLoader
include ILoader include ILoader
sig { params(ref: T.any(String, Pathname, Cask, URI::Generic)).returns(T::Boolean) }
def self.can_load?(ref) def self.can_load?(ref)
ref.is_a?(Cask) ref.is_a?(Cask)
end end
sig { params(cask: Cask).void }
def initialize(cask) def initialize(cask)
@cask = cask @cask = cask
end end
@ -233,25 +256,32 @@ module Cask
# Loads a cask from the JSON API. # Loads a cask from the JSON API.
class FromAPILoader class FromAPILoader
include ILoader include ILoader
attr_reader :token, :path attr_reader :token, :path
sig { returns(T.nilable(Hash)) }
attr_reader :from_json
sig { params(ref: T.any(String, Pathname, Cask, URI::Generic)).returns(T::Boolean) }
def self.can_load?(ref) def self.can_load?(ref)
return false if Homebrew::EnvConfig.no_install_from_api? return false if Homebrew::EnvConfig.no_install_from_api?
return false unless ref.is_a?(String) return false unless ref.is_a?(String)
return false unless ref.match?(HOMEBREW_MAIN_TAP_CASK_REGEX)
token = ref.sub(%r{^homebrew/(?:homebrew-)?cask/}i, "") return false unless (match = ref.match(HOMEBREW_MAIN_TAP_CASK_REGEX))
token = match[:token]
Homebrew::API::Cask.all_casks.key?(token) Homebrew::API::Cask.all_casks.key?(token)
end end
def initialize(token, from_json: nil) sig { params(token: String, from_json: Hash).void }
def initialize(token, from_json: T.unsafe(nil))
@token = token.sub(%r{^homebrew/(?:homebrew-)?cask/}i, "") @token = token.sub(%r{^homebrew/(?:homebrew-)?cask/}i, "")
@path = CaskLoader.default_path(@token) @path = CaskLoader.default_path(@token)
@from_json = from_json @from_json = from_json
end end
def load(config:) def load(config:)
json_cask = @from_json || Homebrew::API::Cask.all_casks[token] json_cask = from_json || Homebrew::API::Cask.all_casks[token]
cask_options = { cask_options = {
loaded_from_api: true, loaded_from_api: true,
@ -401,7 +431,8 @@ module Cask
# Pseudo-loader which raises an error when trying to load the corresponding cask. # Pseudo-loader which raises an error when trying to load the corresponding cask.
class NullLoader < FromPathLoader class NullLoader < FromPathLoader
def self.can_load?(*) sig { params(_ref: T.any(String, Pathname, Cask, URI::Generic)).returns(T::Boolean) }
def self.can_load?(_ref)
true true
end end
@ -468,6 +499,7 @@ module Cask
if [FromAPILoader, FromTapLoader].include?(loader_class) if [FromAPILoader, FromTapLoader].include?(loader_class)
token, tap, = tap_cask_token_type(ref, warn: warn) token, tap, = tap_cask_token_type(ref, warn: warn)
loader_class = T.cast(loader_class, T.any(T.class_of(FromAPILoader), T.class_of(FromTapLoader)))
return loader_class.new("#{tap}/#{token}") return loader_class.new("#{tap}/#{token}")
end end
@ -484,7 +516,9 @@ module Cask
end end
possible_installed_cask = Cask.new(ref) possible_installed_cask = Cask.new(ref)
return FromPathLoader.new(possible_installed_cask.installed_caskfile) if possible_installed_cask.installed? if (installed_caskfile = possible_installed_cask.installed_caskfile)
return FromPathLoader.new(installed_caskfile)
end
NullLoader.new(ref) NullLoader.new(ref)
end end

View File

@ -6,7 +6,7 @@ HOMEBREW_TAP_FORMULA_REGEX = T.let(%r{^([\w-]+)/([\w-]+)/([\w+-.@]+)$}, Regexp)
# Match taps' casks, e.g. `someuser/sometap/somecask` # Match taps' casks, e.g. `someuser/sometap/somecask`
HOMEBREW_TAP_CASK_REGEX = T.let(%r{^([\w-]+)/([\w-]+)/([a-z0-9\-_]+)$}, Regexp) HOMEBREW_TAP_CASK_REGEX = T.let(%r{^([\w-]+)/([\w-]+)/([a-z0-9\-_]+)$}, Regexp)
# Match main cask taps' casks, e.g. `homebrew/cask/somecask` or `somecask` # Match main cask taps' casks, e.g. `homebrew/cask/somecask` or `somecask`
HOMEBREW_MAIN_TAP_CASK_REGEX = T.let(%r{^([Hh]omebrew/(?:homebrew-)?cask/)?[a-z0-9\-_]+$}, Regexp) HOMEBREW_MAIN_TAP_CASK_REGEX = T.let(%r{^(?<tap>[Hh]omebrew/(?:homebrew-)?cask/)?(?<token>[a-z0-9\-_]+)$}, Regexp)
# Match taps' directory paths, e.g. `HOMEBREW_LIBRARY/Taps/someuser/sometap` # Match taps' directory paths, e.g. `HOMEBREW_LIBRARY/Taps/someuser/sometap`
HOMEBREW_TAP_DIR_REGEX = T.let( HOMEBREW_TAP_DIR_REGEX = T.let(
%r{#{Regexp.escape(HOMEBREW_LIBRARY.to_s)}/Taps/(?<user>[\w-]+)/(?<repo>[\w-]+)}, Regexp %r{#{Regexp.escape(HOMEBREW_LIBRARY.to_s)}/Taps/(?<user>[\w-]+)/(?<repo>[\w-]+)}, Regexp