require 'rake'
require 'rake/tasklib'

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

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

      def options
        {:fail_tags => ['correctness'], # differs to default cmd-line behaviour
         :cookbook_paths => @files,
         :exclude_paths => ['test/**/*', 'spec/**/*', 'features/**/*']
        }.merge(@options)
      end

      def define
        desc "Lint Chef cookbooks" unless ::Rake.application.last_comment
        task(name) do
          result = FoodCritic::Linter.new.check(options)
          if result.warnings.any?
            puts result
          end

          fail result.to_s if result.failed?
        end
      end

    end
  end
end

require 'rake'
require 'rake/tasklib'

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

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

      def options
        {:fail_tags => ['correctness'], # differs to default cmd-line behaviour
         :cookbook_paths => @files,
         :exclude_paths => ['test/**/*', 'spec/**/*', 'features/**/*']
        }.merge(@options)
      end

      def define
        desc "Lint Chef cookbooks" unless ::Rake.application.last_comment
        task(name) do
          result = FoodCritic::Linter.new.check(options)
          puts result if result.warnings.any?
          abort if result.failed?
        end
      end

    end
  end
end

Improve output of Rake task if check failed
Improve output of Rake task if check failed

* Do not print out result a second time.
* Do not raise an exception, just exit with 1.

This is close to what RSpec's Rake task does:
https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/rake_task.rb
