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, #generator_config, #knife_config

Methods inherited from Base

#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.



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

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.



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

def cookbook_name_or_path
  @cookbook_name_or_path
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

Instance Method Details

#build_cookbook_parent_is_cookbook?Boolean

Returns:

  • (Boolean)


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

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

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

#delivery_project_dirObject



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

def delivery_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

#delivery_project_git_initialized?Boolean

Returns:

  • (Boolean)


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

def delivery_project_git_initialized?
  File.exist?(File.join(delivery_project_dir, ".git"))
end

#params_valid?Boolean

Returns:

  • (Boolean)


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

def params_valid?
  @params_valid
end

#pipelineObject



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

def pipeline
  config[:pipeline]
end

#read_and_validate_paramsObject



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

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



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

def recipe
  "build_cookbook"
end

#runObject



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

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



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

def setup_context
  super
  Generator.add_attr_to_context(:delivery_project_dir, delivery_project_dir)

  Generator.add_attr_to_context(:delivery_project_git_initialized, delivery_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