Skip to content

Commit 5611e21

Browse files
committed
a*.rb: move to strict Sorbet sigil.
1 parent b6167f6 commit 5611e21

8 files changed

Lines changed: 154 additions & 74 deletions

File tree

Library/Homebrew/api.rb

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

44
require "api/analytics"
@@ -11,10 +11,10 @@ module Homebrew
1111
module API
1212
extend Cachable
1313

14-
HOMEBREW_CACHE_API = (HOMEBREW_CACHE/"api").freeze
15-
HOMEBREW_CACHE_API_SOURCE = (HOMEBREW_CACHE/"api-source").freeze
14+
HOMEBREW_CACHE_API = T.let((HOMEBREW_CACHE/"api").freeze, Pathname)
15+
HOMEBREW_CACHE_API_SOURCE = T.let((HOMEBREW_CACHE/"api-source").freeze, Pathname)
1616

17-
sig { params(endpoint: String).returns(Hash) }
17+
sig { params(endpoint: String).returns(T::Hash[String, T.untyped]) }
1818
def self.fetch(endpoint)
1919
return cache[endpoint] if cache.present? && cache.key?(endpoint)
2020

@@ -33,7 +33,8 @@ def self.fetch(endpoint)
3333
end
3434

3535
sig {
36-
params(endpoint: String, target: Pathname, stale_seconds: Integer).returns([T.any(Array, Hash), T::Boolean])
36+
params(endpoint: String, target: Pathname,
37+
stale_seconds: Integer).returns([T.any(T::Array[T.untyped], T::Hash[String, T.untyped]), T::Boolean])
3738
}
3839
def self.fetch_json_api_file(endpoint, target: HOMEBREW_CACHE_API/endpoint,
3940
stale_seconds: Homebrew::EnvConfig.api_auto_update_secs.to_i)
@@ -96,7 +97,8 @@ def self.fetch_json_api_file(endpoint, target: HOMEBREW_CACHE_API/endpoint,
9697

9798
mtime = insecure_download ? Time.new(1970, 1, 1) : Time.now
9899
FileUtils.touch(target, mtime:) unless skip_download
99-
JSON.parse(target.read(encoding: Encoding::UTF_8), freeze: true)
100+
# Can use `target.read` again when/if https://github.com/sorbet/sorbet/pull/8999 is merged/released.
101+
JSON.parse(File.read(target, encoding: Encoding::UTF_8), freeze: true)
100102
rescue JSON::ParserError
101103
target.unlink
102104
retry_count += 1
@@ -122,8 +124,11 @@ def self.fetch_json_api_file(endpoint, target: HOMEBREW_CACHE_API/endpoint,
122124
end
123125
end
124126

125-
sig { params(json: Hash, bottle_tag: T.nilable(::Utils::Bottles::Tag)).returns(Hash) }
126-
def self.merge_variations(json, bottle_tag: nil)
127+
sig {
128+
params(json: T::Hash[String, T.untyped],
129+
bottle_tag: ::Utils::Bottles::Tag).returns(T::Hash[String, T.untyped])
130+
}
131+
def self.merge_variations(json, bottle_tag: T.unsafe(nil))
127132
return json unless json.key?("variations")
128133

129134
bottle_tag ||= Homebrew::SimulateSystem.current_tag
@@ -147,7 +152,10 @@ def self.write_names_file(names, type, regenerate:)
147152
false
148153
end
149154

150-
sig { params(json_data: Hash).returns([T::Boolean, T.any(String, Array, Hash)]) }
155+
sig {
156+
params(json_data: T::Hash[String, T.untyped])
157+
.returns([T::Boolean, T.any(String, T::Array[T.untyped], T::Hash[String, T.untyped])])
158+
}
151159
private_class_method def self.verify_and_parse_jws(json_data)
152160
signatures = json_data["signatures"]
153161
homebrew_signature = signatures&.find { |sig| sig.dig("header", "kid") == "homebrew-1" }

Library/Homebrew/api_hashable.rb

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

44
# Used to substitute common paths with generic placeholders when generating JSON for the API.
55
module APIHashable
6+
sig { void }
67
def generating_hash!
78
return if generating_hash?
89

910
# Apply monkeypatches for API generation
10-
@old_homebrew_prefix = HOMEBREW_PREFIX
11-
@old_homebrew_cellar = HOMEBREW_CELLAR
12-
@old_home = Dir.home
13-
@old_git_config_global = ENV.fetch("GIT_CONFIG_GLOBAL", nil)
11+
@old_homebrew_prefix = T.let(HOMEBREW_PREFIX, T.nilable(Pathname))
12+
@old_homebrew_cellar = T.let(HOMEBREW_CELLAR, T.nilable(Pathname))
13+
@old_home = T.let(Dir.home, T.nilable(String))
14+
@old_git_config_global = T.let(ENV.fetch("GIT_CONFIG_GLOBAL", nil), T.nilable(String))
1415
Object.send(:remove_const, :HOMEBREW_PREFIX)
1516
Object.const_set(:HOMEBREW_PREFIX, Pathname.new(HOMEBREW_PREFIX_PLACEHOLDER))
1617
ENV["HOME"] = HOMEBREW_HOME_PLACEHOLDER
1718
ENV["GIT_CONFIG_GLOBAL"] = File.join(@old_home, ".gitconfig")
1819

19-
@generating_hash = true
20+
@generating_hash = T.let(true, T.nilable(T::Boolean))
2021
end
2122

23+
sig { void }
2224
def generated_hash!
2325
return unless generating_hash?
2426

@@ -31,6 +33,7 @@ def generated_hash!
3133
@generating_hash = false
3234
end
3335

36+
sig { returns(T::Boolean) }
3437
def generating_hash?
3538
@generating_hash ||= false
3639
@generating_hash == true

Library/Homebrew/bundle/commands/add.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# typed: true # rubocop:todo Sorbet/StrictSigil
1+
# typed: strict
22
# frozen_string_literal: true
33

44
require "bundle/adder"
@@ -7,6 +7,7 @@ module Homebrew
77
module Bundle
88
module Commands
99
module Add
10+
sig { params(args: String, type: Symbol, global: T::Boolean, file: T.nilable(String)).void }
1011
def self.run(*args, type:, global:, file:)
1112
Homebrew::Bundle::Adder.add(*args, type:, global:, file:)
1213
end

Library/Homebrew/dev-cmd/generate-cask-ci-matrix.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def run
6767
end
6868
raise UsageError, "Only one url can be specified" if pr_url&.count&.> 1
6969

70-
labels = if pr_url
71-
pr = GitHub::API.open_rest(pr_url.first)
70+
labels = if pr_url && (first_pr_url = pr_url.first)
71+
pr = GitHub::API.open_rest(first_pr_url)
7272
pr.fetch("labels").map { |l| l.fetch("name") }
7373
else
7474
[]

Library/Homebrew/requirements/arch_requirement.rb

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

44
require "requirement"
@@ -7,10 +7,14 @@
77
class ArchRequirement < Requirement
88
fatal true
99

10+
@arch = T.let(nil, T.nilable(Symbol))
11+
12+
sig { returns(T.nilable(Symbol)) }
1013
attr_reader :arch
1114

15+
sig { params(tags: T::Array[Symbol]).void }
1216
def initialize(tags)
13-
@arch = tags.shift
17+
@arch = T.let(tags.shift, T.nilable(Symbol))
1418
super
1519
end
1620

Library/Homebrew/system_config.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def dump_tap_config(tap, out = $stdout)
132132
out.puts "#{tap_name} branch: #{tap.git_branch || "(none)"}" if default_branches.exclude?(tap.git_branch)
133133
end
134134

135-
if (json_file = Homebrew::API::HOMEBREW_CACHE_API/json_file_name) && json_file.exist?
135+
json_file = Homebrew::API::HOMEBREW_CACHE_API/json_file_name
136+
if json_file.exist?
136137
out.puts "#{tap_name} JSON: #{json_file.mtime.utc.strftime("%d %b %H:%M UTC")}"
137138
elsif !tap.installed?
138139
out.puts "#{tap_name}: N/A"

0 commit comments

Comments
 (0)