Class: Yamlint::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/yamlint/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil, output_format: 'colored') ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
13
# File 'lib/yamlint/runner.rb', line 7

def initialize(config: nil, output_format: 'colored')
  @config = config || Config.load_default
  @output_format = output_format
  @linter = Linter.new(@config)
  @formatter = Formatter.new(@config)
  @output = Output.get(output_format)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/yamlint/runner.rb', line 5

def config
  @config
end

#output_formatObject (readonly)

Returns the value of attribute output_format.



5
6
7
# File 'lib/yamlint/runner.rb', line 5

def output_format
  @output_format
end

Instance Method Details

#format(paths, dry_run: false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/yamlint/runner.rb', line 39

def format(paths, dry_run: false)
  files = collect_files(paths)
  changed_files = []

  files.each do |filepath|
    original = File.read(filepath)
    formatted = @formatter.format(original, dry_run:)

    if original != formatted
      changed_files << filepath
      File.write(filepath, formatted) unless dry_run
    end
  end

  {
    files: files.length,
    changed: changed_files.length,
    changed_files:,
    exit_code: 0
  }
end

#lint(paths) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yamlint/runner.rb', line 15

def lint(paths)
  files = collect_files(paths)
  total_problems = 0
  results = []

  files.each do |filepath|
    problems = @linter.lint_file(filepath)
    total_problems += problems.length

    output = @output.format(filepath, problems)
    results << output unless output.empty?
  end

  summary = @output.format_summary(files.length, total_problems)

  {
    files: files.length,
    problems: total_problems,
    output: results.join("\n\n"),
    summary:,
    exit_code: total_problems.positive? ? 1 : 0
  }
end