class GazelleStyleguide::CLI

Command line tool for managing linter running and pre-commits

Constants

GENERIC_CHECKS
JAVA_CHECKS
RUBY_CHECKS

Public Instance Methods

fix(*files) click to toggle source
# File lib/gazelle_styleguide/cli.rb, line 36
def fix(*files)
  require 'rubocop'
  files = lint_files(files, options).select{|file| file =~ /\.rb|Gemfile|Rakefile|\.task|\.rake/}
  args = ['-a', '-c', GazelleStyleguide.config_for('rubocop.yml')] + files
  args.unshift('-d') if options[:debug]

  Rubocop::CLI.new.run(args)
end
init() click to toggle source
# File lib/gazelle_styleguide/cli.rb, line 14
def init
  setup_git_config
  install_pre_commit
  setup_houndci
end
lint(*files) click to toggle source
# File lib/gazelle_styleguide/cli.rb, line 24
def lint(*files)
  require 'pre-commit'
  files = lint_files(files, options)

  PreCommit::Runner.new($stderr, files).run
end

Private Instance Methods

changed_files() click to toggle source
# File lib/gazelle_styleguide/cli.rb, line 94
def changed_files
  repo.status.untracked.keys + staged_files
end
install_pre_commit() click to toggle source
# File lib/gazelle_styleguide/cli.rb, line 47
def install_pre_commit
  require 'pre-commit/installer'

  PreCommit::Installer.new.install
end
lint_files(files, options) click to toggle source
# File lib/gazelle_styleguide/cli.rb, line 78
def lint_files(files, options)
  if files.any?
    files
  elsif options[:all]
    Dir['./**/*'].select{|f| File.file?(f)}
  elsif options[:staged]
    staged_files
  else
    changed_files
  end
end
project_root() click to toggle source
# File lib/gazelle_styleguide/cli.rb, line 68
def project_root
  @project_root ||= Pathname.new('.').ascend do |path|
    return path if path.join('.git').exist?
  end
end
repo() click to toggle source
# File lib/gazelle_styleguide/cli.rb, line 98
def repo
  @repo ||= Grit::Repo.new('.')
end
ruby?() click to toggle source
# File lib/gazelle_styleguide/cli.rb, line 74
def ruby?
  project_root.join('Gemfile').exist?
end
setup_git_config() click to toggle source
# File lib/gazelle_styleguide/cli.rb, line 53
def setup_git_config
  checks = GENERIC_CHECKS + ', ' + (ruby? ? RUBY_CHECKS : JAVA_CHECKS)
  repo.config['pre-commit.checks'] = checks
  repo.config['pre-commit.jshint.config'] = GazelleStyleguide.config_for('jshintrc.json')
  repo.config['pre-commit.rubocop.config'] = GazelleStyleguide.config_for('rubocop.yml')
  repo.config['pre-commit.coffeelint.config'] = GazelleStyleguide.config_for('coffeelint.json')
  repo.config['pre-commit.checkstyle.config'] = GazelleStyleguide.config_for('sun_checks.xml')
end
setup_houndci() click to toggle source
# File lib/gazelle_styleguide/cli.rb, line 62
def setup_houndci
  return unless ruby?

  FileUtils.cp(GazelleStyleguide.config_for('rubocop.yml'), project_root.join('.hound.yml'))
end
staged_files() click to toggle source
# File lib/gazelle_styleguide/cli.rb, line 90
def staged_files
  repo.status.changed.keys + repo.status.added.keys
end