Module: Origen::Generator::Renderer
- Included in:
- Compiler
- Defined in:
- lib/origen/generator/renderer.rb
Overview
Handles the recursive rendering and importing of sub templates and source files
Instance Method Summary collapse
- #current_pipeline ⇒ Object
-
#insert(content) ⇒ Object
Insert rendered content into any placeholders.
- #insert_content(current, placeholder, content) ⇒ Object
- #options ⇒ Object
- #pipeline ⇒ Object
- #placeholder ⇒ Object
- #render(file, options = {}, &block) ⇒ Object (also: #import)
-
#temporary_file ⇒ Object
Returns a Pathname to a uniquely named temporary file.
Instance Method Details
#current_pipeline ⇒ Object
36 37 38 |
# File 'lib/origen/generator/renderer.rb', line 36 def current_pipeline pipeline.last end |
#insert(content) ⇒ Object
Insert rendered content into any placeholders
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/origen/generator/renderer.rb', line 41 def insert(content) while current_pipeline.size > 0 current = current_pipeline.pop pipeline << [] @current_options = current[:options] self.current_buffer = '' output = compile(current[:file], sub_template: true, block: current[:block], scope: @scope) if current[:indent] && current[:indent] > 0 indent = ' ' * current[:indent] output = output.split("\n").map { |l| indent + l }.join("\n") end @current_options = nil content = insert_content(content, current[:placeholder], output) end pipeline.pop # Always give back a string, this is what existing callers expect # # Possible this could in future run into problems if the whole file cannot be read # into memory, but we can cross that path when we come to it if content.is_a?(Pathname) c = content.read content.delete c else content end end |
#insert_content(current, placeholder, content) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/origen/generator/renderer.rb', line 72 def insert_content(current, placeholder, content) # Start using the disk for storing the output rather than memory # once it starts to exceed this length max_length = 1_000_000 if current.is_a?(Pathname) || content.is_a?(Pathname) || ((current.length + content.length) > max_length) unless current.is_a?(Pathname) t = temporary_file t.open('w') { |f| f.puts current } current = t end new = temporary_file new.open('w') do |new_f| current.each_line do |line| if line.strip == placeholder if content.is_a?(Pathname) content.each_line do |line| new_f.puts line end content.delete else new_f.puts content.chomp end else new_f.puts line end end end current.delete new else current.sub(/ *#{placeholder}/, content) end end |
#options ⇒ Object
26 27 28 |
# File 'lib/origen/generator/renderer.rb', line 26 def @current_options ||= {} end |
#pipeline ⇒ Object
30 31 32 33 34 |
# File 'lib/origen/generator/renderer.rb', line 30 def pipeline @pipeline ||= [] @pipeline << [] if @pipeline.empty? @pipeline end |
#placeholder ⇒ Object
20 21 22 23 24 |
# File 'lib/origen/generator/renderer.rb', line 20 def placeholder @ix ||= 0 @ix += 1 "_origen_render_placeholder_#{@ix}" end |
#render(file, options = {}, &block) ⇒ Object Also known as: import
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/origen/generator/renderer.rb', line 6 def render(file, = {}, &block) fail 'File argument is nil' unless file file = Origen.file_handler.clean_path_to_sub_template(file) current_pipeline << { file: file, options: , placeholder: placeholder, block: block, indent: [:indent] || 0 } if block_given? self.current_buffer += current_pipeline.last[:placeholder] + "\n" end current_pipeline.last[:placeholder] end |
#temporary_file ⇒ Object
Returns a Pathname to a uniquely named temporary file
108 109 110 111 |
# File 'lib/origen/generator/renderer.rb', line 108 def temporary_file # Ensure this is unique so that is doesn't clash with parallel compile processes Pathname.new "#{Origen.root}/tmp/compiler_#{Process.pid}_#{Time.now.to_f}" end |