forked from jetzig-framework/jetzig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile.zig
72 lines (60 loc) · 2.04 KB
/
compile.zig
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const std = @import("std");
fn base64Encode(allocator: std.mem.Allocator, input: []const u8) []const u8 {
const encoder = std.base64.Base64Encoder.init(
std.base64.url_safe_no_pad.alphabet_chars,
std.base64.url_safe_no_pad.pad_char,
);
const size = encoder.calcSize(input.len);
const ptr = allocator.alloc(u8, size) catch @panic("OOM");
_ = encoder.encode(ptr, input);
return ptr;
}
pub fn initDataModule(build: *std.Build) !*std.Build.Module {
const root_path = build.pathFromRoot("..");
var buf = std.ArrayList(u8).init(build.allocator);
defer buf.deinit();
const writer = buf.writer();
const paths = .{
"demo/build.zig",
"demo/src/main.zig",
"demo/src/app/middleware/DemoMiddleware.zig",
"demo/src/app/views/init.zig",
"demo/src/app/views/init/index.zmpl",
"demo/src/app/views/init/_content.zmpl",
"demo/public/jetzig.png",
"demo/public/zmpl.png",
"demo/public/favicon.ico",
"demo/public/styles.css",
"demo/config/database_template.zig",
".gitignore",
};
try writer.writeAll(
\\pub const init_data = .{
\\
);
var dir = try std.fs.openDirAbsolute(root_path, .{});
defer dir.close();
inline for (paths) |path| {
const stat = try dir.statFile(path);
const encoded = base64Encode(
build.allocator,
try dir.readFileAlloc(build.allocator, path, @intCast(stat.size)),
);
defer build.allocator.free(encoded);
const output = try std.fmt.allocPrint(
build.allocator,
\\.{{ .path = "{s}", .data = "{s}" }},
,
.{ path, encoded },
);
defer build.allocator.free(output);
try writer.writeAll(output);
}
try writer.writeAll(
\\};
\\
);
const write_files = build.addWriteFiles();
const init_data_source = write_files.add("init_data.zig", buf.items);
return build.createModule(.{ .root_source_file = init_data_source });
}