Class: AppArchetype::Renderer
- Inherits:
-
Object
- Object
- AppArchetype::Renderer
- Includes:
- Logger
- Defined in:
- lib/app_archetype/renderer.rb
Overview
Renderer renders a plan
Instance Method Summary collapse
-
#copy_file(file) ⇒ Object
Copies source file to planned path only overwriting if permitted by the renderer.
-
#initialize(plan, overwrite = false) ⇒ Renderer
constructor
Creates a renderer instance.
-
#render ⇒ Object
Renders plan to disk.
-
#render_erb_file(file) ⇒ Object
Renders erb template to output location.
-
#render_hbs_file(file) ⇒ Object
Renders handlebars template to output location.
-
#render_template_file(file) ⇒ Object
Copies source file only overwriting if permitted by the renderer.
-
#write_dir(file) ⇒ Object
Creates a directory at the specified location.
Methods included from Logger
#logger, #print_error, #print_message, #print_message_and_exit, #print_warning
Constructor Details
#initialize(plan, overwrite = false) ⇒ Renderer
Creates a renderer instance
16 17 18 19 |
# File 'lib/app_archetype/renderer.rb', line 16 def initialize(plan, overwrite = false) @plan = plan @overwrite = overwrite end |
Instance Method Details
#copy_file(file) ⇒ Object
Copies source file to planned path only overwriting if permitted by the renderer.
130 131 132 133 134 135 136 |
# File 'lib/app_archetype/renderer.rb', line 130 def copy_file(file) raise 'cannot overwrite file' if file.exist? && !@overwrite ("COPY file ->: #{file.path}") FileUtils.cp(file.source_file_path, file.path) end |
#render ⇒ Object
Renders plan to disk. The renderer is capable of:
-
creating directories
-
Rendering ERB templates with plan variables
-
Rendering Handlebars templates with plan variables
-
Copying static files
When a template requests a varaible that does not exist within the plan - then the rendering process stops and a RuntimeError is raised
Similarly when a template cannot be parsed a Runtime Error will be raised.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/app_archetype/renderer.rb', line 35 def render write_dir(File.new(@plan.destination_path)) @last_file = '' @plan.files.each do |file| @last_file = file if file.source_directory? write_dir(file) elsif file.source_erb? render_erb_file(file) elsif file.source_hbs? render_hbs_file(file) elsif file.source_template? render_template_file(file) elsif file.source_file? copy_file(file) end end rescue NoMethodError => e raise "error rendering #{@last_file.path} "\ "cannot find variable `#{e.name}` in template" rescue SyntaxError raise "error parsing #{@last_file.path} template is invalid" end |
#render_erb_file(file) ⇒ Object
Renders erb template to output location
76 77 78 79 80 81 82 83 84 |
# File 'lib/app_archetype/renderer.rb', line 76 def render_erb_file(file) raise 'cannot overwrite file' if file.exist? && !@overwrite ("RENDER erb ->: #{file.path}") input = File.read(file.source_file_path) out = ERB.new(input).result(@plan.variables.instance_eval { binding }) File.open(file.path.gsub('.erb', ''), 'w+') { |f| f.write(out) } end |
#render_hbs_file(file) ⇒ Object
Renders handlebars template to output location
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/app_archetype/renderer.rb', line 91 def render_hbs_file(file) raise 'cannot overwrite file' if file.exist? && !@overwrite ("RENDER hbs ->: #{file.path}") input = File.read(file.source_file_path) hbs = Handlebars::Handlebars.new out = hbs.compile(input).call(@plan.variables.to_h) File.open(file.path.gsub('.hbs', ''), 'w+') { |f| f.write(out) } end |
#render_template_file(file) ⇒ Object
Copies source file only overwriting if permitted by the renderer.
The output file name removes the ‘.template` suffix. This is for circumstances where the template includes a hbs or erb file that we want to render untouched.
114 115 116 117 118 119 120 121 122 |
# File 'lib/app_archetype/renderer.rb', line 114 def render_template_file(file) raise 'cannot overwrite file' if file.exist? && !@overwrite path = file.path.gsub('.template', '') ("RENDER template ->: #{path}") FileUtils.cp(file.source_file_path, path) end |
#write_dir(file) ⇒ Object
Creates a directory at the specified location
65 66 67 68 69 |
# File 'lib/app_archetype/renderer.rb', line 65 def write_dir(file) ("CREATE dir -> #{file.path}") FileUtils.mkdir_p(file.path) end |