Class: Berksfiler::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/berksfiler/generator.rb

Overview

Methods for generating a Berksfile

Constant Summary collapse

BERKSFILE_FRONTMATTER =
"  # Autogenerated by Berksfiler, do not edit\n\n  source 'https://supermarket.chef.io'\n  metadata\n\n".gsub(/^ {6}/, '')

Class Method Summary collapse

Class Method Details

.generate_berksfile(cookbook) ⇒ Object

generate a full berksfile for a cookbook, including all dependencies with their correct sources. Excludes community cookbooks unless they are specified in ‘common_cookbooks`, because the metadata` directive handles these includes.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/berksfiler/generator.rb', line 35

def self::generate_berksfile(cookbook)
  content = '' << BERKSFILE_FRONTMATTER
  content << generate_common_berksfile_section

  cookbooks = get_local_deps(cookbook).map do |cb|
    Formatter.cookbook_line(cb).split(' ')
  end
  unless cookbooks.empty?
    content << "\n# Dependencies of this cookbook\n"
    content << Formatter.aligned_print(cookbooks.sort).join("\n") << "\n"
  end
  content
end

.generate_common_berksfile_sectionObject

generate the ‘common dependencies’ section of a Berksfile



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/berksfiler/generator.rb', line 50

def self::generate_common_berksfile_section
  content = ''
  common_cookbooks = Berksfiler.common_cookbooks.map do |cb|
    Formatter.cookbook_line(cb).split(' ')
  end
  unless common_cookbooks.empty?
    content << "# Common dependencies for all Berksfiles\n"
    content << Formatter.aligned_print(common_cookbooks.sort).join("\n")
    content << "\n"
  end
  content
end

.get_deps(cookbook) ⇒ Object

returns an array of all cookbook dependencies for a given cookbook by running ‘knife deps`



14
15
16
17
18
19
# File 'lib/berksfiler/generator.rb', line 14

def self::get_deps(cookbook)
  # ["cookbooks/foo", "cookbooks/bar"]
  raw_deps = `knife deps cookbooks/#{cookbook} --remote`.split("\n")
  # ["foo", "bar"]
  raw_deps.map { |cb| cb.split('/')[1].strip }
end

.get_local_deps(cookbook) ⇒ Object

returns an array of cookbook dependencies for a given cookbook, limited to cookbooks which are are local or have specific options



23
24
25
26
27
28
29
# File 'lib/berksfiler/generator.rb', line 23

def self::get_local_deps(cookbook)
  all_deps = get_deps(cookbook)
  specific_cookbooks = Berksfiler.specific_cookbooks.reject do |cb|
    cb == cookbook
  end # don't include self
  all_deps.select { |cb| specific_cookbooks.include?(cb) }
end