require 'trollop' module Bourdain module Generators class CookbookGenerator < Generator usage :generator, <<-END Generate a new cookbook in the current repo cookbook [] --dot-chef (-c) :: Generate a .chef directory (default: false) --git-init :: Initialize a git repository (default: true) --attributes :: Generate a default attribute file (default: true) --recipe :: Generate a default recipe (default: true) --berksfile :: Generate a Berksfile (default: true) --gitignore (-i) :: Generate a .gitignore (default: true) --metadata :: Generate cookbook metadata (default: true) --readme (-m) :: Generate a Readme (default: true) --kitchenfile :: Generate a Test Kitchen file (default: true) --vagrantfile :: Generate a Vagrantfile (default: true) --versionfile (-z) :: Generate a VERSION file (default: true) --pre-commit :: Generate a pre-commit hook (default: true) --description (-p) :: Description for resources END def initialize argv super path = argv.shift Trollop::die 'No provided' if path.nil? Trollop::die 'Invalid provided' unless valid? path return unless require_chef! if Dir::exists? path log.warn "Cookbook already exists. Doing nothing." return 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 ] 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, 'test', 'integration', 'default', 'minitest') FileUtils.mkdir_p minitest_path apply_template File.join(minitest_path, 'test_default.rb'), \ template: %w[ cookbook busser_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 end end end