Module: Librarian::Chef::ManifestReader

Extended by:
ManifestReader
Included in:
ManifestReader
Defined in:
lib/librarian/chef/manifest_reader.rb

Constant Summary collapse

MANIFESTS =
%w(metadata.json metadata.yml metadata.yaml metadata.rb)

Instance Method Summary collapse

Instance Method Details

#check_manifest(name, manifest_path) ⇒ Object



40
41
42
43
# File 'lib/librarian/chef/manifest_reader.rb', line 40

def check_manifest(name, manifest_path)
  manifest = read_manifest(name, manifest_path)
  manifest["name"] == name
end

#compile_manifest(name, path) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/librarian/chef/manifest_reader.rb', line 25

def compile_manifest(name, path)
  # Inefficient, if there are many cookbooks with uncompiled metadata.
  require 'chef/json_compat'
  require 'chef/cookbook/metadata'
  md = ::Chef::Cookbook::Metadata.new
  md.name(name)
  md.from_file(path.join('metadata.rb').to_s)
  {"name" => md.name, "version" => md.version, "dependencies" => md.dependencies}
end

#manifest?(name, path) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/librarian/chef/manifest_reader.rb', line 35

def manifest?(name, path)
  path = Pathname.new(path)
  !!manifest_path(path)
end

#manifest_path(path) ⇒ Object



13
14
15
# File 'lib/librarian/chef/manifest_reader.rb', line 13

def manifest_path(path)
  MANIFESTS.map{|s| path.join(s)}.find{|s| s.exist?}
end

#read_manifest(name, manifest_path) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/librarian/chef/manifest_reader.rb', line 17

def read_manifest(name, manifest_path)
  case manifest_path.extname
  when ".json" then JSON.parse(binread(manifest_path))
  when ".yml", ".yaml" then YAML.load(binread(manifest_path))
  when ".rb" then compile_manifest(name, manifest_path.dirname)
  end
end