2
2
# frozen_string_literal: true
3
3
4
4
# Contains shorthand Homebrew utility methods like `ohai`, `opoo`, `odisabled`.
5
- # TODO: move these out of `Kernel`.
5
+ # TODO: move these out of `Kernel` into `Homebrew::GlobalMethods` and add
6
+ # necessary Sorbet and global Kernel inclusions.
6
7
7
8
module Kernel
8
9
sig { params ( env : T . nilable ( String ) ) . returns ( T ::Boolean ) }
@@ -13,23 +14,25 @@ def superenv?(env)
13
14
end
14
15
private :superenv?
15
16
17
+ sig { params ( path : T . nilable ( T . any ( String , Pathname ) ) ) . returns ( T ::Boolean ) }
16
18
def require? ( path )
17
19
return false if path . nil?
18
20
19
21
if defined? ( Warnings )
20
22
# Work around require warning when done repeatedly:
21
23
# https://bugs.ruby-lang.org/issues/21091
22
24
Warnings . ignore ( /already initialized constant/ , /previous definition of/ ) do
23
- require path
25
+ require path . to_s
24
26
end
25
27
else
26
- require path
28
+ require path . to_s
27
29
end
28
30
true
29
31
rescue LoadError
30
32
false
31
33
end
32
34
35
+ sig { params ( title : String ) . returns ( String ) }
33
36
def ohai_title ( title )
34
37
verbose = if respond_to? ( :verbose? )
35
38
T . unsafe ( self ) . verbose?
@@ -42,7 +45,7 @@ def ohai_title(title)
42
45
end
43
46
44
47
def ohai ( title , *sput )
45
- puts ohai_title ( title )
48
+ puts ohai_title ( title . to_s )
46
49
puts sput
47
50
end
48
51
@@ -55,10 +58,11 @@ def odebug(title, *sput, always_display: false)
55
58
56
59
return if !debug && !always_display
57
60
58
- $stderr. puts Formatter . headline ( title , color : :magenta )
61
+ $stderr. puts Formatter . headline ( title . to_s , color : :magenta )
59
62
$stderr. puts sput unless sput . empty?
60
63
end
61
64
65
+ sig { params ( title : String , truncate : T . any ( Symbol , T ::Boolean ) ) . returns ( String ) }
62
66
def oh1_title ( title , truncate : :auto )
63
67
verbose = if respond_to? ( :verbose? )
64
68
T . unsafe ( self ) . verbose?
@@ -70,6 +74,7 @@ def oh1_title(title, truncate: :auto)
70
74
Formatter . headline ( title , color : :green )
71
75
end
72
76
77
+ sig { params ( title : String , truncate : T . any ( Symbol , T ::Boolean ) ) . void }
73
78
def oh1 ( title , truncate : :auto )
74
79
puts oh1_title ( title , truncate :)
75
80
end
@@ -134,6 +139,10 @@ def odie(error)
134
139
end
135
140
136
141
# Output a deprecation warning/error message.
142
+ sig {
143
+ params ( method : String , replacement : T . nilable ( T . any ( String , Symbol ) ) , disable : T ::Boolean ,
144
+ disable_on : T . nilable ( Time ) , disable_for_developers : T ::Boolean , caller : T ::Array [ String ] ) . void
145
+ }
137
146
def odeprecated ( method , replacement = nil ,
138
147
disable : false ,
139
148
disable_on : nil ,
@@ -213,12 +222,20 @@ def odeprecated(method, replacement = nil,
213
222
end
214
223
end
215
224
216
- def odisabled ( method , replacement = nil , **options )
217
- options = { disable : true , caller : } . merge ( options )
225
+ sig {
226
+ params ( method : String , replacement : T . nilable ( T . any ( String , Symbol ) ) , disable : T ::Boolean ,
227
+ disable_on : T . nilable ( Time ) , disable_for_developers : T ::Boolean , caller : T ::Array [ String ] ) . void
228
+ }
229
+ def odisabled ( method , replacement = nil ,
230
+ disable : false ,
231
+ disable_on : nil ,
232
+ disable_for_developers : true ,
233
+ caller : send ( :caller ) )
218
234
# This odeprecated should stick around indefinitely.
219
- odeprecated ( method , replacement , ** options )
235
+ odeprecated ( method , replacement , disable : , disable_on : , disable_for_developers : , caller : )
220
236
end
221
237
238
+ sig { params ( formula : T . any ( String , Formula ) ) . returns ( String ) }
222
239
def pretty_installed ( formula )
223
240
if !$stdout. tty?
224
241
formula . to_s
@@ -229,6 +246,7 @@ def pretty_installed(formula)
229
246
end
230
247
end
231
248
249
+ sig { params ( formula : T . any ( String , Formula ) ) . returns ( String ) }
232
250
def pretty_outdated ( formula )
233
251
if !$stdout. tty?
234
252
formula . to_s
@@ -239,6 +257,7 @@ def pretty_outdated(formula)
239
257
end
240
258
end
241
259
260
+ sig { params ( formula : T . any ( String , Formula ) ) . returns ( String ) }
242
261
def pretty_uninstalled ( formula )
243
262
if !$stdout. tty?
244
263
formula . to_s
@@ -249,6 +268,7 @@ def pretty_uninstalled(formula)
249
268
end
250
269
end
251
270
271
+ sig { params ( seconds : T . nilable ( T . any ( Integer , Float ) ) ) . returns ( String ) }
252
272
def pretty_duration ( seconds )
253
273
seconds = seconds . to_i
254
274
res = +""
@@ -266,9 +286,10 @@ def pretty_duration(seconds)
266
286
res . freeze
267
287
end
268
288
289
+ sig { params ( formula : T . nilable ( Formula ) ) . void }
269
290
def interactive_shell ( formula = nil )
270
291
unless formula . nil?
271
- ENV [ "HOMEBREW_DEBUG_PREFIX" ] = formula . prefix
292
+ ENV [ "HOMEBREW_DEBUG_PREFIX" ] = formula . prefix . to_s
272
293
ENV [ "HOMEBREW_DEBUG_INSTALL" ] = formula . full_name
273
294
end
274
295
@@ -295,6 +316,7 @@ def with_custom_locale(locale, &block)
295
316
296
317
# Kernel.system but with exceptions.
297
318
def safe_system ( cmd , *args , **options )
319
+ # TODO: migrate to utils.rb Homebrew.safe_system
298
320
require "utils"
299
321
300
322
return if Homebrew . system ( cmd , *args , **options )
@@ -306,6 +328,7 @@ def safe_system(cmd, *args, **options)
306
328
#
307
329
# @api internal
308
330
def quiet_system ( cmd , *args )
331
+ # TODO: migrate to utils.rb Homebrew.quiet_system
309
332
require "utils"
310
333
311
334
Homebrew . _system ( cmd , *args ) do
@@ -367,11 +390,13 @@ def which_editor(silent: false)
367
390
editor
368
391
end
369
392
370
- def exec_editor ( *args )
371
- puts "Editing #{ args . join "\n " } "
372
- with_homebrew_path { safe_system ( *which_editor . shellsplit , *args ) }
393
+ sig { params ( filename : T . any ( String , Pathname ) ) . void }
394
+ def exec_editor ( filename )
395
+ puts "Editing #{ filename } "
396
+ with_homebrew_path { safe_system ( *which_editor . shellsplit , filename ) }
373
397
end
374
398
399
+ sig { params ( args : T . any ( String , Pathname ) ) . void }
375
400
def exec_browser ( *args )
376
401
browser = Homebrew ::EnvConfig . browser
377
402
browser ||= OS ::PATH_OPEN if defined? ( OS ::PATH_OPEN )
@@ -384,7 +409,7 @@ def exec_browser(*args)
384
409
end
385
410
end
386
411
387
- IGNORE_INTERRUPTS_MUTEX = Thread ::Mutex . new . freeze
412
+ IGNORE_INTERRUPTS_MUTEX = T . let ( Thread ::Mutex . new . freeze , Thread :: Mutex )
388
413
389
414
def ignore_interrupts
390
415
IGNORE_INTERRUPTS_MUTEX . synchronize do
@@ -417,6 +442,10 @@ def redirect_stdout(file)
417
442
418
443
# Ensure the given formula is installed
419
444
# This is useful for installing a utility formula (e.g. `shellcheck` for `brew style`)
445
+ sig {
446
+ params ( formula_or_name : T . any ( String , Formula ) , reason : String , latest : T ::Boolean , output_to_stderr : T ::Boolean ,
447
+ quiet : T ::Boolean ) . returns ( Formula )
448
+ }
420
449
def ensure_formula_installed! ( formula_or_name , reason : "" , latest : false ,
421
450
output_to_stderr : true , quiet : false )
422
451
if output_to_stderr || quiet
@@ -456,6 +485,7 @@ def ensure_formula_installed!(formula_or_name, reason: "", latest: false,
456
485
end
457
486
458
487
# Ensure the given executable is exist otherwise install the brewed version
488
+ sig { params ( name : String , formula_name : T . nilable ( String ) , reason : String , latest : T ::Boolean ) . returns ( T . nilable ( Pathname ) ) }
459
489
def ensure_executable! ( name , formula_name = nil , reason : "" , latest : false )
460
490
formula_name ||= name
461
491
@@ -472,10 +502,12 @@ def ensure_executable!(name, formula_name = nil, reason: "", latest: false)
472
502
ensure_formula_installed! ( formula_name , reason :, latest :) . opt_bin /name
473
503
end
474
504
505
+ sig { returns ( T ::Array [ Pathname ] ) }
475
506
def paths
476
- @paths ||= ORIGINAL_PATHS . uniq . map ( &:to_s )
507
+ @paths ||= T . let ( ORIGINAL_PATHS . uniq . map ( &:to_s ) , T . nilable ( T :: Array [ Pathname ] ) )
477
508
end
478
509
510
+ sig { params ( size_in_bytes : T . any ( Integer , Float ) ) . returns ( String ) }
479
511
def disk_usage_readable ( size_in_bytes )
480
512
if size_in_bytes . abs >= 1_073_741_824
481
513
size = size_in_bytes . to_f / 1_073_741_824
@@ -509,6 +541,7 @@ def number_readable(number)
509
541
# preserving character encoding validity. The returned string will
510
542
# be not much longer than the specified max_bytes, though the exact
511
543
# shortfall or overrun may vary.
544
+ sig { params ( str : String , max_bytes : Integer , options : T ::Hash [ Symbol , T . untyped ] ) . returns ( String ) }
512
545
def truncate_text_to_approximate_size ( str , max_bytes , options = { } )
513
546
front_weight = options . fetch ( :front_weight , 0.5 )
514
547
raise "opts[:front_weight] must be between 0.0 and 1.0" if front_weight < 0.0 || front_weight > 1.0
@@ -530,7 +563,7 @@ def truncate_text_to_approximate_size(str, max_bytes, options = {})
530
563
front = bytes [ 0 ..( n_front_bytes - 1 ) ]
531
564
back = bytes [ -n_back_bytes ..]
532
565
end
533
- out = front + glue_bytes + back
566
+ out = T . must ( front ) + glue_bytes + T . must ( back )
534
567
out . force_encoding ( "UTF-8" )
535
568
out . encode! ( "UTF-16" , invalid : :replace )
536
569
out . encode! ( "UTF-8" )
@@ -568,6 +601,7 @@ def with_env(hash)
568
601
end
569
602
end
570
603
604
+ sig { returns ( T . proc . params ( a : String , b : String ) . returns ( Integer ) ) }
571
605
def tap_and_name_comparison
572
606
proc do |a , b |
573
607
if a . include? ( "/" ) && b . exclude? ( "/" )
@@ -580,6 +614,7 @@ def tap_and_name_comparison
580
614
end
581
615
end
582
616
617
+ sig { params ( input : String , secrets : T ::Array [ String ] ) . returns ( String ) }
583
618
def redact_secrets ( input , secrets )
584
619
secrets . compact
585
620
. reduce ( input ) { |str , secret | str . gsub secret , "******" }
0 commit comments