class Souschef::Scaffold

Automagically create needed files

Attributes

cookbook[RW]
dir[RW]
metadata[RW]
opts[RW]
recipe[RW]
recipe_file[RW]

Public Class Methods

new(opts) click to toggle source
# File lib/souschef/scaffold.rb, line 9
def initialize(opts)
  @opts = opts
  @dir = Dir.pwd
  metadata_info
end

Public Instance Methods

start() click to toggle source
# File lib/souschef/scaffold.rb, line 15
def start
  check_cookbook_name
  check_for_metadata unless @opts[:force]
  process_templates
end

Private Instance Methods

check_cookbook_name() click to toggle source

Private - Chef if cookbook name is set

Returns nil

# File lib/souschef/scaffold.rb, line 26
def check_cookbook_name
  fail 'Please specify the recipe name' if @opts[:recipe].nil?
end
check_for_directories(type) click to toggle source

Private - Check if directories exist

Return nil

# File lib/souschef/scaffold.rb, line 110
def check_for_directories(type)
  dir = return_directories[type.to_sym]

  if @opts[:versbose]
    unless File.directory?(dir)
      Souschef::Print.info "Creating missing directory #{dir}"
    end
  end
  FileUtils.mkdir_p dir unless File.directory?(dir)
end
check_for_file(file) click to toggle source

Private - Exit if file exists

Returns nil

# File lib/souschef/scaffold.rb, line 141
def check_for_file(file)
  fail "#{file} already exists. Frozen in fear!" if File.exist?(file)
end
check_for_metadata() click to toggle source

Private - Locate metadata.rb

Return nil

# File lib/souschef/scaffold.rb, line 124
def check_for_metadata
  meta = File.join(@opts[:path], 'metadata.rb')
  fail 'Please return to the root of your cookbook' unless File.exist?(meta)
end
create_recipe_file(type) click to toggle source

Private - Creates recipe file based on the input

Retunrns nil

# File lib/souschef/scaffold.rb, line 49
def create_recipe_file(type)
  source = template_location(type)
  Souschef::Print.info("Create #{@opts[:recipe]}[#{type}] from #{source}"
                      ) if @opts[:verbose]
  check_for_directories(type)
  write_file(return_file_location(type), generate_template(source))
end
generate_template(source) click to toggle source

Private - Generate the template

Return String

# File lib/souschef/scaffold.rb, line 60
def generate_template(source)
  rfile = ERB.new(File.read(source))
  @recipe = @opts[:recipe]
  @cookbook = @metadata.name
  @maintainer = @metadata.maintainer
  @license = @metadata.license
  @year = Time.now.year

  rfile.result(binding)
end
metadata_info() click to toggle source

Private - Read Chef metadata

Returns String

# File lib/souschef/scaffold.rb, line 33
def metadata_info
  meta = File.join(@opts[:path], 'metadata.rb')
  @metadata = Chef::Cookbook::Metadata.new
  @metadata.from_file(meta)
end
process_templates() click to toggle source

Private - Process tempaltes

Return inl

# File lib/souschef/scaffold.rb, line 42
def process_templates
  %w( recipe serverspec chefspec ).each { |type| create_recipe_file(type) }
end
return_directories() click to toggle source

Private - Return location of directories

Returns Hash

# File lib/souschef/scaffold.rb, line 86
def return_directories
  { recipe: File.join(@opts[:path], 'recipes'),
    serverspec: File.join(@opts[:path], 'test', 'integration', 'default',
                          'serverspec', 'localhost'),
    chefspec: File.join(@opts[:path], 'spec', 'unit') }
end
return_file_location(type) click to toggle source

Private - Get path to the recipes file

Return String

# File lib/souschef/scaffold.rb, line 96
def return_file_location(type)
  case type
  when 'recipe'
    File.join(return_directories[:recipe], "#{@opts[:recipe]}.rb")
  when 'serverspec'
    File.join(return_directories[:serverspec], "#{opts[:recipe]}_spec.rb")
  when 'chefspec'
    File.join(return_directories[:chefspec], "#{@opts[:recipe]}_spec.rb")
  end
end
template_location(type) click to toggle source

Private - Return location of the template file, depending if custom configuration is set under ~/.souschef/%profile%/ or use the default template provided by Souschef gem.

Returns String

# File lib/souschef/scaffold.rb, line 76
def template_location(type)
  local = File.expand_path(
    "~/.souschef/#{@opts[:profile]}/#{type}/#{type}.erb", __FILE__)
  bundled = File.expand_path("../../../data/#{type}/#{type}.erb", __FILE__)
  File.exist?(local) ? local : bundled
end
write_file(file, data) click to toggle source

Private - Write file

Returns nil

# File lib/souschef/scaffold.rb, line 132
def write_file(file, data)
  check_for_file(file) unless @opts[:force]
  fd = File.open(file, 'w')
  fd.write(data)
end