Add rubocop binstub that rspects file patterns

Best of both worlds!

(Because you can't override the default rubocop includes)

The binstub basically, lets me safely `rubocop test/foo_test.rb`
instead of `bundle exec rubocop test/foo_test.rb`

```bash
  # ~/.profile
  # https://twitter.com/tpope/status/165631968996900865
  # tl;dr `mkdir .git/safe` to add `bin` to path, e.g. `bin/rails`
  PATH=".git/safe/../../bin:$PATH"
```
This commit is contained in:
Benjamin Fleischer 2017-03-12 15:50:05 -05:00
parent 24c0212c83
commit a36b25d2db
4 changed files with 96 additions and 31 deletions

View File

@ -1,10 +1,13 @@
AllCops: AllCops:
TargetRubyVersion: 2.1 TargetRubyVersion: 2.1
Exclude: Exclude:
- config/initializers/forbidden_yaml.rb
- !ruby/regexp /(vendor|bundle|bin|db|tmp)\/.*/ - !ruby/regexp /(vendor|bundle|bin|db|tmp)\/.*/
DisplayCopNames: true DisplayCopNames: true
DisplayStyleGuide: 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: Rails:
Enabled: true Enabled: true

View File

@ -7,6 +7,7 @@ begin
require 'simplecov' require 'simplecov'
rescue LoadError # rubocop:disable Lint/HandleExceptions rescue LoadError # rubocop:disable Lint/HandleExceptions
end end
import('lib/tasks/rubocop.rake')
Bundler::GemHelper.install_tasks Bundler::GemHelper.install_tasks
@ -30,36 +31,6 @@ namespace :yard do
end end
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' require 'rake/testtask'
Rake::TestTask.new(:test) do |t| Rake::TestTask.new(:test) do |t|

38
bin/rubocop Executable file
View File

@ -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
lib/tasks/rubocop.rake Normal file
View File

@ -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