Skip to content

Commit 2d603b0

Browse files
committed
OnSystem: enable strict typing
1 parent 1f37a11 commit 2d603b0

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

Library/Homebrew/extend/on_system.rb

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
# typed: true # rubocop:todo Sorbet/StrictSigil
1+
# typed: strict
22
# frozen_string_literal: true
33

44
require "simulate_system"
55

66
module OnSystem
7-
ARCH_OPTIONS = [:intel, :arm].freeze
8-
BASE_OS_OPTIONS = [:macos, :linux].freeze
9-
ALL_OS_OPTIONS = [*MacOSVersion::SYMBOLS.keys, :linux].freeze
10-
ALL_OS_ARCH_COMBINATIONS = ALL_OS_OPTIONS.product(ARCH_OPTIONS).freeze
11-
12-
VALID_OS_ARCH_TAGS = ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch|
13-
tag = Utils::Bottles::Tag.new(system: os, arch:)
14-
next unless tag.valid_combination?
15-
16-
tag
17-
end.freeze
7+
ARCH_OPTIONS = T.let([:intel, :arm].freeze, T::Array[Symbol])
8+
BASE_OS_OPTIONS = T.let([:macos, :linux].freeze, T::Array[Symbol])
9+
ALL_OS_OPTIONS = T.let([*MacOSVersion::SYMBOLS.keys, :linux].freeze, T::Array[Symbol])
10+
ALL_OS_ARCH_COMBINATIONS = T.let(
11+
ALL_OS_OPTIONS.product(ARCH_OPTIONS).freeze,
12+
T::Array[[Symbol, Symbol]],
13+
)
14+
15+
VALID_OS_ARCH_TAGS = T.let(
16+
ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch|
17+
tag = Utils::Bottles::Tag.new(system: os, arch:)
18+
next unless tag.valid_combination?
19+
20+
tag
21+
end.freeze,
22+
T::Array[Utils::Bottles::Tag],
23+
)
1824

1925
sig { params(arch: Symbol).returns(T::Boolean) }
2026
def self.arch_condition_met?(arch)
@@ -55,15 +61,15 @@ def self.condition_from_method_name(method_name)
5561
method_name.to_s.sub(/^on_/, "").to_sym
5662
end
5763

58-
sig { params(base: Class).void }
64+
sig { params(base: T::Class[T.anything]).void }
5965
def self.setup_arch_methods(base)
6066
ARCH_OPTIONS.each do |arch|
6167
base.define_method(:"on_#{arch}") do |&block|
62-
@on_system_blocks_exist = true
68+
@on_system_blocks_exist = T.let(true, T.nilable(T::Boolean))
6369

6470
return unless OnSystem.arch_condition_met? OnSystem.condition_from_method_name(T.must(__method__))
6571

66-
@called_in_on_system_block = true
72+
@called_in_on_system_block = T.let(true, T.nilable(T::Boolean))
6773
result = block.call
6874
@called_in_on_system_block = false
6975

@@ -82,7 +88,7 @@ def self.setup_arch_methods(base)
8288
end
8389
end
8490

85-
sig { params(base: Class).void }
91+
sig { params(base: T::Class[T.anything]).void }
8692
def self.setup_base_os_methods(base)
8793
BASE_OS_OPTIONS.each do |base_os|
8894
base.define_method(:"on_#{base_os}") do |&block|
@@ -128,7 +134,7 @@ def self.setup_base_os_methods(base)
128134
end
129135
end
130136

131-
sig { params(base: Class).void }
137+
sig { params(base: T::Class[T.anything]).void }
132138
def self.setup_macos_methods(base)
133139
MacOSVersion::SYMBOLS.each_key do |os_name|
134140
base.define_method(:"on_#{os_name}") do |or_condition = nil, &block|
@@ -137,11 +143,14 @@ def self.setup_macos_methods(base)
137143
os_condition = OnSystem.condition_from_method_name T.must(__method__)
138144
return unless OnSystem.os_condition_met? os_condition, or_condition
139145

140-
@on_system_block_min_os = if or_condition == :or_older
141-
@called_in_on_system_block ? @on_system_block_min_os : MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED)
142-
else
143-
MacOSVersion.from_symbol(os_condition)
144-
end
146+
@on_system_block_min_os = T.let(
147+
if or_condition == :or_older
148+
@called_in_on_system_block ? @on_system_block_min_os : MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED)
149+
else
150+
MacOSVersion.from_symbol(os_condition)
151+
end,
152+
T.nilable(MacOSVersion),
153+
)
145154
@called_in_on_system_block = true
146155
result = block.call
147156
@called_in_on_system_block = false
@@ -151,13 +160,13 @@ def self.setup_macos_methods(base)
151160
end
152161
end
153162

154-
sig { params(_base: Class).void }
163+
sig { params(_base: T::Class[T.anything]).void }
155164
def self.included(_base)
156165
raise "Do not include `OnSystem` directly. Instead, include `OnSystem::MacOSAndLinux` or `OnSystem::MacOSOnly`"
157166
end
158167

159168
module MacOSAndLinux
160-
sig { params(base: Class).void }
169+
sig { params(base: T::Class[T.anything]).void }
161170
def self.included(base)
162171
OnSystem.setup_arch_methods(base)
163172
OnSystem.setup_base_os_methods(base)
@@ -166,7 +175,7 @@ def self.included(base)
166175
end
167176

168177
module MacOSOnly
169-
sig { params(base: Class).void }
178+
sig { params(base: T::Class[T.anything]).void }
170179
def self.included(base)
171180
OnSystem.setup_arch_methods(base)
172181
OnSystem.setup_macos_methods(base)

Library/Homebrew/readall.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def self.valid_casks?(_tap, os_name: nil, arch: nil)
9090

9191
sig {
9292
params(
93-
tap: Tap, aliases: T::Boolean, no_simulate: T::Boolean, os_arch_combinations: T::Array[T::Array[String]],
93+
tap: Tap, aliases: T::Boolean, no_simulate: T::Boolean, os_arch_combinations: T::Array[[Symbol, Symbol]],
9494
).returns(T::Boolean)
9595
}
9696
def self.valid_tap?(tap, aliases: false, no_simulate: false,

0 commit comments

Comments
 (0)