class Puppet::Pops::Loader::PuppetPlanInstantiator

The PuppetPlanInstantiator instantiates a Puppet::Functions::PuppetFunction given a Puppet Programming language source that when called evaluates the Puppet logic it contains.

Public Class Methods

create(loader, typed_name, source_ref, pp_code_string) click to toggle source

Produces an instance of the Function class with the given typed_name, or fails with an error if the given puppet source does not produce this instance when evaluated.

@param loader [Loader] The loader the function is associated with @param typed_name [TypedName] the type / name of the function to load @param source_ref [URI, String] a reference to the source / origin of the puppet code to evaluate @param pp_code_string [String] puppet code in a string

@return [Functions::Function] - an instantiated function with global scope closure associated with the given loader

   # File lib/puppet/pops/loader/puppet_plan_instantiator.rb
18 def self.create(loader, typed_name, source_ref, pp_code_string)
19   parser = Parser::EvaluatingParser.new()
20 
21   # parse and validate
22   result = parser.parse_string(pp_code_string, source_ref)
23 
24   # The parser attaches all definitions, including those nested in apply
25   # blocks, to the Program object. Node definitions in apply blocks are
26   # perfectly legal and don't count as the file containing multiple
27   # definitions for this purpose. By this point, we've already validated that
28   # there are no node definitions *outside* apply blocks, so we simply ignore
29   # them here.
30   definitions = result.definitions.reject { |definition| definition.is_a?(Puppet::Pops::Model::NodeDefinition) }
31 
32   # Only one plan is allowed (and no other definitions)
33   case definitions.size
34   when 0
35     raise ArgumentError, _("The code loaded from %{source_ref} does not define the plan '%{plan_name}' - it is empty.") % { source_ref: source_ref, plan_name: typed_name.name }
36   when 1
37     # ok
38   else
39     raise ArgumentError, _("The code loaded from %{source_ref} must contain only the plan '%{plan_name}' - it has additional definitions.") % { source_ref: source_ref, plan_name: typed_name.name }
40   end
41   the_plan_definition = definitions[0]
42 
43   unless the_plan_definition.is_a?(Model::PlanDefinition)
44     raise ArgumentError, _("The code loaded from %{source_ref} does not define the plan '%{plan_name}' - no plan found.") % { source_ref: source_ref, plan_name: typed_name.name }
45   end
46   unless the_plan_definition.name == typed_name.name
47     expected = typed_name.name
48     actual = the_plan_definition.name
49     raise ArgumentError, _("The code loaded from %{source_ref} produced plan with the wrong name, expected %{expected}, actual %{actual}") % { source_ref: source_ref, expected: expected, actual: actual }
50   end
51   unless result.body == the_plan_definition
52     raise ArgumentError, _("The code loaded from %{source} contains additional logic - can only contain the plan %{plan_name}") % { source: source_ref, plan_name: typed_name.name }
53   end
54 
55   # Adapt the function definition with loader - this is used from logic contained in it body to find the
56   # loader to use when making calls to the new function API. Such logic have a hard time finding the closure (where
57   # the loader is known - hence this mechanism
58   private_loader = loader.private_loader
59   Adapters::LoaderAdapter.adapt(the_plan_definition).loader_name = private_loader.loader_name
60 
61   # Cannot bind loaded functions to global scope, that must be done without binding that scope as
62   # loaders survive a compilation.
63   closure_scope = nil
64 
65   created = create_function_class(the_plan_definition)
66   # create the function instance - it needs closure (scope), and loader (i.e. where it should start searching for things
67   # when calling functions etc.
68   # It should be bound to global scope
69 
70   created.new(closure_scope, private_loader)
71 end
create_from_model(plan_definition, loader) click to toggle source

Creates Function class and instantiates it based on a FunctionDefinition model @return [Array<TypedName, Functions.Function>] - array of

typed name, and an instantiated function with global scope closure associated with the given loader
   # File lib/puppet/pops/loader/puppet_plan_instantiator.rb
77 def self.create_from_model(plan_definition, loader)
78   created = create_function_class(plan_definition)
79   typed_name = TypedName.new(:plan, plan_definition.name)
80   [typed_name, created.new(nil, loader)]
81 end
create_function_class(plan_definition) click to toggle source
   # File lib/puppet/pops/loader/puppet_plan_instantiator.rb
83 def self.create_function_class(plan_definition)
84   # Create a 4x function wrapper around a named closure
85   Puppet::Functions.create_function(plan_definition.name, Puppet::Functions::PuppetFunction) do
86     # TODO: should not create a new evaluator per function
87     init_dispatch(Evaluator::Closure::Named.new(
88       plan_definition.name,
89       Evaluator::EvaluatorImpl.new(), plan_definition))
90   end
91 end