Skip to content

Commit 289a470

Browse files
authored
Add JSON Report (#119)
* Update rubocop todo file Improve bin/console experience * Bump GitHub setup * Add JSON generator * Add Changelog entry
1 parent 6917bc8 commit 289a470

File tree

13 files changed

+140
-20
lines changed

13 files changed

+140
-20
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
matrix:
1111
os: [ubuntu]
12-
ruby-version: ["2.6", "2.7", "3.0", "3.1", "3.2"]
12+
ruby-version: ["2.6", "2.7", "3.0", "3.1", "3.2", "3.3"]
1313
steps:
1414
- uses: actions/checkout@v2
1515
- name: Setup Ruby ${{ matrix.ruby-version }}

.github/workflows/skunk.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ jobs:
66
runs-on: ubuntu-latest
77

88
steps:
9-
- uses: actions/checkout@v2
9+
- uses: actions/checkout@v4
1010
- name: Setup Ruby
1111
uses: ruby/setup-ruby@v1
1212
with:
13-
ruby-version: 3.1
13+
ruby-version: 3.3
1414
bundler-cache: true
1515
- name: Run test suite with COVERAGE=true
1616
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
.byebug_history
1212

1313
.ruby-version
14+
.tool-versions

.rubocop_todo.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2023-07-22 00:31:00 UTC using RuboCop version 1.54.2.
3+
# on 2025-05-02 20:16:50 UTC using RuboCop version 1.75.4.
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
@@ -25,12 +25,11 @@ Layout/HeredocIndentation:
2525
Exclude:
2626
- 'lib/skunk/commands/status_reporter.rb'
2727

28-
# Offense count: 2
28+
# Offense count: 1
2929
# Configuration parameters: AllowedParentClasses.
3030
Lint/MissingSuper:
3131
Exclude:
3232
- 'lib/skunk/cli/application.rb'
33-
- 'lib/skunk/commands/base.rb'
3433

3534
# Offense count: 1
3635
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
@@ -51,7 +50,16 @@ Metrics/MethodLength:
5150
# Offense count: 1
5251
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
5352
# SupportedStyles: snake_case, normalcase, non_integer
54-
# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
53+
# AllowedIdentifiers: TLS1_1, TLS1_2, capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
5554
Naming/VariableNumber:
5655
Exclude:
5756
- 'lib/skunk/commands/status_sharer.rb'
57+
58+
# Offense count: 1
59+
# This cop supports unsafe autocorrection (--autocorrect-all).
60+
# Configuration parameters: EnforcedStyle.
61+
# SupportedStyles: always, always_true, never
62+
Style/FrozenStringLiteralComment:
63+
Exclude:
64+
- '**/*.arb'
65+
- 'bin/console'

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
* <INSERT YOUR FEATURE OR BUGFIX HERE>
1111

12+
* [FEATURE: Generate JSON report in background](https://github.com/fastruby/skunk/pull/119)
1213
* [ENHANCEMENT: Better test suite with more relaxed console output expectation](https://github.com/fastruby/skunk/pull/117)
1314
* [ENHANCEMENT: Use minitest 5.22.x (also, stop testing with Ruby 2.4 and 2.5)](https://github.com/fastruby/skunk/pull/115)
1415

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ To only run skunk on specific folders, pass a list of directories in the command
123123

124124
`skunk app lib`
125125

126+
### Generate JSON report in background
127+
128+
When the Skunk command is run, it will generate a JSON report file in the `RubyCritic::Config.root` location.
129+
126130
### Comparing feature branches
127131

128132
When working on new features, run skunk against your feature branch to check if the changes are improving or decreasing the code quality of the project.

bin/console

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
# frozen_string_literal: true
2-
31
#!/usr/bin/env ruby
42

53
require "bundler/setup"
64
require "skunk"
75

8-
# You can add fixtures and/or initialization code here to make experimenting
9-
# with your gem easier. You can also use a different console, if you like.
6+
# use `debugger` to set a breakpoint
7+
require "debug"
108

11-
# (If you use this, don't forget to add pry to your Gemfile!)
12-
# require "pry"
13-
# Pry.start
9+
puts "Welcome to the Skunk console!"
10+
puts ARGV.inspect
1411

15-
require "irb"
16-
IRB.start(__FILE__)
12+
# Run skunk CLI application with the provided arguments
13+
require "skunk/cli/application"
14+
Skunk::Cli::Application.new(ARGV).execute

lib/skunk/commands/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Command
1010
# reporter object.
1111
class Base < RubyCritic::Command::Base
1212
def initialize(options)
13+
super
1314
@options = options
1415
@status_reporter = Skunk::Command::StatusReporter.new(@options)
1516
end

lib/skunk/commands/default.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
require "rubycritic/commands/default"
44
require "rubycritic/analysers_runner"
55
require "rubycritic/revision_comparator"
6-
require "rubycritic/reporter"
76

8-
require "skunk/commands/base"
97
require "skunk/commands/shareable"
108
require "skunk/commands/status_reporter"
9+
require "skunk/reporter"
1110

1211
module Skunk
1312
module Command
@@ -27,7 +26,7 @@ def initialize(options)
2726
#
2827
# @return [Skunk::Command::StatusReporter]
2928
def execute
30-
RubyCritic::Config.formats = []
29+
RubyCritic::Config.formats = [:json]
3130

3231
report(critique)
3332

@@ -39,6 +38,8 @@ def execute
3938
#
4039
# @param [RubyCritic::AnalysedModulesCollection] A collection of analysed modules
4140
def report(analysed_modules)
41+
Reporter.generate_report(analysed_modules)
42+
4243
status_reporter.analysed_modules = analysed_modules
4344
status_reporter.score = analysed_modules.score
4445
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
require "rubycritic/generators/json/simple"
4+
5+
module Skunk
6+
module Generator
7+
module Json
8+
# Generates a JSON report for the analysed modules.
9+
class Simple < RubyCritic::Generator::Json::Simple
10+
def data
11+
{
12+
analysed_modules_count: analysed_modules_count,
13+
skunk_score_average: skunk_score_average,
14+
skunk_score_total: skunk_score_total,
15+
worst_pathname: worst&.pathname,
16+
worst_score: worst&.skunk_score,
17+
files: files
18+
}
19+
end
20+
21+
private
22+
23+
def analysed_modules_count
24+
@analysed_modules_count ||= non_test_modules.count
25+
end
26+
27+
def skunk_score_average
28+
return 0 if analysed_modules_count.zero?
29+
30+
(skunk_score_total.to_d / analysed_modules_count).to_f.round(2)
31+
end
32+
33+
def skunk_score_total
34+
@skunk_score_total ||= non_test_modules.sum(&:skunk_score)
35+
end
36+
37+
def non_test_modules
38+
@non_test_modules ||= @analysed_modules.reject do |a_module|
39+
module_path = a_module.pathname.dirname.to_s
40+
module_path.start_with?("test", "spec") || module_path.end_with?("test", "spec")
41+
end
42+
end
43+
44+
def worst
45+
@worst ||= sorted_modules.first
46+
end
47+
48+
def sorted_modules
49+
@sorted_modules ||= non_test_modules.sort_by(&:skunk_score).reverse!
50+
end
51+
52+
def files
53+
@files ||= sorted_modules.map(&:to_hash)
54+
end
55+
end
56+
end
57+
end
58+
end

0 commit comments

Comments
 (0)