Class: Dupler::Core
- Inherits:
-
Object
- Object
- Dupler::Core
- Defined in:
- lib/dupler/core.rb
Overview
Dupler core functions
Instance Method Summary collapse
- #apply_template(template_path, config) ⇒ Object
- #build(values_file_path, output_dir, template_files) ⇒ Object
- #new_project(project_name) ⇒ Object
- #output_file(filepath, content) ⇒ Object
- #render(template_path, config, output_filepath) ⇒ Object
- #setup(config) ⇒ Object
Instance Method Details
#apply_template(template_path, config) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/dupler/core.rb', line 46 def apply_template(template_path, config) begin template = Tilt.new(template_path) template.render(self, config) rescue Exception => e raise DuplerException.new("Template rendering error: [#{template_path}] #{e.}") end end |
#build(values_file_path, output_dir, template_files) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dupler/core.rb', line 25 def build(values_file_path, output_dir, template_files) yaml = YAML.safe_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 |
#new_project(project_name) ⇒ Object
12 13 14 15 16 |
# File 'lib/dupler/core.rb', line 12 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 |
#output_file(filepath, content) ⇒ Object
55 56 57 58 59 |
# File 'lib/dupler/core.rb', line 55 def output_file(filepath, content) File.open(filepath, "w") do |file| file.write content end end |
#render(template_path, config, output_filepath) ⇒ Object
40 41 42 43 44 |
# File 'lib/dupler/core.rb', line 40 def render(template_path, config, output_filepath) content = apply_template(template_path, config) output_file(output_filepath, content) puts "render: #{output_filepath}" end |
#setup(config) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/dupler/core.rb', line 18 def setup(config) formats = config.dig("Dupler", "time", "format") formats&.each do |key, value| Time::DATE_FORMATS[key.to_sym] = value end end |