Skip to content

Commit 87e929a

Browse files
authored
Merge pull request #2072 from bf4/more_precise_rubocop
Add rubocop binstub that respects file patterns
2 parents 24c0212 + a36b25d commit 87e929a

File tree

4 files changed

+96
-31
lines changed

4 files changed

+96
-31
lines changed

.rubocop.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
AllCops:
22
TargetRubyVersion: 2.1
33
Exclude:
4-
- config/initializers/forbidden_yaml.rb
54
- !ruby/regexp /(vendor|bundle|bin|db|tmp)\/.*/
65
DisplayCopNames: true
76
DisplayStyleGuide: true
7+
# https://github.com/bbatsov/rubocop/blob/master/manual/caching.md
8+
# https://github.com/bbatsov/rubocop/blob/e8680418b351491e111a18cf5b453fc07a3c5239/config/default.yml#L60-L77
9+
UseCache: true
10+
CacheRootDirectory: tmp
811

912
Rails:
1013
Enabled: true

Rakefile

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ begin
77
require 'simplecov'
88
rescue LoadError # rubocop:disable Lint/HandleExceptions
99
end
10+
import('lib/tasks/rubocop.rake')
1011

1112
Bundler::GemHelper.install_tasks
1213

@@ -30,36 +31,6 @@ namespace :yard do
3031
end
3132
end
3233

33-
begin
34-
require 'rubocop'
35-
require 'rubocop/rake_task'
36-
rescue LoadError # rubocop:disable Lint/HandleExceptions
37-
else
38-
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
39-
require 'rbconfig'
40-
# https://github.com/bundler/bundler/blob/1b3eb2465a/lib/bundler/constants.rb#L2
41-
windows_platforms = /(msdos|mswin|djgpp|mingw)/
42-
if RbConfig::CONFIG['host_os'] =~ windows_platforms
43-
desc 'No-op rubocop on Windows-- unsupported platform'
44-
task :rubocop do
45-
puts 'Skipping rubocop on Windows'
46-
end
47-
elsif defined?(::Rubinius)
48-
desc 'No-op rubocop to avoid rbx segfault'
49-
task :rubocop do
50-
puts 'Skipping rubocop on rbx due to segfault'
51-
puts 'https://github.com/rubinius/rubinius/issues/3499'
52-
end
53-
else
54-
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
55-
desc 'Execute rubocop'
56-
RuboCop::RakeTask.new(:rubocop) do |task|
57-
task.options = ['--rails', '--display-cop-names', '--display-style-guide']
58-
task.fail_on_error = true
59-
end
60-
end
61-
end
62-
6334
require 'rake/testtask'
6435

6536
Rake::TestTask.new(:test) do |t|

bin/rubocop

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Usage:
4+
# bin/rubocop [-A|-t|-h]
5+
# bin/rubocop [file or path] [cli options]
6+
#
7+
# Options:
8+
# Autocorrect -A
9+
# AutoGenConfig -t
10+
# Usage -h,--help,help
11+
12+
set -e
13+
14+
case $1 in
15+
-A)
16+
echo "Rubocop autocorrect is ON" >&2
17+
bundle exec rake -f lib/tasks/rubocop.rake rubocop:auto_correct
18+
;;
19+
20+
-t)
21+
echo "Rubocop is generating a new TODO" >&2
22+
bundle exec rake -f lib/tasks/rubocop.rake rubocop:auto_gen_config
23+
;;
24+
25+
-h|--help|help)
26+
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0"
27+
;;
28+
29+
*)
30+
# with no args, run vanilla rubocop
31+
# else assume we're passing in arbitrary arguments
32+
if [ -z "$1" ]; then
33+
bundle exec rake -f lib/tasks/rubocop.rake rubocop
34+
else
35+
bundle exec rubocop "$@"
36+
fi
37+
;;
38+
esac

lib/tasks/rubocop.rake

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
begin
2+
require 'rubocop'
3+
require 'rubocop/rake_task'
4+
rescue LoadError # rubocop:disable Lint/HandleExceptions
5+
else
6+
require 'rbconfig'
7+
# https://github.com/bundler/bundler/blob/1b3eb2465a/lib/bundler/constants.rb#L2
8+
windows_platforms = /(msdos|mswin|djgpp|mingw)/
9+
if RbConfig::CONFIG['host_os'] =~ windows_platforms
10+
desc 'No-op rubocop on Windows-- unsupported platform'
11+
task :rubocop do
12+
puts 'Skipping rubocop on Windows'
13+
end
14+
elsif defined?(::Rubinius)
15+
desc 'No-op rubocop to avoid rbx segfault'
16+
task :rubocop do
17+
puts 'Skipping rubocop on rbx due to segfault'
18+
puts 'https://github.com/rubinius/rubinius/issues/3499'
19+
end
20+
else
21+
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
22+
patterns = [
23+
'Gemfile',
24+
'Rakefile',
25+
'lib/**/*.{rb,rake}',
26+
'config/**/*.rb',
27+
'app/**/*.rb',
28+
'test/**/*.rb'
29+
]
30+
desc 'Execute rubocop'
31+
RuboCop::RakeTask.new(:rubocop) do |task|
32+
task.options = ['--rails', '--display-cop-names', '--display-style-guide']
33+
task.formatters = ['progress']
34+
task.patterns = patterns
35+
task.fail_on_error = true
36+
end
37+
38+
namespace :rubocop do
39+
desc 'Auto-gen rubocop config'
40+
task :auto_gen_config do
41+
options = ['--auto-gen-config'].concat patterns
42+
require 'benchmark'
43+
result = 0
44+
cli = RuboCop::CLI.new
45+
time = Benchmark.realtime do
46+
result = cli.run(options)
47+
end
48+
puts "Finished in #{time} seconds" if cli.options[:debug]
49+
abort('RuboCop failed!') if result.nonzero?
50+
end
51+
end
52+
end
53+
end

0 commit comments

Comments
 (0)