require "rake"
require "rake/tasklib"

module FoodCritic
  module Rake
    class LintTask < ::Rake::TaskLib
      attr_accessor :name, :files, :options

      def initialize(name = :foodcritic)
        @name = name
        @files = [Dir.pwd]
        @options = {}
        yield self if block_given?
        define
      end

      def define
        desc "Lint Chef cookbooks" unless ::Rake.application.last_description
        task(name) do
          puts "Starting Foodcritic linting..."
          result = FoodCritic::Linter.new.check(default_options.merge(options))
          printer = options[:context] ? ContextOutput.new : SummaryOutput.new
          printer.output(result) if result.warnings.any?
          abort if result.failed?
          puts "Completed!"
        end
      end

      private

      def default_options
        {
          fail_tags: ["correctness"], # differs to default cmd-line behaviour
          cookbook_paths: files,
          tags: [],
          exclude_paths: ["test/**/*", "spec/**/*", "features/**/*"],
          context: false,
          chef_version: FoodCritic::Linter::DEFAULT_CHEF_VERSION,
          progress: true,
        }
      end
    end
  end
end

require "rake"
require "rake/tasklib"

module FoodCritic
  module Rake
    class LintTask < ::Rake::TaskLib
      attr_accessor :name, :files, :options

      def initialize(name = :foodcritic)
        @name = name
        @files = [Dir.pwd]
        @options = {}
        yield self if block_given?
        define
      end

      def define
        desc "Lint Chef cookbooks" unless ::Rake.application.last_description
        task(name) do
          puts "Starting Foodcritic linting..."
          result = FoodCritic::Linter.new.check(default_options.merge(options))
          printer = if options[:context]
                      ContextOutput.new($stdout)
                    else
                      SummaryOutput.new($stdout)
                    end
          printer.output(result) if result.warnings.any?
          abort if result.failed?
          puts "Completed!"
        end
      end

      private

      def default_options
        {
          fail_tags: ["correctness"], # differs to default cmd-line behaviour
          cookbook_paths: files,
          tags: [],
          exclude_paths: ["test/**/*", "spec/**/*", "features/**/*"],
          context: false,
          chef_version: FoodCritic::Linter::DEFAULT_CHEF_VERSION,
          progress: true,
        }
      end
    end
  end
end

Fix a regression when running Foodcritic as a Rake task
Fix a regression when running Foodcritic as a Rake task

The Output classes' initializers now [require](https://github.com/acrmp/foodcritic/commit/3c057cb857939b6761effb386fe0b30e9270add2)
an output stream argument.

Signed-off-by: Jonathan Hartman <5c2dd944dde9e08881bef0894fe7b22a5c9c4b06@p4nt5.com>
