diff options
author | John Keiser <john@johnkeiser.com> | 2015-05-02 00:11:40 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-05-13 13:43:14 -0700 |
commit | 8e09b80cbeeef8a1a396434bec9dc76737061b13 (patch) | |
tree | d1f6553fb27141088bcbd2b50c5faead8251ce26 /lib/chef | |
parent | 200934f63c02c2b70532eb8a010337ac3b8e9803 (diff) | |
download | chef-8e09b80cbeeef8a1a396434bec9dc76737061b13.tar.gz |
Move LWRP class deprecation warning to const_missing
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/dsl/resources.rb | 16 | ||||
-rw-r--r-- | lib/chef/provider/lwrp_base.rb | 1 | ||||
-rw-r--r-- | lib/chef/resource.rb | 33 | ||||
-rw-r--r-- | lib/chef/resource/lwrp_base.rb | 26 | ||||
-rw-r--r-- | lib/chef/resource_resolver.rb | 2 |
5 files changed, 45 insertions, 33 deletions
diff --git a/lib/chef/dsl/resources.rb b/lib/chef/dsl/resources.rb index 482b14e3aa..4072ff2c89 100644 --- a/lib/chef/dsl/resources.rb +++ b/lib/chef/dsl/resources.rb @@ -8,14 +8,20 @@ class Chef # @api private module Resources def self.add_resource_dsl(dsl_name) - module_eval <<-EOM, __FILE__, __LINE__+1 - def #{dsl_name}(name, created_at=nil, &block) - declare_resource(#{dsl_name.inspect}, name, created_at || caller[0], &block) + begin + module_eval(<<-EOM, __FILE__, __LINE__+1) + def #{dsl_name}(name, created_at=nil, &block) + declare_resource(#{dsl_name.inspect}, name, created_at || caller[0], &block) + end + EOM + rescue SyntaxError + define_method(dsl_name.to_sym) do |name, created_at=nil, &block| + declare_resource(dsl_name, name, created_at || caller[0], &block) end - EOM + end end def self.remove_resource_dsl(dsl_name) - remove_method dsl_name if method_defined?(dsl_name) + remove_method(dsl_name) if method_defined?(dsl_name) end end end diff --git a/lib/chef/provider/lwrp_base.rb b/lib/chef/provider/lwrp_base.rb index 492ddda6da..0eadfe9467 100644 --- a/lib/chef/provider/lwrp_base.rb +++ b/lib/chef/provider/lwrp_base.rb @@ -93,6 +93,7 @@ class Chef else provider_class = Class.new(self) Chef::Provider.const_set(class_name, provider_class) + provider_class.provides provider_name.to_sym provider_class.class_from_file(filename) Chef::Log.debug("Loaded contents of #{filename} into a provider named #{provider_name} defined in Chef::Provider::#{class_name}") end diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 7e0dd6395a..565313e3e1 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -1133,16 +1133,43 @@ class Chef begin rname = convert_to_class_name(short_name.to_s) result = Chef::Resource.const_get(rname) - Chef::Log.deprecation("Class Chef::Resource::#{rname} does not declare `provides #{short_name.inspect}`.") - Chef::Log.deprecation("This will no longer work in Chef 13: you must use `provides` to provide DSL.") - result + if result.is_a?(Chef::Resource) + Chef::Log.deprecation("Class Chef::Resource::#{rname} does not declare `provides #{short_name.inspect}`.") + Chef::Log.deprecation("This will no longer work in Chef 13: you must use `provides` to provide DSL.") + result + end rescue NameError nil end end + def self.const_missing(class_name) + if deprecated_constants[class_name.to_sym] + Chef::Log.deprecation("Using an LWRP by its name (#{class_name}) directly is no longer supported in Chef 12 and will be removed. Use Chef::Resource.resource_for_node(node, name) instead.") + deprecated_constants[class_name.to_sym] + else + raise NameError, "uninitialized constant Chef::Resource::#{class_name}" + end + end + + # @api private + def self.create_deprecated_lwrp_class(resource_class) + # Create a class in Chef::Resource::MyResource with deprecation + # warnings if you try to access it + class_name = convert_to_class_name(resource_class.resource_name) + if Chef::Resource.const_defined?(class_name, false) + Chef::Log.warn "#{class_name} already exists! Cannot create deprecation class for #{resource_class}" + else + deprecated_constants[class_name.to_sym] = resource_class + end + end + private + def self.deprecated_constants + @deprecated_constants ||= {} + end + def lookup_provider_constant(name) begin self.class.provider_base.const_get(convert_to_class_name(name.to_s)) diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb index f702db5c96..3e3670b4ac 100644 --- a/lib/chef/resource/lwrp_base.rb +++ b/lib/chef/resource/lwrp_base.rb @@ -50,6 +50,7 @@ class Chef resource_class = Class.new(self) resource_class.resource_name = resource_name resource_class.run_context = run_context + resource_class.provides resource_name.to_sym resource_class.class_from_file(filename) # Respect resource_name set inside the LWRP @@ -62,34 +63,11 @@ class Chef Chef::Log.debug("Loaded contents of #{filename} into #{resource_class}") - create_deprecated_class_in_chef_resource(resource_class) + Chef::Resource.create_deprecated_lwrp_class(resource_class) resource_class end - def self.create_deprecated_class_in_chef_resource(resource_class) - # Create a class in Chef::Resource::MyResource with deprecation - # warnings if you try to instantiate or inherit it (for Chef 12 compatibility) - class_name = convert_to_class_name(resource_class.resource_name) - if Chef::Resource.const_defined?(class_name, false) - Chef::Log.debug "#{class_name} already exists! Cannot create deprecation class #{resource_class}" - else - Chef::Resource.const_set(class_name, Class.new(resource_class) do - self.resource_name = resource_class.resource_name - self.run_context = resource_class.run_context - define_method(:initialize) do |*args, &block| - Chef::Log.warn("Using an LWRP by its name (#{class_name}) directly is no longer supported in Chef 12 and will be removed.") - super(*args, &block) - end - define_singleton_method(:inherited) do |*args, &block| - Chef::Log.warn("Using an LWRP by its name (#{class_name}) directly is no longer supported in Chef 12 and will be removed.") - super(*args, &block) - end - end) - end - Chef::Resource.const_get(class_name) - end - def self.resource_name(arg = NULL_ARG) if arg.equal?(NULL_ARG) @resource_name || dsl_name diff --git a/lib/chef/resource_resolver.rb b/lib/chef/resource_resolver.rb index 4560bac347..d4151e7125 100644 --- a/lib/chef/resource_resolver.rb +++ b/lib/chef/resource_resolver.rb @@ -30,7 +30,7 @@ class Chef def initialize(node, resource) @node = node - @resource = resource + @resource = resource.to_sym end # return a deterministically sorted list of Chef::Resource subclasses |