forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
144 lines (119 loc) · 3.94 KB
/
Rakefile
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# frozen_string_literal: true
# For code coverage measurements to work properly, `SimpleCov` should be loaded
# and started before any application code is loaded.
require 'simplecov' if ENV['COVERAGE']
require 'bundler'
require 'bundler/gem_tasks'
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
warn e.message
warn 'Run `bundle install` to install missing gems'
exit e.status_code
end
require 'rake'
require 'rubocop/rake_task'
Dir['tasks/**/*.rake'].each { |t| load t }
desc 'Deprecated: Run test and RuboCop in parallel'
task :parallel do
warn '`rake parallel` is deprecated. Use `rake default` instead.'
Rake::Task[:default].execute
end
desc 'Run RuboCop over itself'
RuboCop::RakeTask.new(:internal_investigation).tap do |task|
if RUBY_ENGINE == 'ruby' &&
RbConfig::CONFIG['host_os'] !~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/
task.options = %w[--parallel]
end
end
task default: %i[
build_config
documentation_syntax_check
generate_cops_documentation
spec ascii_spec
internal_investigation
confirm_config
]
require 'yard'
YARD::Rake::YardocTask.new
desc 'Benchmark a cop on given source file/dir'
task :bench_cop, %i[cop srcpath times] do |_task, args|
require 'benchmark'
require 'rubocop'
include RuboCop
include RuboCop::Formatter::TextUtil
cop_name = args[:cop]
src_path = args[:srcpath]
iterations = args[:times] ? Integer(args[:times]) : 1
cop_class = if cop_name.include?('/')
Cop::Cop.all.find { |klass| klass.cop_name == cop_name }
else
Cop::Cop.all.find do |klass|
klass.cop_name[/[a-zA-Z]+$/] == cop_name
end
end
raise "No such cop: #{cop_name}" if cop_class.nil?
config = ConfigLoader.load_file(ConfigLoader::DEFAULT_FILE)
cop = cop_class.new(config)
puts "Benchmarking #{cop.cop_name} on #{src_path} (using default config)"
files = if File.directory?(src_path)
Dir[File.join(src_path, '**', '*.rb')]
else
[src_path]
end
puts "(#{pluralize(iterations, 'iteration')}, " \
"#{pluralize(files.size, 'file')})"
ruby_version = RuboCop::Config::KNOWN_RUBIES.last
srcs = files.map { |file| ProcessedSource.from_file(file, ruby_version) }
puts 'Finished parsing source, testing inspection...'
puts(Benchmark.measure do
iterations.times do
commissioner = Cop::Commissioner.new([cop], [])
srcs.each { |src| commissioner.investigate(src) }
end
end)
end
desc 'Syntax check for the documentation comments'
task documentation_syntax_check: :yard_for_generate_documentation do
require 'parser/ruby25'
ok = true
YARD::Registry.load!
cops = RuboCop::Cop::Cop.registry
cops.each do |cop|
next if %i[RSpec Capybara FactoryBot].include?(cop.department)
examples = YARD::Registry.all(:class).find do |code_object|
next unless RuboCop::Cop::Badge.for(code_object.to_s) == cop.badge
break code_object.tags('example')
end
examples.each do |example|
begin
buffer = Parser::Source::Buffer.new('<code>', 1)
buffer.source = example.text
parser = Parser::Ruby25.new(RuboCop::AST::Builder.new)
parser.diagnostics.all_errors_are_fatal = true
parser.parse(buffer)
rescue Parser::SyntaxError => ex
path = example.object.file
puts "#{path}: Syntax Error in an example. #{ex}"
ok = false
end
end
end
abort unless ok
end
desc 'Build config/default.yml'
task :build_config do
sh('bin/build_config')
end
desc 'Confirm config/default.yml is up to date'
task confirm_config: :build_config do
# Do not print diff and yield whether exit code was zero
sh('git diff --quiet config') do |outcome, _|
return if outcome
# Output diff before raising error
sh('GIT_PAGER=cat git diff config')
warn 'The config/default.yml is out of sync. ' \
'Run `rake build_config` and commit the results.'
exit!
end
end