Class: Steep::CLI

Inherits:
Object show all
Defined in:
lib/steep/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout:, stdin:, stderr:, argv:) ⇒ CLI

Returns a new instance of CLI.



11
12
13
14
15
16
# File 'lib/steep/cli.rb', line 11

def initialize(stdout:, stdin:, stderr:, argv:)
  @stdout = stdout
  @stdin = stdin
  @stderr = stderr
  @argv = argv
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



5
6
7
# File 'lib/steep/cli.rb', line 5

def argv
  @argv
end

#commandObject (readonly)

Returns the value of attribute command.



9
10
11
# File 'lib/steep/cli.rb', line 9

def command
  @command
end

#stderrObject (readonly)

Returns the value of attribute stderr.



8
9
10
# File 'lib/steep/cli.rb', line 8

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



7
8
9
# File 'lib/steep/cli.rb', line 7

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



6
7
8
# File 'lib/steep/cli.rb', line 6

def stdout
  @stdout
end

Class Method Details

.available_commandsObject



18
19
20
# File 'lib/steep/cli.rb', line 18

def self.available_commands
  [:init, :check, :validate, :annotations, :version, :project, :watch, :langserver, :stats, :binstub, :checkfile, :server, :query]
end

Instance Method Details

#handle_jobs_option(option, opts) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/steep/cli.rb', line 87

def handle_jobs_option(option, opts)
  opts.on("-j N", "--jobs=N", "Specify the number of type check workers (defaults: #{option.default_jobs_count})") do |count|
    option.jobs_count = Integer(count) if Integer(count) > 0
  end

  opts.on("--steep-command=COMMAND", "Specify command to exec Steep CLI for worker (defaults: steep)") do |cmd|
    option.steep_command = cmd
  end
end

#handle_logging_options(opts) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/steep/cli.rb', line 69

def handle_logging_options(opts)
  opts.on("--log-level=LEVEL", "Specify log level: debug, info, warn, error, fatal") do |level|
    # @type var level: String
    Steep.logger.level = level
    Steep.ui_logger.level = level
  end

  opts.on("--log-output=PATH", "Print logs to given path") do |file|
    # @type var file: String
    Steep.log_output = file
  end

  opts.on("--verbose", "Set log level to debug") do
    Steep.logger.level = Logger::DEBUG
    Steep.ui_logger.level = Logger::DEBUG
  end
end

#handle_steepfile_option(opts, command) ⇒ Object



65
66
67
# File 'lib/steep/cli.rb', line 65

def handle_steepfile_option(opts, command)
  opts.on("--steepfile=PATH", "Specify path to Steepfile") {|path| command.steepfile = Pathname(path) }
end

#process_annotationsObject



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/steep/cli.rb', line 280

def process_annotations
  Drivers::Annotations.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep annotations [options] [sources]\n\nDescription:\nPrints the type annotations in the Ruby code.\n\nOptions:\n"
      handle_logging_options opts
    end.parse!(argv)

    command.command_line_patterns.push(*argv)
  end.run
end

#process_binstubObject



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/steep/cli.rb', line 382

def process_binstub
  path = Pathname("bin/steep")
  root_path = Pathname.pwd
  force = false

  OptionParser.new do |opts|
    opts.banner = "Usage: steep binstub [options]\n\nDescription:\nGenerate a binstub which set up ruby executables and bundlers.\n\nOptions:\n"
    handle_logging_options opts

    opts.on("-o PATH", "--output=PATH", "The path of the executable file (defaults to `bin/steep`)") do |v|
      path = Pathname(v)
    end

    opts.on("--root=PATH", "The repository root path (defaults to `.`)") do |v|
      root_path = (Pathname.pwd + v).cleanpath
    end

    opts.on("--[no-]force", "Overwrite file (defaults to false)") do
      force = true
    end
  end.parse!(argv)

  binstub_path = (Pathname.pwd + path).cleanpath
  bindir_path = binstub_path.parent

  bindir_path.mkpath

  gemfile_path =
    if defined?(Bundler)
      Bundler.default_gemfile.relative_path_from(bindir_path)
    else
      Pathname("../Gemfile")
    end

  if binstub_path.file?
    if force
      stdout.puts Rainbow("#{path} already exists. Overwriting...").yellow
    else
      stdout.puts Rainbow(''"⚠️ #{path} already exists. Bye! 👋").red
      return 0
    end
  end

  template = "#!/usr/bin/env bash\n\nBINSTUB_DIR=$(cd $(dirname $0); pwd)\nGEMFILE=$(readlink -f ${BINSTUB_DIR}/\#{gemfile_path})\nROOT_DIR=$(readlink -f ${BINSTUB_DIR}/\#{root_path.relative_path_from(bindir_path)})\n\nSTEEP=\"bundle exec --gemfile=${GEMFILE} steep\"\n\nif type \"rbenv\" > /dev/null 2>&1; then\n  STEEP=\"rbenv exec ${STEEP}\"\nelse\n  if type \"rvm\" > /dev/null 2>&1; then\nif [ -e ${ROOT_DIR}/.ruby-version ]; then\n  STEEP=\"rvm ${ROOT_DIR} do ${STEEP}\"\nfi\n  fi\nfi\n\nexec $STEEP $@\n"

  binstub_path.write(template)
  binstub_path.chmod(0755)

  stdout.puts Rainbow("Successfully generated executable #{path} 🎉").blue

  0
end

#process_checkObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/steep/cli.rb', line 128

def process_check
  Drivers::Check.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep check [options] [paths]\n   steep check [options] -e 'expr' [-e 'expr' ...]\n\nDescription:\nType check the program.\n\nIf paths are specified, it type checks and validates the files at the given path.\nOtherwise, it type checks and validates all files in the project or the groups if specified.\n\nOptions:\n"

      handle_steepfile_option(opts, command)
      opts.on("-e", "--expression=EXPRESSION", "Type check a Ruby expression (may be specified multiple times)") do |expr|
        command.expressions << expr
      end
      opts.on("--with-expectations[=PATH]", "Type check with expectations saved in PATH (or steep_expectations.yml)") do |path|
        command.with_expectations_path = Pathname(path || "steep_expectations.yml")
      end
      opts.on("--save-expectations[=PATH]", "Save expectations with current type check result to PATH (or steep_expectations.yml)") do |path|
        command.save_expectations_path = Pathname(path || "steep_expectations.yml")
      end
      opts.on("--severity-level=LEVEL", /^error|warning|information|hint$/, "Specify the minimum diagnostic severity to be recognized as an error (defaults: warning): error, warning, information, or hint") do |level|
        command.severity_level = level.to_sym
      end

      opts.on("--group=GROUP", "Specify target/group name to type check") do |arg|
        # @type var arg: String
        target, group = arg.split(".")
        target or raise
        case group
        when "*"
          command.active_group_names << [target.to_sym, true]
        when nil
          command.active_group_names << [target.to_sym, nil]
        else
          command.active_group_names << [target.to_sym, group.to_sym]
        end
      end

      opts.on("--[no-]type-check", "Type check Ruby code") do |v|
        command.type_check_code = v ? true : false
      end

      opts.on("--validate=OPTION", ["skip", "group", "project", "library"], "Validation levels of signatures (default: group, options: skip,group,project,library)") do |level|
        case level
        when "skip"
          command.validate_group_signatures = false
          command.validate_project_signatures = false
          command.validate_library_signatures = false
        when "group"
          command.validate_group_signatures = true
          command.validate_project_signatures = false
          command.validate_library_signatures = false
        when "project"
          command.validate_group_signatures = true
          command.validate_project_signatures = true
          command.validate_library_signatures = false
        when "library"
          command.validate_group_signatures = true
          command.validate_project_signatures = true
          command.validate_library_signatures = true
        end
      end

      opts.on("--format=FORMATTER", ["code", "github"], "Output formatters (default: code, options: code,github)") do |formatter|
        command.formatter = formatter
      end

      opts.on("--[no-]daemon", "Use daemon server if available (default: true)") do |v|
        command.use_daemon = v ? true : false
      end

      handle_jobs_option command.jobs_option, opts
      handle_logging_options opts
    end.parse!(argv)

    setup_jobs_for_ci(command.jobs_option)

    command.command_line_patterns.push(*argv)

    unless command.expressions.empty? || argv.empty?
      abort "Cannot specify both -e and file paths"
    end
  end.run
end

#process_checkfileObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/steep/cli.rb', line 219

def process_checkfile
  Drivers::Checkfile.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep checkfile [options] [files]\n\nDescription:\nDeprecated: Use `steep check` instead.\n\nOptions:\n"

      handle_steepfile_option(opts, command)
      opts.on("--all-rbs", "Type check all RBS files") { command.all_rbs = true }
      opts.on("--all-ruby", "Type check all Ruby files") { command.all_ruby = true }
      opts.on("--stdin", "Read files to type check from stdin") do
        while line = stdin.gets()
          object = JSON.parse(line, symbolize_names: true)
          Steep.logger.info { "Loading content of `#{object[:path]}` from stdin: #{object[:content].lines[0].chomp}" }
          command.stdin_input[Pathname(object[:path])] = object[:content]
        end
      end
      handle_jobs_option command.jobs_option, opts
      handle_logging_options opts
    end.parse!(argv)

    setup_jobs_for_ci(command.jobs_option)

    command.command_line_args.push(*argv)
  end.run
end

#process_global_optionsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/steep/cli.rb', line 22

def process_global_options
  OptionParser.new do |opts|
    opts.banner = "      Usage: steep [options]\n\n      Available commands:\n          \#{CLI.available_commands.join(', ')}\n\n      Options:\n    USAGE\n\n    opts.on(\"--version\", \"Print Steep version\") do\n      process_version\n      exit 0\n    end\n\n    handle_logging_options(opts)\n  end.order!(argv)\n\n  true\nend\n"

#process_initObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/steep/cli.rb', line 109

def process_init
  Drivers::Init.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep init [options]\n\nDescription:\nGenerates a Steepfile at specified path.\n\nOptions:\n"
      handle_steepfile_option(opts, command)
      opts.on("--force", "Overwrite the Steepfile if it already exists") { command.force_write = true }

      handle_logging_options opts
    end.parse!(argv)
  end.run()
end

#process_langserverObject



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/steep/cli.rb', line 345

def process_langserver
  Drivers::Langserver.new(stdout: stdout, stderr: stderr, stdin: stdin).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep langserver [options]\n\nDescription:\nStarts language server, which is assumed to be invoked from language client.\n\nOptions:\n"
      handle_steepfile_option(opts, command)
      opts.on("--refork") { command.refork = true }
      opts.on("--[no-]command-socket", "Accept `steep query`/`steep check` connections on a UNIX socket (default: true)") do |v|
        command.command_socket = v ? true : false
      end
      handle_jobs_option command.jobs_option, opts
      handle_logging_options opts
    end.parse!(argv)
  end.run
end

#process_projectObject



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/steep/cli.rb', line 298

def process_project
  Drivers::PrintProject.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep project [options]\n\nDescription:\nPrints the project configuration.\n\nOptions:\n"
      handle_steepfile_option(opts, command)
      opts.on("--[no-]print-files", "Print files") {|v|
        command.print_files = v ? true : false
      }
      handle_logging_options opts
    end.parse!(argv)
  end.run
end

#process_queryObject



608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/steep/cli.rb', line 608

def process_query
  subcommand = argv.shift

  if subcommand.nil? || subcommand == "--help" || subcommand == "-h"
    stderr.puts "      Usage: steep query <subcommand> [options]\n\n      Description:\n          Query type information from the Steep daemon server.\n          The daemon must be running (start it with `steep server start`).\n\n      Note:\n          `steep query` is an experimental command.\n          The user interface and output format may change without deprecation.\n\n      Available subcommands:\n          hover       Get hover information (type, documentation) for a position\n          definition  Get the definition(s) of a class, type alias, constant, or method name\n          diagnostics Get the type check diagnostics the server has computed\n\n      Options:\n          --help    Show this help message\n\n      Examples:\n          steep query hover lib/foo.rb:10:5\n          steep query definition RBS::Location\n          steep query definition RBS::Parser.parse_signature\n          steep query diagnostics lib/foo.rb sig/foo.rbs\n    HELP\n    return 0\n  end\n\n  case subcommand\n  when \"hover\"\n    OptionParser.new do |opts|\n      opts.banner = <<BANNER\nUsage: steep query hover [options] FILE:LINE:COL [FILE:LINE:COL ...]\n\nDescription:\nGet hover information for the specified position(s).\nConnects to the running Steep daemon and returns type information as JSONL\n(one JSON object per line for each queried position).\n\nFILE:LINE:COL - File path with 1-based line and column numbers.\n\nNote:\nThis is an experimental command.\nThe user interface and output format may change without deprecation.\n\nOptions:\nBANNER\n      handle_logging_options opts\n    end.parse!(argv)\n\n    if argv.empty?\n      stderr.puts \"Error: Missing FILE:LINE:COL argument\"\n      stderr.puts \"  Usage: steep query hover FILE:LINE:COL [FILE:LINE:COL ...]\"\n      return 1\n    end\n\n    locations = [] #: Array[[String, Integer, Integer]]\n\n    argv.each do |location|\n      match = location.match(/\\A(.+):(\\d+):(\\d+)\\z/)\n      unless match\n        stderr.puts \"Error: Invalid format: \#{location}\"\n        stderr.puts \"  Expected format: FILE:LINE:COL (e.g., lib/foo.rb:10:5)\"\n        return 1\n      end\n\n      path = match[1] or raise\n      line = match[2].to_i\n      column = match[3].to_i\n\n      if line < 1 || column < 1\n        stderr.puts \"Error: LINE and COL must be positive integers (1-based)\"\n        return 1\n      end\n\n      locations << [path, line, column]\n    end\n\n    Drivers::Query.new(stdout: stdout, stderr: stderr).run_hover(locations: locations)\n  when \"definition\"\n    OptionParser.new do |opts|\n      opts.banner = <<BANNER\nUsage: steep query definition [options] NAME [NAME ...]\n\nDescription:\nGet the definition(s) of the specified name(s).\nConnects to the running Steep daemon and returns both RBS declarations and\nRuby definitions as JSONL (one JSON object per line for each queried name).\n\nNAME can be one of:\n  * A class, module, interface, type alias, or constant (e.g., RBS::Location)\n  * An instance method (e.g., RBS::Parser#parse_type)\n  * A singleton method (e.g., RBS::Parser.parse_signature)\n\nNote:\nThis is an experimental command.\nThe user interface and output format may change without deprecation.\n\nOptions:\nBANNER\n      handle_logging_options opts\n    end.parse!(argv)\n\n    if argv.empty?\n      stderr.puts \"Error: Missing NAME argument\"\n      stderr.puts \"  Usage: steep query definition NAME [NAME ...]\"\n      return 1\n    end\n\n    Drivers::Query.new(stdout: stdout, stderr: stderr).run_definition(names: argv.dup)\n  when \"diagnostics\"\n    OptionParser.new do |opts|\n      opts.banner = <<BANNER\nUsage: steep query diagnostics [options] [FILE ...]\n\nDescription:\nGet the type check diagnostics the server has computed.\nConnects to the running Steep server, reloads files changed on disk, waits for the\ntype checking to finish, and prints the diagnostics as JSONL (one JSON object per\nline for each file).\n\nWhen FILEs are given, only the diagnostics of those files are printed.\n`\"diagnostics\": null` means the server has not type checked the file yet.\n\nNote:\nThis is an experimental command.\nThe user interface and output format may change without deprecation.\n\nOptions:\nBANNER\n      handle_logging_options opts\n    end.parse!(argv)\n\n    Drivers::Query.new(stdout: stdout, stderr: stderr).run_diagnostics(paths: argv.dup)\n  else\n    stderr.puts \"Unknown query subcommand: \#{subcommand}\"\n    stderr.puts \"  available subcommands: hover, definition, diagnostics\"\n    1\n  end\nend\n"

#process_serverObject



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'lib/steep/cli.rb', line 498

def process_server
  subcommand = argv.shift

  if subcommand.nil? || subcommand == "--help" || subcommand == "-h"
    stderr.puts "      Usage: steep server <subcommand> [options]\n\n      Description:\n          Manage the Steep daemon server for faster type checking.\n          The daemon keeps RBS environment loaded in memory.\n\n      Available subcommands:\n          start     Start the daemon server\n          stop      Stop the daemon server\n          restart   Restart the daemon server\n          status    Show daemon server status\n\n      Options:\n          --help    Show this help message\n\n      Examples:\n          steep server start\n          steep server stop\n          steep server restart\n          steep server status\n    HELP\n    return 0\n  end\n\n  case subcommand\n  when \"start\"\n    unless Steep.can_fork?\n      stderr.puts \"Error: `steep server start` is not supported on this platform (fork() is not available)\"\n      return 1\n    end\n    Drivers::StartServer.new(stdout: stdout, stderr: stderr).tap do |command|\n      OptionParser.new do |opts|\n        opts.banner = <<BANNER\nUsage: steep server start [options]\n\nDescription:\nStarts a persistent daemon server for faster type checking.\nThe daemon keeps RBS environment loaded in memory.\n\nOptions:\nBANNER\n        handle_logging_options opts\n      end.parse!(argv)\n    end.run\n  when \"stop\"\n    Drivers::StopServer.new(stdout: stdout, stderr: stderr).tap do |command|\n      OptionParser.new do |opts|\n        opts.banner = <<BANNER\nUsage: steep server stop [options]\n\nDescription:\nStops the running daemon server.\n\nOptions:\nBANNER\n        handle_logging_options opts\n      end.parse!(argv)\n    end.run\n  when \"restart\"\n    unless Steep.can_fork?\n      stderr.puts \"Error: `steep server restart` is not supported on this platform (fork() is not available)\"\n      return 1\n    end\n    OptionParser.new do |opts|\n      opts.banner = <<BANNER\nUsage: steep server restart [options]\n\nDescription:\nRestarts the daemon server (stops and then starts it).\n\nOptions:\nBANNER\n      handle_logging_options opts\n    end.parse!(argv)\n\n    stop_command = Drivers::StopServer.new(stdout: stdout, stderr: stderr)\n    stop_command.run\n\n    # Brief pause to ensure clean shutdown\n    sleep 0.5\n\n    start_command = Drivers::StartServer.new(stdout: stdout, stderr: stderr)\n    start_command.run\n  when \"status\"\n    OptionParser.new do |opts|\n      opts.banner = <<BANNER\nUsage: steep server status [options]\n\nDescription:\nShows the status of the daemon server.\n\nOptions:\nBANNER\n      handle_logging_options opts\n    end.parse!(argv)\n\n    Daemon.status(stderr: stderr)\n    0\n  else\n    stderr.puts \"Unknown server subcommand: \#{subcommand}\"\n    stderr.puts \"  available subcommands: start, stop, restart, status\"\n    1\n  end\nend\n"

#process_statsObject



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/steep/cli.rb', line 251

def process_stats
  Drivers::Stats.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep stats [options] [sources]\n\nDescription:\nDisplays statistics about the typing of method calls.\n\nOptions:\n"

      handle_steepfile_option(opts, command)
      opts.on("--format=FORMAT", "Specify output format: csv, table") {|format| command.format = format }
      handle_jobs_option command.jobs_option, opts
      handle_logging_options opts
    end.parse!(argv)

    setup_jobs_for_ci(command.jobs_option)

    command.command_line_patterns.push(*argv)
  end.run
end

#process_validateObject



275
276
277
278
# File 'lib/steep/cli.rb', line 275

def process_validate
  stderr.puts "`steep validate` is deprecated. Use `steep check` with `--validate` option instead."
  1
end

#process_vendorObject



367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/steep/cli.rb', line 367

def process_vendor
  Drivers::Vendor.new(stdout: stdout, stderr: stderr, stdin: stdin).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep vendor [options] [dir]"
      handle_logging_options opts

      opts.on("--[no-]clean") do |v|
        command.clean_before = v
      end
    end.parse!(argv)

    command.vendor_dir = Pathname(argv[0] || "vendor/sigs")
  end.run
end

#process_versionObject



462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/steep/cli.rb', line 462

def process_version
  OptionParser.new do |opts|
    opts.banner = "Usage: steep version [options]\n\nDescription:\nPrints Steep version.\n"
  end.parse!(argv)

  stdout.puts Steep::VERSION
  0
end

#process_watchObject



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/steep/cli.rb', line 318

def process_watch
  Drivers::Watch.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep watch [options] [dirs]\n\nDescription:\nMonitors file changes and automatically type checks updated files.\nUsing LSP is recommended for better performance and more features.\n\nOptions:\n"
      opts.on("--severity-level=LEVEL", /^error|warning|information|hint$/, "Specify the minimum diagnostic severity to be recognized as an error (defaults: warning): error, warning, information, or hint") do |level|
        # @type var level: String
        command.severity_level = _ = level.to_sym
      end
      handle_jobs_option command.jobs_option, opts
      handle_logging_options opts
    end.parse!(argv)

    setup_jobs_for_ci(command.jobs_option)

    dirs = argv.map {|dir| Pathname(dir) }
    command.dirs.push(*dirs)
  end.run
end

#process_workerObject



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/steep/cli.rb', line 476

def process_worker
  Drivers::Worker.new(stdout: stdout, stderr: stderr, stdin: stdin).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep worker [options] [dir]"
      handle_logging_options opts

      opts.on("--interaction") { command.worker_type = :interaction }
      opts.on("--typecheck") { command.worker_type = :typecheck }
      handle_steepfile_option(opts, command)
      opts.on("--name=NAME") {|name| command.worker_name = name }
      opts.on("--delay-shutdown") { command.delay_shutdown = true }
      opts.on("--max-index=COUNT") {|count| command.max_index = Integer(count) }
      opts.on("--index=INDEX") {|index| command.index = Integer(index) }
    end.parse!(argv)

    # Disable any `ui_logger` output in workers
    Steep.ui_logger.level = :fatal

    command.commandline_args.push(*argv)
  end.run
end

#runObject



57
58
59
60
61
62
63
# File 'lib/steep/cli.rb', line 57

def run
  process_global_options or return 1
  setup_command or return 1

  method_name = command.to_s.gsub('-', '_')
  __send__(:"process_#{method_name}")
end

#setup_commandObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/steep/cli.rb', line 44

def setup_command
  return false unless command = argv.shift&.to_sym
  @command = command

  if CLI.available_commands.include?(@command) || @command == :worker || @command == :vendor
    true
  else
    stderr.puts "Unknown command: #{command}"
    stderr.puts "  available commands: #{CLI.available_commands.join(', ')}"
    false
  end
end

#setup_jobs_for_ci(jobs_option) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/steep/cli.rb', line 97

def setup_jobs_for_ci(jobs_option)
  if ENV["CI"]
    unless jobs_option.jobs_count
      stderr.puts Rainbow("CI environment is detected but no `--jobs` option is given.").yellow
      stderr.puts "  Using `[2, #{jobs_option.default_jobs_count} (# or processors)].min` to avoid hitting memory limit."
      stderr.puts "  Specify `--jobs` option to increase the number of jobs."

      jobs_option.jobs_count = [2, jobs_option.default_jobs_count].min
    end
  end
end