class ChefDK::Command::GeneratorCommands::Base

## Base

Base class for `chef generate` subcommands. Contains basic behaviors for setting up the generator context, detecting git, and launching a chef converge.

The behavior of the generators is largely delegated to a chef cookbook. The default implementation is the `code_generator` cookbook in chef-dk/skeletons/code_generator.

Attributes

params[R]

Public Class Methods

new(params) click to toggle source
Calls superclass method ChefDK::Command::Base::new
# File lib/chef-dk/command/generator_commands/base.rb, line 44
def initialize(params)
  super()
  @params = params

  @generator_cookbook_path = nil
  @generator_cookbook_name = nil
end

Public Instance Methods

chef_runner() click to toggle source

An instance of ChefRunner. Calling ChefRunner#converge will trigger convergence and generate the desired code.

# File lib/chef-dk/command/generator_commands/base.rb, line 54
def chef_runner
  @chef_runner ||= ChefRunner.new(generator_cookbook_path, ["recipe[#{generator_cookbook_name}::#{recipe}]"])
end
generator_cookbook_name() click to toggle source
# File lib/chef-dk/command/generator_commands/base.rb, line 64
def generator_cookbook_name
  detect_generator_cookbook_name_and_path! unless @generator_cookbook_name
  @generator_cookbook_name
end
generator_cookbook_path() click to toggle source

Path to the directory where the code_generator cookbook is located.

# File lib/chef-dk/command/generator_commands/base.rb, line 59
def generator_cookbook_path
  detect_generator_cookbook_name_and_path! unless @generator_cookbook_path
  @generator_cookbook_path
end
have_git?() click to toggle source

Checks the `PATH` for the presence of a `git` (or `git.exe`, on windows) executable.

# File lib/chef-dk/command/generator_commands/base.rb, line 85
def have_git?
  path = ENV["PATH"] || ""
  paths = path.split(File::PATH_SEPARATOR)
  exts = [RbConfig::CONFIG["EXEEXT"]]
  exts.concat(ENV["PATHEXT"].split(";")) unless ENV["PATHEXT"].nil?
  paths.any? do |bin_path|
    exts.any? do |ext|
      File.exist?(File.join(bin_path, "git#{ext}"))
    end
  end
end
setup_context() click to toggle source

Sets git related generator_context values.

# File lib/chef-dk/command/generator_commands/base.rb, line 70
def setup_context
  apply_generator_values_from_config
  Generator.add_attr_to_context(:have_git, have_git?)
  Generator.add_attr_to_context(:skip_git_init, false)
  config.each do |k, v|
    Generator.add_attr_to_context(k, v)
  end
  # inject the arbitrary args supplied on cmdline, default = []
  config[:generator_arg].each do |k, v|
    Generator.add_attr_to_context(k, v)
  end
end

Private Instance Methods

apply_generator_values_from_config() click to toggle source

Load any values that were not defined via cli switches from Chef configuration

# File lib/chef-dk/command/generator_commands/base.rb, line 131
def apply_generator_values_from_config
  config[:copyright_holder] ||= coerce_generator_copyright_holder
  config[:email] ||= coerce_generator_email
  config[:license] ||= coerce_generator_license
end
coerce_generator_email() click to toggle source
# File lib/chef-dk/command/generator_commands/base.rb, line 143
def coerce_generator_email
  generator_config.email ||
    knife_config.cookbook_email ||
    "you@example.com"
end
coerce_generator_license() click to toggle source
# File lib/chef-dk/command/generator_commands/base.rb, line 149
def coerce_generator_license
  generator_config.license ||
    knife_config.cookbook_license ||
    "all_rights"
end
detect_generator_cookbook_name_and_path!() click to toggle source

Inspects the `config` option to determine the generator_cookbook_name and generator_cookbook_path. There are two supported ways this can work:

  • `config` is the full path to the generator

cookbook. In this case, the last path component is the cookbook name, and the parent directory is the cookbook path

  • `config` is the path to a directory that

contains a cookbook named “code_generator” (DEPRECATED). This is how the `–generator-cookbook` feature was originally written, so we support this for backwards compatibility. This way has poor UX and we'd like to get rid of it, so a warning is printed in this case.

# File lib/chef-dk/command/generator_commands/base.rb, line 111
def detect_generator_cookbook_name_and_path!
  given_path = generator_cookbook_option
  code_generator_subdir = File.join(given_path, "code_generator")
  if File.directory?(code_generator_subdir)
    @generator_cookbook_name = "code_generator"
    @generator_cookbook_path = given_path
    err("WARN: Please configure the generator cookbook by giving the full path to the desired cookbook (like '#{code_generator_subdir}')")
  else
    @generator_cookbook_name = File.basename(given_path)
    @generator_cookbook_path = File.dirname(given_path)
  end
end
generator_cookbook_option() click to toggle source
# File lib/chef-dk/command/generator_commands/base.rb, line 124
def generator_cookbook_option
  config[:generator_cookbook] || chefdk_config.generator_cookbook
end