Class: ChefDK::Command::GeneratorCommands::BuildCookbook

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

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?

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(params) ⇒ BuildCookbook

Returns a new instance of BuildCookbook.



40
41
42
43
44
# File 'lib/chef-dk/command/generator_commands/build_cookbook.rb', line 40

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

Instance Attribute Details

#cookbook_name_or_pathObject (readonly)

Returns the value of attribute cookbook_name_or_path.



31
32
33
# File 'lib/chef-dk/command/generator_commands/build_cookbook.rb', line 31

def cookbook_name_or_path
  @cookbook_name_or_path
end

#errorsObject (readonly)

Returns the value of attribute errors.



29
30
31
# File 'lib/chef-dk/command/generator_commands/build_cookbook.rb', line 29

def errors
  @errors
end

Instance Method Details

#build_cookbook_parent_is_cookbook?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
# File 'lib/chef-dk/command/generator_commands/build_cookbook.rb', line 79

def build_cookbook_parent_is_cookbook?
   = File.join(workflow_project_dir, "metadata.json")
   = File.join(workflow_project_dir, "metadata.rb")

  File.exist?() || File.exist?()
end

#params_valid?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/chef-dk/command/generator_commands/build_cookbook.rb', line 119

def params_valid?
  @params_valid
end

#pipelineObject



71
72
73
# File 'lib/chef-dk/command/generator_commands/build_cookbook.rb', line 71

def pipeline
  config[:pipeline]
end

#read_and_validate_paramsObject



111
112
113
114
115
116
117
# File 'lib/chef-dk/command/generator_commands/build_cookbook.rb', line 111

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

#recipeObject



75
76
77
# File 'lib/chef-dk/command/generator_commands/build_cookbook.rb', line 75

def recipe
  "build_cookbook"
end

#runObject



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

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

#setup_contextObject



61
62
63
64
65
66
67
68
69
# File 'lib/chef-dk/command/generator_commands/build_cookbook.rb', line 61

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

#workflow_project_dirObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef-dk/command/generator_commands/build_cookbook.rb', line 86

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

#workflow_project_git_initialized?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/chef-dk/command/generator_commands/build_cookbook.rb', line 107

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