#!/usr/bin/env ruby require 'simplabs/excellent' require 'pathname' require 'optparse' options = {} optparse = OptionParser.new do |opt| opt.banner = 'Usage: excellent [OPTIONS] [ ...]' opt.on('--output', '-o [FILE]', 'Write HTML output to [FILE]') do |target| options[:output] = target end opt.on('--checks', 'Show a list of the enabled checks and their configuraton values') do |target| options[:print_checks] = true end opt.on_tail('--help', '-h', 'Help') do puts optparse exit end opt.on_tail('--version', '-v', 'Version') do puts Simplabs::Excellent::VERSION exit end end optparse.parse! target = options[:output] if target begin fileio = File.open(target, 'w+') formatter = Simplabs::Excellent::Formatters::Html.new(fileio) rescue Errno::ENOENT puts "\n **#{target} cannot be opened for writing. Specify a file name like so:\n" puts "\n excellent -o excellent.html lib/\n\n" end else formatter = Simplabs::Excellent::Formatters::Text.new end custom_config = {} custom_config_file = File.join(Dir.pwd, '.excellent.yml') default_config_file = File.join(File.expand_path('~'), '.excellent.yml') if File.exists?(default_config_file) || File.exists?(custom_config_file) require 'yaml' begin default_config = if File.exists?(default_config_file) YAML.load(File.open(default_config_file)) || {} else {} end custom_config = if File.exists?(custom_config_file) YAML.load(File.open(custom_config_file)) || {} else {} end custom_config = default_config.merge(custom_config) config_valid = custom_config.is_a?(Hash) && custom_config.all? do |key, value| key.is_a?(String) || key.is_a?(Symbol) end rescue Exception config_valid = false end unless config_valid puts "\n ** The configuration is not valid!\n\n" puts " Please make sure you specify a hash with check names as keys and hashes or truthy/falsy values as hash values, e.g.:\n\n" puts " AbcMetricMethodCheck:" puts " false" puts " ClassLineCountCheck:" puts " threshold: 10" puts "\n\n" exit 1 end end ignore_paths = [] ignore_file = File.join(Dir.pwd, '.excellentignore') if File.exists?(ignore_file) ignore_paths = File.readlines(ignore_file).map(&:strip).select { |line| !line.empty? } end excellent = Simplabs::Excellent::Runner.new(custom_config) if options[:print_checks] puts "\n Configured Checks\n\n" excellent.checks.each do |check| puts " #{check.class.name.gsub('Simplabs::Excellent::Checks::', '')}" check.options.each do |key, value| puts " #{key}: #{value}" end end exit 0 end paths = ARGV.dup if paths.empty? puts "\n **You must specify one or more directories to analyse, e.g.:\n" puts "\n excellent lib/\n\n" exit 1 end begin excellent.check_paths(paths, formatter, ignore_paths) rescue ArgumentError => e puts e.inspect puts "\n** Excellent cannot find the paths specified!\n\n" exit 1 end exit 0