Class: Textus::Action::RuleLint

Inherits:
Base
  • Object
show all
Defined in:
lib/textus/action/rule_lint.rb

Class Method Summary collapse

Methods inherited from Base

inherited, proposal_from

Methods included from Contract::DSL

#arg, #cli, #cli_stdin, #contract, #contract?, #summary, #surfaces, #verb, #view

Class Method Details

.call(container:, call:, candidate_yaml:) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/textus/action/rule_lint.rb', line 16

def self.call(container:, call:, candidate_yaml:) # rubocop:disable Lint/UnusedMethodArgument
  root = container.root
  live_rules = current_rules(root)
  candidate_result = parse_candidate(candidate_yaml)
  return candidate_result if candidate_result.is_a?(Dry::Monads::Result::Failure)

  candidate_rules = candidate_result

  live_by_match = live_rules.to_h { |rule| [rule["match"], rule] }
  candidate_by_match = candidate_rules.to_h { |rule| [rule["match"], rule] }

  steps = (candidate_by_match.keys - live_by_match.keys).map do |match|
    { "op" => "add_rule", "match" => match, "rule" => candidate_by_match[match] }
  end
  (live_by_match.keys - candidate_by_match.keys).each do |match|
    steps << { "op" => "remove_rule", "match" => match }
  end
  (live_by_match.keys & candidate_by_match.keys).each do |match|
    next if live_by_match[match] == candidate_by_match[match]

    steps << {
      "op" => "change_rule",
      "match" => match,
      "from" => live_by_match[match],
      "to" => candidate_by_match[match],
    }
  end

  Success(Textus::Store::Jobs::Plan.new(steps: steps, warnings: []))
end

.current_rules(root) ⇒ Object



47
48
49
50
# File 'lib/textus/action/rule_lint.rb', line 47

def self.current_rules(root)
  raw = YAML.safe_load_file(File.join(root, "manifest.yaml"), permitted_classes: [Symbol], aliases: false)
  Array(raw["rules"])
end

.parse_candidate(yaml_text) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/textus/action/rule_lint.rb', line 52

def self.parse_candidate(yaml_text)
  raw = YAML.safe_load(yaml_text, permitted_classes: [Symbol], aliases: false)
  return Failure(code: :usage_error, message: "candidate is not a YAML mapping") unless raw.is_a?(Hash)

  Array(raw["rules"])
rescue Psych::Exception => e
  Failure(code: :usage_error, message: "candidate YAML parse error: #{e.message}")
end