class ChefCLI::Command::GeneratorCommands::Base
chef-cli/skeletons/code_generator.
The default implementation is the ‘code_generator` cookbook in
The behavior of the generators is largely delegated to a chef cookbook.
chef converge.
for setting up the generator context, detecting git, and launching a
Base class for `chef generate` subcommands. Contains basic behaviors
## Base
def apply_generator_values_from_config
configuration
Load any values that were not defined via cli switches from Chef
def apply_generator_values_from_config config[:copyright_holder] ||= coerce_generator_copyright_holder config[:email] ||= coerce_generator_email config[:license] ||= coerce_generator_license end
def chef_runner
An instance of ChefRunner. Calling ChefRunner#converge will trigger
def chef_runner @chef_runner ||= ChefRunner.new(generator_cookbook_path, ["recipe[#{generator_cookbook_name}::#{recipe}]"]) end
def coerce_generator_copyright_holder
def coerce_generator_copyright_holder generator_config.copyright_holder || knife_config.cookbook_copyright || "The Authors" end
def coerce_generator_email
def coerce_generator_email generator_config.email || knife_config.cookbook_email || "you@example.com" end
def coerce_generator_license
def coerce_generator_license generator_config.license || knife_config.cookbook_license || "all_rights" end
def detect_generator_cookbook_name_and_path!
support this for backwards compatibility. This way has poor UX and
the `--generator-cookbook` feature was originally written, so we
contains a cookbook named "code_generator" (DEPRECATED). This is how
* `config[:generator_cookbook]` is the path to a directory that
and the parent directory is the cookbook path
cookbook. In this case, the last path component is the cookbook name,
* `config[:generator_cookbook]` is the full path to the generator
supported ways this can work:
generator_cookbook_name and generator_cookbook_path. There are two
Inspects the `config[:generator_cookbook]` option to determine the
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
def generator_cookbook_name
def generator_cookbook_name detect_generator_cookbook_name_and_path! unless @generator_cookbook_name @generator_cookbook_name end
def generator_cookbook_option
def generator_cookbook_option config[:generator_cookbook] || chefcli_config.generator_cookbook end
def generator_cookbook_path
def generator_cookbook_path detect_generator_cookbook_name_and_path! unless @generator_cookbook_path @generator_cookbook_path end
def have_git?
Checks the `PATH` for the presence of a `git` (or `git.exe`, on
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
def initialize(params)
def initialize(params) super() @params = params @generator_cookbook_path = nil @generator_cookbook_name = nil end
def setup_context
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