class SlimLint::RakeTask

Rake task interface for slim-lint command line interface.

@example

# Add the following to your Rakefile...
require 'slim_lint/rake_task'

SlimLint::RakeTask.new do |t|
  t.config = 'path/to/custom/slim-lint.yml'
  t.files = %w[app/views/**/*.slim custom/*.slim]
  t.quiet = true # Don't display output from slim-lint
end

# ...and then execute from the command line:
rake slim_lint

You can also specify the list of files as explicit task arguments:

@example

# Add the following to your Rakefile...
require 'slim_lint/rake_task'

SlimLint::RakeTask.new

# ...and then execute from the command line (single quotes prevent shell
# glob expansion and allow us to have a space after commas):
rake 'slim_lint[app/views/**/*.slim, other_files/**/*.slim]'

Attributes

config[RW]

Configuration file to use. @return [String]

files[RW]

List of files to lint (can contain shell globs).

Note that this will be ignored if you explicitly pass a list of files as task arguments via the command line or a task definition. @return [Array<String>]

name[RW]

Name of the task. @return [String]

quiet[RW]

Whether output from slim-lint should not be displayed to the standard out stream. @return [true,false]

Public Class Methods

new(name = :slim_lint) { |self| ... } click to toggle source

Create the task so it exists in the current namespace.

@param name [Symbol] task name

# File lib/slim_lint/rake_task.rb, line 59
def initialize(name = :slim_lint)
  @name = name
  @files = ['.'] # Search for everything under current directory by default
  @quiet = false

  yield self if block_given?

  define
end

Private Instance Methods

default_description() click to toggle source

Friendly description that shows the full command that will be executed.

This allows us to change the information displayed by `rake –tasks` based on the options passed to the constructor which defined the task.

@return [String]

# File lib/slim_lint/rake_task.rb, line 117
def default_description
  description = "Run `#{SlimLint::APP_NAME}"
  description += " --config #{config}" if config
  description += " #{files.join(' ')}" if files.any?
  description += ' [files...]`'
  description
end
define() click to toggle source

Defines the Rake task.

# File lib/slim_lint/rake_task.rb, line 72
def define
  desc default_description unless ::Rake.application.last_description

  task(name, [:files]) do |_task, task_args|
    # Lazy-load so task doesn't affect Rakefile load time
    require 'slim_lint'
    require 'slim_lint/cli'

    run_cli(task_args)
  end
end
files_to_lint(task_args) click to toggle source

Returns the list of files that should be linted given the specified task arguments.

@param task_args [Rake::TaskArguments]

# File lib/slim_lint/rake_task.rb, line 100
def files_to_lint(task_args)
  # Note: we're abusing Rake's argument handling a bit here. We call the
  # first argument `files` but it's actually only the first file--we pull
  # the rest out of the `extras` from the task arguments. This is so we
  # can specify an arbitrary list of files separated by commas on the
  # command line or in a custom task definition.
  explicit_files = Array(task_args[:files]) + Array(task_args.extras)

  explicit_files.any? ? explicit_files : files
end
run_cli(task_args) click to toggle source

Executes the CLI given the specified task arguments.

@param task_args [Rake::TaskArguments]

# File lib/slim_lint/rake_task.rb, line 87
def run_cli(task_args)
  cli_args = ['--config', config] if config

  logger = quiet ? SlimLint::Logger.silent : SlimLint::Logger.new(STDOUT)
  result = SlimLint::CLI.new(logger).run(Array(cli_args) + files_to_lint(task_args))

  fail "#{SlimLint::APP_NAME} failed with exit code #{result}" unless result == 0
end