Something went wrong. Try again.
Templating language written in Zig
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417const std = @import("std");const Build = std.Build;const ArrayList = std.ArrayList;const Allocator = std.mem.Allocator;const Writer = std.Io.Writer;const builtin = @import("builtin");const Encoder = std.base64.standard.Encoder;const Data = @import("src/zmpl.zig").Data;pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const options_files = b.addWriteFiles();
var threaded: std.Io.Threaded = .init_single_threaded; // This deinit is not necessary since it's stack allocated but it's ok to // keep, in case we want to change the threaded initialization at some point defer threaded.deinit(); const io = threaded.io();
const use_llvm = b.option( bool, "use_llvm", "Use LLVM", );
const test_filters = b.option( []const []const u8, "test-filter", "Skip tests that do not match any filter", ) orelse &.{};
const zmpl_auto_build_option = b.option( bool, "zmpl_auto_build", "Automatically compile Zmpl templates (default: true)", );
const zmpl_markdown_fragments_option = b.option( []const u8, "zmpl_markdown_fragments", "Custom markdown fragments", ) orelse "";
const zmpl_options_header_option = b.option( []const u8, "zmpl_options_header", "Additional options header", ) orelse "";
const zmpl_manifest_header_option = b.option( []const u8, "zmpl_manifest_header", "Additional manifest header", ) orelse "";
const zmpl_constants_option = b.option( []const u8, "zmpl_constants", "Template constants", ) orelse "";
const templates_paths = b.option( []const []const u8, "zmpl_templates_paths", "Directories to search for .zmpl templates. Format: `prefix=...,path=...", ) orelse try templatesPaths( io, b.allocator, &.{.{ .prefix = "templates", .path = &.{ "src", "templates" }, }}, );
const build_options = b.addOptions(); build_options.addOption( bool, "sanitize", b.option( bool, "sanitize", "Disable default sanitization of data references.", ) orelse true, );
const zmpl_options = b.addModule("zmpl_options", .{ .root_source_file = options_files.add( "zmpl_options.zig", try generateZmplOptions( b.allocator, zmpl_options_header_option, zmpl_markdown_fragments_option, zmpl_constants_option, zmpl_manifest_header_option, ), ), });
const zmd = b.dependency("zmd", .{ .target = target, .optimize = optimize, }).module("zmd");
const jetcommon = b.dependency("jetcommon", .{ .target = target, .optimize = optimize, }).module("jetcommon");
const entry = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/main.zig"), });
const manifest_m = b.createModule(.{ .root_source_file = b.path("src/manifest/main.zig"), .target = b.graph.host, .optimize = optimize, });
const manifest_exe = b.addExecutable(.{ .name = "manifest", .use_llvm = use_llvm, .root_module = manifest_m, });
const zmpl = b.addModule("zmpl", .{ .root_source_file = b.path("src/zmpl.zig"), });
const dummy_manifest = b.createModule(.{ .root_source_file = b.path("src/dummy_manifest.zig"), });
const dummy_zmpl_options = b.createModule(.{ .root_source_file = b.path("src/manifest/dummy_zmpl_options.zig"), });
const tests = b.createModule(.{ .root_source_file = b.path("src/tests.zig"), .target = target, .optimize = optimize, });
const zmpl_m = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/zmpl.zig"), });
const lib = b.addLibrary(.{ .name = "zmpl", .linkage = .static, .use_llvm = use_llvm, .root_module = zmpl_m, });
const exe = b.addExecutable(.{ .name = "zmpl", .use_llvm = use_llvm, .root_module = entry, });
const run_artifact = b.addRunArtifact(exe); const run_step = b.step("run", "Run benchmarking"); run_step.dependOn(&run_artifact.step);
lib.root_module.addImport("zmpl", zmpl); exe.root_module.addImport("zmpl", zmpl);
zmpl.addOptions("build_options", build_options);
lib.root_module.addImport("zmd", zmd); zmpl.addImport("zmd", zmd);
lib.root_module.addImport("jetcommon", jetcommon); zmpl.addImport("jetcommon", jetcommon);
const auto_build = if (zmpl_auto_build_option) |opt| opt else true;
zmpl_options.addImport("zmd", zmd); manifest_exe.root_module.addImport("zmpl_options", zmpl_options); manifest_exe.root_module.addImport("zmd", zmd); manifest_exe.root_module.addImport("jetcommon", jetcommon);
const manifest_exe_run = b.addRunArtifact(manifest_exe); b.getInstallStep().dependOn(&manifest_exe_run.step); const manifest_lazy_path = manifest_exe_run.addOutputFileArg("zmpl.manifest.zig");
manifest_exe_run.setCwd(.{ .cwd_relative = try std.Io.Dir.cwd().realPathFileAlloc(io, ".", b.allocator), }); manifest_exe_run.expectExitCode(0); manifest_exe_run.addArg(try std.mem.join(b.allocator, ";", templates_paths));
lib.step.dependOn(&manifest_exe_run.step);
for (try findTemplates(b, io, templates_paths)) |path| manifest_exe_run.addFileArg(.{ .cwd_relative = path });
const compile_step = b.step("compile", "Compile Zmpl templates"); compile_step.dependOn(&manifest_exe_run.step);
const manifest = b.addModule("zmpl.manifest", .{ .root_source_file = manifest_lazy_path, });
manifest.addImport("zmpl", zmpl); manifest.addImport("zmd", zmd); zmpl.addImport("zmpl.manifest", manifest);
if (auto_build) { const template_tests = b.addTest(.{ .filters = test_filters, .root_module = tests, });
const zmpl_tests = b.addTest(.{ .filters = test_filters, .root_module = b.createModule(.{ .root_source_file = b.path("src/zmpl.zig"), .target = target, .optimize = optimize, }), });
const manifest_tests = b.addTest(.{ .filters = test_filters, .root_module = b.createModule(.{ .root_source_file = b.path("src/manifest/main.zig"), .target = target, .optimize = optimize, }), });
template_tests.root_module.addImport("zmpl", zmpl); template_tests.root_module.addImport("zmpl.manifest", manifest); template_tests.root_module.addImport("jetcommon", jetcommon);
zmpl_tests.root_module.addImport("jetcommon", jetcommon); zmpl_tests.root_module.addImport("zmpl.manifest", dummy_manifest); zmpl_tests.root_module.addImport("zmd", zmd);
manifest_tests.root_module.addImport("zmpl_options", dummy_zmpl_options); manifest_tests.root_module.addImport("zmd", zmd);
const run_template_tests = b.addRunArtifact(template_tests); const run_zmpl_tests = b.addRunArtifact(zmpl_tests); const run_manifest_tests = b.addRunArtifact(manifest_tests);
const test_step = b.step("test", "Run library tests"); test_step.dependOn(&run_template_tests.step); test_step.dependOn(&run_zmpl_tests.step); test_step.dependOn(&run_manifest_tests.step); }
b.installArtifact(lib);
const docs_step = b.step("docs", "Generate documentation"); const docs_install = b.addInstallDirectory(.{ .source_dir = lib.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", });
docs_step.dependOn(&docs_install.step);}
const TemplatesPath = struct { prefix: []const u8, path: []const []const u8,};
pub fn templatesPaths(io: std.Io, allocator: Allocator, paths: []const TemplatesPath) ![]const []const u8 { var buf: ArrayList([]const u8) = .empty; defer buf.deinit(allocator); for (paths) |path| { const joined = try std.fs.path.join(allocator, path.path); defer allocator.free(joined);
const absolute_path = if (std.fs.path.isAbsolute(joined)) try allocator.dupe(u8, joined) else std.Io.Dir.cwd().realPathFileAlloc(io, joined, allocator) catch |err| switch (err) { error.FileNotFound => "_", else => return err, };
try buf.append( allocator, try std.mem.concat( allocator, u8, &.{ "prefix=", path.prefix, ",path=", absolute_path }, ), ); }
return buf.toOwnedSlice(allocator);}
pub fn addTemplateConstants(b: *Build, comptime constants: type) ![]const u8 { const fields = switch (@typeInfo(constants)) { .@"struct" => |info| info.fields, else => @panic("Expected struct, found: " ++ @typeName(constants)), }; var array: [fields.len][]const u8 = undefined;
inline for (fields, 0..) |field, index| { array[index] = std.fmt.comptimePrint( "{s}#{s}", .{ field.name, @typeName(field.type) }, ); }
return std.mem.join(b.allocator, "|", &array);}
fn findTemplates(b: *Build, io: std.Io, templates_paths: []const []const u8) ![][]const u8 { var templates: ArrayList([]const u8) = .empty; defer templates.deinit(b.allocator);
var templates_paths_buf: ArrayList([]const u8) = .empty; defer templates_paths_buf.deinit(b.allocator); for (templates_paths) |syntax| { const prefix_end = std.mem.indexOf(u8, syntax, ",path=").?; const path_start = prefix_end + ",path=".len; const path = syntax[path_start..]; try templates_paths_buf.append(b.allocator, path); }
for (templates_paths_buf.items) |templates_path| { if (std.mem.eql(u8, templates_path, "_")) continue;
var dir = std.Io.Dir.cwd().openDir( io, templates_path, .{ .iterate = true }, ) catch |err| { switch (err) { error.FileNotFound => { std.log.warn( "[zmpl] Template directory `{s}` not found, skipping.", .{templates_path}, ); continue; }, else => return err, } };
var walker = try dir.walk(b.allocator); defer walker.deinit();
while (try walker.next(io)) |entry| { if (entry.kind != .file) continue; const extension = std.fs.path.extension(entry.path); if (!std.mem.eql(u8, extension, ".zmpl")) continue; try templates.append(b.allocator, try dir.realPathFileAlloc(io, entry.path, b.allocator)); } } return templates.toOwnedSlice(b.allocator);}
fn generateZmplOptions( allocator: Allocator, options_header_option: []const u8, markdown_fragments: []const u8, constants: []const u8, manifest_header_option: []const u8,) ![]const u8 { var aw: Writer.Allocating = .init(allocator);
try aw.writer.print( "//Generated in build.zig\n{[options]s}\n\n", .{ .options = options_header_option }, );
try parseZmplConstants( &aw.writer, constants, );
const encodedHeader: []u8 = try allocator.alloc(u8, Encoder.calcSize(manifest_header_option.len)); defer allocator.free(encodedHeader); const base64Header = Encoder.encode(encodedHeader, manifest_header_option);
try aw.writer.print( "{s}\n\npub const manifest_header: []const u8 = \"{s}\";\n", .{ markdown_fragments, base64Header }, );
return aw.toOwnedSlice();}
fn parseZmplConstants(writer: *Writer, constants_string: []const u8) !void { if (constants_string.len == 0) return; try writer.writeAll("pub const template_constants = struct {\n"); var pairs_it = std.mem.splitScalar(u8, constants_string, '|'); while (pairs_it.next()) |pair| { var arg_it = std.mem.splitScalar(u8, pair, '#'); const const_name = arg_it.first(); const const_type = arg_it.next() orelse { std.log.err("Incomplete Zmpl constants argument: {s}", .{constants_string}); return error.ZmplConstantsOptionError; }; // this should be null if (arg_it.next()) |_| { std.log.err("Incoherent Zmpl constants argument: {s}", .{constants_string}); return error.ZmplConstantsOptionError; } try writer.print(" {s}: {s},\n", .{ const_name, const_type }); } try writer.writeAll("};\n");}