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



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]
end

Instance Method Details

#handle_jobs_option(option, opts) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/steep/cli.rb', line 79

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/steep/cli.rb', line 63

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

  opts.on("--log-output=PATH", "Print logs to given path") do |file|
    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

#process_annotationsObject



226
227
228
229
230
231
232
233
234
235
# File 'lib/steep/cli.rb', line 226

def process_annotations
  Drivers::Annotations.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep annotations [options] [sources]"
      handle_logging_options opts
    end.parse!(argv)

    command.command_line_patterns.push *argv
  end.run
end

#process_binstubObject



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/steep/cli.rb', line 294

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

  OptionParser.new do |opts|
    opts.banner = "Usage: steep binstub [options]\n\nGenerate a binstub to execute Steep with setting up Bundler and rbenv/rvm.\nUse the executable for LSP integration setup.\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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
# File 'lib/steep/cli.rb', line 114

def process_check
  Drivers::Check.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep check [options] [sources]"

      opts.on("--steepfile=PATH") {|path| command.steepfile = Pathname(path) }
      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

      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_checkfileObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/steep/cli.rb', line 179

def process_checkfile
  Drivers::Checkfile.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep checkfile [options] [files]"

      opts.on("--steepfile=PATH") {|path| command.steepfile = Pathname(path) }
      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
# File 'lib/steep/cli.rb', line 22

def process_global_options
  OptionParser.new do |opts|
    opts.banner = "      Usage: steep [options]\n\n      available commands: \#{CLI.available_commands.join(', ')}\n\n      Options:\n    USAGE\n\n    opts.on(\"--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



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/steep/cli.rb', line 101

def process_init
  Drivers::Init.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep init [options]"

      opts.on("--steepfile=PATH") {|path| command.steepfile = Pathname(path) }
      opts.on("--force") { command.force_write = true }

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

#process_langserverObject



269
270
271
272
273
274
275
276
277
# File 'lib/steep/cli.rb', line 269

def process_langserver
  Drivers::Langserver.new(stdout: stdout, stderr: stderr, stdin: stdin).tap do |command|
    OptionParser.new do |opts|
      opts.on("--steepfile=PATH") {|path| command.steepfile = Pathname(path) }
      handle_jobs_option command.jobs_option, opts
      handle_logging_options opts
    end.parse!(argv)
  end.run
end

#process_projectObject



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/steep/cli.rb', line 237

def process_project
  Drivers::PrintProject.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep project [options]"
      opts.on("--steepfile=PATH") {|path| command.steepfile = Pathname(path) }
      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_statsObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/steep/cli.rb', line 204

def process_stats
  Drivers::Stats.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep stats [options] [sources]"

      opts.on("--steepfile=PATH") {|path| command.steepfile = Pathname(path) }
      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



221
222
223
224
# File 'lib/steep/cli.rb', line 221

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

#process_vendorObject



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

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



374
375
376
377
# File 'lib/steep/cli.rb', line 374

def process_version
  stdout.puts Steep::VERSION
  0
end

#process_watchObject



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

def process_watch
  Drivers::Watch.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep watch [options] [dirs]"
      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



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/steep/cli.rb', line 379

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 }
      opts.on("--steepfile=PATH") {|path| command.steepfile = Pathname(path) }
      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



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

def run
  process_global_options or return 1
  setup_command or return 1

  __send__(:"process_#{command}")
end

#setup_commandObject



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

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



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/steep/cli.rb', line 89

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