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

Inherits:
Base
  • Object
show all
Defined in:
lib/chef-dk/command/generator_commands.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

App, Cookbook, CookbookCodeFile

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.



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

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

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#chef_runnerObject

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



74
75
76
# File 'lib/chef-dk/command/generator_commands.rb', line 74

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

#generator_contextObject

Delegates to ‘Generator.context`, the singleton instance of Generator::Context



93
94
95
# File 'lib/chef-dk/command/generator_commands.rb', line 93

def generator_context
  Generator.context
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.



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

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)


99
100
101
102
103
# File 'lib/chef-dk/command/generator_commands.rb', line 99

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.



86
87
88
89
# File 'lib/chef-dk/command/generator_commands.rb', line 86

def setup_context
  Generator.context.have_git = have_git?
  Generator.context.skip_git_init = false
end