Class: RubyLint::RakeTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/ruby-lint/rake_task.rb

Overview

Class for easily creating Rake tasks without having to shell out to the commandline executable.

Basic usage:

require 'ruby-lint/rake_task'

RubyLint::RakeTask.new do |task|
  task.name  = 'lint'
  task.files = ['lib/my-project-name']
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ RakeTask

Returns a new instance of RakeTask.

Yields:

  • (_self)

Yield Parameters:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby-lint/rake_task.rb', line 39

def initialize(options = {})
  @name        = 'lint'
  @description = 'Check source code using ruby-lint'

  options.each do |key, value|
    instance_variable_set("@#{key}", value) if respond_to?(key)
  end

  yield self if block_given?

  desc(description)
  task(name) do
    validate!
    run_task
  end
end

Instance Attribute Details

#configurationString



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruby-lint/rake_task.rb', line 33

class RakeTask < Rake::TaskLib
  attr_accessor :name, :description, :debug, :files, :configuration

  ##
  # @param [Hash] options
  #
  def initialize(options = {})
    @name        = 'lint'
    @description = 'Check source code using ruby-lint'

    options.each do |key, value|
      instance_variable_set("@#{key}", value) if respond_to?(key)
    end

    yield self if block_given?

    desc(description)
    task(name) do
      validate!
      run_task
    end
  end

  ##
  # Checks if the task is configured properly, exists with code 1 if this
  # isn't the case.
  #
  def validate!
    if configuration and !File.file?(configuration)
      abort "The configuration file #{configuration} does not exist"
    end

    if files.empty?
      abort 'No files to check were specified'
    end
  end

  ##
  # Processes a list of files and writes the output to STDOUT.
  #
  def run_task
    config = create_configuration
    runner = RubyLint::Runner.new(config)
    list   = FileList.new
    output = runner.analyze(list.process(files))

    puts(output) unless output.empty?
  end

  ##
  # @return [RubyLint::Configuration]
  #
  def create_configuration
    config_files = RubyLint::Configuration.configuration_files

    if configuration
      config_files = [configuration]
    end

    config       = RubyLint::Configuration.load_from_file(config_files)
    config.debug = debug

    return config
  end
end

#debugTrueClass|FalseClass



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruby-lint/rake_task.rb', line 33

class RakeTask < Rake::TaskLib
  attr_accessor :name, :description, :debug, :files, :configuration

  ##
  # @param [Hash] options
  #
  def initialize(options = {})
    @name        = 'lint'
    @description = 'Check source code using ruby-lint'

    options.each do |key, value|
      instance_variable_set("@#{key}", value) if respond_to?(key)
    end

    yield self if block_given?

    desc(description)
    task(name) do
      validate!
      run_task
    end
  end

  ##
  # Checks if the task is configured properly, exists with code 1 if this
  # isn't the case.
  #
  def validate!
    if configuration and !File.file?(configuration)
      abort "The configuration file #{configuration} does not exist"
    end

    if files.empty?
      abort 'No files to check were specified'
    end
  end

  ##
  # Processes a list of files and writes the output to STDOUT.
  #
  def run_task
    config = create_configuration
    runner = RubyLint::Runner.new(config)
    list   = FileList.new
    output = runner.analyze(list.process(files))

    puts(output) unless output.empty?
  end

  ##
  # @return [RubyLint::Configuration]
  #
  def create_configuration
    config_files = RubyLint::Configuration.configuration_files

    if configuration
      config_files = [configuration]
    end

    config       = RubyLint::Configuration.load_from_file(config_files)
    config.debug = debug

    return config
  end
end

#descriptionString



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruby-lint/rake_task.rb', line 33

class RakeTask < Rake::TaskLib
  attr_accessor :name, :description, :debug, :files, :configuration

  ##
  # @param [Hash] options
  #
  def initialize(options = {})
    @name        = 'lint'
    @description = 'Check source code using ruby-lint'

    options.each do |key, value|
      instance_variable_set("@#{key}", value) if respond_to?(key)
    end

    yield self if block_given?

    desc(description)
    task(name) do
      validate!
      run_task
    end
  end

  ##
  # Checks if the task is configured properly, exists with code 1 if this
  # isn't the case.
  #
  def validate!
    if configuration and !File.file?(configuration)
      abort "The configuration file #{configuration} does not exist"
    end

    if files.empty?
      abort 'No files to check were specified'
    end
  end

  ##
  # Processes a list of files and writes the output to STDOUT.
  #
  def run_task
    config = create_configuration
    runner = RubyLint::Runner.new(config)
    list   = FileList.new
    output = runner.analyze(list.process(files))

    puts(output) unless output.empty?
  end

  ##
  # @return [RubyLint::Configuration]
  #
  def create_configuration
    config_files = RubyLint::Configuration.configuration_files

    if configuration
      config_files = [configuration]
    end

    config       = RubyLint::Configuration.load_from_file(config_files)
    config.debug = debug

    return config
  end
end

#filesArray



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruby-lint/rake_task.rb', line 33

class RakeTask < Rake::TaskLib
  attr_accessor :name, :description, :debug, :files, :configuration

  ##
  # @param [Hash] options
  #
  def initialize(options = {})
    @name        = 'lint'
    @description = 'Check source code using ruby-lint'

    options.each do |key, value|
      instance_variable_set("@#{key}", value) if respond_to?(key)
    end

    yield self if block_given?

    desc(description)
    task(name) do
      validate!
      run_task
    end
  end

  ##
  # Checks if the task is configured properly, exists with code 1 if this
  # isn't the case.
  #
  def validate!
    if configuration and !File.file?(configuration)
      abort "The configuration file #{configuration} does not exist"
    end

    if files.empty?
      abort 'No files to check were specified'
    end
  end

  ##
  # Processes a list of files and writes the output to STDOUT.
  #
  def run_task
    config = create_configuration
    runner = RubyLint::Runner.new(config)
    list   = FileList.new
    output = runner.analyze(list.process(files))

    puts(output) unless output.empty?
  end

  ##
  # @return [RubyLint::Configuration]
  #
  def create_configuration
    config_files = RubyLint::Configuration.configuration_files

    if configuration
      config_files = [configuration]
    end

    config       = RubyLint::Configuration.load_from_file(config_files)
    config.debug = debug

    return config
  end
end

#nameString



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruby-lint/rake_task.rb', line 33

class RakeTask < Rake::TaskLib
  attr_accessor :name, :description, :debug, :files, :configuration

  ##
  # @param [Hash] options
  #
  def initialize(options = {})
    @name        = 'lint'
    @description = 'Check source code using ruby-lint'

    options.each do |key, value|
      instance_variable_set("@#{key}", value) if respond_to?(key)
    end

    yield self if block_given?

    desc(description)
    task(name) do
      validate!
      run_task
    end
  end

  ##
  # Checks if the task is configured properly, exists with code 1 if this
  # isn't the case.
  #
  def validate!
    if configuration and !File.file?(configuration)
      abort "The configuration file #{configuration} does not exist"
    end

    if files.empty?
      abort 'No files to check were specified'
    end
  end

  ##
  # Processes a list of files and writes the output to STDOUT.
  #
  def run_task
    config = create_configuration
    runner = RubyLint::Runner.new(config)
    list   = FileList.new
    output = runner.analyze(list.process(files))

    puts(output) unless output.empty?
  end

  ##
  # @return [RubyLint::Configuration]
  #
  def create_configuration
    config_files = RubyLint::Configuration.configuration_files

    if configuration
      config_files = [configuration]
    end

    config       = RubyLint::Configuration.load_from_file(config_files)
    config.debug = debug

    return config
  end
end

Instance Method Details

#create_configurationRubyLint::Configuration



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ruby-lint/rake_task.rb', line 85

def create_configuration
  config_files = RubyLint::Configuration.configuration_files

  if configuration
    config_files = [configuration]
  end

  config       = RubyLint::Configuration.load_from_file(config_files)
  config.debug = debug

  return config
end

#run_taskObject

Processes a list of files and writes the output to STDOUT.



73
74
75
76
77
78
79
80
# File 'lib/ruby-lint/rake_task.rb', line 73

def run_task
  config = create_configuration
  runner = RubyLint::Runner.new(config)
  list   = FileList.new
  output = runner.analyze(list.process(files))

  puts(output) unless output.empty?
end

#validate!Object

Checks if the task is configured properly, exists with code 1 if this isn't the case.



60
61
62
63
64
65
66
67
68
# File 'lib/ruby-lint/rake_task.rb', line 60

def validate!
  if configuration and !File.file?(configuration)
    abort "The configuration file #{configuration} does not exist"
  end

  if files.empty?
    abort 'No files to check were specified'
  end
end