From 7802018fa8655c066bcb267cd9a8544dd2351069 Mon Sep 17 00:00:00 2001 From: Andrew Crump Date: Sun, 22 Jan 2012 13:10:11 +0000 Subject: [PATCH] Better separation of concerns. --- bin/foodcritic | 8 +++- features/support/command_helpers.rb | 2 +- lib/foodcritic.rb | 2 + lib/foodcritic/command_line.rb | 59 +++++++++++++++++++++++++++++ lib/foodcritic/linter.rb | 22 +++-------- lib/foodcritic/output.rb | 13 +++++++ 6 files changed, 86 insertions(+), 20 deletions(-) create mode 100644 lib/foodcritic/command_line.rb create mode 100644 lib/foodcritic/output.rb diff --git a/bin/foodcritic b/bin/foodcritic index 133b95e1..d7ee58d0 100755 --- a/bin/foodcritic +++ b/bin/foodcritic @@ -1,4 +1,8 @@ #!/usr/bin/env ruby require_relative '../lib/foodcritic' -result, status = FoodCritic::Linter.check(ARGV) -puts result; exit status.to_i +module FoodCritic + cmd_line = CommandLine.new(ARGV) + review, status = Linter.check(cmd_line) + SummaryOutput.new.output(review) + exit status.to_i +end diff --git a/features/support/command_helpers.rb b/features/support/command_helpers.rb index b5a93576..dc16d320 100644 --- a/features/support/command_helpers.rb +++ b/features/support/command_helpers.rb @@ -125,7 +125,7 @@ def assert_no_error_occurred def run_lint(cmd_args) cmd_args.unshift '--repl' if with_repl? in_current_dir do - review, @status = FoodCritic::Linter.check(cmd_args) + review, @status = FoodCritic::Linter.check(CommandLine.new(cmd_args)) @review = review.nil? || (review.respond_to?(:warnings) && review.warnings.empty?) ? '' : "#{review.to_s}\n" end end diff --git a/lib/foodcritic.rb b/lib/foodcritic.rb index 55f62a3b..261ac5e7 100644 --- a/lib/foodcritic.rb +++ b/lib/foodcritic.rb @@ -1,8 +1,10 @@ require 'chef' require 'pry' +require_relative 'foodcritic/command_line' require_relative 'foodcritic/domain' require_relative 'foodcritic/error_checker' require_relative 'foodcritic/helpers' require_relative 'foodcritic/dsl' require_relative 'foodcritic/linter' +require_relative 'foodcritic/output' require_relative 'foodcritic/version' diff --git a/lib/foodcritic/command_line.rb b/lib/foodcritic/command_line.rb new file mode 100644 index 00000000..a4c18e9a --- /dev/null +++ b/lib/foodcritic/command_line.rb @@ -0,0 +1,59 @@ +module FoodCritic + + # Command line parsing. + class CommandLine + + # Create a new instance of CommandLine + # + # @param [Array] args The command line arguments + def initialize(args) + @args = args + @options = {} + @options[:fail_tags] = []; @options[:tags] = [] + @parser = OptionParser.new do |opts| + opts.banner = 'foodcritic [cookbook_path]' + opts.on("-r", "--[no-]repl", "Drop into a REPL for interactive rule editing.") {|r|options[:repl] = r} + opts.on("-t", "--tags TAGS", "Only check against rules with the specified tags.") {|t|options[:tags] << t} + opts.on("-f", "--epic-fail TAGS", "Fail the build if any of the specified tags are matched.") {|t|options[:fail_tags] << t} + end + @parser.parse!(args) unless show_help? + end + + # Show the command help to the end user? + # + # @return [Boolean] True if help should be shown. + def show_help? + @args.length == 1 and @args.first == '--help' + end + + # The help text. + # + # @return [String] Help text describing the command-line options available. + def help + @parser.help + end + + # If the cookbook path provided is valid + # + # @return [Boolean] True if the path is a directory that exists. + def valid_path? + @args.length == 1 and Dir.exists?(@args[0]) + end + + # The cookbook path + # + # @return [String] Path to the cookbook(s) being checked. + def cookbook_path + @args[0] + end + + # Parsed command-line options + # + # @return [Hash] The parsed command-line options. + def options + @options + end + + end + +end \ No newline at end of file diff --git a/lib/foodcritic/linter.rb b/lib/foodcritic/linter.rb index 8aa1fa41..edf49c2b 100644 --- a/lib/foodcritic/linter.rb +++ b/lib/foodcritic/linter.rb @@ -13,25 +13,13 @@ class Linter # # @param [Array] args The command-line arguments to parse # @return [Array] Pair - the first item is string output, the second is the exit code. - def self.check(args) - options = {} - options[:fail_tags] = []; options[:tags] = [] - parser = OptionParser.new do |opts| - opts.banner = 'foodcritic [cookbook_path]' - opts.on("-r", "--[no-]repl", "Drop into a REPL for interactive rule editing.") {|r|options[:repl] = r} - opts.on("-t", "--tags TAGS", "Only check against rules with the specified tags.") {|t|options[:tags] << t} - opts.on("-f", "--epic-fail TAGS", "Fail the build if any of the specified tags are matched.") {|t|options[:fail_tags] << t} - end - - return [parser.help, 0] if args.length == 1 and args.first == '--help' - - parser.parse!(args) - - if args.length == 1 and Dir.exists?(args[0]) - review = FoodCritic::Linter.new.check(args[0], options) + def self.check(cmd_line) + return [cmd_line.help, 0] if cmd_line.show_help? + if cmd_line.valid_path? + review = FoodCritic::Linter.new.check(cmd_line.cookbook_path, cmd_line.options) [review, review.failed? ? 3 : 0] else - [parser.help, 2] + [cmd_line.help, 2] end end diff --git a/lib/foodcritic/output.rb b/lib/foodcritic/output.rb new file mode 100644 index 00000000..31893bf9 --- /dev/null +++ b/lib/foodcritic/output.rb @@ -0,0 +1,13 @@ +module FoodCritic + + # Default output showing a summary view. + class SummaryOutput + # Output a summary view only listing the matching rules, file and line number. + # + # @param [Review] review The review to output. + def output(review) + puts review.to_s + end + end + +end \ No newline at end of file