Skip to content

Commit

Permalink
[WIP] move from mustache-zig to ztl templates
Browse files Browse the repository at this point in the history
Hitting some critical bugs right now, that's why it's draft.

Signed-off-by: Eric Joldasov <[email protected]>
  • Loading branch information
BratishkaErik committed Jan 30, 2025
1 parent e88e61d commit 37d6f30
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
40 changes: 40 additions & 0 deletions share/templates/gentoo.ebuild.ztl
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 25 additions & 15 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.", .{});

Expand Down

0 comments on commit 37d6f30

Please sign in to comment.