class Berksfiler::Generator

Methods for generating a Berksfile

Constants

BERKSFILE_FRONTMATTER

Public Class Methods

generate_berksfile(cookbook) click to toggle source

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.

# 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_section() click to toggle source

generate the ‘common dependencies’ section of a Berksfile

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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