Skip to content

Instantly share code, notes, and snippets.

@daryllxd
Created September 20, 2017 13:59
Show Gist options
  • Select an option

  • Save daryllxd/f6e6739f7c3fe0e3e70c1eedb2310d7a to your computer and use it in GitHub Desktop.

Select an option

Save daryllxd/f6e6739f7c3fe0e3e70c1eedb2310d7a to your computer and use it in GitHub Desktop.

Revisions

  1. daryllxd created this gist Sep 20, 2017.
    36 changes: 36 additions & 0 deletions rubocop.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/usr/bin/env ruby

    require 'english'
    require 'rubocop'
    require 'rb-readline'
    require 'pry'

    cached_changes = `git diff --cached`
    forbidden_words = ['binding.pry', 'console.log', 'debugger', 'byebug', '!important']

    if forbidden_words.any? { |word| cached_changes.include?(word) }
    puts "Looks like you are trying to commit something (any one of `#{forbidden_words.join(', ')}`) you shouldn't. Please fix your diff, or run 'git commit --no-verify' to skip this check, if you must."
    exit 1
    end

    # Try out rubocop
    ADDED_OR_MODIFIED = /A|AM|^M/.freeze

    changed_files = `git status --porcelain`.split(/\n/).
    select { |file_name_with_status|
    file_name_with_status =~ ADDED_OR_MODIFIED
    }.
    map { |file_name_with_status|
    file_name_with_status.split(' ')[1]
    }.
    select { |file_name|
    File.extname(file_name) == '.rb'
    }.
    delete_if { |file_name| file_name == 'db/schema.rb' }
    .join(' ')

    system("bundle exec rubocop #{changed_files}") unless changed_files.empty?

    exit $CHILD_STATUS.to_s[-1].to_i

    system('RAILS_ENV=test bundle exec rake swagger:docs')