Module: Hyraft::AssetPreloader

Defined in:
lib/hyraft/boot/asset_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

.asset_preloaded?(asset_path) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.asset_preloaded?(asset_path)
  @preloaded_assets.key?(asset_path)
end

.compress_asset(content, extname) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/hyraft/boot/asset_preloader.rb', line 94

def self.compress_asset(content, extname)
  case extname.downcase
  when '.css'
    compress_css(content)
  when '.js'
    compress_js(content)
  else
    content
  end
end

.compress_css(css) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/hyraft/boot/asset_preloader.rb', line 105

def self.compress_css(css)
  css.gsub(/\/\*.*?\*\//m, '')
     .gsub(/\s+/, ' ')
     .gsub(/;\s*}/, '}')
     .gsub(/\s*{\s*/, '{')
     .gsub(/\s*}\s*/, '}')
     .gsub(/:\s+/, ':')
     .gsub(/,\s+/, ',')
     .strip
end

.compress_js(js) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/hyraft/boot/asset_preloader.rb', line 116

def self.compress_js(js)
  js.gsub(/\/\/.*?$/, '')
    .gsub(/\/\*.*?\*\//m, '')
    .gsub(/\s+/, ' ')
    .gsub(/\s*([=+\-\/*{}()\[\],;:])\s*/, '\1')  
    .strip
end

.content_type_for(file_path) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/hyraft/boot/asset_preloader.rb', line 124

def self.content_type_for(file_path)
  case File.extname(file_path).downcase
  when '.css' then 'text/css'
  when '.js' then 'application/javascript'
  when '.jpg', '.jpeg' then 'image/jpeg'
  when '.png' then 'image/png'
  when '.gif' then 'image/gif'
  when '.svg' then 'image/svg+xml'
  when '.webp' then 'image/webp'
  when '.ico' then 'image/x-icon'
  else 'application/octet-stream'
  end
end

.discover_assets(public_path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hyraft/boot/asset_preloader.rb', line 39

def self.discover_assets(public_path)
  return [] unless File.exist?(public_path)
  
  assets = []
  
  # Look for assets in common locations
  asset_patterns = [
    "styles/css/**/*.css",
    "css/**/*.css", 
    "images/**/*.{jpg,jpeg,png,gif,svg,webp,ico}",
    "js/**/*.js",
    "favicon.ico"
  ]
  
  asset_patterns.each do |pattern|
    full_pattern = File.join(public_path, pattern)
    assets += Dir.glob(full_pattern)
  end
  
  assets.sort
end

.get_asset(asset_path) ⇒ Object



138
139
140
# File 'lib/hyraft/boot/asset_preloader.rb', line 138

def self.get_asset(asset_path)
  @preloaded_assets[asset_path]
end

.get_windows_memoryObject



178
179
180
181
182
# File 'lib/hyraft/boot/asset_preloader.rb', line 178

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_usageObject



168
169
170
171
172
173
174
175
176
# File 'lib/hyraft/boot/asset_preloader.rb', line 168

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_asset(asset_path, public_path) ⇒ Object



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

def self.preload_asset(asset_path, public_path)
  relative_path = asset_path.sub(public_path + '/', '')
  
  begin
    content = File.read(asset_path, mode: 'rb')
    compressed = compress_asset(content, File.extname(asset_path))
    
    @preloaded_assets[relative_path] = {
      content: content,
      compressed: compressed,
      size: content.bytesize,
      compressed_size: compressed.bytesize,
      mtime: File.mtime(asset_path),
      content_type: content_type_for(asset_path)
    }
    
    @stats[:total_assets] += 1
    @stats[:total_bytes] += content.bytesize
    @stats[:compressed_bytes] += compressed.bytesize
    
    compression_ratio = ((1 - compressed.bytesize.to_f / content.bytesize) * 100).round(2)
    
    puts "  #{COLORS[:green]}#{COLORS[:reset]} #{COLORS[:lightblue]}#{relative_path}#{COLORS[:reset]} " \
         "(#{COLORS[:yellow]}#{(content.bytesize / 1024.0).round(2)} KB#{COLORS[:reset]}" \
         "#{COLORS[:cyan]}#{(compressed.bytesize / 1024.0).round(2)} KB#{COLORS[:reset]} " \
         "#{COLORS[:green]}#{compression_ratio}%#{COLORS[:reset]})"
         
  rescue => e
    puts "  #{COLORS[:red]}#{COLORS[:reset]} #{COLORS[:cyan]}#{relative_path}#{COLORS[:reset]} " \
         "#{COLORS[:red]}Error: #{e.message}#{COLORS[:reset]}"
  end
end

.preload_assets(public_path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hyraft/boot/asset_preloader.rb', line 23

def self.preload_assets(public_path)
  puts "#{COLORS[:green]} Hyraft Asset Preloader: Scanning for assets...#{COLORS[:reset]}"
  
  start_time = Time.now
  assets_found = discover_assets(public_path)
  
  puts "Found #{assets_found.size} asset files"
  
  assets_found.each do |asset_path|
    preload_asset(asset_path, public_path)
  end
  
  @stats[:load_time] = Time.now - start_time
  print_stats
end


157
158
159
160
161
162
163
164
165
166
# File 'lib/hyraft/boot/asset_preloader.rb', line 157

def self.print_stats
  puts "\n#{COLORS[:green]}Asset Preload Statistics:#{COLORS[:reset]}"
  puts "   #{COLORS[:cyan]}Assets:#{COLORS[:reset]} #{@stats[:total_assets]}"
  puts "   #{COLORS[:cyan]}Original Size:#{COLORS[:reset]} #{(@stats[:total_bytes] / 1024.0).round(2)} KB" 
  puts "   #{COLORS[:cyan]}Compressed Size:#{COLORS[:reset]} #{(@stats[:compressed_bytes] / 1024.0).round(2)} KB"
  puts "   #{COLORS[:cyan]}Total Savings:#{COLORS[:reset]} #{((@stats[:total_bytes] - @stats[:compressed_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"
  puts "   #{COLORS[:cyan]}Status:#{COLORS[:reset]} #{COLORS[:green]}ASSETS PRELOADED#{COLORS[:reset]}\n\n"
end

.serve_asset(asset_path) ⇒ Object



146
147
148
149
# File 'lib/hyraft/boot/asset_preloader.rb', line 146

def self.serve_asset(asset_path)
  return nil unless asset_preloaded?(asset_path)
  get_asset(asset_path)
end

.statsObject



151
152
153
# File 'lib/hyraft/boot/asset_preloader.rb', line 151

def self.stats
  @stats.dup
end