class SCSSLint::RakeTask

Rake task for scss-lint CLI.

@example

# Add the following to your Rakefile...
require 'scss_lint/rake_task'
SCSSLint::RakeTask.new

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

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

@example

# Add the following to your Rakefile...
require 'scss_lint/rake_task'
SCSSLint::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 'scss_lint[app/assets/**/*.scss, other_files/**/*.scss]'

Attributes

args[RW]

Command-line args to use. @return [Array<String>]

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 in the task definition. @return [Array<String>]

name[RW]

Name of the task. @return [String]

quiet[RW]

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

Public Class Methods

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

Create the task so it is accessible via +Rake::Task+.

# File lib/scss_lint/rake_task.rb, line 51
def initialize(name = :scss_lint)
  @name = name
  @files = []
  @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.

# File lib/scss_lint/rake_task.rb, line 115
def default_description
  description = 'Run `scss-lint'
  description += " --config #{config}" if config
  description += " #{args}" if args
  description += " #{files.join(' ')}" if files.any?
  description += ' [files...]`'
  description
end
define() click to toggle source
# File lib/scss_lint/rake_task.rb, line 63
def define
  # Generate a default description if one hasn't been provided
  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 'scss_lint'
    require 'scss_lint/cli'

    run_cli(task_args)
  end
end
files_to_lint(task_args) click to toggle source
# File lib/scss_lint/rake_task.rb, line 97
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)

  if explicit_files.any?
    explicit_files
  elsif files.any?
    files
  else
    [] # Will fall back to scss_files option if defined
  end
end
run_cli(task_args) click to toggle source
# File lib/scss_lint/rake_task.rb, line 76
def run_cli(task_args)
  cli_args = ['--config', config] if config

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

  message =
    case result
    when CLI::EXIT_CODES[:error], CLI::EXIT_CODES[:warning]
      'scss-lint found one or more lints'
    when CLI::EXIT_CODES[:ok]
      'scss-lint found no lints'
    else
      'scss-lint failed with an error'
    end

  logger.log message
  exit result unless result == 0
end