Skip to content

Commit

Permalink
Merge pull request #2072 from bf4/more_precise_rubocop
Browse files Browse the repository at this point in the history
Add rubocop binstub that respects file patterns
  • Loading branch information
bf4 committed Mar 12, 2017
2 parents 24c0212 + a36b25d commit 87e929a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 31 deletions.
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
AllCops:
TargetRubyVersion: 2.1
Exclude:
- config/initializers/forbidden_yaml.rb
- !ruby/regexp /(vendor|bundle|bin|db|tmp)\/.*/
DisplayCopNames: true
DisplayStyleGuide: true
# https://github.com/bbatsov/rubocop/blob/master/manual/caching.md
# https://github.com/bbatsov/rubocop/blob/e8680418b351491e111a18cf5b453fc07a3c5239/config/default.yml#L60-L77
UseCache: true
CacheRootDirectory: tmp

Rails:
Enabled: true
Expand Down
31 changes: 1 addition & 30 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ begin
require 'simplecov'
rescue LoadError # rubocop:disable Lint/HandleExceptions
end
import('lib/tasks/rubocop.rake')

Bundler::GemHelper.install_tasks

Expand All @@ -30,36 +31,6 @@ namespace :yard do
end
end

begin
require 'rubocop'
require 'rubocop/rake_task'
rescue LoadError # rubocop:disable Lint/HandleExceptions
else
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
require 'rbconfig'
# https://github.com/bundler/bundler/blob/1b3eb2465a/lib/bundler/constants.rb#L2
windows_platforms = /(msdos|mswin|djgpp|mingw)/
if RbConfig::CONFIG['host_os'] =~ windows_platforms
desc 'No-op rubocop on Windows-- unsupported platform'
task :rubocop do
puts 'Skipping rubocop on Windows'
end
elsif defined?(::Rubinius)
desc 'No-op rubocop to avoid rbx segfault'
task :rubocop do
puts 'Skipping rubocop on rbx due to segfault'
puts 'https://github.com/rubinius/rubinius/issues/3499'
end
else
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
desc 'Execute rubocop'
RuboCop::RakeTask.new(:rubocop) do |task|
task.options = ['--rails', '--display-cop-names', '--display-style-guide']
task.fail_on_error = true
end
end
end

require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
Expand Down
38 changes: 38 additions & 0 deletions bin/rubocop
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
#
# Usage:
# bin/rubocop [-A|-t|-h]
# bin/rubocop [file or path] [cli options]
#
# Options:
# Autocorrect -A
# AutoGenConfig -t
# Usage -h,--help,help

set -e

case $1 in
-A)
echo "Rubocop autocorrect is ON" >&2
bundle exec rake -f lib/tasks/rubocop.rake rubocop:auto_correct
;;

-t)
echo "Rubocop is generating a new TODO" >&2
bundle exec rake -f lib/tasks/rubocop.rake rubocop:auto_gen_config
;;

-h|--help|help)
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0"
;;

*)
# with no args, run vanilla rubocop
# else assume we're passing in arbitrary arguments
if [ -z "$1" ]; then
bundle exec rake -f lib/tasks/rubocop.rake rubocop
else
bundle exec rubocop "$@"
fi
;;
esac
53 changes: 53 additions & 0 deletions lib/tasks/rubocop.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
begin
require 'rubocop'
require 'rubocop/rake_task'
rescue LoadError # rubocop:disable Lint/HandleExceptions
else
require 'rbconfig'
# https://github.com/bundler/bundler/blob/1b3eb2465a/lib/bundler/constants.rb#L2
windows_platforms = /(msdos|mswin|djgpp|mingw)/
if RbConfig::CONFIG['host_os'] =~ windows_platforms
desc 'No-op rubocop on Windows-- unsupported platform'
task :rubocop do
puts 'Skipping rubocop on Windows'
end
elsif defined?(::Rubinius)
desc 'No-op rubocop to avoid rbx segfault'
task :rubocop do
puts 'Skipping rubocop on rbx due to segfault'
puts 'https://github.com/rubinius/rubinius/issues/3499'
end
else
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
patterns = [
'Gemfile',
'Rakefile',
'lib/**/*.{rb,rake}',
'config/**/*.rb',
'app/**/*.rb',
'test/**/*.rb'
]
desc 'Execute rubocop'
RuboCop::RakeTask.new(:rubocop) do |task|
task.options = ['--rails', '--display-cop-names', '--display-style-guide']
task.formatters = ['progress']
task.patterns = patterns
task.fail_on_error = true
end

namespace :rubocop do
desc 'Auto-gen rubocop config'
task :auto_gen_config do
options = ['--auto-gen-config'].concat patterns
require 'benchmark'
result = 0
cli = RuboCop::CLI.new
time = Benchmark.realtime do
result = cli.run(options)
end
puts "Finished in #{time} seconds" if cli.options[:debug]
abort('RuboCop failed!') if result.nonzero?
end
end
end
end

0 comments on commit 87e929a

Please sign in to comment.