class ChefCLI::Command::GeneratorCommands::BuildCookbook

def build_cookbook_parent_is_cookbook?

def build_cookbook_parent_is_cookbook?
  metadata_json_path = File.join(workflow_project_dir, "metadata.json")
  metadata_rb_path = File.join(workflow_project_dir, "metadata.rb")
  File.exist?(metadata_json_path) || File.exist?(metadata_rb_path)
end

def initialize(params)

def initialize(params)
  @params_valid = true
  @cookbook_name = nil
  super
end

def params_valid?

def params_valid?
  @params_valid
end

def pipeline

def pipeline
  config[:pipeline]
end

def read_and_validate_params

def read_and_validate_params
  arguments = parse_options(params)
  @cookbook_name_or_path = arguments[0]
  unless @cookbook_name_or_path
    @params_valid = false
  end
end

def recipe

def recipe
  "build_cookbook"
end

def run

def run
  read_and_validate_params
  if params_valid?
    setup_context
    chef_runner.converge
    0
  else
    err(opt_parser)
    1
  end
rescue ChefCLI::ChefRunnerError => e
  err("ERROR: #{e}")
  1
end

def setup_context

def setup_context
  super
  Generator.add_attr_to_context(:workflow_project_dir, workflow_project_dir)
  Generator.add_attr_to_context(:workflow_project_git_initialized, workflow_project_git_initialized?)
  Generator.add_attr_to_context(:build_cookbook_parent_is_cookbook, build_cookbook_parent_is_cookbook?)
  Generator.add_attr_to_context(:pipeline, pipeline)
end

def workflow_project_dir

def workflow_project_dir
  project_dir = File.expand_path(cookbook_name_or_path, Dir.pwd)
  # Detect if we were invoked with arguments like
  #
  #     chef generate build-cookbook project/.delivery/build_cookbook
  #
  # If so, normalize paths so we don't make a directory structure like
  # `.delivery/.delivery/build_cookbook`.
  #
  # Note that we don't check the name of the build cookbook the user
  # asked for and we hard-code to naming it "build_cookbook". We also
  # don't catch the case that the user requested something like
  # `project/.delivery/build_cookbook/extra-thing-that-shouldn't-be-here`
  Pathname.new(project_dir).ascend do |dir|
    if File.basename(dir) == ".delivery"
      project_dir = File.dirname(dir)
    end
  end
  project_dir
end

def workflow_project_git_initialized?

def workflow_project_git_initialized?
  File.exist?(File.join(workflow_project_dir, ".git"))
end