Backlog/buildgen/generateModApi.zig

50 lines
1.7 KiB
Zig

const std = @import("std");
pub fn usage() void {
std.debug.print("generates a loader module which calls the start_modules for all the modules that the game module depends on.\ngenerate-mod-api <output file name> <module_name> <module list>\n", .{});
}
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
// Get output filename from build system
const args = try std.process.argsAlloc(allocator);
if (args.len < 3) {
usage();
@panic("Missing output filename\n");
}
const out_path = args[1];
const module_name = args[2];
const module_list = args[3..];
var writer = std.ArrayList(u8){};
try writer.print(allocator, "pub const moduleName:[]const u8 = \"{s}\";\n", .{module_name});
try writer.print(allocator, "pub const moduleList:[]const []const u8 = &.{{\n", .{});
for (module_list) |mod| {
try writer.print(allocator, "\"{s}\",\n", .{mod});
}
try writer.print(allocator, "}};\n", .{});
for (module_list) |mod| {
try writer.print(allocator, "pub const {s} = @import(\"{s}\").module;\n", .{ mod, mod });
}
try writer.append(allocator, 0);
const file = try std.fs.cwd().createFile(out_path, .{});
defer file.close();
var ast = try std.zig.Ast.parse(allocator, @as([:0]const u8, @ptrCast(writer.items[0 .. writer.items.len - 1])), .zig);
// std.debug.print("output=\n{s}", .{writer.items[0 .. writer.items.len - 1]});
defer ast.deinit(allocator);
const out = try ast.renderAlloc(allocator);
// Write the content to the file
try file.writeAll(out);
}