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

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]

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 42
def initialize(name = :scss_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.

# File lib/scss_lint/rake_task.rb, line 98
def default_description
  description = 'Run `scss-lint'
  description += " --config #{config}" if config
  description += " #{files.join(' ')}" if files.any?
  description += ' [files...]`'
  description
end
define() click to toggle source
# File lib/scss_lint/rake_task.rb, line 54
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 86
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
# File lib/scss_lint/rake_task.rb, line 67
def run_cli(task_args)
  cli_args = ['--config', config] if config

  result = SCSSLint::CLI.new.run(Array(cli_args) + files_to_lint(task_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

  puts message
  exit result unless result == 0
end