class ChefDK::Command::GeneratorCommands::GeneratorGenerator

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.

Attributes

destination_dir[R]
ui[RW]

Public Class Methods

new(*args) click to toggle source
# 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

Public Instance Methods

cookbook_name() click to toggle source

@api private

# 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
run() click to toggle source
# 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)
  update_metadata_rb
  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
source() click to toggle source

@api private

# 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!() click to toggle source

@api private

# 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

Private Instance Methods

check_for_conflicting_dir(path) click to toggle source
# File lib/chef-dk/command/generator_commands/generator_generator.rb, line 141
def check_for_conflicting_dir(path)
  conflicting_subdir_path = File.join(path, "code_generator")
  if File.exist?(path) &&
      File.directory?(path) &&
      File.exist?(conflicting_subdir_path)
    ui.err("ERROR: file or directory #{conflicting_subdir_path} exists.")
    true
  else
    false
  end
end
check_for_conflicting_file(path) click to toggle source
# File lib/chef-dk/command/generator_commands/generator_generator.rb, line 153
def check_for_conflicting_file(path)
  if File.exist?(path) && !File.directory?(path)
    ui.err("ERROR: #{path} exists and is not a directory.")
    true
  else
    false
  end
end
check_for_missing_parent_dir(path) click to toggle source
# File lib/chef-dk/command/generator_commands/generator_generator.rb, line 162
def check_for_missing_parent_dir(path)
  parent = File.dirname(path)
  if !File.exist?(parent)
    ui.err("ERROR: enclosing directory #{parent} does not exist.")
    true
  else
    false
  end
end
conflicting_file_exists?(path) click to toggle source
# File lib/chef-dk/command/generator_commands/generator_generator.rb, line 137
def conflicting_file_exists?(path)
  File.exist?(path) && File.file?(path)
end
created_cookbook_path() click to toggle source
# File lib/chef-dk/command/generator_commands/generator_generator.rb, line 101
def created_cookbook_path
  if custom_cookbook_name?
    destination_dir
  else
    File.join(destination_dir, "code_generator")
  end
end
custom_cookbook_name?() click to toggle source
# File lib/chef-dk/command/generator_commands/generator_generator.rb, line 120
def custom_cookbook_name?
  @custom_cookbook_name
end
metadata_rb() click to toggle source

@api private

# File lib/chef-dk/command/generator_commands/generator_generator.rb, line 110
        def metadata_rb
          <<~METADATA
            name             '#{cookbook_name}'
            description      'Custom code generator cookbook for use with #{ChefDK::Dist::PRODUCT}'
            long_description 'Custom code generator cookbook for use with #{ChefDK::Dist::PRODUCT}'
            version          '0.1.0'

          METADATA
        end
set_destination_dir_from_args(given_path) click to toggle source
# File lib/chef-dk/command/generator_commands/generator_generator.rb, line 124
def set_destination_dir_from_args(given_path)
  path = File.expand_path(given_path)
  if check_for_conflicting_dir(path) ||
      check_for_conflicting_file(path) ||
      check_for_missing_parent_dir(path)
    false
  else
    @destination_dir = File.expand_path(path)
    @custom_cookbook_name = !File.exist?(destination_dir)
    true
  end
end
update_metadata_rb() click to toggle source

@api private

# File lib/chef-dk/command/generator_commands/generator_generator.rb, line 95
def update_metadata_rb
  File.open(File.join(created_cookbook_path, "metadata.rb"), "w+") do |f|
    f.print(metadata_rb)
  end
end