Class: Spoom::Cli::Bump

Inherits:
Thor
  • Object
show all
Extended by:
T::Sig
Includes:
Helper
Defined in:
lib/spoom/cli/bump.rb

Constant Summary

Constants included from Helper

Helper::HIGHLIGHT_COLOR

Instance Method Summary collapse

Methods included from Helper

#blue, #check_sorbet_segfault, #color?, #colorize, #cyan, #exec_path, #gray, #green, #highlight, #in_sorbet_project!, #in_sorbet_project?, #red, #say, #say_error, #sorbet_config, #sorbet_config_file, #yellow

Methods included from Spoom::Colorize

#set_color

Instance Method Details

#bump(directory = ".") ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/spoom/cli/bump.rb', line 32

def bump(directory = ".")
  in_sorbet_project!

  from = options[:from]
  to = options[:to]
  force = options[:force]
  dry = options[:dry]
  only = options[:only]
  cmd = options[:suggest_bump_command]
  exec_path = File.expand_path(self.exec_path)

  unless Sorbet::Sigils.valid_strictness?(from)
    say_error("Invalid strictness `#{from}` for option `--from`")
    exit(1)
  end

  unless Sorbet::Sigils.valid_strictness?(to)
    say_error("Invalid strictness `#{to}` for option `--to`")
    exit(1)
  end

  if options[:count_errors] && !dry
    say_error("`--count-errors` can only be used with `--dry`")
    exit(1)
  end

  say("Checking files...")

  directory = File.expand_path(directory)
  files_to_bump = Sorbet::Sigils.files_with_sigil_strictness(directory, from)

  files_from_config = config_files(path: exec_path)
  files_to_bump.select! { |file| files_from_config.include?(file) }

  if only
    list = File.read(only).lines.map { |file| File.expand_path(file.strip) }
    files_to_bump.select! { |file| list.include?(File.expand_path(file)) }
  end

  say("\n")

  if files_to_bump.empty?
    say("No file to bump from `#{from}` to `#{to}`")
    exit(0)
  end

  Sorbet::Sigils.change_sigil_in_files(files_to_bump, to)

  if force
    print_changes(files_to_bump, command: cmd, from: from, to: to, dry: dry, path: exec_path)
    undo_changes(files_to_bump, from) if dry
    exit(files_to_bump.empty?)
  end

  error_url_base = Spoom::Sorbet::Errors::DEFAULT_ERROR_URL_BASE
  result = Sorbet.srb_tc(
    "--no-error-sections",
    "--error-url-base=#{error_url_base}",
    path: exec_path,
    capture_err: true,
    sorbet_bin: options[:sorbet]
  )

  check_sorbet_segfault(result.exit_code) do
    say_error("      It means one of the file bumped to `typed: \#{to}` made Sorbet crash.\n      Run `spoom bump -f` locally followed by `bundle exec srb tc` to investigate the problem.\n    ERR\n    undo_changes(files_to_bump, from)\n  end\n\n  if result.status\n    print_changes(files_to_bump, command: cmd, from: from, to: to, dry: dry, path: exec_path)\n    undo_changes(files_to_bump, from) if dry\n    exit(files_to_bump.empty?)\n  end\n\n  errors = Sorbet::Errors::Parser.parse_string(result.err, error_url_base: error_url_base)\n\n  files_with_errors = errors.map do |err|\n    path = File.expand_path(err.file)\n    next unless path.start_with?(directory)\n    next unless File.file?(path)\n    next unless files_to_bump.include?(path)\n    path\n  end.compact.uniq\n\n  undo_changes(files_with_errors, from)\n\n  say(\"Found \#{errors.length} type checking error\#{'s' if errors.length > 1}\") if options[:count_errors]\n\n  files_changed = files_to_bump - files_with_errors\n  print_changes(files_changed, command: cmd, from: from, to: to, dry: dry, path: exec_path)\n  undo_changes(files_to_bump, from) if dry\n  exit(files_changed.empty?)\nend\n", status: nil)