Module: Spoom::Sorbet

Extended by:
T::Sig
Defined in:
lib/spoom/sorbet.rb,
lib/spoom/sorbet/config.rb,
lib/spoom/sorbet/errors.rb,
lib/spoom/sorbet/sigils.rb,
lib/spoom/sorbet/metrics.rb

Defined Under Namespace

Modules: Errors, MetricsParser, Sigils Classes: Config, Error

Constant Summary collapse

CONFIG_PATH =
"sorbet/config"
GEM_PATH =
T.let(Gem::Specification.find_by_name("sorbet-static").full_gem_path, String)
BIN_PATH =
T.let((Pathname.new(GEM_PATH) / "libexec" / "sorbet").to_s, String)
KILLED_CODE =
137
SEGFAULT_CODE =
139

Class Method Summary collapse

Class Method Details

.srb(*arg, path: ".", capture_err: false, sorbet_bin: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/spoom/sorbet.rb', line 54

def srb(*arg, path: ".", capture_err: false, sorbet_bin: nil)
  if sorbet_bin
    arg.prepend(sorbet_bin)
  else
    arg.prepend("bundle", "exec", "srb")
  end
  result = Spoom.exec(*T.unsafe(arg), path: path, capture_err: capture_err)

  case result.exit_code
  when KILLED_CODE
    raise Error::Killed.new("Sorbet was killed.", result)
  when SEGFAULT_CODE
    raise Error::Segfault.new("Sorbet segfaulted.", result)
  end

  result
end

.srb_files(config, path: ".") ⇒ Object



87
88
89
90
91
92
93
# File 'lib/spoom/sorbet.rb', line 87

def srb_files(config, path: ".")
  regs = config.ignore.map { |string| Regexp.new(Regexp.escape(string)) }
  exts = config.allowed_extensions.empty? ? [".rb", ".rbi"] : config.allowed_extensions
  Dir.glob((Pathname.new(path) / "**/*{#{exts.join(",")}}").to_s).reject do |f|
    regs.any? { |re| re.match?(f) }
  end.sort
end

.srb_metrics(*arg, path: ".", capture_err: false, sorbet_bin: nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/spoom/sorbet.rb', line 128

def srb_metrics(*arg, path: ".", capture_err: false, sorbet_bin: nil)
  metrics_file = "metrics.tmp"
  metrics_path = "#{path}/#{metrics_file}"
  T.unsafe(self).srb_tc(
    "--metrics-file",
    metrics_file,
    *arg,
    path: path,
    capture_err: capture_err,
    sorbet_bin: sorbet_bin,
  )
  if File.exist?(metrics_path)
    metrics = Spoom::Sorbet::MetricsParser.parse_file(metrics_path)
    File.delete(metrics_path)
    return metrics
  end
  nil
end

.srb_tc(*arg, path: ".", capture_err: false, sorbet_bin: nil) ⇒ Object



80
81
82
83
# File 'lib/spoom/sorbet.rb', line 80

def srb_tc(*arg, path: ".", capture_err: false, sorbet_bin: nil)
  arg.prepend("tc") unless sorbet_bin
  srb(*T.unsafe(arg), path: path, capture_err: capture_err, sorbet_bin: sorbet_bin)
end

.srb_version(*arg, path: ".", capture_err: false, sorbet_bin: nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/spoom/sorbet.rb', line 103

def srb_version(*arg, path: ".", capture_err: false, sorbet_bin: nil)
  result = T.let(
    T.unsafe(self).srb_tc(
      "--no-config",
      "--version",
      *arg,
      path: path,
      capture_err: capture_err,
      sorbet_bin: sorbet_bin,
    ),
    ExecResult,
  )
  return nil unless result.status

  result.out.split(" ")[2]
end

.version_from_gemfile_lock(gem: "sorbet", path: ".") ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/spoom/sorbet.rb', line 151

def version_from_gemfile_lock(gem: "sorbet", path: ".")
  gemfile_path = "#{path}/Gemfile.lock"
  return nil unless File.exist?(gemfile_path)

  content = File.read(gemfile_path).match(/^    #{gem} \(.*(\d+\.\d+\.\d+).*\)/)
  return nil unless content

  content[1]
end