Class: ChefDK::Command::GeneratorCommands::Base

Inherits:
Base
  • Object
show all
Includes:
ChefDK::Configurable
Defined in:
lib/chef-dk/command/generator_commands/base.rb

Overview

## 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.

Direct Known Subclasses

Cookbook, CookbookCodeFile, Policyfile, Repo

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ChefDK::Configurable

#chef_config, #chefdk_config, #config_loader

Methods inherited from Base

#needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#err, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_install?, #omnibus_root, #stderr, #stdout, #system_command

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



41
42
43
44
45
46
47
# File 'lib/chef-dk/command/generator_commands/base.rb', line 41

def initialize(params)
  super()
  @params = params

  @generator_cookbook_path = nil
  @generator_cookbook_name = nil
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



37
38
39
# File 'lib/chef-dk/command/generator_commands/base.rb', line 37

def params
  @params
end

Instance Method Details

#chef_runnerObject

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



51
52
53
# File 'lib/chef-dk/command/generator_commands/base.rb', line 51

def chef_runner
  @chef_runner ||= ChefRunner.new(generator_cookbook_path, ["recipe[#{generator_cookbook_name}::#{recipe}]"])
end

#generator_cookbook_nameObject



61
62
63
64
# File 'lib/chef-dk/command/generator_commands/base.rb', line 61

def generator_cookbook_name
  detect_generator_cookbook_name_and_path! unless @generator_cookbook_name
  @generator_cookbook_name
end

#generator_cookbook_pathObject

Path to the directory where the code_generator cookbook is located.



56
57
58
59
# File 'lib/chef-dk/command/generator_commands/base.rb', line 56

def generator_cookbook_path
  detect_generator_cookbook_name_and_path! unless @generator_cookbook_path
  @generator_cookbook_path
end

#have_git?Boolean

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

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/chef-dk/command/generator_commands/base.rb', line 81

def have_git?
  path = ENV["PATH"] || ""
  paths = path.split(File::PATH_SEPARATOR)
  paths.any? {|bin_path| File.exist?(File.join(bin_path, "git#{RbConfig::CONFIG['EXEEXT']}"))}
end

#setup_contextObject

Sets git related generator_context values.



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/chef-dk/command/generator_commands/base.rb', line 67

def setup_context
  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