Module: Yaml::Merge

Defined in:
lib/yaml/merge.rb,
lib/yaml/merge/emitter.rb,
lib/yaml/merge/version.rb,
lib/yaml/merge/debug_logger.rb,
lib/yaml/merge/merge_result.rb,
lib/yaml/merge/node_wrapper.rb,
lib/yaml/merge/smart_merger.rb,
lib/yaml/merge/file_analysis.rb,
sig/yaml/merge.rbs

Defined Under Namespace

Modules: DebugLogger, Version Classes: DestinationParseError, Emitter, Error, FileAnalysis, MergeResult, NodeWrapper, ParseError, SmartMerger, TemplateParseError

Constant Summary collapse

PACKAGE_NAME =
'yaml-merge'
DESTINATION_WINS_ARRAY_POLICY =
{
  surface: 'array',
  name: 'destination_wins_array'
}.freeze
BACKEND_REFERENCE =
TreeHaver::KREUZBERG_LANGUAGE_PACK_BACKEND
BACKEND_REGISTRY =
Struct.new(:registered, :mutex).new(false, Mutex.new)
VERSION =

Current gem version exposed at the traditional constant location.

Returns:

  • (String)
Version::VERSION

Class Method Summary collapse

Class Method Details

.analyze_yaml_document(parsed, dialect) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/yaml/merge.rb', line 100

def analyze_yaml_document(parsed, dialect)
  return unsupported_feature_parse_result("Unsupported YAML dialect #{dialect}.") unless dialect == 'yaml'
  return parse_error_result('YAML documents must parse to a mapping root.') unless parsed.is_a?(Hash)

  validated = validate_yaml_node(parsed, '')
  return { ok: false, diagnostics: [validated[:diagnostic]], policies: [] } unless validated[:ok]

  {
    ok: true,
    diagnostics: [],
    analysis: {
      kind: 'yaml',
      dialect: 'yaml',
      normalized_source: canonical_yaml(validated[:value]),
      document: validated[:value],
      root_kind: 'mapping',
      owners: collect_yaml_owners(validated[:value])
    },
    policies: []
  }
end

.available_yaml_backendsObject



53
54
55
# File 'lib/yaml/merge.rb', line 53

def available_yaml_backends
  yaml_backend_available_for_analysis?(BACKEND_REFERENCE.id) ? [BACKEND_REFERENCE] : []
end

.match_yaml_owners(template, destination) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/yaml/merge.rb', line 122

def match_yaml_owners(template, destination)
  destination_paths = destination[:owners].to_h { |owner| [owner[:path], true] }
  template_paths = template[:owners].to_h { |owner| [owner[:path], true] }

  {
    matched: template[:owners]
             .filter { |owner| destination_paths[owner[:path]] }
             .map { |owner| { template_path: owner[:path], destination_path: owner[:path] } },
    unmatched_template: template[:owners].map { |owner| owner[:path] }.reject { |path| destination_paths[path] },
    unmatched_destination: destination[:owners].map { |owner| owner[:path] }.reject { |path| template_paths[path] }
  }
end

.merge_yaml(template_source, destination_source, dialect, backend: nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/yaml/merge.rb', line 135

def merge_yaml(template_source, destination_source, dialect, backend: nil)
  requested = backend.to_s.empty? ? nil : backend.to_s
  unless yaml_backend_available_for_analysis?(requested)
    diagnostic_backend = requested || TreeHaver.current_backend_id || 'tree-sitter'
    return unsupported_feature_merge_result("Unsupported YAML backend #{diagnostic_backend}.")
  end

  merge_yaml_with_parser(template_source, destination_source, dialect,
                         backend: requested) do |source, parse_dialect|
    parse_yaml(source, parse_dialect, backend: requested)
  end
end

.merge_yaml_with_parser(template_source, destination_source, dialect, backend: nil) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/yaml/merge.rb', line 148

def merge_yaml_with_parser(template_source, destination_source, dialect, backend: nil)
  template = yield(template_source, dialect)
  return { ok: false, diagnostics: template[:diagnostics], policies: [] } unless template[:ok]

  destination = yield(destination_source, dialect)
  unless destination[:ok]
    return {
      ok: false,
      diagnostics: destination[:diagnostics].map do |diagnostic|
        diagnostic[:category] == 'parse_error' ? diagnostic.merge(category: 'destination_parse_error') : diagnostic
      end,
      policies: []
    }
  end

  template_document = template.dig(:analysis, :document)
  destination_document = destination.dig(:analysis, :document)
  unless template_document.is_a?(Hash) && destination_document.is_a?(Hash)
    return parse_error_merge_result('YAML documents must parse to a mapping root.')
  end

  {
    ok: true,
    diagnostics: [],
    output: with_tree_sitter_backend(backend) do
      SmartMerger.new(
        template_source,
        destination_source,
        add_template_only_nodes: true,
        merge_sequences: false
      ).merge_result.to_yaml
    end,
    policies: [DESTINATION_WINS_ARRAY_POLICY]
  }
rescue StandardError => e
  {
    ok: false,
    diagnostics: [{ severity: 'error', category: 'destination_parse_error', message: e.message }],
    policies: []
  }
end

.parse_yaml(source, dialect, backend: nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/yaml/merge.rb', line 83

def parse_yaml(source, dialect, backend: nil)
  return unsupported_feature_parse_result("Unsupported YAML dialect #{dialect}.") unless dialect == 'yaml'

  requested = backend.to_s.empty? ? nil : backend.to_s
  unless yaml_backend_available_for_analysis?(requested)
    diagnostic_backend = requested || TreeHaver.current_backend_id || 'tree-sitter'
    return unsupported_feature_parse_result("Unsupported YAML backend #{diagnostic_backend}.")
  end

  tree = parse_tree_sitter_source(:yaml, source, backend: requested)
  collect_parse_errors(tree.root_node)

  analyze_yaml_document(yaml_value_from_tree(tree.root_node, source), dialect)
rescue TreeHaver::Error, StandardError => e
  parse_error_result(e.message)
end

.register_backend!Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yaml/merge.rb', line 32

def register_backend!
  BACKEND_REGISTRY.mutex.synchronize do
    return if BACKEND_REGISTRY.registered && TreeHaver.registered_languages(:yaml).any?

    TreeHaver::BackendRegistry.register(BACKEND_REFERENCE)

    grammar_finder = TreeHaver::GrammarFinder.new(:yaml)
    grammar_finder.register! if grammar_finder.available?

    BACKEND_REGISTRY.registered = true
  end
end

.yaml_backend_feature_profile(backend: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/yaml/merge.rb', line 57

def yaml_backend_feature_profile(backend: nil)
  resolved_backend = resolve_backend(backend)
  unless resolved_backend == BACKEND_REFERENCE.id && yaml_backend_available_for_analysis?(resolved_backend)
    return unsupported_feature_result("Unsupported YAML backend #{resolved_backend}.")
  end

  yaml_feature_profile.merge(
    backend: BACKEND_REFERENCE.id,
    backend_ref: BACKEND_REFERENCE.to_h
  )
end

.yaml_feature_profileObject



45
46
47
48
49
50
51
# File 'lib/yaml/merge.rb', line 45

def yaml_feature_profile
  {
    family: 'yaml',
    supported_dialects: ['yaml'],
    supported_policies: [DESTINATION_WINS_ARRAY_POLICY]
  }
end

.yaml_plan_context(backend: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/yaml/merge.rb', line 69

def yaml_plan_context(backend: nil)
  profile = yaml_backend_feature_profile(backend: backend)
  return profile if profile[:ok] == false

  {
    family_profile: yaml_feature_profile,
    feature_profile: {
      backend: profile[:backend],
      supports_dialects: false,
      supported_policies: profile[:supported_policies]
    }
  }
end