class ChefCLI::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-cli/skeletons/code_generator.
Attributes
Public Class Methods
ChefCLI::Command::Base::new
# File lib/chef-cli/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
An instance of ChefRunner
. Calling ChefRunner#converge
will trigger convergence and generate the desired code.
# File lib/chef-cli/command/generator_commands/base.rb, line 54 def chef_runner @chef_runner ||= ChefRunner.new(generator_cookbook_path, ["recipe[#{generator_cookbook_name}::#{recipe}]"]) end
# File lib/chef-cli/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
Path to the directory where the code_generator cookbook is located.
# File lib/chef-cli/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
Checks the `PATH` for the presence of a `git` (or `git.exe`, on windows) executable.
# File lib/chef-cli/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
Sets git related generator_context values.
# File lib/chef-cli/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
Load any values that were not defined via cli switches from Chef
configuration
# File lib/chef-cli/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
# File lib/chef-cli/command/generator_commands/base.rb, line 137 def coerce_generator_copyright_holder generator_config.copyright_holder || knife_config.cookbook_copyright || "The Authors" end
# File lib/chef-cli/command/generator_commands/base.rb, line 143 def coerce_generator_email generator_config.email || knife_config.cookbook_email || "you@example.com" end
# File lib/chef-cli/command/generator_commands/base.rb, line 149 def coerce_generator_license generator_config.license || knife_config.cookbook_license || "all_rights" end
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-cli/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
# File lib/chef-cli/command/generator_commands/base.rb, line 124 def generator_cookbook_option config[:generator_cookbook] || chefcli_config.generator_cookbook end