Module: ChefZero::ChefData::CookbookData

Defined in:
lib/chef_zero/chef_data/cookbook_data.rb

Defined Under Namespace

Classes: PretendCookbook, PretendCookbookMetadata

Class Method Summary collapse

Class Method Details

.metadata_from(directory, name, version, recipe_names) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chef_zero/chef_data/cookbook_data.rb', line 30

def self.(directory, name, version, recipe_names)
   = PretendCookbookMetadata.new(PretendCookbook.new(name, recipe_names))
  # If both .rb and .json exist, read .rb
  # TODO if recipes has 3 recipes in it, and the Ruby/JSON has only one, should
  # the resulting recipe list have 1, or 3-4 recipes in it?
  if has_child(directory, 'metadata.rb')
    begin
      file = filename(directory, 'metadata.rb') || "(#{name}/metadata.rb)"
      .instance_eval(read_file(directory, 'metadata.rb'), file)
    rescue
      ChefZero::Log.error("Error loading cookbook #{name}: #{$!}\n  #{$!.backtrace.join("\n  ")}")
    end
  elsif has_child(directory, 'metadata.json')
    .from_json(read_file(directory, 'metadata.json'))
  end
  result = {}
  .to_hash.each_pair do |key,value|
    result[key.to_sym] = value
  end
  result[:version] = version if version
  result
end

.to_hash(cookbook, name, version = nil) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/chef_zero/chef_data/cookbook_data.rb', line 7

def self.to_hash(cookbook, name, version=nil)
  frozen = false
  if cookbook.has_key?(:frozen)
    frozen = cookbook[:frozen]
    cookbook = cookbook.dup
    cookbook.delete(:frozen)
  end

  result = files_from(cookbook)
  recipe_names = result[:recipes].map do |recipe|
    recipe_name = recipe[:name][0..-2]
    recipe_name == 'default' ? name : "#{name}::#{recipe_name}"
  end
  result[:metadata] = (cookbook, name, version, recipe_names)
  result[:name] = "#{name}-#{result[:metadata][:version]}"
  result[:json_class] = 'Chef::CookbookVersion'
  result[:cookbook_name] = name
  result[:version] = result[:metadata][:version]
  result[:chef_type] = 'cookbook_version'
  result[:frozen?] = true if frozen
  result
end