Class: ERBLint::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/erb_lint/cli.rb

Defined Under Namespace

Classes: ExitWithFailure, ExitWithSuccess, Stats

Constant Summary collapse

DEFAULT_CONFIG_FILENAME =
'.erb-lint.yml'
DEFAULT_LINT_ALL_GLOB =
"**/*.html{+*,}.erb"

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



27
28
29
30
31
32
# File 'lib/erb_lint/cli.rb', line 27

def initialize
  @options = {}
  @config = nil
  @files = []
  @stats = Stats.new
end

Instance Method Details

#run(args = ARGV) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/erb_lint/cli.rb', line 34

def run(args = ARGV)
  load_options(args)
  @files = args.dup

  load_config

  if lint_files.empty?
    success!("no files given...\n#{option_parser}")
  end

  ensure_files_exist(lint_files)

  if enabled_linter_classes.empty?
    failure!('no linter available with current configuration')
  end

  puts "Linting #{lint_files.size} files with "\
    "#{enabled_linter_classes.size} #{'autocorrectable ' if autocorrect?}linters..."
  puts

  lint_files.each do |filename|
    begin
      run_with_corrections(filename)
    rescue => e
      puts "Exception occured when processing: #{relative_filename(filename)}"
      puts e.message
      puts e.backtrace.join("\n").red
      puts
    end
  end

  if @stats.corrected > 0
    corrected_found_diff = @stats.found - @stats.corrected
    if corrected_found_diff > 0
      warn "#{@stats.corrected} error(s) corrected and #{corrected_found_diff} error(s) remaining in ERB files".red
    else
      puts "#{@stats.corrected} error(s) corrected in ERB files".green
    end
  elsif @stats.found > 0
    warn "#{@stats.found} error(s) were found in ERB files".red
  else
    puts "No errors were found in ERB files".green
  end

  @stats.found == 0
rescue OptionParser::InvalidOption, OptionParser::InvalidArgument, ExitWithFailure => e
  warn e.message.red
  false
rescue ExitWithSuccess => e
  puts e.message
  true
rescue => e
  warn "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}".red
  false
end