lib/dupler/core.rb in dupler-0.1.0 vs lib/dupler/core.rb in dupler-0.2.0

- old
+ new

@@ -1,30 +1,31 @@ -require 'tilt' -require 'yaml' -require 'hashie' -require 'fileutils' -require 'active_support/all' +# frozen_string_literal: true +require "tilt" +require "yaml" +require "hashie" +require "fileutils" +require "active_support/all" + module Dupler + # Dupler core functions class Core def new_project(project_name) FileUtils.mkdir_p project_name - project_template_dir = Dir.glob File.join(Dupler::home, "project_template/*") + 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 + formats&.each do |key, value| + Time::DATE_FORMATS[key.to_sym] = value end end def build(values_file_path, output_dir, template_files) - yaml = YAML.load File.read(values_file_path) + yaml = YAML.safe_load File.read(values_file_path) setup(yaml) yaml.delete("Dupler") config = Hashie::Mash.new yaml FileUtils.mkdir_p output_dir @@ -41,11 +42,15 @@ 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) + begin + template = Tilt.new(template_path) + template.render(self, config) + rescue Exception => e + raise DuplerException.new("Template rendering error: [#{template_path}] #{e.message}") + end end def output_file(filepath, content) File.open(filepath, "w") do |file| file.write content