lib/ridley/chef/cookbook.rb in ridley-2.1.0 vs lib/ridley/chef/cookbook.rb in ridley-2.2.0
- old
+ new
@@ -31,16 +31,16 @@
# @raise [IOError] if the path does not contain a metadata.rb or metadata.json file
#
# @return [Ridley::Chef::Cookbook]
def from_path(path, options = {})
path = Pathname.new(path)
- metadata = if path.join('metadata.rb').exist?
- Cookbook::Metadata.from_file(path.join('metadata.rb'))
- elsif path.join('metadata.json').exist?
- Cookbook::Metadata.from_json(File.read(path.join('metadata.json')))
+ metadata = if (metadata_file = path.join(Metadata::COMPILED_FILE_NAME)).exist?
+ Cookbook::Metadata.from_json(File.read(metadata_file))
+ elsif (metadata_file = path.join(Metadata::RAW_FILE_NAME)).exist?
+ Cookbook::Metadata.from_file(metadata_file)
else
- raise IOError, "no metadata.rb or metadata.json found at #{path}"
+ raise IOError, "no #{Metadata::COMPILED_FILE_NAME} or #{Metadata::RAW_FILE_NAME} found at #{path}"
end
metadata.name(options[:name].presence || metadata.name.presence || File.basename(path))
new(metadata.name, path, metadata)
end
@@ -52,10 +52,11 @@
extend Forwardable
attr_reader :cookbook_name
attr_reader :path
attr_reader :metadata
+
# @return [Hashie::Mash]
# a Hashie::Mash containing Cookbook file category names as keys and an Array of Hashes
# containing metadata about the files belonging to that category. This is used
# to communicate what a Cookbook looks like when uploading to a Chef Server.
#
@@ -115,10 +116,29 @@
checksums[self.class.checksum(file)] = file
end
end
end
+ # Compiles the raw metadata of the cookbook and writes it to a metadata.json file at the given
+ # out path. The default out path is the directory containing the cookbook itself.
+ #
+ # @param [String] out
+ # directory to output compiled metadata to
+ def compile_metadata(out = self.path)
+ File.open(File.join(out, Metadata::COMPILED_FILE_NAME), "w+") do |f|
+ f.write(metadata.to_json)
+ end
+ end
+
+ # Returns true if the cookbook instance has a compiled metadata file and false if it
+ # does not.
+ #
+ # @return [Boolean]
+ def compiled_metadata?
+ manifest[:root_files].any? { |file| file[:name].downcase == Metadata::COMPILED_FILE_NAME }
+ end
+
# @param [Symbol] category
# the category of file to generate metadata about
# @param [String] target
# the filepath to the file to get metadata information about
#
@@ -164,9 +184,14 @@
#
# example:
# "nginx-0.101.2"
def name
"#{cookbook_name}-#{version}"
+ end
+
+ # Reload the cookbook from the files located on disk at `#path`.
+ def reload
+ load_files
end
def validate
raise IOError, "No Cookbook found at: #{path}" unless path.exist?