Class: Bourdain::Generators::RecipeGenerator

Inherits:
Generator show all
Defined in:
lib/bourdain/resources/generators/recipe.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) ⇒ RecipeGenerator

Returns a new instance of RecipeGenerator.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bourdain/resources/generators/recipe.rb', line 12

def initialize argv
  super

  name = argv.shift
  Trollop::die 'No <name> provided' if name.nil?
  Trollop::die 'Invalid <name> provided' unless valid? name
  name = normalized(name)

  return unless require_cookbook!

  FileUtils.mkdir_p 'recipes'
  path = File.join('recipes', name + '.rb')

  if File::exists? path
    log.warn 'Recipe already exists. Doing nothing.'
    return
  end

  cookbook_name = File.basename Dir.pwd

  apply_template path, \
    template: %w[ cookbook recipes example.rb ],
    locals: {
      cookbook_name: cookbook_name,
      name: name
    }

  if opts[:minitest]
    minitest_path = File.join('files', 'default', 'test', "#{name}_test.rb")
    FileUtils.mkdir_p File.dirname(minitest_path)
    apply_template minitest_path, \
      template: %w[ cookbook busser_minitest.rb ], locals: {
        cookbook_name: cookbook_name, recipe_name: name
      }
    log.info "Generated test for recipe at #{minitest_path}."
  end

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