Class: ChefCLI::Command::GeneratorCommands::GeneratorGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/chef-cli/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 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(*args) ⇒ GeneratorGenerator

Returns a new instance of GeneratorGenerator.



41
42
43
44
45
46
# File 'lib/chef-cli/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-cli/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-cli/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.



60
61
62
63
64
65
66
# File 'lib/chef-cli/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-cli/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("  chefcli.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.



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

def source
  # Hard-coded to the built-in generator, because otherwise setting
  # chefcli.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", __dir__)
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.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chef-cli/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