exe/cuke_linter
#!/usr/bin/env ruby
require 'cuke_linter'
require 'optparse'
params = {}
params[:paths] = []
params[:formatters] = []
params[:outs] = []
params[:requires] = []
# rubocop:disable Metrics/BlockLength
parser = OptionParser.new do |options|
options.set_summary_width(30)
options.on('-p', '--path PATH', String,
'The file path that should be linted. Can be a file or directory.',
'This option can be specified multiple times in order to lint',
'multiple, unconnected locations.') do |path|
params[:paths] << path
end
options.on('-f', '--formatter FORMATTER', String,
'The formatter used for generating linting output. This option',
'can be specified multiple times in order to use more than one',
'formatter. Formatters must be specified using their fully',
'qualified class name (e.g CukeLinter::PrettyFormatter). Uses',
'the default formatter if none are specified.') do |format|
params[:formatters] << format
end
options.on('-o', '--out OUT', String,
'The file path to which linting results are output. Can be specified',
'multiple times. Specified files are matched to formatters in the',
'same order that the formatters are specified. Any formatter without',
'a corresponding file path will output to STDOUT instead.') do |out|
params[:outs] << out
end
options.on('-r', '--require FILEPATH', String,
'A file that will be required before further processing. Likely',
'needed when using custom linters or formatters in order to ensure',
'that the specified classes have been read into memory. This option',
'can be specified multiple times in order to load more than one file.') do |file_path|
params[:requires] << file_path
end
options.on('-c', '--config FILEPATH', String,
'The configuration file that will be used. Will use the default',
'configuration file (if present) if this option is not specified.') do |file_path|
if params[:config]
puts 'Cannot specify more than one configuration file!'
exit(2)
end
params[:config] = file_path
end
options.on('-h', '--help', 'Display the help that you are reading now.') do
puts options.help
exit
end
options.on('-v', '--version', 'Display the version of the gem being used.') do
puts CukeLinter::VERSION
exit
end
end
# rubocop:enable Metrics/BlockLength
begin
parser.parse!
rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
puts e.message
puts parser.help
exit(2)
end
require_files = params[:requires]
require_files.each do |file|
require file
end
file_paths = params[:paths]
formatters = params[:formatters].map { |formatter| Kernel.const_get(formatter).new }
output_paths = params[:outs]
bundled_formatters = [].tap do |formatter_output_pairs|
[formatters.count, output_paths.count].max.times do |count|
formatter_output_pairs << [formatters[count] || CukeLinter::PrettyFormatter.new, output_paths[count]]
end
end
options = {}
options[:formatters] = bundled_formatters unless bundled_formatters.empty?
options[:file_paths] = file_paths
if params[:config]
CukeLinter.load_configuration(config_file_path: params[:config])
elsif File.exist?("#{Dir.pwd}/.cuke_linter")
CukeLinter.load_configuration
end
results = CukeLinter.lint(**options)
exit(1) unless results.empty?