class Puppet::InfoService::ClassInformationService

Public Class Methods

new() click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
 7 def initialize
 8   @file_to_result = {}
 9   @parser = Puppet::Pops::Parser::EvaluatingParser.new()
10 end

Public Instance Methods

classes_per_environment(env_file_hash) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
12 def classes_per_environment(env_file_hash)
13   # In this version of puppet there is only one way to parse manifests, as feature switches per environment
14   # are added or removed, this logic needs to change to compute the result per environment with the correct
15   # feature flags in effect.
16 
17   unless env_file_hash.is_a?(Hash)
18     raise ArgumentError, _('Given argument must be a Hash')
19   end
20 
21   result = {}
22 
23   # for each environment
24   #   for each file
25   #     if file already processed, use last result or error
26   #
27   env_file_hash.each do |env, files|
28     env_result = result[env] = {}
29     files.each do |f|
30       env_result[f] = result_of(f)
31     end
32   end
33   result
34 end

Private Instance Methods

extract_default(structure, p) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
81 def extract_default(structure, p)
82   value_expr = p.value
83   return structure if value_expr.nil?
84   default_value = value_as_literal(value_expr)
85   structure[:default_literal] = default_value unless default_value.nil?
86   structure[:default_source] = extract_value_source(value_expr)
87   structure
88 end
extract_param(p) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
71 def extract_param(p)
72   extract_default(extract_type({:name => p.name}, p), p)
73 end
extract_type(structure, p) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
75 def extract_type(structure, p)
76   return structure if p.type_expr.nil?
77   structure[:type] = typeexpr_to_string(p.type_expr)
78   structure
79 end
extract_value_source(value_expr) click to toggle source

Extracts the source for the expression

    # File lib/puppet/info_service/class_information_service.rb
107 def extract_value_source(value_expr)
108   value_expr.locator.extract_tree_text(value_expr)
109 end
literal_evaluator() click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
42 def literal_evaluator
43   @@literal_evaluator ||= Puppet::Pops::Evaluator::JsonStrictLiteralEvaluator.new
44 end
parse_file(f) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
54 def parse_file(f)
55   return {:error => _("The file %{f} does not exist") % { f: f }} unless Puppet::FileSystem.exist?(f)
56 
57   begin
58     parse_result = @parser.parse_file(f)
59     {:classes =>
60       parse_result.definitions.select {|d| d.is_a?(Puppet::Pops::Model::HostClassDefinition)}.map do |d|
61         {:name   => d.name,
62          :params => d.parameters.map {|p| extract_param(p) }
63         }
64       end
65     }
66   rescue StandardError => e
67     {:error => e.message }
68   end
69 end
result_of(f) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
46 def result_of(f)
47   entry =  @file_to_result[f]
48   if entry.nil?
49     @file_to_result[f] = entry = parse_file(f)
50   end
51   entry
52 end
type_parser() click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
38 def type_parser
39   Puppet::Pops::Types::TypeParser.singleton
40 end
typeexpr_to_string(type_expr) click to toggle source
   # File lib/puppet/info_service/class_information_service.rb
90 def typeexpr_to_string(type_expr)
91   begin
92     type_parser.interpret_any(type_expr, nil).to_s
93   rescue Puppet::ParseError
94     # type is to complex - contains expressions that are not literal
95     nil
96   end
97 end
value_as_literal(value_expr) click to toggle source
    # File lib/puppet/info_service/class_information_service.rb
 99 def value_as_literal(value_expr)
100   catch(:not_literal) do
101     return literal_evaluator.literal(value_expr)
102   end
103   nil
104 end