Class: Herb::Embedded::Runner

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

Overview

The Ruby replacement for cli.js's filesystem role: discovers files, iterates, and aggregates results. Formatters are pure functions of the Report this produces.

Instance Method Summary collapse

Constructor Details

#initialize(root:, config:, bridge:) ⇒ Runner

Returns a new instance of Runner.



14
15
16
17
18
19
# File 'lib/herb/embedded/runner.rb', line 14

def initialize(root:, config:, bridge:)
  @root = root
  @config = config
  @bridge = bridge
  @custom_rules_loaded = false
end

Instance Method Details

#fix(paths = nil, rules: nil, unsafe: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/herb/embedded/runner.rb', line 33

def fix(paths = nil, rules: nil, unsafe: false)
  load_custom_rules!

  Report.new.tap do |report|
    files_for(paths).each do |absolute_path|
      file = relative_path(absolute_path)
      original_source = File.read(absolute_path)
      result = @bridge.autofix(original_source, file: file, rules: rules, unsafe: unsafe)

      File.write(absolute_path, result[:source]) if result[:source] != original_source

      diagnostics = @bridge.lint(result[:source], file: file, rules: rules)
      report.add(LintResult.new(file: file, diagnostics: diagnostics))
    end
  end
end

#load_custom_rules!Object

Idempotent and safe to call ahead of #run/#fix — e.g. so a caller can validate rule names (like a CLI's --only flag) against Bridge#rule_names with custom rules already registered.



53
54
55
56
57
58
# File 'lib/herb/embedded/runner.rb', line 53

def load_custom_rules!
  return if @custom_rules_loaded

  CustomRuleLoader.new(root: @root, bridge: @bridge).load_all
  @custom_rules_loaded = true
end

#run(paths = nil, rules: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/herb/embedded/runner.rb', line 21

def run(paths = nil, rules: nil)
  load_custom_rules!

  Report.new.tap do |report|
    files_for(paths).each do |absolute_path|
      file = relative_path(absolute_path)
      diagnostics = @bridge.lint(File.read(absolute_path), file: file, rules: rules)
      report.add(LintResult.new(file: file, diagnostics: diagnostics))
    end
  end
end