Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e956db2

Browse files
committedFeb 11, 2023
Move console report
As first step we need to move the console report away of the status reporter class. Related #50
1 parent 6a601a5 commit e956db2

File tree

5 files changed

+101
-89
lines changed

5 files changed

+101
-89
lines changed
 

Diff for: ‎.github/workflows/main.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ on:
66
branches:
77
- main
88
pull_request:
9-
branches:
10-
- main
9+
# branches:
10+
# - main
1111

1212
jobs:
1313
test-ruby-2-4-x:

Diff for: ‎.rubocop_todo.yml

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2021-08-31 01:33:08 UTC using RuboCop version 1.9.1.
3+
# on 2023-02-07 02:28:48 UTC using RuboCop version 1.24.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 2
9+
# Offense count: 1
1010
# Configuration parameters: Include.
1111
# Include: **/*.gemspec
1212
Gemspec/RequiredRubyVersion:
1313
Exclude:
14-
- 'gemfiles/skunk.gemspec'
1514
- 'skunk.gemspec'
1615

1716
# Offense count: 1
1817
# Cop supports --auto-correct.
1918
Layout/ClosingHeredocIndentation:
2019
Exclude:
21-
- 'lib/skunk/cli/commands/status_reporter.rb'
20+
- 'lib/skunk/cli/generators/console_report.rb'
2221

2322
# Offense count: 1
2423
# Cop supports --auto-correct.
2524
Layout/HeredocIndentation:
2625
Exclude:
27-
- 'lib/skunk/cli/commands/status_reporter.rb'
26+
- 'lib/skunk/cli/generators/console_report.rb'
2827

2928
# Offense count: 2
3029
Lint/MissingSuper:

Diff for: ‎lib/skunk/cli/commands/status_reporter.rb

+2-82
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,16 @@
11
# frozen_string_literal: true
22

3-
require "erb"
43
require "rubycritic/commands/status_reporter"
5-
require "terminal-table"
4+
require "skunk/cli/generators/console_report"
65

76
module Skunk
87
module Command
98
# Knows how to report status for stinky files
109
class StatusReporter < RubyCritic::Command::StatusReporter
1110
attr_accessor :analysed_modules
1211

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
3012
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
9414
end
9515
end
9616
end

Diff for: ‎lib/skunk/cli/commands/status_sharer.rb

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require "json"
66

77
require "skunk/cli/commands/status_reporter"
8+
require "skunk/cli/generators/console_report"
89

910
module Skunk
1011
module Command

Diff for: ‎lib/skunk/cli/generators/console_report.rb

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

0 commit comments

Comments
 (0)
Please sign in to comment.