Module: PuppetLanguageServerSidecar::PuppetParserHelper

Defined in:
lib/puppet-languageserver-sidecar/puppet_parser_helper.rb

Class Method Summary collapse

Class Method Details

.compile_node_graph(content) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/puppet-languageserver-sidecar/puppet_parser_helper.rb', line 5

def self.compile_node_graph(content)
  result = PuppetLanguageServerSidecar::Protocol::PuppetNodeGraph.new

  begin
    node_graph = compile_to_pretty_relationship_graph(content)
    if node_graph.vertices.count.zero?
      result.set_error('There were no resources created in the node graph. Is there an include statement missing?')
      return result
    end

    result.vertices = []
    result.edges = []

    node_graph.vertices.each do |vertex|
      result.vertices << { label: vertex.to_s }
    end
    node_graph.edges.each do |edge|
      result.edges << { source: edge.source.to_s, target: edge.target.to_s }
    end
  rescue StandardError => e
    result.set_error("Error while parsing the file. #{e}")
  rescue LoadError => e
    result.set_error("Load error while parsing the file. #{e}")
  end

  result
end

.compile_to_catalog(string, node = Puppet::Node.new('test')) ⇒ Object



34
35
36
37
38
# File 'lib/puppet-languageserver-sidecar/puppet_parser_helper.rb', line 34

def self.compile_to_catalog(string, node = Puppet::Node.new('test'))
  Puppet[:code] = string
  # see lib/puppet/indirector/catalog/compiler.rb#filter
  Puppet::Parser::Compiler.compile(node).filter(&:virtual?)
end

.compile_to_pretty_relationship_graph(manifest, prioritizer = Puppet::Graph::SequentialPrioritizer.new) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet-languageserver-sidecar/puppet_parser_helper.rb', line 57

def self.compile_to_pretty_relationship_graph(manifest, prioritizer = Puppet::Graph::SequentialPrioritizer.new)
  graph = compile_to_relationship_graph(manifest, prioritizer)

  # Remove vertexes which just clutter the graph

  # Remove all of the Puppet::Type::Whit nodes.  This is an internal only class
  list = graph.vertices.select { |node| node.is_a?(Puppet::Type::Whit) }
  list.each { |node| graph.remove_vertex!(node) }

  # Remove all of the Puppet::Type::Schedule nodes
  list = graph.vertices.select { |node| node.is_a?(Puppet::Type::Schedule) }
  list.each { |node| graph.remove_vertex!(node) }

  # Remove all of the Puppet::Type::Filebucket nodes
  list = graph.vertices.select { |node| node.is_a?(Puppet::Type::Filebucket) }
  list.each { |node| graph.remove_vertex!(node) }

  graph
end

.compile_to_ral(manifest, node = Puppet::Node.new('test')) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/puppet-languageserver-sidecar/puppet_parser_helper.rb', line 40

def self.compile_to_ral(manifest, node = Puppet::Node.new('test'))
  # Add the node facts if they don't already exist
  node.merge(Facter.to_hash) if node.facts.nil?

  catalog = compile_to_catalog(manifest, node)
  ral = catalog.to_ral
  ral.finalize
  ral
end

.compile_to_relationship_graph(manifest, prioritizer = Puppet::Graph::SequentialPrioritizer.new) ⇒ Object



50
51
52
53
54
55
# File 'lib/puppet-languageserver-sidecar/puppet_parser_helper.rb', line 50

def self.compile_to_relationship_graph(manifest, prioritizer = Puppet::Graph::SequentialPrioritizer.new)
  ral = compile_to_ral(manifest)
  graph = Puppet::Graph::RelationshipGraph.new(prioritizer)
  graph.populate_from(ral)
  graph
end