From 6670513b2fa538b193cf69a432e3f45d3d61a2bc Mon Sep 17 00:00:00 2001 From: Eric Joldasov Date: Thu, 30 Jan 2025 21:53:07 +0500 Subject: [PATCH] [WIP] move from `mustache-zig` to `ztl` templates Hitting some critical bugs right now, that's why it's draft. Signed-off-by: Eric Joldasov --- share/templates/gentoo.ebuild.ztl | 40 +++++++++++++++++++++++++++++++ src/main.zig | 40 +++++++++++++++++++------------ 2 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 share/templates/gentoo.ebuild.ztl diff --git a/share/templates/gentoo.ebuild.ztl b/share/templates/gentoo.ebuild.ztl new file mode 100644 index 0000000..9e2815d --- /dev/null +++ b/share/templates/gentoo.ebuild.ztl @@ -0,0 +1,40 @@ +// SPDX-FileCopyrightText: 2024 BratishkaErik +// SPDX-License-Identifier: 0BSD +// +// Text above is about this template, not generated file itself. +// +// Text below is just an example for more easy generation of ebuilds +// for ::gentoo and ::guru repos, you can relicense this output however you want. +# Copyright <%= @year %> Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +# Generated by zig-ebuilder <%= @generator_version %> + +EAPI=8 + +DESCRIPTION="Write manually" +HOMEPAGE="Write manually" + +ZIG_SLOT="<%= @zbs["slot"] %>" +inherit zig + +src_configure() { + local my_zbs_args=( + # Here you have list of options that are exposed by build.zig, + # pass USE flags and default values here. + + <% foreach (@zbs["report"]["user_options"]) |user_option| { %> + # <%= user_option["name"] %>: <%= user_option["description"] %> + <%- } %> + + <% foreach (@zbs["report"]["user_options"]) |user_option| { %> + # -D<%= user_option["name"] %>=[<%= user_option["type"] %>] + <%- } %> + + <% foreach (@zbs["report"]["system_integrations"]) |system_integration| { %> + -fsys=<%= system_integration %> + <%- } %> + ) + + zig_src_configure +} diff --git a/src/main.zig b/src/main.zig index e6e5129..160832b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,7 +3,7 @@ // SPDX-License-Identifier: EUPL-1.2 const std = @import("std"); -const mustache = @import("mustache"); +const ztl = @import("ztl"); const BuildZigZon = @import("BuildZigZon.zig"); const Dependencies = @import("Dependencies.zig"); @@ -247,26 +247,25 @@ pub fn main() !void { return error.InvalidTemplate; } else - generator_setup.templates.dir.readFileAlloc(gpa, "gentoo.ebuild.mustache", 1 * 1024 * 1024) catch |err| { + generator_setup.templates.dir.readFileAlloc(gpa, "gentoo.ebuild.ztl", 1 * 1024 * 1024) catch |err| { file_searching_events.err(@src(), "Error when searching default \"gentoo\" template: \"{s}\".", .{@errorName(err)}); return error.InvalidTemplate; }; defer gpa.free(template_text); - const template = switch (try mustache.parseText(gpa, template_text, .{}, .{ .copy_strings = false })) { - .parse_error => |detail| { - file_searching_events.err(@src(), "Error when loading file: {s} caused by \"{s}\" at {d}:{d}.", .{ - @errorName(detail.parse_error), - if (optional_custom_template_path) |custom_template_path| custom_template_path else "(default template)", - detail.lin, - detail.col, - }); - return error.InvalidTemplate; - }, - .success => |template| template, + var template: ztl.Template(void) = .init(gpa, {}); + defer template.deinit(); + + var template_errors: ztl.CompileErrorReport = .{}; + template.compile(template_text, .{ .error_report = &template_errors }) catch |err| { + file_searching_events.err(@src(), "Error when loading file: {s} caused by \"{s}\": {}", .{ + @errorName(err), + if (optional_custom_template_path) |custom_template_path| custom_template_path else "(default template)", + template_errors, + }); + return error.InvalidTemplate; }; - defer template.deinit(gpa); const dependencies: Dependencies = if (global.fetch_mode != .skip) fetch: { const build_zig_zon_loc = if (project_setup.build_zig_zon) |build_zig_zon| build_zig_zon else { @@ -361,7 +360,18 @@ pub fn main() !void { defer gpa.free(context.generator_version); main_log.info(@src(), "Writing generated ebuild to STDOUT...", .{}); - try mustache.render(template, context, stdout); + + var render_errors: ztl.RenderErrorReport = .{}; + defer render_errors.deinit(); + + template.render(stdout, context, .{ .error_report = &render_errors }) catch |err| { + file_searching_events.err(@src(), "Error when rendering template: {s} caused by: {}", .{ + @errorName(err), + render_errors, + }); + return error.InvalidTemplate; + }; + main_log.info(@src(), "Generated ebuild was written to STDOUT.", .{}); main_log.info(@src(), "Note (if using default template): license header there (with \"Gentoo Authors\" and GNU GPLv2) is just an convenience default for making ebuilds for ::gentoo and ::guru repos easier, you can relicense output however you want.", .{});