Class: ChefCLI::Command::GeneratorCommands::Base

Inherits:
Base
  • Object
show all
Includes:
ChefCLI::Configurable
Defined in:
lib/chef-cli/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-cli/skeletons/code_generator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ChefCLI::Configurable

#chef_config, #chefcli_config, #config_loader, #generator_config, #knife_config, #reset_config!

Methods inherited from Base

#check_license_acceptance, #needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_bin_dir, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_expand_path, #omnibus_install?, #omnibus_root, #package_home, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



44
45
46
47
48
49
50
# 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

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



40
41
42
# File 'lib/chef-cli/command/generator_commands/base.rb', line 40

def params
  @params
end

Instance Method Details

#chef_runnerObject

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



54
55
56
# 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

#generator_cookbook_nameObject



64
65
66
67
# 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

#generator_cookbook_pathObject

Path to the directory where the code_generator cookbook is located.



59
60
61
62
# 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

#have_git?Boolean

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

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
# 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

#setup_contextObject

Sets git related generator_context values.



70
71
72
73
74
75
76
77
78
79
80
81
# 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