Skip to content

Commit 6c6ab63

Browse files
authored
Merge pull request #11 from kivikakk/genent
build.zig: generate entities using the provided APIs.
2 parents 9cc3600 + 7b9ad1b commit 6c6ab63

File tree

5 files changed

+96
-2306
lines changed

5 files changed

+96
-2306
lines changed

build.zig

+11-56
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,40 @@
11
const std = @import("std");
22
const assert = std.debug.assert;
3-
const zig = std.zig;
43

54
pub fn build(b: *std.Build) !void {
6-
try generateEntities();
5+
const genent = b.addExecutable(.{
6+
.name = "generate_entities",
7+
.root_source_file = b.path("src/generate_entities.zig"),
8+
.target = b.host,
9+
});
10+
const genent_step = b.addRunArtifact(genent);
11+
const genent_out = genent_step.addOutputFileArg("entities.zig");
712

813
const target = b.standardTargetOptions(.{});
914
const optimize = b.standardOptimizeOption(.{});
1015

11-
_ = b.addModule("htmlentities", .{
16+
const mod = b.addModule("htmlentities", .{
1217
.root_source_file = b.path("src/main.zig"),
1318
.optimize = optimize,
1419
.target = target,
1520
});
21+
mod.addAnonymousImport("entities", .{ .root_source_file = genent_out });
1622

1723
const lib = b.addStaticLibrary(.{
1824
.name = "htmlentities.zig",
1925
.root_source_file = b.path("src/main.zig"),
2026
.optimize = optimize,
2127
.target = target,
2228
});
29+
lib.root_module.addAnonymousImport("entities", .{ .root_source_file = genent_out });
2330
b.installArtifact(lib);
2431

2532
var main_tests = b.addTest(.{
2633
.root_source_file = b.path("src/main.zig"),
2734
.optimize = optimize,
2835
});
36+
main_tests.root_module.addAnonymousImport("entities", .{ .root_source_file = genent_out });
2937

3038
const test_step = b.step("test", "Run library tests");
3139
test_step.dependOn(&main_tests.step);
3240
}
33-
34-
const embedded_json = @embedFile("entities.json");
35-
36-
fn strLessThan(_: void, lhs: []const u8, rhs: []const u8) bool {
37-
return std.mem.lessThan(u8, lhs, rhs);
38-
}
39-
40-
fn generateEntities() !void {
41-
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
42-
defer arena.deinit();
43-
44-
const allocator = arena.allocator();
45-
46-
var tree = try std.json.parseFromSlice(std.json.Value, allocator, embedded_json, .{});
47-
48-
var buffer = std.ArrayList(u8).init(allocator);
49-
var writer = buffer.writer();
50-
51-
try writer.writeAll("pub const ENTITIES = [_]@import(\"main.zig\").Entity{\n");
52-
53-
var keys = try std.ArrayList([]const u8).initCapacity(allocator, tree.value.object.count());
54-
var entries_it = tree.value.object.iterator();
55-
while (entries_it.next()) |entry| {
56-
keys.appendAssumeCapacity(entry.key_ptr.*);
57-
}
58-
59-
std.mem.sortUnstable([]const u8, keys.items, {}, strLessThan);
60-
61-
for (keys.items) |key| {
62-
var value = tree.value.object.get(key).?.object;
63-
try std.fmt.format(writer, ".{{ .entity = \"{}\", .codepoints = ", .{zig.fmtEscapes(key)});
64-
65-
const codepoints_array = value.get("codepoints").?.array;
66-
if (codepoints_array.items.len == 1) {
67-
try std.fmt.format(writer, ".{{ .Single = {} }}, ", .{codepoints_array.items[0].integer});
68-
} else {
69-
try std.fmt.format(writer, ".{{ .Double = [2]u32{{ {}, {} }} }}, ", .{ codepoints_array.items[0].integer, codepoints_array.items[1].integer });
70-
}
71-
72-
try std.fmt.format(writer, ".characters = \"{}\" }},\n", .{zig.fmtEscapes(value.get("characters").?.string)});
73-
}
74-
75-
try writer.writeAll("};\n");
76-
77-
try buffer.append(0);
78-
79-
var zig_tree = try zig.Ast.parse(allocator, buffer.items[0 .. buffer.items.len - 1 :0], .zig);
80-
81-
var out_file = try std.fs.cwd().createFile("src/entities.zig", .{});
82-
const formatted = try zig_tree.render(allocator);
83-
try out_file.writer().writeAll(formatted);
84-
out_file.close();
85-
}
File renamed without changes.

0 commit comments

Comments
 (0)