From e956db2a10b5909efaf3616a378ecef295499e2c Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Mon, 6 Feb 2023 20:30:02 -0600 Subject: [PATCH] Move console report As first step we need to move the console report away of the status reporter class. Related https://github.com/fastruby/skunk/issues/50 --- .github/workflows/main.yml | 4 +- .rubocop_todo.yml | 9 +-- lib/skunk/cli/commands/status_reporter.rb | 84 +------------------- lib/skunk/cli/commands/status_sharer.rb | 1 + lib/skunk/cli/generators/console_report.rb | 92 ++++++++++++++++++++++ 5 files changed, 101 insertions(+), 89 deletions(-) create mode 100644 lib/skunk/cli/generators/console_report.rb diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f38b272..ce5bfa6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,8 +6,8 @@ on: branches: - main pull_request: - branches: - - main + # branches: + # - main jobs: test-ruby-2-4-x: diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index c2f3a3c..f5b5034 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,30 +1,29 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2021-08-31 01:33:08 UTC using RuboCop version 1.9.1. +# on 2023-02-07 02:28:48 UTC using RuboCop version 1.24.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 2 +# Offense count: 1 # Configuration parameters: Include. # Include: **/*.gemspec Gemspec/RequiredRubyVersion: Exclude: - - 'gemfiles/skunk.gemspec' - 'skunk.gemspec' # Offense count: 1 # Cop supports --auto-correct. Layout/ClosingHeredocIndentation: Exclude: - - 'lib/skunk/cli/commands/status_reporter.rb' + - 'lib/skunk/cli/generators/console_report.rb' # Offense count: 1 # Cop supports --auto-correct. Layout/HeredocIndentation: Exclude: - - 'lib/skunk/cli/commands/status_reporter.rb' + - 'lib/skunk/cli/generators/console_report.rb' # Offense count: 2 Lint/MissingSuper: diff --git a/lib/skunk/cli/commands/status_reporter.rb b/lib/skunk/cli/commands/status_reporter.rb index be43eef..2979ac7 100644 --- a/lib/skunk/cli/commands/status_reporter.rb +++ b/lib/skunk/cli/commands/status_reporter.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true -require "erb" require "rubycritic/commands/status_reporter" -require "terminal-table" +require "skunk/cli/generators/console_report" module Skunk module Command @@ -10,87 +9,8 @@ module Command class StatusReporter < RubyCritic::Command::StatusReporter attr_accessor :analysed_modules - HEADINGS = %w[file skunk_score churn_times_cost churn cost coverage].freeze - HEADINGS_WITHOUT_FILE = HEADINGS - %w[file] - HEADINGS_WITHOUT_FILE_WIDTH = HEADINGS_WITHOUT_FILE.size * 17 # padding - - TEMPLATE = ERB.new(<<-TEMPL -<%= _ttable %>\n -SkunkScore Total: <%= total_skunk_score %> -Modules Analysed: <%= analysed_modules_count %> -SkunkScore Average: <%= skunk_score_average %> -<% if worst %>Worst SkunkScore: <%= worst.skunk_score %> (<%= worst.pathname %>)<% end %> - -Generated with Skunk v<%= Skunk::VERSION %> -TEMPL - ) - - # Returns a status message with a table of all analysed_modules and - # a skunk score average def update_status_message - opts = table_options.merge(headings: HEADINGS, rows: table) - - _ttable = Terminal::Table.new(opts) - - @status_message = TEMPLATE.result(binding) - end - - private - - def analysed_modules_count - @analysed_modules_count ||= non_test_modules.count - end - - def non_test_modules - @non_test_modules ||= analysed_modules.reject do |a_module| - module_path = a_module.pathname.dirname.to_s - module_path.start_with?("test", "spec") || module_path.end_with?("test", "spec") - end - end - - def worst - @worst ||= sorted_modules.first - end - - def sorted_modules - @sorted_modules ||= non_test_modules.sort_by(&:skunk_score).reverse! - end - - def total_skunk_score - @total_skunk_score ||= non_test_modules.sum(&:skunk_score) - end - - def total_churn_times_cost - non_test_modules.sum(&:churn_times_cost) - end - - def skunk_score_average - return 0 if analysed_modules_count.zero? - - (total_skunk_score.to_d / analysed_modules_count).to_f.round(2) - end - - def table_options - max = sorted_modules.max_by { |a_mod| a_mod.pathname.to_s.length } - width = max.pathname.to_s.length + HEADINGS_WITHOUT_FILE_WIDTH - { - style: { - width: width - } - } - end - - def table - sorted_modules.map do |a_mod| - [ - a_mod.pathname, - a_mod.skunk_score, - a_mod.churn_times_cost, - a_mod.churn, - a_mod.cost.round(2), - a_mod.coverage.round(2) - ] - end + Skunk::Generator::ConsoleReport.new(analysed_modules).generate_report end end end diff --git a/lib/skunk/cli/commands/status_sharer.rb b/lib/skunk/cli/commands/status_sharer.rb index af480d0..b0e36c5 100644 --- a/lib/skunk/cli/commands/status_sharer.rb +++ b/lib/skunk/cli/commands/status_sharer.rb @@ -5,6 +5,7 @@ require "json" require "skunk/cli/commands/status_reporter" +require "skunk/cli/generators/console_report" module Skunk module Command diff --git a/lib/skunk/cli/generators/console_report.rb b/lib/skunk/cli/generators/console_report.rb new file mode 100644 index 0000000..b650e2e --- /dev/null +++ b/lib/skunk/cli/generators/console_report.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +require "erb" +require "terminal-table" +require "rubycritic/generators/console_report" + +module Skunk + module Generator + # Returns a status message with a table of all analysed_modules and + # a skunk score average + class ConsoleReport < RubyCritic::Generator::ConsoleReport + HEADINGS = %w[file skunk_score churn_times_cost churn cost coverage].freeze + HEADINGS_WITHOUT_FILE = HEADINGS - %w[file] + HEADINGS_WITHOUT_FILE_WIDTH = HEADINGS_WITHOUT_FILE.size * 17 # padding + + TEMPLATE = ERB.new(<<-TEMPL +<%= _ttable %>\n +SkunkScore Total: <%= total_skunk_score %> +Modules Analysed: <%= analysed_modules_count %> +SkunkScore Average: <%= skunk_score_average %> +<% if worst %>Worst SkunkScore: <%= worst.skunk_score %> (<%= worst.pathname %>)<% end %> + +Generated with Skunk v<%= Skunk::VERSION %> +TEMPL + ) + + def generate_report + opts = table_options.merge(headings: HEADINGS, rows: table) + _ttable = Terminal::Table.new(opts) + TEMPLATE.result(binding) + end + + private + + def analysed_modules_count + @analysed_modules_count ||= non_test_modules.count + end + + def non_test_modules + @non_test_modules ||= @analysed_modules.reject do |a_module| + module_path = a_module.pathname.dirname.to_s + module_path.start_with?("test", "spec") || module_path.end_with?("test", "spec") + end + end + + def worst + @worst ||= sorted_modules.first + end + + def sorted_modules + @sorted_modules ||= non_test_modules.sort_by(&:skunk_score).reverse! + end + + def total_skunk_score + @total_skunk_score ||= non_test_modules.sum(&:skunk_score) + end + + def total_churn_times_cost + non_test_modules.sum(&:churn_times_cost) + end + + def skunk_score_average + return 0 if analysed_modules_count.zero? + + (total_skunk_score.to_d / analysed_modules_count).to_f.round(2) + end + + def table_options + max = sorted_modules.max_by { |a_mod| a_mod.pathname.to_s.length } + width = max.pathname.to_s.length + HEADINGS_WITHOUT_FILE_WIDTH + { + style: { + width: width + } + } + end + + def table + sorted_modules.map do |a_mod| + [ + a_mod.pathname, + a_mod.skunk_score, + a_mod.churn_times_cost, + a_mod.churn, + a_mod.cost.round(2), + a_mod.coverage.round(2) + ] + end + end + end + end +end