From bb21cf68929dae45a4ebde11603cdd602aeb8174 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Fri, 26 Feb 2021 12:42:40 +0100 Subject: [PATCH] Update to new zig render API --- generator/main.zig | 23 +++++++++++++++-------- generator/vulkan/build_integration.zig | 10 ++++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/generator/main.zig b/generator/main.zig index 5011a2d..0e46adb 100644 --- a/generator/main.zig +++ b/generator/main.zig @@ -60,15 +60,22 @@ pub fn main() !void { return; }; - const out_file = cwd.createFile(out_path, .{}) catch |err| { - try stderr.writer().print("Error: Failed to create output file '{s}' ({s})\n", .{ out_path, @errorName(err) }); - return; - }; - defer out_file.close(); - var out_buffer = std.ArrayList(u8).init(allocator); try generate(allocator, xml_src, out_buffer.writer()); - const tree = try std.zig.parse(allocator, out_buffer.items); - _ = try std.zig.render(allocator, out_file.writer(), tree); + const tree = try std.zig.parse(allocator, out_buffer.items); + const formatted = try tree.render(allocator); + defer allocator.free(formatted); + + if (std.fs.path.dirname(out_path)) |dir| { + cwd.makePath(dir) catch |err| { + try stderr.writer().print("Error: Failed to create output directory '{s}' ({s})\n", .{ dir, @errorName(err) }); + return; + }; + } + + cwd.writeFile(out_path, formatted) catch |err| { + try stderr.writer().print("Error: Failed to write to output file '{s}' ({s})\n", .{ out_path, @errorName(err) }); + return; + }; } diff --git a/generator/vulkan/build_integration.zig b/generator/vulkan/build_integration.zig index 8386b28..1f85b08 100644 --- a/generator/vulkan/build_integration.zig +++ b/generator/vulkan/build_integration.zig @@ -65,16 +65,18 @@ pub const GenerateStep = struct { fn make(step: *Step) !void { const self = @fieldParentPtr(GenerateStep, "step", step); const cwd = std.fs.cwd(); - var out_buffer = std.ArrayList(u8).init(self.builder.allocator); + const spec = try cwd.readFileAlloc(self.builder.allocator, self.spec_path, std.math.maxInt(usize)); + + var out_buffer = std.ArrayList(u8).init(self.builder.allocator); try generate(self.builder.allocator, spec, out_buffer.writer()); const tree = try std.zig.parse(self.builder.allocator, out_buffer.items); + var formatted = try tree.render(self.builder.allocator); + const dir = path.dirname(self.package.path).?; try cwd.makePath(dir); - const output_file = cwd.createFile(self.package.path, .{}) catch unreachable; - defer output_file.close(); - _ = try std.zig.render(self.builder.allocator, output_file.writer(), tree); + try cwd.writeFile(self.package.path, formatted); } };