Enable more typing

This commit is contained in:
Douglas Eichelberger 2023-03-12 17:06:29 -07:00
parent b4aac9d4a0
commit 04fa6e24d7
8 changed files with 58 additions and 26 deletions

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
# This script is loaded by formula_installer as a separate instance. # This script is loaded by formula_installer as a separate instance.
@ -235,12 +235,12 @@ rescue Exception => e # rubocop:disable Lint/RescueException
# BuildErrors are specific to build processes and not other # BuildErrors are specific to build processes and not other
# children, which is why we create the necessary state here # children, which is why we create the necessary state here
# and not in Utils.safe_fork. # and not in Utils.safe_fork.
case error_hash["json_class"] case e
when "BuildError" when BuildError
error_hash["cmd"] = e.cmd error_hash["cmd"] = e.cmd
error_hash["args"] = e.args error_hash["args"] = e.args
error_hash["env"] = e.env error_hash["env"] = e.env
when "ErrorDuringExecution" when ErrorDuringExecution
error_hash["cmd"] = e.cmd error_hash["cmd"] = e.cmd
error_hash["status"] = if e.status.is_a?(Process::Status) error_hash["status"] = if e.status.is_a?(Process::Status)
{ {

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "cask/cask_loader" require "cask/cask_loader"
@ -83,6 +83,19 @@ module Cask
@tap @tap
end end
sig {
params(
token: String,
sourcefile_path: T.nilable(Pathname),
source: T.nilable(String),
tap: T.nilable(Tap),
loaded_from_api: T::Boolean,
config: T.nilable(Config),
allow_reassignment: T::Boolean,
loader: T.nilable(CaskLoader::ILoader),
block: T.nilable(T.proc.bind(DSL).void),
).void
}
def initialize(token, sourcefile_path: nil, source: nil, tap: nil, loaded_from_api: false, def initialize(token, sourcefile_path: nil, source: nil, tap: nil, loaded_from_api: false,
config: nil, allow_reassignment: false, loader: nil, &block) config: nil, allow_reassignment: false, loader: nil, &block)
@token = token @token = token
@ -92,7 +105,8 @@ module Cask
@allow_reassignment = allow_reassignment @allow_reassignment = allow_reassignment
@loaded_from_api = loaded_from_api @loaded_from_api = loaded_from_api
@loader = loader @loader = loader
@block = block # https://github.com/sorbet/sorbet/issues/6843
instance_variable_set(:@block, block)
@default_config = config || Config.new @default_config = config || Config.new
@ -123,9 +137,10 @@ module Cask
sig { returns(T::Array[[String, String]]) } sig { returns(T::Array[[String, String]]) }
def timestamped_versions def timestamped_versions
Pathname.glob(metadata_timestamped_path(version: "*", timestamp: "*")) relative_paths = Pathname.glob(metadata_timestamped_path(version: "*", timestamp: "*"))
.map { |p| p.relative_path_from(p.parent.parent) } .map { |p| p.relative_path_from(p.parent.parent) }
.sort_by(&:basename) # sort by timestamp # https://github.com/sorbet/sorbet/issues/6844
T.unsafe(relative_paths).sort_by(&:basename) # sort by timestamp
.map { |p| p.split.map(&:to_s) } .map { |p| p.split.map(&:to_s) }
end end
@ -150,7 +165,7 @@ module Cask
version_os_hash version_os_hash
ensure ensure
MacOS.full_version = actual_version MacOS.full_version = actual_version || MacOS.full_version.to_s
end end
end end

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "cask/cache" require "cask/cache"
@ -12,8 +12,18 @@ module Cask
module CaskLoader module CaskLoader
extend Context extend Context
module ILoader
extend T::Sig
extend T::Helpers
interface!
sig { abstract.params(config: Config).returns(Cask) }
def load(config:); end
end
# Loads a cask from a string. # Loads a cask from a string.
class FromContentLoader class FromContentLoader
include ILoader
attr_reader :content, :tap attr_reader :content, :tap
def self.can_load?(ref) def self.can_load?(ref)
@ -112,7 +122,6 @@ module Cask
return false unless ref.to_s.match?(@uri_regex) return false unless ref.to_s.match?(@uri_regex)
uri = URI(ref) uri = URI(ref)
return false unless uri
return false unless uri.path return false unless uri.path
true true
@ -123,7 +132,7 @@ module Cask
sig { params(url: T.any(URI::Generic, String)).void } sig { params(url: T.any(URI::Generic, String)).void }
def initialize(url) def initialize(url)
@url = URI(url) @url = URI(url)
super Cache.path/File.basename(@url.path) super Cache.path/File.basename(T.must(@url.path))
end end
def load(config:) def load(config:)
@ -185,6 +194,7 @@ module Cask
# Loads a cask from an existing {Cask} instance. # Loads a cask from an existing {Cask} instance.
class FromInstanceLoader class FromInstanceLoader
include ILoader
def self.can_load?(ref) def self.can_load?(ref)
ref.is_a?(Cask) ref.is_a?(Cask)
end end
@ -200,6 +210,7 @@ module Cask
# Loads a cask from the JSON API. # Loads a cask from the JSON API.
class FromAPILoader class FromAPILoader
include ILoader
attr_reader :token, :path attr_reader :token, :path
def self.can_load?(ref) def self.can_load?(ref)

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "locale" require "locale"
@ -196,7 +196,7 @@ module Cask
# @api public # @api public
def url(*args, **options, &block) def url(*args, **options, &block)
caller_location = caller_locations[0] caller_location = T.must(caller_locations).fetch(0)
set_unique_stanza(:url, args.empty? && options.empty? && !block) do set_unique_stanza(:url, args.empty? && options.empty? && !block) do
if block if block
@ -237,14 +237,14 @@ module Cask
set_unique_stanza(:sha256, should_return) do set_unique_stanza(:sha256, should_return) do
@on_system_blocks_exist = true if arm.present? || intel.present? @on_system_blocks_exist = true if arm.present? || intel.present?
arg ||= on_arch_conditional(arm: arm, intel: intel) val = arg || on_arch_conditional(arm: arm, intel: intel)
case arg case val
when :no_check when :no_check
arg val
when String when String
Checksum.new(arg) Checksum.new(val)
else else
raise CaskInvalidError.new(cask, "invalid 'sha256' value: #{arg.inspect}") raise CaskInvalidError.new(cask, "invalid 'sha256' value: #{val.inspect}")
end end
end end
end end

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "cask/utils" require "cask/utils"
@ -26,7 +26,7 @@ module Cask
# rubocop:disable Style/MissingRespondToMissing # rubocop:disable Style/MissingRespondToMissing
def method_missing(method, *) def method_missing(method, *)
if method if method
underscored_class = self.class.name.gsub(/([[:lower:]])([[:upper:]][[:lower:]])/, '\1_\2').downcase underscored_class = T.must(self.class.name).gsub(/([[:lower:]])([[:upper:]][[:lower:]])/, '\1_\2').downcase
section = underscored_class.split("::").last section = underscored_class.split("::").last
Utils.method_missing_message(method, @cask.to_s, section) Utils.method_missing_message(method, @cask.to_s, section)
nil nil

View File

@ -0,0 +1,6 @@
# typed: strict
module OnSystem::MacOSOnly
sig { params(arm: T.nilable(String), intel: T.nilable(String)).returns(T.nilable(String)) }
def on_arch_conditional(arm: nil, intel: nil); end
end

View File

@ -82,7 +82,7 @@ module OS
nil nil
end end
def sdk_path def sdk_path(_version = nil)
nil nil
end end

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "os/mac/version" require "os/mac/version"
@ -38,7 +38,7 @@ module OS
end end
end end
sig { params(version: Version).void } sig { params(version: String).void }
def full_version=(version) def full_version=(version)
@full_version = Version.new(version.chomp) @full_version = Version.new(version.chomp)
@version = nil @version = nil