Sha256: 06c15946de4c7e3b4f20927d0a6a6e00deaf0e684df9b6907efa53aa16ced803

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

require "fileutils"

module Conan
  class Initializer
    TEMPLATE_PATH = File.expand_path("../template", __FILE__)
    ShellCommandError = Class.new(RuntimeError)

    def self.run(where, settings)
      new(where, settings).run
    end

    def initialize(where, settings)
      @destination = File.expand_path(where)
      @settings = settings
    end

    def run
      copy_template
      add_gitignore
      add_git_submodule
    end

  private
    def add_gitignore
      gitignore = ".gitignore"
      File.open(".gitignore", "a") do |f|
        f.puts
        f.puts "/deploy/chef/dna/generated.json"
        f.puts "/deploy/chef/dna/aliases.json"
      end
    end

    def add_git_submodule
      return unless File.directory?(".git")
      sh "git submodule add git://github.com/madebymany/cookbooks.git deploy/chef/recipes/cookbooks >/dev/null 2>&1"
    end

    def copy_template
      Dir.chdir(TEMPLATE_PATH) do
        Dir["**/*"].each do |source|
          target = File.join(@destination, source)
          if File.directory?(source)
            FileUtils.mkdir_p target
          else
            content = File.read(source)
            content.gsub!(/\{\{([A-Z_]+)\}\}/){ @settings[$1] || "TODO" }
            File.open(target, "w") do |f|
              f << content
            end
            File.chmod(File.stat(source).mode, target)
          end
        end
      end
    end

    def sh(command)
      system command or raise ShellCommandError, command
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
conan-0.2.1 lib/conan/initializer.rb
conan-0.2.0 lib/conan/initializer.rb