Class: ChefDK::Command::GeneratorCommands::GeneratorGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/chef-dk/command/generator_commands/generator_generator.rb

Overview

chef generate generator [NAME] – There is already a ‘Generator` class a few levels up that other classes are referring to via relative constant, so name this `GeneratorGenerator` to avoid causing a conflict.

Instance Attribute Summary collapse

Attributes inherited from Base

#params

Instance Method Summary collapse

Methods inherited from Base

#chef_runner, #generator_cookbook_name, #generator_cookbook_path, #have_git?, #setup_context

Methods included from ChefDK::Configurable

#chef_config, #chefdk_config, #config_loader, #default_chef_server_http_client, #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

#chefdk_home, #err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_install?, #omnibus_root, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initialize(*args) ⇒ GeneratorGenerator

Returns a new instance of GeneratorGenerator.



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

def initialize(*args)
  super
  @destination_dir = nil
  @ui = UI.new
  @custom_cookbook_name = false
end

Instance Attribute Details

#destination_dirObject (readonly)

Returns the value of attribute destination_dir.



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

def destination_dir
  @destination_dir
end

#uiObject

Returns the value of attribute ui.



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

def ui
  @ui
end

Instance Method Details

#cookbook_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



60
61
62
63
64
65
66
# File 'lib/chef-dk/command/generator_commands/generator_generator.rb', line 60

def cookbook_name
  if custom_cookbook_name?
    File.basename(destination_dir)
  else
    "code_generator"
  end
end

#runObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/chef-dk/command/generator_commands/generator_generator.rb', line 48

def run
  return 1 unless verify_params!

  FileUtils.cp_r(source, destination_dir)
  
  ui.msg("Copied built-in generator cookbook to #{created_cookbook_path}")
  ui.msg("Add the following to your config file to enable it:")
  ui.msg("  chefdk.generator_cookbook \"#{created_cookbook_path}\"")
  0
end

#sourceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



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

def source
  # Hard-coded to the built-in generator, because otherwise setting
  # chefdk.generator_cookbook would make this command copy the custom
  # generator, but that doesn't make sense because the user can easily
  # do that anyway.
  File.expand_path("../../../skeletons/code_generator", __FILE__)
end

#verify_params!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chef-dk/command/generator_commands/generator_generator.rb', line 69

def verify_params!
  case params.size
  when 0
    @destination_dir = Dir.pwd
    true
  when 1
    set_destination_dir_from_args(params.first)
  else
    ui.err("ERROR: Too many arguments.")
    ui.err(opt_parser)
    false
  end
end