From 4da88fb4cd57871f16649d50572240ac3f7420f0 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Fri, 13 Aug 2010 15:43:34 -0700 Subject: [#4496]+[#4521]+[#4522] Add structures to the AST to represent type definitions (classes, definitions, and nodes). Previously, type definitions were not represented directly in the AST. Instead, the parser would instantiate types and insert them into known_resource_types as soon as they were parsed. This made it difficult to distinguish which types had come from the file that was just parsed and which types had been loaded previously, which led to bug 4496. A side-effect of this change is that the user is no longer allowed to define types inside of conditional constructs (such as if/else). This was allowed before but had unexpected semantics (bugs 4521 and 4522). It is still possible, however, to place an "include" statement inside a conditional construct, and have that "include" statement trigger the autoloading of a file that instantiates types. --- lib/puppet/parser/ast/node.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 lib/puppet/parser/ast/node.rb (limited to 'lib/puppet/parser/ast/node.rb') diff --git a/lib/puppet/parser/ast/node.rb b/lib/puppet/parser/ast/node.rb new file mode 100644 index 000000000..c19a24ce0 --- /dev/null +++ b/lib/puppet/parser/ast/node.rb @@ -0,0 +1,17 @@ +require 'puppet/parser/ast/top_level_construct' + +class Puppet::Parser::AST::Node < Puppet::Parser::AST::TopLevelConstruct + attr_accessor :names + + def initialize(names, context = {}) + raise ArgumentError, "names should be an array" unless names.is_a? Array + @names = names + @context = context + end + + def instantiate(modname) + @names.collect do |name| + Puppet::Resource::Type.new(:node, name, @context.merge(:module_name => modname)) + end + end +end -- cgit From 6b278503021c4404904f56ced6995d0fbfa5b8fe Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Thu, 2 Sep 2010 18:10:37 -0700 Subject: [#4657] Customer-supplied .rb files are not compatible with multiple environments or staleness check Changed the resource type API to create AST objects rather than directly instantiating resource types. This allows the same code paths to be used to handle the results of parsing both .pp and .rb files. This makes .rb files work properly in multiple environments, because the types are now instantiated by code that is aware of which environment the compilation is happening in. It also reduces the risk of future changes breaking .rb file support. Also, switched to using "instance_eval" rather than "require" to evaluate the contents of the .rb file. This ensures that if the file has to be recompiled (because it became stale), it will actually get re-evaluated. As a side benefit, ResourceTypeAPI is now a class rather than a mixin to Object, so its methods do not pollute the global namespace. To reduce the risk of customers coming to rely on implementation details of the resource type API, changed its methods to return nil, and removed methods from it that were misleadingly labeled as "private". --- lib/puppet/parser/ast/node.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/ast/node.rb') diff --git a/lib/puppet/parser/ast/node.rb b/lib/puppet/parser/ast/node.rb index c19a24ce0..9767399f7 100644 --- a/lib/puppet/parser/ast/node.rb +++ b/lib/puppet/parser/ast/node.rb @@ -3,15 +3,18 @@ require 'puppet/parser/ast/top_level_construct' class Puppet::Parser::AST::Node < Puppet::Parser::AST::TopLevelConstruct attr_accessor :names - def initialize(names, context = {}) + def initialize(names, context = {}, &ruby_code) raise ArgumentError, "names should be an array" unless names.is_a? Array @names = names @context = context + @ruby_code = ruby_code end def instantiate(modname) @names.collect do |name| - Puppet::Resource::Type.new(:node, name, @context.merge(:module_name => modname)) + new_node = Puppet::Resource::Type.new(:node, name, @context.merge(:module_name => modname)) + new_node.ruby_code = @ruby_code if @ruby_code + new_node end end end -- cgit From ce9bf1edcaac4901de6e0a7da413d1742d216eb0 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Tue, 7 Sep 2010 18:01:42 -0700 Subject: Modified the error message that is generated when a class, definition, or node occurs in a conditional construct so that it contains the proper line number. --- lib/puppet/parser/ast/node.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/ast/node.rb') diff --git a/lib/puppet/parser/ast/node.rb b/lib/puppet/parser/ast/node.rb index c19a24ce0..4951a6365 100644 --- a/lib/puppet/parser/ast/node.rb +++ b/lib/puppet/parser/ast/node.rb @@ -1,7 +1,7 @@ require 'puppet/parser/ast/top_level_construct' class Puppet::Parser::AST::Node < Puppet::Parser::AST::TopLevelConstruct - attr_accessor :names + attr_accessor :names, :context def initialize(names, context = {}) raise ArgumentError, "names should be an array" unless names.is_a? Array -- cgit