Class: Puppet::Pal::CatalogCompiler

Inherits:
Compiler show all
Defined in:
lib/puppet_pal.rb

Overview

A CatalogCompiler is a compiler that builds a catalog of resources and dependencies as a side effect of evaluating puppet language code. When the compilation of the given input manifest(s)/code string/file is finished the catalog is complete for encoding and use. It is also possible to evaluate more strings within the same compilation context to add or remove things from the catalog.

Instance Method Summary collapse

Methods inherited from Compiler

#call_function, #create, #evaluate_file, #evaluate_literal, #evaluate_string, #function_signature, #initialize, #list_functions, #parse_file, #parse_string, #type

Constructor Details

This class inherits a constructor from Puppet::Pal::Compiler

Instance Method Details

#evaluate(ast) ⇒ Object

Evaluates an AST obtained from ‘parse_string` or `parse_file` in topscope. If the ast is a `Puppet::Pops::Model::Program` (what is returned from the `parse` methods, any definitions in the program (that is, any function, plan, etc. that is defined will be made available for use).

Parameters:



356
357
358
359
360
361
362
363
364
365
# File 'lib/puppet_pal.rb', line 356

def evaluate(ast)
  if ast.is_a?(Puppet::Pops::Model::Program)
    bridged = Puppet::Parser::AST::PopsBridge::Program.new(ast)
    # define all catalog types
    internal_compiler.environment.known_resource_types.import_ast(bridged, "")
    bridged.evaluate(internal_compiler.topscope)
  else
    internal_evaluator.evaluate(topscope, ast)
  end
end

#has_catalog?Boolean

Returns true if this is a compiler that compiles a catalog. This implementation returns ‘true`

Returns:

  • (Boolean)

    true



331
332
333
# File 'lib/puppet_pal.rb', line 331

def has_catalog?
  true
end

#with_json_encoding(pretty: true, exclude_virtual: true) {|JsonCatalogEncoder.new(catalog, pretty: pretty, exclude_virtual: exclude_virtual)| ... } ⇒ Object

Calls a block of code and yields a configured ‘JsonCatalogEncoder` to the block.

Examples:

Get resulting catalog as pretty printed Json

Puppet::Pal.in_environment(...) do |pal|
  pal.with_catalog_compiler(...) do |compiler|
    compiler.with_json_encoding {| encoder | encoder.encode
  end
end

Yields:



345
346
347
# File 'lib/puppet_pal.rb', line 345

def with_json_encoding(pretty: true, exclude_virtual: true)
  yield JsonCatalogEncoder.new(catalog, pretty: pretty, exclude_virtual: exclude_virtual)
end