forked from rubocop/rubocop
/
rake_task.rb
80 lines (67 loc) · 2.07 KB
/
rake_task.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# frozen_string_literal: true
require 'rake'
require 'rake/tasklib'
module RuboCop
# Provides a custom rake task.
#
# require 'rubocop/rake_task'
# RuboCop::RakeTask.new
class RakeTask < Rake::TaskLib
attr_accessor :name
attr_accessor :verbose
attr_accessor :fail_on_error
attr_accessor :patterns
attr_accessor :formatters
attr_accessor :requires
attr_accessor :options
def initialize(name = :rubocop, *args, &task_block)
setup_ivars(name)
desc 'Run RuboCop' unless ::Rake.application.last_description
task(name, *args) do |_, task_args|
RakeFileUtils.verbose(verbose) do
yield(*[self, task_args].slice(0, task_block.arity)) if block_given?
run_cli(verbose, full_options)
end
end
setup_subtasks(name, *args, &task_block)
end
private
def run_cli(verbose, options)
# We lazy-load rubocop so that the task doesn't dramatically impact the
# load time of your Rakefile.
require 'rubocop'
cli = CLI.new
puts 'Running RuboCop...' if verbose
result = cli.run(options)
abort('RuboCop failed!') if result.nonzero? && fail_on_error
end
def full_options
formatters.map { |f| ['--format', f] }.flatten
.concat(requires.map { |r| ['--require', r] }.flatten)
.concat(options.flatten)
.concat(patterns)
end
def setup_ivars(name)
@name = name
@verbose = true
@fail_on_error = true
@patterns = []
@requires = []
@options = []
@formatters = []
end
def setup_subtasks(name, *args, &task_block)
namespace(name) do
desc 'Auto-correct RuboCop offenses'
task(:auto_correct, *args) do |_, task_args|
RakeFileUtils.verbose(verbose) do
yield(*[self, task_args].slice(0, task_block.arity)) if block_given?
options = full_options.unshift('--auto-correct-all')
options.delete('--parallel')
run_cli(verbose, options)
end
end
end
end
end
end