#!/usr/bin/env ruby # frozen_string_literal: true require 'chutney' require 'chutney/formatter' require 'chutney/formatter/json_formatter' require 'chutney/formatter/pie_formatter' require 'chutney/formatter/rainbow_formatter' require 'optparse' formatters = Set.new quiet = false # rubocop:disable Metrics/BlockLength OptionParser.new do |opts| opts.banner = 'Usage: chutney [files]' opts.on('-f', '--format [formatter]', 'One of JSONFormatter, PieFormatter or RainbowFormatter (default).') do |formatter| raise 'No Such Formatter' unless %w[JSONFormatter PieFormatter RainbowFormatter].include? formatter formatters << formatter end opts.on('-q', '--quiet', 'Disable chutney usage warnings. Does not affect the output of the formatters.') do quiet = true end opts.on('-v', '--version', 'Display the version.') do puts Chutney::VERSION exit end opts.on('-l', '--linters', 'List the linter status by this configuration and exit.') do pastel = Pastel.new chutney_config = Chutney::ChutneyLint.new.configuration max_name_length = chutney_config.keys.map(&:length).max + 1 chutney_config.each do |linter, value| print pastel.cyan(linter.ljust(max_name_length)) if value['Enabled'] puts pastel.green('enabled') else puts pastel.red('disabled') end end exit end opts.on('--init', 'Install a `chutney.yml` configuration file.') do config_dest = if File.exist?('config') && File.directory?('config') 'config' else '.' end config_path = File.join(config_dest, 'chutney.yml') default_path = Chutney::ChutneyLint.new.configuration.default_configuration_path if File.exist?(config_path) puts "#{config_path} already exists - remove it first if you want to overwrite." else FileUtils.cp(default_path, config_path) puts "#{config_path} created." end exit end end.parse! # rubocop:enable Metrics/BlockLength formatters << 'RainbowFormatter' if formatters.empty? files = ARGV.map { |pattern| Dir.glob(pattern) }.flatten files = Dir.glob('features/**/*.feature') if ARGV.empty? linter = Chutney::ChutneyLint.new(*files) linter.configuration.quiet! if quiet report = linter.analyse formatters.each do |formatter| f = Object.const_get("Chutney::#{formatter}").new f.results = report f.format end basic_formatter = Chutney::Formatter.new basic_formatter.results = report if basic_formatter.files_with_issues.empty? exit else exit(false) end