Sha256: c1640809c36ed698d5f8796e1b771cd86d5f421a7bb1ed67cdb4bd84293eeab7

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'tilt'
require 'yaml'
require 'hashie'
require 'fileutils'
require 'active_support/all'

module Dupler
  class Core
    def new_project(project_name)
      FileUtils.mkdir_p project_name
      project_template_dir = Dir.glob File.join(Dupler::home, "project_template/*")
      FileUtils.cp_r(project_template_dir, project_name)
    end

    def setup(config)
      formats = config.dig("Dupler", "time", "format")
      if formats
        formats.each do |key, value|
            Time::DATE_FORMATS[key.to_sym] = value
        end
      end
    end

    def build(values_file_path, output_dir, template_files)
      yaml = YAML.load File.read(values_file_path)

      setup(yaml)
      yaml.delete("Dupler")
      config = Hashie::Mash.new yaml
      FileUtils.mkdir_p output_dir

      template_files.each do |path|
        output_filename = File.basename(path, File.extname(path))
        output_filepath = File.join(output_dir, output_filename)
        render(path, config, output_filepath)
      end
    end

    def render(template_path, config, output_filepath)
      content = apply_template(template_path, config)
      output_file(output_filepath, content)
      puts "render: #{output_filepath}"
    end

    def apply_template(template_path, config)
      template = Tilt.new(template_path)
      output = template.render(self, config)
    end

    def output_file(filepath, content)
      File.open(filepath, "w") do |file|
        file.write content
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dupler-0.1.0 lib/dupler/core.rb