Skip to content

Commit

Permalink
build.zig: generate source without polluting user dirs.
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed Jun 11, 2024
1 parent 9cc3600 commit b98ea9d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2,290 deletions.
67 changes: 11 additions & 56 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,85 +1,40 @@
const std = @import("std");
const assert = std.debug.assert;
const zig = std.zig;

pub fn build(b: *std.Build) !void {
try generateEntities();
const genent = b.addExecutable(.{
.name = "generate_entities",
.root_source_file = b.path("tools/generate_entities.zig"),
.target = b.host,
});
const genent_step = b.addRunArtifact(genent);
const genent_out = genent_step.addOutputFileArg("entities.zig");

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

_ = b.addModule("htmlentities", .{
const mod = b.addModule("htmlentities", .{
.root_source_file = b.path("src/main.zig"),
.optimize = optimize,
.target = target,
});
mod.addAnonymousImport("entities", .{ .root_source_file = genent_out });

const lib = b.addStaticLibrary(.{
.name = "htmlentities.zig",
.root_source_file = b.path("src/main.zig"),
.optimize = optimize,
.target = target,
});
lib.root_module.addAnonymousImport("entities", .{ .root_source_file = genent_out });
b.installArtifact(lib);

var main_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.optimize = optimize,
});
main_tests.root_module.addAnonymousImport("entities", .{ .root_source_file = genent_out });

const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step);
}

const embedded_json = @embedFile("entities.json");

fn strLessThan(_: void, lhs: []const u8, rhs: []const u8) bool {
return std.mem.lessThan(u8, lhs, rhs);
}

fn generateEntities() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();

const allocator = arena.allocator();

var tree = try std.json.parseFromSlice(std.json.Value, allocator, embedded_json, .{});

var buffer = std.ArrayList(u8).init(allocator);
var writer = buffer.writer();

try writer.writeAll("pub const ENTITIES = [_]@import(\"main.zig\").Entity{\n");

var keys = try std.ArrayList([]const u8).initCapacity(allocator, tree.value.object.count());
var entries_it = tree.value.object.iterator();
while (entries_it.next()) |entry| {
keys.appendAssumeCapacity(entry.key_ptr.*);
}

std.mem.sortUnstable([]const u8, keys.items, {}, strLessThan);

for (keys.items) |key| {
var value = tree.value.object.get(key).?.object;
try std.fmt.format(writer, ".{{ .entity = \"{}\", .codepoints = ", .{zig.fmtEscapes(key)});

const codepoints_array = value.get("codepoints").?.array;
if (codepoints_array.items.len == 1) {
try std.fmt.format(writer, ".{{ .Single = {} }}, ", .{codepoints_array.items[0].integer});
} else {
try std.fmt.format(writer, ".{{ .Double = [2]u32{{ {}, {} }} }}, ", .{ codepoints_array.items[0].integer, codepoints_array.items[1].integer });
}

try std.fmt.format(writer, ".characters = \"{}\" }},\n", .{zig.fmtEscapes(value.get("characters").?.string)});
}

try writer.writeAll("};\n");

try buffer.append(0);

var zig_tree = try zig.Ast.parse(allocator, buffer.items[0 .. buffer.items.len - 1 :0], .zig);

var out_file = try std.fs.cwd().createFile("src/entities.zig", .{});
const formatted = try zig_tree.render(allocator);
try out_file.writer().writeAll(formatted);
out_file.close();
}
Loading

0 comments on commit b98ea9d

Please sign in to comment.