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

Class Method Details

.apply_locals_to_compiled(compiled_html, locals) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/hyraft/boot/preloader.rb', line 132

def self.apply_locals_to_compiled(compiled_html, locals)
  return compiled_html if locals.empty?
  
  locals.each do |key, value|
    compiled_html = compiled_html.gsub(/\[\.#{key}\.\]/, value.to_s)
    compiled_html = compiled_html.gsub(/:\[#{key}\]/, value.to_s)
  end
  compiled_html
end

.discover_templatesObject



45
46
47
# File 'lib/hyraft/boot/preloader.rb', line 45

def self.discover_templates
  Dir.glob(File.join('app', '**', '*.hyr')).sort
end

.get_template(template_key) ⇒ Object



106
107
108
# File 'lib/hyraft/boot/preloader.rb', line 106

def self.get_template(template_key)
  @preloaded_templates[template_key]
end

.memory_usageObject



165
166
167
168
169
170
171
172
173
# File 'lib/hyraft/boot/preloader.rb', line 165

def self.memory_usage
  if Gem.win_platform?
    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)
  else
    (`ps -o rss= -p #{Process.pid}`.to_i / 1024.0).round(2)
  end
end

.preload_template(file_path) ⇒ Object



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
# File 'lib/hyraft/boot/preloader.rb', line 49

def self.preload_template(file_path)
  template_key = file_path
    .delete_prefix('app/')
    .sub(/\.hyr$/, '')
  
  begin
    content = File.read(file_path)
    parser = Hyraft::Compiler::HyraftParser.new(content)
    parsed = parser.parse
    
    # Check if template has dynamic Ruby code
    has_ruby = parsed[:ruby].to_s.strip.length > 0
    has_elevhyra = parsed[:elevhyra].to_s.strip.length > 0
    
    compiled_html = nil
    compile_success = false
    
    if @layout_content && !has_ruby && !has_elevhyra
      begin
        # Only pre-compile static HTML templates
        renderer = Hyraft::Compiler::HyraftRenderer.new
        compiled_html = renderer.render_with_layout(parsed, @layout_content.dup, {})
        compile_success = true
      rescue => e
        # Compilation failed, will use sections only
      end
    end
    
    @preloaded_templates[template_key] = {
      parsed: parsed,
      compiled_html: compiled_html,
      compile_success: compile_success,
      has_ruby: has_ruby,
      has_elevhyra: has_elevhyra,
      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 has_ruby || has_elevhyra
      puts "  #{COLORS[:blue]}#{COLORS[:reset]} #{COLORS[:lightblue]}#{template_key}#{COLORS[:reset]}#{COLORS[:orange]}.hyr#{COLORS[:reset]} (#{COLORS[:cyan]}dynamic - Ruby/JS#{COLORS[:reset]})"
    else
      puts "  #{COLORS[:yellow]}#{COLORS[:reset]} #{COLORS[:lightblue]}#{template_key}#{COLORS[:reset]}#{COLORS[:orange]}.hyr#{COLORS[:reset]} (#{COLORS[:red]}static - not compiled#{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.message}#{COLORS[:reset]}"
  end
end

.preload_templatesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hyraft/boot/preloader.rb', line 25

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


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/hyraft/boot/preloader.rb', line 146

def self.print_stats
  compiled_count = @preloaded_templates.count { |_, t| t[:compile_success] }
  dynamic_count = @preloaded_templates.count { |_, t| t[:has_ruby] || t[:has_elevhyra] }
  static_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, #{static_count} static)"
  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]}RUNTIME RENDERING#{COLORS[:reset]}\n\n"
  end
end

.render_template(template_key, locals = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hyraft/boot/preloader.rb', line 114

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)
  
  # Use pre-compiled HTML if available
  if template_data[:compiled_html] && template_data[:compile_success]
    return apply_locals_to_compiled(template_data[:compiled_html].dup, locals)
  end
  
  # Otherwise render with full processor
 # layout_file = File.join(ROOT, 'public', 'index.html')
  layout_content = File.read(layout_file) if File.exist?(layout_file)
  
  # Use PageProcessor for dynamic templates
  Hyraft::Compiler::PageProcessor.process(template_data[:full_path], template_key, locals)
end

.statsObject



142
143
144
# File 'lib/hyraft/boot/preloader.rb', line 142

def self.stats
  @stats.dup
end

.template_preloaded?(template_key) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/hyraft/boot/preloader.rb', line 110

def self.template_preloaded?(template_key)
  @preloaded_templates.key?(template_key)
end