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

Inherits:
Base
  • Object
show all
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, Repo

Instance Attribute Summary collapse

Instance Method Summary collapse

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_embedded_bin_dir, #omnibus_root, #stderr, #stdout, #system_command

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



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

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

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



34
35
36
# File 'lib/chef-dk/command/generator_commands/base.rb', line 34

def params
  @params
end

Instance Method Details

#chef_runnerObject

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



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

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

#generator_cookbook_pathObject

Path to the directory where the code_generator cookbook is located. For now, this is hard coded to the ‘skeletons’ directory in this repo.



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

def generator_cookbook_path
  config[:generator_cookbook]
end

#have_git?Boolean

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

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/chef-dk/command/generator_commands/base.rb', line 67

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.



57
58
59
60
61
62
63
# File 'lib/chef-dk/command/generator_commands/base.rb', line 57

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
end