class ChefCLI::Command::GeneratorCommands::GeneratorGenerator
‘GeneratorGenerator` to avoid causing a conflict.
are referring to via relative constant, so name this
There is already a `Generator` class a few levels up that other classes
–
chef generate generator [NAME]
def check_for_conflicting_dir(path)
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
def check_for_conflicting_file(path)
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
def check_for_missing_parent_dir(path)
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
def conflicting_file_exists?(path)
def conflicting_file_exists?(path) File.exist?(path) && File.file?(path) end
def cookbook_name
- Api: - private
def cookbook_name if custom_cookbook_name? File.basename(destination_dir) else "code_generator" end end
def created_cookbook_path
def created_cookbook_path if custom_cookbook_name? destination_dir else File.join(destination_dir, "code_generator") end end
def custom_cookbook_name?
def custom_cookbook_name? @custom_cookbook_name end
def initialize(*args)
def initialize(*args) super @destination_dir = nil @ui = UI.new @custom_cookbook_name = false end
def metadata_rb
- Api: - private
def metadata_rb <<~METADATA name '#{cookbook_name}' description 'Custom code generator cookbook for use with #{ChefCLI::Dist::PRODUCT}' version '0.1.0' METADATA end
def run
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(" chefcli.generator_cookbook \"#{created_cookbook_path}\"") 0 end
def set_destination_dir_from_args(given_path)
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
def source
- Api: - private
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", __FILE__) end
def update_metadata_rb
- Api: - private
def update_metadata_rb File.open(File.join(created_cookbook_path, "metadata.rb"), "w+") do |f| f.print(metadata_rb) end end
def verify_params!
- Api: - private
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