class SandiMeter::CLI

Public Class Methods

execute() click to toggle source
# File lib/sandi_meter/cli.rb, line 88
def execute
  cli = CommandParser.new
  cli.parse_options

  cli.config[:output_path] ||= File.expand_path(File.join(cli.config[:path], 'sandi_meter'))

  cli.config[:rule_thresholds] = cli.config[:rule_thresholds].split(",").map(&:to_i)

  if cli.config[:graph]
    FileUtils.mkdir_p(cli.config[:output_path]) unless Dir.exists?(cli.config[:output_path])

    create_config_file(cli.config[:output_path], '.sandi_meter', %w(db vendor).join("\n"))
    create_config_file(cli.config[:output_path], 'config.yml', YAML.dump({ thresholds: [90, 90, 90, 90] }))
  end

  if cli.config[:version]
    puts version_info
    exit 0
  end

  if cli.config[:rules]
    show_sandi_rules
    exit 0
  end

  scanner = SandiMeter::FileScanner.new(cli.config[:log])
  data = scanner.scan(cli.config[:path], cli.config[:details] || cli.config[:graph])

  if cli.config[:json]
    formatter = SandiMeter::JsonFormatter.new
  else
    formatter = SandiMeter::Formatter.new
  end

  formatter.print_data(data)

  if cli.config[:graph]
    if File.directory?(cli.config[:output_path])
      logger = SandiMeter::Logger.new(data)
      logger.log!(cli.config[:output_path])

      html_generator = SandiMeter::HtmlGenerator.new
      html_generator.copy_assets!(cli.config[:output_path])
      html_generator.generate_data!(cli.config[:output_path])
      html_generator.generate_details!(cli.config[:output_path], data)

      index_html_path = File.join(cli.config[:output_path], 'index.html')
      unless cli.config[:quiet]
        open_in_browser(index_html_path)
      end
    else
      puts "WARNING!!! HTML mode works only if you scan folder."
    end
  end

  config_file_path = File.join(cli.config[:output_path], 'config.yml')
  config =  if File.exists?(config_file_path)
              YAML.load(File.read(config_file_path))
            else
              { thresholds: cli.config[:rule_thresholds] }
            end

  if RulesChecker.new(data, config).ok?
    exit 0
  else
    exit 1
  end
end
show_sandi_rules() click to toggle source
# File lib/sandi_meter/cli.rb, line 157
def show_sandi_rules
  puts %(
    1. 100 lines per class
    2. 5 lines per method
    3. 4 params per method call (and don't even try cheating with hash params)
    4. 1 instance variables per controller' action
  )
end

Private Class Methods

create_config_file(path, relative_path, content) click to toggle source
# File lib/sandi_meter/cli.rb, line 167
def create_config_file(path, relative_path, content)
  file_path = File.join(path, relative_path)
  if File.directory?(path) && !File.exists?(file_path)
    File.open(file_path, "w") do |file|
      file.write(content)
    end
  end
end
open_in_browser(url) click to toggle source
# File lib/sandi_meter/cli.rb, line 181
def open_in_browser(url)
  Launchy.open(url)
end
version_info() click to toggle source
# File lib/sandi_meter/cli.rb, line 176
def version_info
  # stolen from gem 'bubs' :)
  "SandiMeter ".tr('A-Za-z1-90', 'Ⓐ-Ⓩⓐ-ⓩ①-⑨⓪').split('').join(' ') + SandiMeter::VERSION
end