Class: Spoom::Cli::Srb::Coverage

Inherits:
Thor
  • Object
show all
Includes:
Helper
Defined in:
lib/spoom/cli/srb/coverage.rb

Constant Summary collapse

DATA_DIR =
"spoom_data"

Constants included from Helper

Helper::HIGHLIGHT_COLOR

Instance Method Summary collapse

Methods included from Helper

#blue, #color?, #colorize, #context, #context_requiring_sorbet!, #cyan, #exec_path, #gray, #green, #highlight, #red, #say, #say_error, #say_warning, #yellow

Methods included from Spoom::Colorize

#set_color

Instance Method Details

#open(file = "spoom_report.html") ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/spoom/cli/srb/coverage.rb', line 174

def open(file = "spoom_report.html")
  unless File.exist?(file)
    say_error("No report file to open `#{file}`")
    say_error(<<~ERR, status: nil)

      If you already generated a report under another name use #{blue("spoom coverage open PATH")}.

      To generate a report run #{blue("spoom coverage report")}.
    ERR
    exit(1)
  end

  exec("open #{file}")
end

#reportObject



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
# File 'lib/spoom/cli/srb/coverage.rb', line 143

def report
  context = context_requiring_sorbet!

  data_dir = options[:data]
  files = Dir.glob("#{data_dir}/*.json")
  if files.empty?
    message_no_data(data_dir)
    exit(1)
  end

  snapshots = files.sort.map do |file|
    json = File.read(file)
    Spoom::Coverage::Snapshot.from_json(json)
  end.filter(&:commit_timestamp).sort_by!(&:commit_timestamp)

  palette = Spoom::Coverage::D3::ColorPalette.new(
    ignore: options[:color_ignore],
    false: options[:color_false],
    true: options[:color_true],
    strict: options[:color_strict],
    strong: options[:color_strong],
  )

  report = Spoom::Coverage.report(context, snapshots, palette: palette)
  file = options[:file]
  File.write(file, report.html)
  say("Report generated under `#{file}`")
  say("\nUse `spoom coverage open` to open it.")
end

#snapshotObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spoom/cli/srb/coverage.rb', line 21

def snapshot
  context = context_requiring_sorbet!
  sorbet = options[:sorbet]

  snapshot = Spoom::Coverage.snapshot(context, rbi: options[:rbi], sorbet_bin: sorbet)
  snapshot.print

  save_dir = options[:save]
  return unless save_dir

  FileUtils.mkdir_p(save_dir)
  file = "#{save_dir}/#{snapshot.commit_sha || snapshot.timestamp}.json"
  File.write(file, snapshot.to_json)
  say("\nSnapshot data saved under `#{file}`")
end

#timelineObject



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
# File 'lib/spoom/cli/srb/coverage.rb', line 43

def timeline
  context = context_requiring_sorbet!
  path = exec_path
  sorbet = options[:sorbet]

  ref_before = context.git_current_branch
  ref_before = context.git_last_commit&.sha unless ref_before
  unless ref_before
    say_error("Not in a git repository")
    say_error("\nSpoom needs to checkout into your previous commits to build the timeline.", status: nil)
    exit(1)
  end

  unless context.git_workdir_clean?
    say_error("Uncommitted changes")
    say_error(<<~ERR, status: nil)

      Spoom needs to checkout into your previous commits to build the timeline."

      Please `git commit` or `git stash` your changes then try again
    ERR
    exit(1)
  end

  save_dir = options[:save]
  FileUtils.mkdir_p(save_dir) if save_dir

  from = parse_time(options[:from], "--from")
  to = parse_time(options[:to], "--to")

  unless from
    intro_commit = context.sorbet_intro_commit
    intro_commit = T.must(intro_commit) # we know it's in there since in_sorbet_project!
    from = intro_commit.time
  end

  timeline = Spoom::Timeline.new(context, from, to)
  ticks = timeline.ticks

  if ticks.empty?
    say_error("No commits to replay, try different `--from` and `--to` options")
    exit(1)
  end

  ticks.each_with_index do |commit, i|
    say("Analyzing commit `#{commit.sha}` - #{commit.time.strftime("%F")} (#{i + 1} / #{ticks.size})")

    context.git_checkout!(ref: commit.sha)

    snapshot = T.let(nil, T.nilable(Spoom::Coverage::Snapshot))
    if options[:bundle_install]
      Bundler.with_unbundled_env do
        next unless bundle_install(path, commit.sha)

        snapshot = Spoom::Coverage.snapshot(context, sorbet_bin: sorbet)
      end
    else
      snapshot = Spoom::Coverage.snapshot(context, sorbet_bin: sorbet)
    end
    next unless snapshot

    snapshot.print(indent_level: 2)
    say("\n")

    next unless save_dir

    file = "#{save_dir}/#{commit.sha}.json"
    File.write(file, snapshot.to_json)
    say("  Snapshot data saved under `#{file}`\n\n")
  end
  context.git_checkout!(ref: ref_before)
end