lib/chef/cookbook/cookbook_version_loader.rb in chef-11.6.0.hotfix.1 vs lib/chef/cookbook/cookbook_version_loader.rb in chef-11.6.0.rc.0

- old
+ new

@@ -16,19 +16,18 @@ :library_filenames, :resource_filenames, :provider_filenames] - attr_reader :cookbook_name + attr_reader :cookbook_pathname attr_reader :cookbook_settings attr_reader :metadata_filenames def initialize(path, chefignore=nil) @cookbook_path = File.expand_path( path ) - @cookbook_name = File.basename( path ) + @cookbook_pathname = File.basename( path ) @chefignore = chefignore - @metadata = Hash.new @relative_path = /#{Regexp.escape(@cookbook_path)}\/(.+)$/ @cookbook_settings = { :attribute_filenames => {}, :definition_filenames => {}, :recipe_filenames => {}, @@ -63,17 +62,22 @@ end if empty? Chef::Log.warn "found a directory #{cookbook_name} in the cookbook path, but it contains no cookbook files. skipping." end + @cookbook_settings end + def cookbook_name + metadata.name || @cookbook_pathname + end + def cookbook_version return nil if empty? - Chef::CookbookVersion.new(@cookbook_name.to_sym).tap do |c| + Chef::CookbookVersion.new(@cookbook_pathname.to_sym).tap do |c| c.root_dir = @cookbook_path c.attribute_filenames = cookbook_settings[:attribute_filenames].values c.definition_filenames = cookbook_settings[:definition_filenames].values c.recipe_filenames = cookbook_settings[:recipe_filenames].values c.template_filenames = cookbook_settings[:template_filenames].values @@ -81,16 +85,20 @@ c.library_filenames = cookbook_settings[:library_filenames].values c.resource_filenames = cookbook_settings[:resource_filenames].values c.provider_filenames = cookbook_settings[:provider_filenames].values c.root_filenames = cookbook_settings[:root_filenames].values c.metadata_filenames = @metadata_filenames - c.metadata = metadata(c) + c.metadata = load_metadata(c) end end + def metadata + @metadata ||= load_metadata(nil) + end + # Generates the Cookbook::Metadata object - def metadata(cookbook_version) + def load_metadata(cookbook_version) @metadata = Chef::Cookbook::Metadata.new(cookbook_version) @metadata_filenames.each do |metadata_file| case metadata_file when /\.rb$/ apply_ruby_metadata(metadata_file) @@ -98,10 +106,11 @@ apply_json_metadata(metadata_file) else raise RuntimeError, "Invalid metadata file: #{metadata_file} for cookbook: #{cookbook_version}" end end + Chef::Log.warn "Inferring cookbook name from directory name (#@cookbook_pathname) is deprecated, please set a name in the metadata." unless @metadata.name @metadata end def empty? @cookbook_settings.values.all? { |files_hash| files_hash.empty? } @@ -148,21 +157,21 @@ end end def apply_ruby_metadata(file) begin - @metadata.from_file(file) + metadata.from_file(file) rescue JSON::ParserError - Chef::Log.error("Error evaluating metadata.rb for #@cookbook_name in " + file) + Chef::Log.error("Error evaluating metadata.rb for #{cookbook_name} in " + file) raise end end def apply_json_metadata(file) begin - @metadata.from_json(IO.read(file)) + metadata.from_json(IO.read(file)) rescue JSON::ParserError - Chef::Log.error("Couldn't parse cookbook metadata JSON for #@cookbook_name in " + file) + Chef::Log.error("Couldn't parse cookbook metadata JSON for #{cookbook_name} in " + file) raise end end end