Sha256: e8cbd81f32c35902c9d179d5e2d624e8b60ecf1d37aac0f9a877e4240852ee34

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

require "chef-cooker/version"
require 'thor'
require 'thor/group'

module Chef
  module Cooker
    class Generator < Thor::Group
      include Thor::Actions

      argument :cookbook

      def manifest
        generate_cookbook
        append_package
        add_and_commit_with_git
      end

      private
      def generate_cookbook
        `bundle exec knife cookbook create #{cookbook} --cookbook-path cookbooks`
      end

      def append_package
        if yes? "Do you want to append package to cookbooks/#{cookbook}/recipes/default.rb? [y/N] :"
          append_file "cookbooks/#{cookbook}/recipes/default.rb", deindent(<<-EOS)

            package "#{cookbook}"
          EOS
        end
      end

      def add_and_commit_with_git
        if yes? 'Do you want to commit? [y/N] :'
          git add: "cookbooks/#{cookbook}", commit: "-m \"Add #{cookbook}\""
        end
      end

      # Run a command in git.
      #
      #   git :init
      #   git :add => "this.file that.rb"
      #   git :add => "onefile.rb", :rm => "badfile.cxx"
      def git(commands={})
        if commands.is_a?(Symbol)
          run "git #{commands}"
        else
          commands.each do |cmd, options|
            run "git #{cmd} #{options}"
          end
        end
      end

      def append_newline(path)
        gsub_file path, /\z/, "\n"
      end

      def deindent(string, width = nil)
        width ||= string.lines.grep(/./).map do |line|
          line[/^ */].length
        end.min || 0
        string.gsub(/^ {#{width}}/, '')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chef-cooker-0.0.3 lib/chef-cooker.rb
chef-cooker-0.0.2 lib/chef-cooker.rb