Skip to content
This repository has been archived by the owner on Sep 19, 2020. It is now read-only.

Commit

Permalink
Better separation of concerns.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Crump committed Jan 22, 2012
1 parent 5c9114b commit 7802018
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 20 deletions.
8 changes: 6 additions & 2 deletions 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
2 changes: 1 addition & 1 deletion features/support/command_helpers.rb
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions 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'
59 changes: 59 additions & 0 deletions 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
22 changes: 5 additions & 17 deletions lib/foodcritic/linter.rb
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions 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

0 comments on commit 7802018

Please sign in to comment.