experteer/codeqa

View on GitHub
bin/codeqa

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

$: << File.join(File.dirname(__FILE__), '../lib') unless $PROGRAM_NAME == 'codeqa'
require 'rubygems'
require 'codeqa'

require 'pathname'
require 'optparse'

options = { :silent => false, :failfast => false }

ARGV.options do |opts|
  opts.banner = "Usage:  #{File.basename($PROGRAM_NAME)} [OPTIONS] FILES"

  opts.separator ''
  opts.separator 'Specific Options:'

  opts.on('-s', '--silent', 'No output') do
    options[:silent] = true
  end

  opts.on('-e', '--silent-success', 'only Output errors') do
    options[:silent_success] = true
  end

  opts.on('-f', '--fail-fast', 'Fail right after the first error.') do
    options[:failfast] = true
  end

  opts.on('-i', '--install', 'Install a basic pre-commit hook.') do
    options[:install] = true
  end

  opts.separator 'Common Options:'

  opts.on('-h', '--help',
          'Show this message.') do
    puts opts
    exit
  end
  opts.on('-v', '--version',
          "Show version (it's #{Codeqa::VERSION})") do
    puts Codeqa::VERSION
    exit
  end

  begin
    opts.parse!
  rescue
    puts opts
    exit
  end
end

if options[:install]
  Codeqa.install(ARGV.first || '.')
  exit
end

exploded_args = ARGV.map do |e|
  if File.directory?(e)
    Dir["#{e}/**/*"]
  else
    e
  end
end

files_to_check = exploded_args.
  flatten.
  map{ |e| Pathname.new(e).realpath.to_s }.
  reject{ |e| File.directory?(e) || Codeqa.configuration.excluded?(e) }.
  uniq

puts "#{files_to_check.count} files to check" unless options[:silent]
if options[:failfast]
  # all? is already lazy/fail-fast so it will stop after the first false
  exit 1 unless files_to_check.all?{ |e| Codeqa.check(e, options) }
else
  exit 1 unless files_to_check.map{ |e| Codeqa.check(e, options) }.all?
end