-
Notifications
You must be signed in to change notification settings - Fork 17
/
loader.cr
56 lines (45 loc) · 1.53 KB
/
loader.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require "base64"
require "compress/gzip"
require "./string_encoder"
module BakedFileSystem
module Loader
class Error < Exception
end
def self.load(io, root_path)
if !File.exists?(root_path)
raise Error.new "path does not exist: #{root_path}"
elsif !File.directory?(root_path)
raise Error.new "path is not a directory: #{root_path}"
elsif !File.readable?(root_path)
raise Error.new "path is not readable: #{root_path}"
end
root_path_length = root_path.size
result = [] of String
files = Dir.glob(File.join(root_path, "**", "*"))
# Reject hidden entities and directories
.reject { |path| File.directory?(path) || !(path =~ /(\/\..+)/).nil? }
files.each do |path|
io << "bake_file BakedFileSystem::BakedFile.new(\n"
io << " path: " << path[root_path_length..-1].dump << ",\n"
io << " size: " << File.info(path).size << ",\n"
compressed = path.ends_with?("gz")
io << " compressed: " << compressed << ",\n"
File.open(path, "rb") do |file|
io << " slice: \""
StringEncoder.open(io) do |encoder|
if compressed
IO.copy file, encoder
else
Compress::Gzip::Writer.open(encoder) do |writer|
IO.copy file, writer
end
end
io << "\".to_slice,\n"
end
end
io << ")\n"
io << "\n"
end
end
end
end