|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 |
| -require "erb" |
4 | 3 | require "rubycritic/commands/status_reporter"
|
5 |
| -require "terminal-table" |
| 4 | +require "skunk/cli/generators/console_report" |
6 | 5 |
|
7 | 6 | module Skunk
|
8 | 7 | module Command
|
9 | 8 | # Knows how to report status for stinky files
|
10 | 9 | class StatusReporter < RubyCritic::Command::StatusReporter
|
11 | 10 | attr_accessor :analysed_modules
|
12 | 11 |
|
13 |
| - HEADINGS = %w[file skunk_score churn_times_cost churn cost coverage].freeze |
14 |
| - HEADINGS_WITHOUT_FILE = HEADINGS - %w[file] |
15 |
| - HEADINGS_WITHOUT_FILE_WIDTH = HEADINGS_WITHOUT_FILE.size * 17 # padding |
16 |
| - |
17 |
| - TEMPLATE = ERB.new(<<-TEMPL |
18 |
| -<%= _ttable %>\n |
19 |
| -SkunkScore Total: <%= total_skunk_score %> |
20 |
| -Modules Analysed: <%= analysed_modules_count %> |
21 |
| -SkunkScore Average: <%= skunk_score_average %> |
22 |
| -<% if worst %>Worst SkunkScore: <%= worst.skunk_score %> (<%= worst.pathname %>)<% end %> |
23 |
| -
|
24 |
| -Generated with Skunk v<%= Skunk::VERSION %> |
25 |
| -TEMPL |
26 |
| - ) |
27 |
| - |
28 |
| - # Returns a status message with a table of all analysed_modules and |
29 |
| - # a skunk score average |
30 | 12 | def update_status_message
|
31 |
| - opts = table_options.merge(headings: HEADINGS, rows: table) |
32 |
| - |
33 |
| - _ttable = Terminal::Table.new(opts) |
34 |
| - |
35 |
| - @status_message = TEMPLATE.result(binding) |
36 |
| - end |
37 |
| - |
38 |
| - private |
39 |
| - |
40 |
| - def analysed_modules_count |
41 |
| - @analysed_modules_count ||= non_test_modules.count |
42 |
| - end |
43 |
| - |
44 |
| - def non_test_modules |
45 |
| - @non_test_modules ||= analysed_modules.reject do |a_module| |
46 |
| - module_path = a_module.pathname.dirname.to_s |
47 |
| - module_path.start_with?("test", "spec") || module_path.end_with?("test", "spec") |
48 |
| - end |
49 |
| - end |
50 |
| - |
51 |
| - def worst |
52 |
| - @worst ||= sorted_modules.first |
53 |
| - end |
54 |
| - |
55 |
| - def sorted_modules |
56 |
| - @sorted_modules ||= non_test_modules.sort_by(&:skunk_score).reverse! |
57 |
| - end |
58 |
| - |
59 |
| - def total_skunk_score |
60 |
| - @total_skunk_score ||= non_test_modules.sum(&:skunk_score) |
61 |
| - end |
62 |
| - |
63 |
| - def total_churn_times_cost |
64 |
| - non_test_modules.sum(&:churn_times_cost) |
65 |
| - end |
66 |
| - |
67 |
| - def skunk_score_average |
68 |
| - return 0 if analysed_modules_count.zero? |
69 |
| - |
70 |
| - (total_skunk_score.to_d / analysed_modules_count).to_f.round(2) |
71 |
| - end |
72 |
| - |
73 |
| - def table_options |
74 |
| - max = sorted_modules.max_by { |a_mod| a_mod.pathname.to_s.length } |
75 |
| - width = max.pathname.to_s.length + HEADINGS_WITHOUT_FILE_WIDTH |
76 |
| - { |
77 |
| - style: { |
78 |
| - width: width |
79 |
| - } |
80 |
| - } |
81 |
| - end |
82 |
| - |
83 |
| - def table |
84 |
| - sorted_modules.map do |a_mod| |
85 |
| - [ |
86 |
| - a_mod.pathname, |
87 |
| - a_mod.skunk_score, |
88 |
| - a_mod.churn_times_cost, |
89 |
| - a_mod.churn, |
90 |
| - a_mod.cost.round(2), |
91 |
| - a_mod.coverage.round(2) |
92 |
| - ] |
93 |
| - end |
| 13 | + Skunk::Generator::ConsoleReport.new(analysed_modules).generate_report |
94 | 14 | end
|
95 | 15 | end
|
96 | 16 | end
|
|
0 commit comments