Class: Bourdain::Generators::CookbookGenerator

Inherits:
Generator show all
Defined in:
lib/bourdain/resources/generators/cookbook.rb

Instance Attribute Summary

Attributes inherited from Resource

#opts, #raw_usage, #resource_name, #spec, #status

Instance Method Summary collapse

Methods inherited from Resource

log=, raw_usage, resource_name, usage

Constructor Details

#initialize(argv) ⇒ CookbookGenerator

Returns a new instance of CookbookGenerator.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/bourdain/resources/generators/cookbook.rb', line 27

def initialize argv
  super

  path = argv.shift
  Trollop::die 'No <path> provided' if path.nil?
  Trollop::die 'Invalid <path> provided' unless valid? path

  return unless require_kitchen!

  if Dir::exists? path
    log.warn "Cookbook already exists. Doing nothing."
    return
  end

  type = case path
  when /apps/   then :app
  when /realms/ then :realm
  when /bases/  then :base
  when /forks/  then :fork
  end
  name = File.basename(normalized(path))

  FileUtils.mkdir_p path

  if opts[:dot_chef]
    FileUtils.mkdir_p File.join(path, '.chef/data_bags')
    FileUtils.mkdir_p File.join(path, '.chef/nodes')
  end

  if opts[:attributes]
    FileUtils.mkdir_p File.join(path, 'attributes')
    apply_template File.join(path, 'attributes', 'default.rb'), \
      template: %w[ cookbook attributes example.rb ], locals: {
        name: 'default', cookbook_name: name
      }
  end

  if opts[:recipe]
    FileUtils.mkdir_p File.join(path, 'recipes')
    apply_template File.join(path, 'recipes', 'default.rb'), \
      template: %w[ cookbook recipes example.rb ], locals: {
        name: 'default', cookbook_name: name
      }
  end

  if opts[:berksfile]
    apply_template File.join(path, 'Berksfile'), \
      template: %w[ cookbook Berksfile ]
  end

  if opts[:gitignore]
    apply_template File.join(path, '.gitignore'), \
      template: %w[ cookbook gitignore ], locals: { type: type }
  end

  if opts[:rakefile]
    apply_template File.join(path, 'Rakefile'), \
      template: %w[ cookbook Rakefile ]
  end

  if opts[:metadata]
    apply_template File.join(path, 'metadata.rb'), \
      template: %w[ cookbook metadata.rb ], locals: { name: name }
  end

  if opts[:readme]
    apply_template File.join(path, 'Readme.md'), \
      template: %w[ cookbook Readme.md ], locals: { name: name }
  end

  if opts[:kitchenfile]
    minitest_path = File.join(path, 'files', 'default', 'test')
    FileUtils.mkdir_p minitest_path
    apply_template File.join(minitest_path, 'default_test.rb'), \
      template: %w[ cookbook minitest.rb ], locals: {
        cookbook_name: name, recipe_name: 'default'
      }
    apply_template File.join(path, '.kitchen.yml'), \
      template: %w[ cookbook kitchen.yml ], locals: { name: name }
  end

  if opts[:vagrantfile]
    apply_template File.join(path, 'Vagrantfile'), \
      template: %w[ cookbook Vagrantfile ], locals: { name: name }
  end

  if opts[:versionfile]
    apply_template File.join(path, 'VERSION'), \
      template: %w[ cookbook VERSION ]
  end

  if opts[:pre_commit]
    hooks = File.join(path, '.git', 'hooks')
    FileUtils.mkdir_p hooks
    pre_commit_hook = File.join(hooks, 'pre-commit')
    apply_template pre_commit_hook, template: %w[ cookbook pre-commit ]
    FileUtils.chmod 0755, pre_commit_hook
  end

  if opts[:git_init]
    Dir.chdir(path) do
      `git init .`
    end
  end

  log.info "Generated cookbook at #{path}."
end