rubocop

Set up rubcop, fix violations and create a todo config that exludes open violations
Icons/chart bar
Used 192 times
Created by
T TobiasBales

Usage

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/V33sBe"
Template Source

Review the code before running this template on your machine.

def create_bin(name, command = nil)
  create_file "bin/#{name}" do <<~EOF
    #!/usr/bin/env ruby
    APP_ROOT = File.expand_path('..', __dir__)
    Dir.chdir(APP_ROOT) do
      begin
        exec '#{command || name}'
      rescue Errno::ENOENT
        $stderr.puts "#{name} executable was not detected in the system."
        exit 1
      end
    end
  EOF
  end
  `chmod +x bin/#{name}`
end

gem_group :development, :test do
  gem 'rubocop'
  gem 'rubocop-minitest'
  gem 'rubocop-performance'
  gem 'rubocop-rails'
end

Bundler.with_unbundled_env { run 'bundle install' }

create_file '.rubocop.yml' do <<~YAML
    require:
      - rubocop-rails
      - rubocop-performance
      - rubocop-minitest

    inherit_mode:
      merge:
        - Exclude

    AllCops:
      Exclude:
        - 'lib/tasks/auto_annotate_models.rake'

    Bundler/OrderedGems:
      Enabled: true
      AutoCorrect: true
    Layout/EmptyLinesAroundAttributeAccessor:
      Enabled: true
    Layout/LineLength:
      Enabled: true
      AutoCorrect: true
    Layout/SpaceAroundMethodCallOperator:
      Enabled: true
    Lint/DeprecatedOpenSSLConstant:
      Enabled: true
    Lint/MixedRegexpCaptureTypes:
      Enabled: true
    Lint/RaiseException:
      Enabled: true
    Lint/StructNewOverride:
      Enabled: true
    Metrics/BlockLength:
      Enabled: true
      Exclude:
        - 'config/environments/development.rb'
    Performance/AncestorsInclude:
      Enabled: true
    Performance/BigDecimalWithNumericArgument:
      Enabled: true
    Performance/RedundantSortBlock:
      Enabled: true
    Performance/RedundantStringChars:
      Enabled: true
    Performance/ReverseFirst:
      Enabled: true
    Performance/SortReverse:
      Enabled: true
    Performance/Squeeze:
      Enabled: true
    Performance/StringInclude:
      Enabled: true
    Style/AccessorGrouping:
      Enabled: true
    Style/BisectedAttrAccessor:
      Enabled: true
    Style/ClassAndModuleChildren:
      Enabled: true
      AutoCorrect: true
    Style/Documentation:
      Enabled: false
    Style/ExponentialNotation:
      Enabled: true
    Style/HashEachMethods:
      Enabled: true
    Style/HashTransformKeys:
      Enabled: true
    Style/HashTransformValues:
      Enabled: true
    Style/RedundantAssignment:
      Enabled: true
    Style/RedundantFetchBlock:
      Enabled: true
    Style/RedundantRegexpCharacterClass:
      Enabled: true
    Style/RedundantRegexpEscape:
      Enabled: true
    Style/SlicingWithRange:
      Enabled: true
  YAML
end

gsub_file 'config/environments/development.rb', "Rails.root.join('tmp', 'caching-dev.txt')", "Rails.root.join('tmp/caching-dev.txt')"

create_bin 'rubocop', 'rubocop'
create_bin 'rubocop_fix', 'rubocop --auto-correct-all'

system('bin/rubocop_fix')
system('rubocop --auto-correct-all --disable-uncorrectable')
Comments

Sign up or Login to leave a comment.