Module: Hyraft::Preloader
- Defined in:
- lib/hyraft/boot/preloader.rb
Constant Summary collapse
- COLORS =
{ green: "\e[32m", cyan: "\e[36m", yellow: "\e[33m", red: "\e[31m", orange: "\e[38;5;214m", blue: "\e[34m", lightblue: "\e[94m", reset: "\e[0m" }
Class Method Summary collapse
-
.apply_locals_to_compiled(compiled_html, locals) ⇒ Object
FAST: Apply locals to pre-compiled HTML (only for placeholders, no Ruby execution).
- .discover_templates ⇒ Object
- .extract_section(content, section_name) ⇒ Object
- .extract_sections(content) ⇒ Object
- .extract_styles(content) ⇒ Object
- .get_template(template_key) ⇒ Object
- .get_windows_memory ⇒ Object
- .memory_usage ⇒ Object
- .preload_template(file_path) ⇒ Object
- .preload_templates ⇒ Object
- .print_stats ⇒ Object
- .render_template(template_key, locals = {}) ⇒ Object
- .stats ⇒ Object
- .template_preloaded?(template_key) ⇒ Boolean
Class Method Details
.apply_locals_to_compiled(compiled_html, locals) ⇒ Object
FAST: Apply locals to pre-compiled HTML (only for placeholders, no Ruby execution)
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/hyraft/boot/preloader.rb', line 134 def self.apply_locals_to_compiled(compiled_html, locals) return compiled_html if locals.empty? # Simple string replacement for [.variable.] placeholders locals.each do |key, value| placeholder = "[.#{key}.]" compiled_html = compiled_html.gsub(placeholder, value.to_s) end compiled_html end |
.discover_templates ⇒ Object
44 45 46 |
# File 'lib/hyraft/boot/preloader.rb', line 44 def self.discover_templates Dir.glob(File.join('adapter-intake', '**', '*.hyr')).sort end |
.extract_section(content, section_name) ⇒ Object
161 162 163 164 |
# File 'lib/hyraft/boot/preloader.rb', line 161 def self.extract_section(content, section_name) match = content.match(/<#{section_name}.*?>(.*?)<\/#{section_name}>/m) match ? match[1].strip : '' end |
.extract_sections(content) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/hyraft/boot/preloader.rb', line 151 def self.extract_sections(content) { metadata: extract_section(content, 'metadata'), displayer: extract_section(content, 'displayer'), transmuter: extract_section(content, 'transmuter'), manifestor: extract_section(content, 'manifestor'), styles: extract_styles(content) } end |
.extract_styles(content) ⇒ Object
166 167 168 |
# File 'lib/hyraft/boot/preloader.rb', line 166 def self.extract_styles(content) content.scan(/<style[^>]*href="([^"]*)"/).flatten end |
.get_template(template_key) ⇒ Object
107 108 109 |
# File 'lib/hyraft/boot/preloader.rb', line 107 def self.get_template(template_key) @preloaded_templates[template_key] end |
.get_windows_memory ⇒ Object
199 200 201 202 203 |
# File 'lib/hyraft/boot/preloader.rb', line 199 def self.get_windows_memory memory_kb = `tasklist /FI "PID eq #{Process.pid}" /FO CSV /NH` .split(",")[4].to_s.gsub('"','').gsub(/[^0-9]/, '').to_i (memory_kb / 1024.0).round(2) end |
.memory_usage ⇒ Object
189 190 191 192 193 194 195 196 197 |
# File 'lib/hyraft/boot/preloader.rb', line 189 def self.memory_usage if Gem.win_platform? # Windows (no `ps -o rss=`) get_windows_memory else # Linux/Mac (`ps -o rss= -p #{Process.pid}`.to_i / 1024.0).round(2) end end |
.preload_template(file_path) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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/hyraft/boot/preloader.rb', line 48 def self.preload_template(file_path) template_key = file_path .delete_prefix('adapter-intake/') .sub(/\.hyr$/, '') begin content = File.read(file_path) sections = extract_sections(content) # ONLY pre-compile templates WITHOUT transmuter code (pure HTML templates) compiled_html = nil compile_success = false compile_error = nil if @layout_content && sections[:transmuter].to_s.strip.empty? begin # Only compile templates that don't have Ruby code in transmuter renderer = Hyraft::Compiler::HyraftRenderer.new # For pure HTML templates, we can safely compile with empty locals # The [.variable.] placeholders will remain as-is for runtime replacement compiled_html = renderer.render(@layout_content.dup, sections, {}) compile_success = true rescue => e compile_error = e. end else compile_error = "has transmuter code" if sections[:transmuter] && !sections[:transmuter].empty? end @preloaded_templates[template_key] = { sections: sections, compiled_html: compiled_html, compile_success: compile_success, compile_error: compile_error, bytesize: content.bytesize, compiled_bytes: compiled_html&.bytesize || 0, mtime: File.mtime(file_path), full_path: file_path } @stats[:total_templates] += 1 @stats[:total_bytes] += content.bytesize @stats[:compiled_bytes] += compiled_html&.bytesize || 0 if compile_success puts " #{COLORS[:green]}✓#{COLORS[:reset]} #{COLORS[:lightblue]}#{template_key}#{COLORS[:reset]}#{COLORS[:orange]}.hyr#{COLORS[:reset]} (#{COLORS[:yellow]}#{(compiled_html.bytesize / 1024.0).round(2)} KB#{COLORS[:reset]})" elsif compile_error == "has transmuter code" puts " #{COLORS[:blue]}ℹ#{COLORS[:reset]} #{COLORS[:lightblue]}#{template_key}#{COLORS[:reset]}#{COLORS[:orange]}.hyr#{COLORS[:reset]} (#{COLORS[:cyan]}dynamic template#{COLORS[:reset]})" else puts " #{COLORS[:yellow]}⚠#{COLORS[:reset]} #{COLORS[:lightblue]}#{template_key}#{COLORS[:reset]}#{COLORS[:orange]}.hyr#{COLORS[:reset]} (#{COLORS[:red]}sections only#{COLORS[:reset]})" end rescue => e puts " #{COLORS[:red]}✗#{COLORS[:reset]} #{COLORS[:cyan]}#{template_key}#{COLORS[:reset]}#{COLORS[:green]}.hyr#{COLORS[:reset]} #{COLORS[:red]}Error: #{e.}#{COLORS[:reset]}" end end |
.preload_templates ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/hyraft/boot/preloader.rb', line 24 def self.preload_templates puts "#{COLORS[:green]} Hyraft Preloader: Scanning for templates...#{COLORS[:reset]}" start_time = Time.now # Load layout first layout_file = File.join(ROOT, 'public', 'index.html') @layout_content = File.read(layout_file) if File.exist?(layout_file) templates_found = discover_templates puts "Found #{templates_found.size} template files" templates_found.each do |file_path| preload_template(file_path) end @stats[:load_time] = Time.now - start_time print_stats end |
.print_stats ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/hyraft/boot/preloader.rb', line 170 def self.print_stats compiled_count = @preloaded_templates.count { |_, t| t[:compile_success] } dynamic_count = @preloaded_templates.count { |_, t| t[:compile_error] == "has transmuter code" } sections_count = @preloaded_templates.count - compiled_count - dynamic_count puts "\n#{COLORS[:green]}Preload Statistics:#{COLORS[:reset]}" puts " #{COLORS[:cyan]}Templates:#{COLORS[:reset]} #{@stats[:total_templates]} (#{compiled_count} pre-compiled, #{dynamic_count} dynamic, #{sections_count} sections only)" puts " #{COLORS[:cyan]}Source Size:#{COLORS[:reset]} #{(@stats[:total_bytes] / 1024.0).round(2)} KB" puts " #{COLORS[:cyan]}Compiled Size:#{COLORS[:reset]} #{(@stats[:compiled_bytes] / 1024.0).round(2)} KB" puts " #{COLORS[:cyan]}Load Time:#{COLORS[:reset]} #{@stats[:load_time].round(3)}s" puts " #{COLORS[:cyan]}Memory:#{COLORS[:reset]} #{memory_usage} MB" if compiled_count > 0 puts " #{COLORS[:cyan]}Status:#{COLORS[:reset]} #{COLORS[:green]}PARTIALLY PRE-COMPILED#{COLORS[:reset]}\n\n" else puts " #{COLORS[:cyan]}Status:#{COLORS[:reset]} #{COLORS[:yellow]}SECTIONS ONLY#{COLORS[:reset]}\n\n" end end |
.render_template(template_key, locals = {}) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/hyraft/boot/preloader.rb', line 115 def self.render_template(template_key, locals = {}) return "<h1>Template not found: #{template_key}</h1>" unless template_preloaded?(template_key) template_data = get_template(template_key) # ULTRA FAST PATH: Use pre-compiled HTML (only for templates without transmuter) if template_data[:compiled_html] && template_data[:compile_success] return apply_locals_to_compiled(template_data[:compiled_html].dup, locals) end # FAST PATH: Use preloaded sections with HyraftRenderer (for dynamic templates) layout_file = File.join(ROOT, 'public', 'index.html') layout_content = File.read(layout_file) if File.exist?(layout_file) renderer = Hyraft::Compiler::HyraftRenderer.new renderer.render(layout_content, template_data[:sections], locals) end |
.stats ⇒ Object
145 146 147 |
# File 'lib/hyraft/boot/preloader.rb', line 145 def self.stats @stats.dup end |
.template_preloaded?(template_key) ⇒ Boolean
111 112 113 |
# File 'lib/hyraft/boot/preloader.rb', line 111 def self.template_preloaded?(template_key) @preloaded_templates.key?(template_key) end |