lib/puppet-debugger/support.rb in puppet-debugger-0.19.0 vs lib/puppet-debugger/support.rb in puppet-debugger-1.0.0

- old
+ new

@@ -81,29 +81,29 @@ hostclasses: scope.environment.known_resource_types.hostclasses.keys, definitions: scope.environment.known_resource_types.definitions.keys, nodes: scope.environment.known_resource_types.nodes.keys } if sites = scope.environment.known_resource_types.instance_variable_get(:@sites) - res[:sites] = scope.environment.known_resource_types.instance_variable_get(:@sites).first + res[:sites] = sites end - if scope.environment.known_resource_types.respond_to?(:applications) - res[:applications] = scope.environment.known_resource_types.applications.keys + if apps = scope.environment.known_resource_types.respond_to?(:applications) + res[:applications] = apps end # some versions of puppet do not support capabilities - if scope.environment.known_resource_types.respond_to?(:capability_mappings) - res[:capability_mappings] = scope.environment.known_resource_types.capability_mappings.keys + if maps = scope.environment.known_resource_types.respond_to?(:capability_mappings) + res[:capability_mappings] = maps end res end # this is required in order to load things only when we need them def do_initialize Puppet.initialize_settings Puppet[:parser] = 'future' # this is required in order to work with puppet 3.8 Puppet[:trusted_node_data] = true - rescue ArgumentError => e - rescue Puppet::DevError => e + rescue ArgumentError + rescue Puppet::DevError # do nothing otherwise calling init twice raises an error end # @param String - any valid puppet language code # @return Hostclass - a puppet Program object which is considered the main class @@ -129,34 +129,44 @@ end # Create the "main" class for the content - this content will get merged with all other "main" content ::Puppet::Parser::AST::Hostclass.new('', code: ast_code) end + # @return [String] the path to the manifest file + # @param input [String] the manfiest content + # @summary creates a manifest file unless one already exist + def manifest_file(input) + file = Tempfile.new(['puppet_debugger_input', '.pp']) + File.open(file, 'w') do |f| + f.write(input) + end + file + end + # @param String - any valid puppet language code # @return Object - returns either a string of the result or object from puppet evaulation - def puppet_eval(input) + def puppet_eval(input, file: nil) # in order to add functions to the scope the loaders must be created # in order to call native functions we need to set the global_scope - ast = generate_ast(input) # record the input for puppet to retrieve and reference later - file = Tempfile.new(['puppet_debugger_input', '.pp']) - File.open(file, 'w') do |f| - f.write(input) - end - Puppet.override({ current_environment: puppet_environment, code: input, + manifest_file = file || manifest_file(input) + manfifest_content = input || File.read(manifest_file) + ast = generate_ast(manfifest_content) + + Puppet.override({ current_environment: puppet_environment, manifest: manifest_file, global_scope: scope, loaders: scope.compiler.loaders }, 'For puppet-debugger') do - # because the repl is not a module we leave the modname blank + # because the debugger is not a module we leave the modname blank scope.environment.known_resource_types.import_ast(ast, '') exec_hook :before_eval, '', self, self if bench result = nil time = Benchmark.realtime do - result = parser.evaluate_string(scope, input, File.expand_path(file)) + result = parser.evaluate_string(scope, manfifest_content, File.expand_path(manifest_file)) end out = [result, "Time elapsed #{(time * 1000).round(2)} ms"] else - out = parser.evaluate_string(scope, input, File.expand_path(file)) + out = parser.evaluate_string(scope, manfifest_content, File.expand_path(manifest_file)) end exec_hook :after_eval, out, self, self out end end