diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2020-04-07 19:28:32 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2020-04-08 11:06:01 -0700 |
commit | 3443d63a8d9a699c48b59a7d369e4c7036ef7d1e (patch) | |
tree | c252c6b1c092165298c45cbb191a3a72adc8e4ae /lib | |
parent | 8b7e30a57a726539dfc0e493913eee464597b8fd (diff) | |
download | chef-3443d63a8d9a699c48b59a7d369e4c7036ef7d1e.tar.gz |
Add resource partials
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/provider.rb | 14 | ||||
-rw-r--r-- | lib/chef/resource.rb | 14 | ||||
-rw-r--r-- | lib/chef/run_context/cookbook_compiler.rb | 2 |
3 files changed, 30 insertions, 0 deletions
diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb index 3783bd9d5f..973d76325e 100644 --- a/lib/chef/provider.rb +++ b/lib/chef/provider.rb @@ -81,6 +81,20 @@ class Chef # Chef.deprecated(:use_inline_resources, "The use_inline_resources mode is no longer optional and the line enabling it can be removed") end + # Use a partial code fragment. This can be used for code sharing between multiple resources. + # + # Do not wrap the code fragment in a class or module. It also does not support the use of super + # to subclass any methods defined in the fragment, the methods will just be overwritten. + # + # @param partial [String] the code fragment to eval against the class + # + def self.use(partial) + dirname = ::File.dirname(partial) + basename = ::File.basename(partial, ".rb") + basename = basename[1..-1] if basename[0] == "_" + class_eval IO.read(::File.expand_path("#{dirname}/_#{basename}.rb", ::File.dirname(caller_locations.first.absolute_path))) + end + # delegate to the resource # def_delegators :@new_resource, :property_is_set? diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index bbbe714f3d..d18d6ab158 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -1461,6 +1461,20 @@ class Chef @default_description end + # Use a partial code fragment. This can be used for code sharing between multiple resources. + # + # Do not wrap the code fragment in a class or module. It also does not support the use of super + # to subclass any methods defined in the fragment, the methods will just be overwritten. + # + # @param partial [String] the code fragment to eval against the class + # + def self.use(partial) + dirname = ::File.dirname(partial) + basename = ::File.basename(partial, ".rb") + basename = basename[1..-1] if basename[0] == "_" + class_eval IO.read(::File.expand_path("#{dirname}/_#{basename}.rb", ::File.dirname(caller_locations.first.absolute_path))) + end + # The cookbook in which this Resource was defined (if any). # # @return Chef::CookbookVersion The cookbook in which this Resource was defined. diff --git a/lib/chef/run_context/cookbook_compiler.rb b/lib/chef/run_context/cookbook_compiler.rb index 4dda6aeb2e..36ee34c62e 100644 --- a/lib/chef/run_context/cookbook_compiler.rb +++ b/lib/chef/run_context/cookbook_compiler.rb @@ -245,11 +245,13 @@ class Chef def load_lwrps_from_cookbook(cookbook_name) files_in_cookbook_by_segment(cookbook_name, :providers).each do |filename| next unless File.extname(filename) == ".rb" + next if File.basename(filename).match?(/^_/) load_lwrp_provider(cookbook_name, filename) end files_in_cookbook_by_segment(cookbook_name, :resources).each do |filename| next unless File.extname(filename) == ".rb" + next if File.basename(filename).match?(/^_/) load_lwrp_resource(cookbook_name, filename) end |