Module: Yaml::Merge

Defined in:
lib/yaml/merge/provider.rb,
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

Overview

Portable YAML workflow integration.

Defined Under Namespace

Modules: DebugLogger, Version Classes: DestinationParseError, Emitter, Error, FileAnalysis, MergeResult, NodeWrapper, ParseError, Provider, 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



101
102
103
# File 'lib/yaml/merge.rb', line 101

def analyze_yaml_document(parsed, dialect)
  Ast::Merge::YamlDocument.analyze(parsed, dialect)
end

.available_yaml_backendsObject



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

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

.match_yaml_owners(template, destination) ⇒ Object



105
106
107
# File 'lib/yaml/merge.rb', line 105

def match_yaml_owners(template, destination)
  Ast::Merge::YamlDocument.match_owners(template, destination)
end

.merge_providerProvider

Returns:



69
70
71
# File 'lib/yaml/merge/provider.rb', line 69

def merge_provider
  @merge_provider ||= Provider.new
end

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



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/yaml/merge.rb', line 109

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/yaml/merge.rb', line 122

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



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

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



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

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

.register_provider!(replace: false) ⇒ Object

Parameters:

  • replace: (Boolean) (defaults to: false)

Returns:

  • (Object)


73
74
75
# File 'lib/yaml/merge/provider.rb', line 73

def register_provider!(replace: false)
  Ast::Merge.register_provider(merge_provider, replace: replace)
end

.yaml_backend_feature_profile(backend: nil) ⇒ Object



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

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



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

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

.yaml_plan_context(backend: nil) ⇒ Object



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

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