From f6f63c825670e01a9fc01853848990345c1612ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20V=C3=A1squez?= Date: Sun, 23 Jul 2023 07:33:09 -0600 Subject: [PATCH] Mimic Rubycritic Folder Structure (#112) * Mimic Rubycritic folder structure - [x] Move command_factory file - [x] Move commands folder * Update rubocop_todo file * Update .reek file --- .reek.yml | 7 +- .rubocop_todo.yml | 32 ++++----- README.md | 26 ++++---- lib/skunk/cli/application.rb | 6 +- lib/skunk/cli/command_factory.rb | 27 -------- lib/skunk/cli/commands/base.rb | 22 ------- lib/skunk/cli/commands/compare.rb | 66 ------------------- lib/skunk/cli/commands/compare_score.rb | 41 ------------ lib/skunk/cli/commands/default.rb | 49 -------------- lib/skunk/cli/commands/help.rb | 27 -------- lib/skunk/cli/commands/shareable.rb | 27 -------- lib/skunk/cli/commands/version.rb | 22 ------- lib/skunk/command_factory.rb | 25 +++++++ lib/skunk/commands/base.rb | 20 ++++++ lib/skunk/commands/compare.rb | 64 ++++++++++++++++++ lib/skunk/commands/compare_score.rb | 39 +++++++++++ lib/skunk/commands/default.rb | 47 +++++++++++++ lib/skunk/commands/help.rb | 25 +++++++ lib/skunk/{cli => }/commands/output.rb | 0 lib/skunk/commands/shareable.rb | 25 +++++++ .../{cli => }/commands/status_reporter.rb | 0 lib/skunk/{cli => }/commands/status_sharer.rb | 2 +- lib/skunk/commands/version.rb | 20 ++++++ .../{cli => }/commands/compare_score_test.rb | 14 ++-- .../skunk/{cli => }/commands/compare_test.rb | 8 +-- .../skunk/{cli => }/commands/default_test.rb | 6 +- .../lib/skunk/{cli => }/commands/help_test.rb | 6 +- .../commands/status_reporter_test.rb | 2 +- .../skunk/{cli => }/commands/version_test.rb | 6 +- 29 files changed, 323 insertions(+), 338 deletions(-) delete mode 100644 lib/skunk/cli/command_factory.rb delete mode 100644 lib/skunk/cli/commands/base.rb delete mode 100644 lib/skunk/cli/commands/compare.rb delete mode 100644 lib/skunk/cli/commands/compare_score.rb delete mode 100644 lib/skunk/cli/commands/default.rb delete mode 100644 lib/skunk/cli/commands/help.rb delete mode 100644 lib/skunk/cli/commands/shareable.rb delete mode 100644 lib/skunk/cli/commands/version.rb create mode 100644 lib/skunk/command_factory.rb create mode 100644 lib/skunk/commands/base.rb create mode 100644 lib/skunk/commands/compare.rb create mode 100644 lib/skunk/commands/compare_score.rb create mode 100644 lib/skunk/commands/default.rb create mode 100644 lib/skunk/commands/help.rb rename lib/skunk/{cli => }/commands/output.rb (100%) create mode 100644 lib/skunk/commands/shareable.rb rename lib/skunk/{cli => }/commands/status_reporter.rb (100%) rename lib/skunk/{cli => }/commands/status_sharer.rb (97%) create mode 100644 lib/skunk/commands/version.rb rename test/lib/skunk/{cli => }/commands/compare_score_test.rb (68%) rename test/lib/skunk/{cli => }/commands/compare_test.rb (79%) rename test/lib/skunk/{cli => }/commands/default_test.rb (73%) rename test/lib/skunk/{cli => }/commands/help_test.rb (85%) rename test/lib/skunk/{cli => }/commands/status_reporter_test.rb (97%) rename test/lib/skunk/{cli => }/commands/version_test.rb (76%) diff --git a/.reek.yml b/.reek.yml index c080fbf..5efc18b 100644 --- a/.reek.yml +++ b/.reek.yml @@ -24,6 +24,7 @@ detectors: - Skunk::Cli::Options::Argv#parse UtilityFunction: exclude: - - Skunk::Cli::Command::Compare#analyse_modified_files - - Skunk::Cli::Command::Compare#build_details_path - - Skunk::Cli::Command::Shareable#sharing? + - capture_output_streams + - Skunk::Command::Compare#analyse_modified_files + - Skunk::Command::Compare#build_details_path + - Skunk::Command::Shareable#sharing? diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index c2f3a3c..f9e2e39 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,57 +1,57 @@ # 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-07-22 00:31:00 UTC using RuboCop version 1.54.2. # 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 -# Configuration parameters: Include. +# Offense count: 1 +# Configuration parameters: Severity, Include. # Include: **/*.gemspec Gemspec/RequiredRubyVersion: Exclude: - - 'gemfiles/skunk.gemspec' - 'skunk.gemspec' # Offense count: 1 -# Cop supports --auto-correct. +# This cop supports safe autocorrection (--autocorrect). Layout/ClosingHeredocIndentation: Exclude: - - 'lib/skunk/cli/commands/status_reporter.rb' + - 'lib/skunk/commands/status_reporter.rb' # Offense count: 1 -# Cop supports --auto-correct. +# This cop supports safe autocorrection (--autocorrect). Layout/HeredocIndentation: Exclude: - - 'lib/skunk/cli/commands/status_reporter.rb' + - 'lib/skunk/commands/status_reporter.rb' # Offense count: 2 +# Configuration parameters: AllowedParentClasses. Lint/MissingSuper: Exclude: - 'lib/skunk/cli/application.rb' - - 'lib/skunk/cli/commands/base.rb' + - 'lib/skunk/commands/base.rb' # Offense count: 1 -# Configuration parameters: IgnoredMethods, CountRepeatedAttributes. +# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. Metrics/AbcSize: Max: 18 # Offense count: 7 -# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. -# IgnoredMethods: refine +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. +# AllowedMethods: refine Metrics/BlockLength: Max: 76 # Offense count: 2 -# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: Max: 13 # Offense count: 1 -# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers. +# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns. # SupportedStyles: snake_case, normalcase, non_integer -# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339 +# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64 Naming/VariableNumber: Exclude: - - 'lib/skunk/cli/commands/status_sharer.rb' + - 'lib/skunk/commands/status_sharer.rb' diff --git a/README.md b/README.md index 97cdf47..9933cea 100644 --- a/README.md +++ b/README.md @@ -83,22 +83,22 @@ The result is a list of smelly files and the SkunkScore of the project/files: | file | skunk_score | churn_times_cost | churn | cost | coverage | +-----------------------------------------------------+----------------+------------------+--------------+--------------+--------------+ | samples/rubycritic/analysed_module.rb | 258.88 | 12.94 | 5 | 2.59 | 0.0 | -| lib/skunk/cli/commands/compare.rb | 85.41 | 32.32 | 14 | 2.31 | 63.64 | +| lib/skunk/commands/compare.rb | 85.41 | 32.32 | 14 | 2.31 | 63.64 | | lib/skunk/rubycritic/analysed_modules_collection.rb | 31.76 | 3.18 | 5 | 0.64 | 50.0 | -| lib/skunk/cli/commands/status_reporter.rb | 11.33 | 68.0 | 18 | 3.78 | 97.5 | -| lib/skunk/cli/command_factory.rb | 8.3 | 1.95 | 4 | 0.49 | 83.33 | -| lib/skunk/cli/commands/status_sharer.rb | 8.17 | 10.9 | 4 | 2.72 | 97.67 | +| lib/skunk/commands/status_reporter.rb | 11.33 | 68.0 | 18 | 3.78 | 97.5 | +| lib/skunk/command_factory.rb | 8.3 | 1.95 | 4 | 0.49 | 83.33 | +| lib/skunk/commands/status_sharer.rb | 8.17 | 10.9 | 4 | 2.72 | 97.67 | | lib/skunk/cli/application.rb | 7.06 | 21.19 | 18 | 1.18 | 94.12 | | lib/skunk/cli/options/argv.rb | 4.08 | 7.35 | 9 | 0.82 | 95.24 | -| lib/skunk/cli/commands/compare_score.rb | 3.77 | 2.51 | 4 | 0.63 | 94.74 | +| lib/skunk/commands/compare_score.rb | 3.77 | 2.51 | 4 | 0.63 | 94.74 | | lib/skunk/rubycritic/analysed_module.rb | 3.37 | 33.74 | 10 | 3.37 | 100.0 | -| lib/skunk/cli/commands/version.rb | 2.64 | 0.7 | 8 | 0.09 | 70.0 | -| lib/skunk/cli/commands/output.rb | 1.86 | 0.09 | 1 | 0.09 | 80.0 | +| lib/skunk/commands/version.rb | 2.64 | 0.7 | 8 | 0.09 | 70.0 | +| lib/skunk/commands/output.rb | 1.86 | 0.09 | 1 | 0.09 | 80.0 | | lib/skunk/cli/options.rb | 0.68 | 5.44 | 8 | 0.68 | 100.0 | -| lib/skunk/cli/commands/default.rb | 0.4 | 3.23 | 8 | 0.4 | 100.0 | -| lib/skunk/cli/commands/shareable.rb | 0.2 | 0.4 | 2 | 0.2 | 100.0 | -| lib/skunk/cli/commands/help.rb | 0.2 | 1.2 | 6 | 0.2 | 100.0 | -| lib/skunk/cli/commands/base.rb | 0.1 | 0.49 | 5 | 0.1 | 100.0 | +| lib/skunk/commands/default.rb | 0.4 | 3.23 | 8 | 0.4 | 100.0 | +| lib/skunk/commands/shareable.rb | 0.2 | 0.4 | 2 | 0.2 | 100.0 | +| lib/skunk/commands/help.rb | 0.2 | 1.2 | 6 | 0.2 | 100.0 | +| lib/skunk/commands/base.rb | 0.1 | 0.49 | 5 | 0.1 | 100.0 | | lib/skunk.rb | 0.0 | 0.0 | 6 | 0.0 | 100.0 | | lib/skunk/version.rb | 0.0 | 0.0 | 12 | 0.0 | 0.0 | +-----------------------------------------------------+----------------+------------------+--------------+--------------+--------------+ @@ -158,7 +158,7 @@ If you want to share the results of your Skunk report with the Ruby community, r SkunkScore Total: 126.99 Modules Analysed: 17 SkunkScore Average: 7.47 -Worst SkunkScore: 41.92 (lib/skunk/cli/commands/status_sharer.rb) +Worst SkunkScore: 41.92 (lib/skunk/commands/status_sharer.rb) Generated with Skunk v0.5.0 Shared at: https://skunk.fastruby.io/ @@ -174,7 +174,7 @@ If you prefer to post results to your server, set your own `SHARE_URL`: SkunkScore Total: 126.99 Modules Analysed: 17 SkunkScore Average: 7.47 -Worst SkunkScore: 41.92 (lib/skunk/cli/commands/status_sharer.rb) +Worst SkunkScore: 41.92 (lib/skunk/commands/status_sharer.rb) Generated with Skunk v0.5.0 Shared at: https://path.to.your.skunk-fyi-server.example.com diff --git a/lib/skunk/cli/application.rb b/lib/skunk/cli/application.rb index 138647b..ba915bc 100644 --- a/lib/skunk/cli/application.rb +++ b/lib/skunk/cli/application.rb @@ -6,8 +6,8 @@ require "skunk" require "skunk/rubycritic/analysed_module" require "skunk/cli/options" -require "skunk/cli/command_factory" -require "skunk/cli/commands/status_sharer" +require "skunk/command_factory" +require "skunk/commands/status_sharer" module Skunk module Cli @@ -26,7 +26,7 @@ def execute # :reek:NilCheck @parsed_options = @options.parse.to_h - command = Skunk::Cli::CommandFactory.create(@parsed_options) + command = Skunk::CommandFactory.create(@parsed_options) reporter = command.execute print(reporter.status_message) diff --git a/lib/skunk/cli/command_factory.rb b/lib/skunk/cli/command_factory.rb deleted file mode 100644 index 6037299..0000000 --- a/lib/skunk/cli/command_factory.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -require "rubycritic/command_factory" - -module Skunk - module Cli - # Knows how to calculate the command that was request by the CLI user - class CommandFactory < RubyCritic::CommandFactory - COMMAND_CLASS_MODES = %i[version help default compare].freeze - - # Returns the command class based on the command that was executed - # - # @param mode - # @return [Class] - def self.command_class(mode) - mode = mode.to_s.split("_").first.to_sym - if COMMAND_CLASS_MODES.include? mode - require "skunk/cli/commands/#{mode}" - Command.const_get(mode.capitalize) - else - require "skunk/cli/commands/default" - Command::Default - end - end - end - end -end diff --git a/lib/skunk/cli/commands/base.rb b/lib/skunk/cli/commands/base.rb deleted file mode 100644 index 1fa5913..0000000 --- a/lib/skunk/cli/commands/base.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -require "rubycritic/commands/base" -require "skunk/cli/commands/status_reporter" - -module Skunk - module Cli - module Command - # Base class for `Skunk` commands. It knows how to build a command with - # options. It always uses a [Skunk::Command::StatusReporter] as its - # reporter object. - class Base < RubyCritic::Command::Base - def initialize(options) - @options = options - @status_reporter = Skunk::Command::StatusReporter.new(@options) - end - - def share(_); end - end - end - end -end diff --git a/lib/skunk/cli/commands/compare.rb b/lib/skunk/cli/commands/compare.rb deleted file mode 100644 index 5243fc0..0000000 --- a/lib/skunk/cli/commands/compare.rb +++ /dev/null @@ -1,66 +0,0 @@ -# frozen_string_literal: true - -require "rubycritic/commands/compare" -require "skunk/rubycritic/analysed_modules_collection" -require "skunk/cli/commands/output" -require "skunk/cli/commands/shareable" -require "skunk/cli/commands/compare_score" - -# nodoc # -module Skunk - module Cli - module Command - # Knows how to compare two branches and their skunk score average - class Compare < RubyCritic::Command::Compare - include Skunk::Cli::Command::Shareable - - def initialize(options) - super - @options = options - @status_reporter = Skunk::Command::StatusReporter.new(options) - end - - def execute - compare_branches - status_reporter - end - - # switch branch and analyse files but don't generate a report - def analyse_branch(branch) - ::RubyCritic::SourceControlSystem::Git.switch_branch(::RubyCritic::Config.send(branch)) - critic = critique(branch) - ::RubyCritic::Config.send(:"#{branch}_score=", critic.skunk_score_average) - ::RubyCritic::Config.root = branch_directory(branch) - end - - # generate report only for modified files but don't report it - def analyse_modified_files - modified_files = ::RubyCritic::Config - .feature_branch_collection - .where(::RubyCritic::SourceControlSystem::Git.modified_files) - ::RubyCritic::AnalysedModulesCollection.new(modified_files.map(&:path), - modified_files) - ::RubyCritic::Config.root = "#{::RubyCritic::Config.root}/compare" - end - - # create a txt file with the branch score details - def build_details - details = CompareScore.new( - ::RubyCritic::Config.base_branch, - ::RubyCritic::Config.feature_branch, - ::RubyCritic::Config.base_branch_score.to_f.round(2), - ::RubyCritic::Config.feature_branch_score.to_f.round(2) - ).message - - Skunk::Command::Output.create_directory(::RubyCritic::Config.compare_root_directory) - File.open(build_details_path, "w") { |file| file.write(details) } - puts details - end - - def build_details_path - "#{::RubyCritic::Config.compare_root_directory}/build_details.txt" - end - end - end - end -end diff --git a/lib/skunk/cli/commands/compare_score.rb b/lib/skunk/cli/commands/compare_score.rb deleted file mode 100644 index 7ec8d70..0000000 --- a/lib/skunk/cli/commands/compare_score.rb +++ /dev/null @@ -1,41 +0,0 @@ -# frozen_string_literal: true - -# nodoc # -module Skunk - module Cli - module Command - # Knows how to describe score evolution between two branches - class CompareScore - def initialize(base_branch, feature_branch, base_branch_score, feature_branch_score) - @base_branch = base_branch - @feature_branch = feature_branch - @base_branch_score = base_branch_score - @feature_branch_score = feature_branch_score - end - - def message - "Base branch (#{@base_branch}) "\ - "average skunk score: #{@base_branch_score} \n"\ - "Feature branch (#{@feature_branch}) "\ - "average skunk score: #{@feature_branch_score} \n"\ - "#{score_evolution_message}" - end - - def score_evolution_message - "Skunk score average is #{score_evolution} #{score_evolution_appreciation} \n" - end - - def score_evolution_appreciation - @feature_branch_score > @base_branch_score ? "worse" : "better" - end - - def score_evolution - return "Infinitely" if @base_branch_score.zero? - - precentage = (100 * (@base_branch_score - @feature_branch_score) / @base_branch_score) - "#{precentage.round(0).abs}%" - end - end - end - end -end diff --git a/lib/skunk/cli/commands/default.rb b/lib/skunk/cli/commands/default.rb deleted file mode 100644 index bc9b987..0000000 --- a/lib/skunk/cli/commands/default.rb +++ /dev/null @@ -1,49 +0,0 @@ -# frozen_string_literal: true - -require "rubycritic/commands/default" -require "rubycritic/analysers_runner" -require "rubycritic/revision_comparator" -require "rubycritic/reporter" - -require "skunk/cli/commands/base" -require "skunk/cli/commands/shareable" -require "skunk/cli/commands/status_reporter" - -module Skunk - module Cli - module Command - # Default command runs a critique using RubyCritic and uses - # Skunk::Command::StatusReporter to report status - class Default < RubyCritic::Command::Default - include Skunk::Cli::Command::Shareable - - def initialize(options) - super - @options = options - @status_reporter = Skunk::Command::StatusReporter.new(options) - end - - # It generates a report and it returns an instance of - # Skunk::Command::StatusReporter - # - # @return [Skunk::Command::StatusReporter] - def execute - RubyCritic::Config.formats = [] - - report(critique) - - status_reporter - end - - # It connects the Skunk::Command::StatusReporter with the collection - # of analysed modules. - # - # @param [RubyCritic::AnalysedModulesCollection] A collection of analysed modules - def report(analysed_modules) - status_reporter.analysed_modules = analysed_modules - status_reporter.score = analysed_modules.score - end - end - end - end -end diff --git a/lib/skunk/cli/commands/help.rb b/lib/skunk/cli/commands/help.rb deleted file mode 100644 index 3a7bbdb..0000000 --- a/lib/skunk/cli/commands/help.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -require "skunk/cli/commands/base" -require "rubycritic/commands/help" - -module Skunk - module Cli - module Command - # Knows how to guide user into using `skunk` properly - class Help < Skunk::Cli::Command::Base - # Outputs a help message - def execute - puts options[:help_text] - status_reporter - end - - def sharing? - false - end - - private - - attr_reader :options, :status_reporter - end - end - end -end diff --git a/lib/skunk/cli/commands/shareable.rb b/lib/skunk/cli/commands/shareable.rb deleted file mode 100644 index 9459398..0000000 --- a/lib/skunk/cli/commands/shareable.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -module Skunk - module Cli - module Command - # This is a module that will be used for sharing reports to a server - module Shareable - # It shares the report using SHARE_URL or https://skunk.fastruby.io. It - # will post all results in JSON format and return a status message. - # - # @param [Skunk::Command::StatusReporter] A status reporter with analysed modules - # :reek:FeatureEnvy - def share(reporter) - sharer = Skunk::Command::StatusSharer.new(@options) - sharer.status_reporter = reporter - sharer.share - end - - # @return [Boolean] If the environment is set to share to an external - # service - def sharing? - ENV["SHARE"] == "true" - end - end - end - end -end diff --git a/lib/skunk/cli/commands/version.rb b/lib/skunk/cli/commands/version.rb deleted file mode 100644 index 844e93b..0000000 --- a/lib/skunk/cli/commands/version.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -require "rubycritic/commands/version" - -# nodoc # -module Skunk - module Cli - module Command - # Shows skunk version - class Version < RubyCritic::Command::Version - def execute - print Skunk::VERSION - status_reporter - end - - def sharing? - false - end - end - end - end -end diff --git a/lib/skunk/command_factory.rb b/lib/skunk/command_factory.rb new file mode 100644 index 0000000..5dddf01 --- /dev/null +++ b/lib/skunk/command_factory.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "rubycritic/command_factory" + +module Skunk + # Knows how to calculate the command that was request by the CLI user + class CommandFactory < RubyCritic::CommandFactory + COMMAND_CLASS_MODES = %i[version help default compare].freeze + + # Returns the command class based on the command that was executed + # + # @param mode + # @return [Class] + def self.command_class(mode) + mode = mode.to_s.split("_").first.to_sym + if COMMAND_CLASS_MODES.include? mode + require "skunk/commands/#{mode}" + Command.const_get(mode.capitalize) + else + require "skunk/commands/default" + Command::Default + end + end + end +end diff --git a/lib/skunk/commands/base.rb b/lib/skunk/commands/base.rb new file mode 100644 index 0000000..2d6e1df --- /dev/null +++ b/lib/skunk/commands/base.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "rubycritic/commands/base" +require "skunk/commands/status_reporter" + +module Skunk + module Command + # Base class for `Skunk` commands. It knows how to build a command with + # options. It always uses a [Skunk::Command::StatusReporter] as its + # reporter object. + class Base < RubyCritic::Command::Base + def initialize(options) + @options = options + @status_reporter = Skunk::Command::StatusReporter.new(@options) + end + + def share(_); end + end + end +end diff --git a/lib/skunk/commands/compare.rb b/lib/skunk/commands/compare.rb new file mode 100644 index 0000000..46cc4d9 --- /dev/null +++ b/lib/skunk/commands/compare.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +require "rubycritic/commands/compare" +require "skunk/rubycritic/analysed_modules_collection" +require "skunk/commands/output" +require "skunk/commands/shareable" +require "skunk/commands/compare_score" + +# nodoc # +module Skunk + module Command + # Knows how to compare two branches and their skunk score average + class Compare < RubyCritic::Command::Compare + include Skunk::Command::Shareable + + def initialize(options) + super + @options = options + @status_reporter = Skunk::Command::StatusReporter.new(options) + end + + def execute + compare_branches + status_reporter + end + + # switch branch and analyse files but don't generate a report + def analyse_branch(branch) + ::RubyCritic::SourceControlSystem::Git.switch_branch(::RubyCritic::Config.send(branch)) + critic = critique(branch) + ::RubyCritic::Config.send(:"#{branch}_score=", critic.skunk_score_average) + ::RubyCritic::Config.root = branch_directory(branch) + end + + # generate report only for modified files but don't report it + def analyse_modified_files + modified_files = ::RubyCritic::Config + .feature_branch_collection + .where(::RubyCritic::SourceControlSystem::Git.modified_files) + ::RubyCritic::AnalysedModulesCollection.new(modified_files.map(&:path), + modified_files) + ::RubyCritic::Config.root = "#{::RubyCritic::Config.root}/compare" + end + + # create a txt file with the branch score details + def build_details + details = CompareScore.new( + ::RubyCritic::Config.base_branch, + ::RubyCritic::Config.feature_branch, + ::RubyCritic::Config.base_branch_score.to_f.round(2), + ::RubyCritic::Config.feature_branch_score.to_f.round(2) + ).message + + Skunk::Command::Output.create_directory(::RubyCritic::Config.compare_root_directory) + File.open(build_details_path, "w") { |file| file.write(details) } + puts details + end + + def build_details_path + "#{::RubyCritic::Config.compare_root_directory}/build_details.txt" + end + end + end +end diff --git a/lib/skunk/commands/compare_score.rb b/lib/skunk/commands/compare_score.rb new file mode 100644 index 0000000..c5e8b78 --- /dev/null +++ b/lib/skunk/commands/compare_score.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +# nodoc # +module Skunk + module Command + # Knows how to describe score evolution between two branches + class CompareScore + def initialize(base_branch, feature_branch, base_branch_score, feature_branch_score) + @base_branch = base_branch + @feature_branch = feature_branch + @base_branch_score = base_branch_score + @feature_branch_score = feature_branch_score + end + + def message + "Base branch (#{@base_branch}) "\ + "average skunk score: #{@base_branch_score} \n"\ + "Feature branch (#{@feature_branch}) "\ + "average skunk score: #{@feature_branch_score} \n"\ + "#{score_evolution_message}" + end + + def score_evolution_message + "Skunk score average is #{score_evolution} #{score_evolution_appreciation} \n" + end + + def score_evolution_appreciation + @feature_branch_score > @base_branch_score ? "worse" : "better" + end + + def score_evolution + return "Infinitely" if @base_branch_score.zero? + + precentage = (100 * (@base_branch_score - @feature_branch_score) / @base_branch_score) + "#{precentage.round(0).abs}%" + end + end + end +end diff --git a/lib/skunk/commands/default.rb b/lib/skunk/commands/default.rb new file mode 100644 index 0000000..796973f --- /dev/null +++ b/lib/skunk/commands/default.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require "rubycritic/commands/default" +require "rubycritic/analysers_runner" +require "rubycritic/revision_comparator" +require "rubycritic/reporter" + +require "skunk/commands/base" +require "skunk/commands/shareable" +require "skunk/commands/status_reporter" + +module Skunk + module Command + # Default command runs a critique using RubyCritic and uses + # Skunk::Command::StatusReporter to report status + class Default < RubyCritic::Command::Default + include Skunk::Command::Shareable + + def initialize(options) + super + @options = options + @status_reporter = Skunk::Command::StatusReporter.new(options) + end + + # It generates a report and it returns an instance of + # Skunk::Command::StatusReporter + # + # @return [Skunk::Command::StatusReporter] + def execute + RubyCritic::Config.formats = [] + + report(critique) + + status_reporter + end + + # It connects the Skunk::Command::StatusReporter with the collection + # of analysed modules. + # + # @param [RubyCritic::AnalysedModulesCollection] A collection of analysed modules + def report(analysed_modules) + status_reporter.analysed_modules = analysed_modules + status_reporter.score = analysed_modules.score + end + end + end +end diff --git a/lib/skunk/commands/help.rb b/lib/skunk/commands/help.rb new file mode 100644 index 0000000..2f87ae5 --- /dev/null +++ b/lib/skunk/commands/help.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "skunk/commands/base" +require "rubycritic/commands/help" + +module Skunk + module Command + # Knows how to guide user into using `skunk` properly + class Help < Skunk::Command::Base + # Outputs a help message + def execute + puts options[:help_text] + status_reporter + end + + def sharing? + false + end + + private + + attr_reader :options, :status_reporter + end + end +end diff --git a/lib/skunk/cli/commands/output.rb b/lib/skunk/commands/output.rb similarity index 100% rename from lib/skunk/cli/commands/output.rb rename to lib/skunk/commands/output.rb diff --git a/lib/skunk/commands/shareable.rb b/lib/skunk/commands/shareable.rb new file mode 100644 index 0000000..29fe97e --- /dev/null +++ b/lib/skunk/commands/shareable.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module Skunk + module Command + # This is a module that will be used for sharing reports to a server + module Shareable + # It shares the report using SHARE_URL or https://skunk.fastruby.io. It + # will post all results in JSON format and return a status message. + # + # @param [Skunk::Command::StatusReporter] A status reporter with analysed modules + # :reek:FeatureEnvy + def share(reporter) + sharer = Skunk::Command::StatusSharer.new(@options) + sharer.status_reporter = reporter + sharer.share + end + + # @return [Boolean] If the environment is set to share to an external + # service + def sharing? + ENV["SHARE"] == "true" + end + end + end +end diff --git a/lib/skunk/cli/commands/status_reporter.rb b/lib/skunk/commands/status_reporter.rb similarity index 100% rename from lib/skunk/cli/commands/status_reporter.rb rename to lib/skunk/commands/status_reporter.rb diff --git a/lib/skunk/cli/commands/status_sharer.rb b/lib/skunk/commands/status_sharer.rb similarity index 97% rename from lib/skunk/cli/commands/status_sharer.rb rename to lib/skunk/commands/status_sharer.rb index af480d0..596bc9e 100644 --- a/lib/skunk/cli/commands/status_sharer.rb +++ b/lib/skunk/commands/status_sharer.rb @@ -4,7 +4,7 @@ require "net/https" require "json" -require "skunk/cli/commands/status_reporter" +require "skunk/commands/status_reporter" module Skunk module Command diff --git a/lib/skunk/commands/version.rb b/lib/skunk/commands/version.rb new file mode 100644 index 0000000..4a0f0a9 --- /dev/null +++ b/lib/skunk/commands/version.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "rubycritic/commands/version" + +# nodoc # +module Skunk + module Command + # Shows skunk version + class Version < RubyCritic::Command::Version + def execute + print Skunk::VERSION + status_reporter + end + + def sharing? + false + end + end + end +end diff --git a/test/lib/skunk/cli/commands/compare_score_test.rb b/test/lib/skunk/commands/compare_score_test.rb similarity index 68% rename from test/lib/skunk/cli/commands/compare_score_test.rb rename to test/lib/skunk/commands/compare_score_test.rb index ec37a74..461b079 100644 --- a/test/lib/skunk/cli/commands/compare_score_test.rb +++ b/test/lib/skunk/commands/compare_score_test.rb @@ -2,39 +2,39 @@ require "test_helper" -require "skunk/cli/commands/compare_score" +require "skunk/commands/compare_score" -describe Skunk::Cli::Command::CompareScore do +describe Skunk::Command::CompareScore do describe "#message" do it "outputs comparaison message" do expected = "Base branch (main) average skunk score: 10 \n"\ "Feature branch (feature) average skunk score: 2 \n"\ "Skunk score average is 80% better \n" - compare_score = Skunk::Cli::Command::CompareScore.new("main", "feature", 10, 2) + compare_score = Skunk::Command::CompareScore.new("main", "feature", 10, 2) compare_score.message.must_equal expected end end describe "#score_evolution_message" do it "computes skunk score_evolution_message with negative impact" do - compare_score = Skunk::Cli::Command::CompareScore.new("main", "feature", 10.1, 12.1) + compare_score = Skunk::Command::CompareScore.new("main", "feature", 10.1, 12.1) compare_score.score_evolution_message.must_equal "Skunk score average is 20% worse \n" end it "computes skunk score_evolution_message with positive impact" do - compare_score = Skunk::Cli::Command::CompareScore.new("main", "feature", 12.1, 8) + compare_score = Skunk::Command::CompareScore.new("main", "feature", 12.1, 8) compare_score.score_evolution_message.must_equal "Skunk score average is 34% better \n" end it "computes skunk score_evolution_message when base_branch_score is 0" do - compare_score = Skunk::Cli::Command::CompareScore.new("main", "feature", 0, 2) + compare_score = Skunk::Command::CompareScore.new("main", "feature", 0, 2) compare_score .score_evolution_message .must_equal "Skunk score average is Infinitely worse \n" end it "computes skunk score_evolution_message when feature_branch_score is 0" do - compare_score = Skunk::Cli::Command::CompareScore.new("main", "feature", 10, 0) + compare_score = Skunk::Command::CompareScore.new("main", "feature", 10, 0) compare_score.score_evolution_message.must_equal "Skunk score average is 100% better \n" end end diff --git a/test/lib/skunk/cli/commands/compare_test.rb b/test/lib/skunk/commands/compare_test.rb similarity index 79% rename from test/lib/skunk/cli/commands/compare_test.rb rename to test/lib/skunk/commands/compare_test.rb index cc820f5..4f8e239 100644 --- a/test/lib/skunk/cli/commands/compare_test.rb +++ b/test/lib/skunk/commands/compare_test.rb @@ -4,9 +4,9 @@ require "minitest/stub_const" require "skunk/rubycritic/analysed_module" -require "skunk/cli/commands/compare" +require "skunk/commands/compare" -describe Skunk::Cli::Command::Compare do +describe Skunk::Command::Compare do let(:paths) { "samples/rubycritic" } let(:compare_root_directory) { "test/support/tmp/rubycritic/compare/" } @@ -18,7 +18,7 @@ it "sets the skunk_score_average as the branch score" do ::RubyCritic::SourceControlSystem::Git.stub :switch_branch, nil do - compare = Skunk::Cli::Command::Compare.new(paths: "samples/rubycritic") + compare = Skunk::Command::Compare.new(paths: "samples/rubycritic") compare.analyse_branch(:base_branch) _(RubyCritic::Config.base_branch_score).must_equal 58.88 @@ -28,7 +28,7 @@ it "creates the compare_root_directory if it doesn't exist" do ::RubyCritic::Config.configuration.stub(:compare_root_directory, compare_root_directory) do - Skunk::Cli::Command::Compare.new(paths: "samples/rubycritic").build_details + Skunk::Command::Compare.new(paths: "samples/rubycritic").build_details _(File.exist?(compare_root_directory)).must_equal true end end diff --git a/test/lib/skunk/cli/commands/default_test.rb b/test/lib/skunk/commands/default_test.rb similarity index 73% rename from test/lib/skunk/cli/commands/default_test.rb rename to test/lib/skunk/commands/default_test.rb index 14da362..3b77532 100644 --- a/test/lib/skunk/cli/commands/default_test.rb +++ b/test/lib/skunk/commands/default_test.rb @@ -3,11 +3,11 @@ require "test_helper" require "minitest/stub_const" -require "skunk/cli/commands/default" +require "skunk/commands/default" -describe Skunk::Cli::Command::Default do +describe Skunk::Command::Default do describe "#sharing?" do - let(:subject) { Skunk::Cli::Command::Default.new({}) } + let(:subject) { Skunk::Command::Default.new({}) } it "returns true" do env = ENV.to_hash.merge("SHARE" => "true") diff --git a/test/lib/skunk/cli/commands/help_test.rb b/test/lib/skunk/commands/help_test.rb similarity index 85% rename from test/lib/skunk/cli/commands/help_test.rb rename to test/lib/skunk/commands/help_test.rb index 0a3505d..994118c 100644 --- a/test/lib/skunk/cli/commands/help_test.rb +++ b/test/lib/skunk/commands/help_test.rb @@ -2,10 +2,10 @@ require "test_helper" -require "skunk/cli/commands/help" +require "skunk/commands/help" require "skunk/cli/options" -describe Skunk::Cli::Command::Help do +describe Skunk::Command::Help do describe "#execute" do let(:msg) do <<~HELP @@ -18,7 +18,7 @@ end let(:options) { ["--help"] } let(:opts) { Skunk::Cli::Options.new(options).parse } - let(:subject) { Skunk::Cli::Command::Help.new(opts.to_h) } + let(:subject) { Skunk::Command::Help.new(opts.to_h) } it "outputs the right help message" do assert_output(msg) do diff --git a/test/lib/skunk/cli/commands/status_reporter_test.rb b/test/lib/skunk/commands/status_reporter_test.rb similarity index 97% rename from test/lib/skunk/cli/commands/status_reporter_test.rb rename to test/lib/skunk/commands/status_reporter_test.rb index ad73997..92302d9 100644 --- a/test/lib/skunk/cli/commands/status_reporter_test.rb +++ b/test/lib/skunk/commands/status_reporter_test.rb @@ -3,7 +3,7 @@ require "test_helper" require "rubycritic/analysers_runner" -require "skunk/cli/commands/status_reporter" +require "skunk/commands/status_reporter" describe Skunk::Command::StatusReporter do let(:paths) { "samples/rubycritic" } diff --git a/test/lib/skunk/cli/commands/version_test.rb b/test/lib/skunk/commands/version_test.rb similarity index 76% rename from test/lib/skunk/cli/commands/version_test.rb rename to test/lib/skunk/commands/version_test.rb index b75fc50..4db208f 100644 --- a/test/lib/skunk/cli/commands/version_test.rb +++ b/test/lib/skunk/commands/version_test.rb @@ -2,15 +2,15 @@ require "test_helper" -require "skunk/cli/commands/version" +require "skunk/commands/version" require "skunk/cli/options" -describe Skunk::Cli::Command::Version do +describe Skunk::Command::Version do describe "#execute" do let(:msg) { Skunk::VERSION } let(:options) { ["--version"] } let(:opts) { Skunk::Cli::Options.new(options).parse } - let(:subject) { Skunk::Cli::Command::Version.new(opts.to_h) } + let(:subject) { Skunk::Command::Version.new(opts.to_h) } it "outputs the right version message" do assert_output(msg) do