diff --git a/build.zig b/build.zig index f86691f..4f91f1f 100644 --- a/build.zig +++ b/build.zig @@ -7,11 +7,20 @@ pub const StaticRequest = @import("src/jetzig.zig").StaticRequest; pub const http = @import("src/jetzig/http.zig"); pub const data = @import("src/jetzig/data.zig"); pub const views = @import("src/jetzig/views.zig"); +const zmpl_build = @import("zmpl"); pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const template_path = b.option([]const u8, "zmpl_templates_path", "Path to templates") orelse "src/app/views/"; + const template_path_option = b.option([]const u8, "zmpl_templates_path", "Path to templates") orelse + "src/app/views/"; + const template_path: []const u8 = if (std.fs.path.isAbsolute(template_path_option)) + try b.allocator.dupe(u8, template_path_option) + else + std.fs.cwd().realpathAlloc(b.allocator, template_path_option) catch |err| switch (err) { + error.FileNotFound => "", + else => return err, + }; const lib = b.addStaticLibrary(.{ .name = "jetzig", @@ -28,6 +37,24 @@ pub fn build(b: *std.Build) !void { jetzig_module.addImport("mime_types", mime_module); lib.root_module.addImport("jetzig", jetzig_module); + const zmpl_version = b.option( + enum { v1, v2 }, + "zmpl_version", + "Zmpl syntax version (default: v1)", + ) orelse .v2; + + if (zmpl_version == .v1) { + std.debug.print( + \\[WARN] Zmpl v1 is deprecated and will soon be removed. + \\ Update to v2 by modifying `jetzigInit` in your `build.zig`: + \\ + \\ try jetzig.jetzigInit(b, exe, .{{ .zmpl_version = .v2 }}); + \\ + \\ See https://jetzig.dev/documentation.html for information on migrating to Zmpl v2. + \\ + , .{}); + } + const zmpl_dep = b.dependency( "zmpl", .{ @@ -35,10 +62,16 @@ pub fn build(b: *std.Build) !void { .optimize = optimize, .zmpl_templates_path = template_path, .zmpl_auto_build = false, + .zmpl_version = zmpl_version, + .zmpl_constants = try zmpl_build.addTemplateConstants(b, struct { + jetzig_view: []const u8, + jetzig_action: []const u8, + }), }, ); const zmpl_module = zmpl_dep.module("zmpl"); + // This is the way to make it look nice in the zig build script // If we would do it the other way around, we would have to do // b.dependency("jetzig",.{}).builder.dependency("zmpl",.{}).module("zmpl"); @@ -46,19 +79,6 @@ pub fn build(b: *std.Build) !void { const zmd_dep = b.dependency("zmd", .{ .target = target, .optimize = optimize }); - const ZmplBuild = @import("zmpl").ZmplBuild; - const ZmplTemplate = @import("zmpl").Template; - var zmpl_build = ZmplBuild.init(b, lib, template_path); - const TemplateConstants = struct { - jetzig_view: []const u8, - jetzig_action: []const u8, - }; - const ZmplOptions = struct { - pub const template_constants = TemplateConstants; - }; - const manifest_module = try zmpl_build.compile(ZmplTemplate, ZmplOptions); - zmpl_module.addImport("zmpl.manifest", manifest_module); - lib.root_module.addImport("zmpl", zmpl_module); jetzig_module.addImport("zmpl", zmpl_module); jetzig_module.addImport("args", zig_args_dep.module("args")); @@ -86,17 +106,17 @@ pub fn build(b: *std.Build) !void { test_step.dependOn(&run_main_tests.step); } -/// Placeholder for potential options we may add in future without breaking -/// backward-compatibility. -pub const JetzigInitOptions = struct {}; +/// Build-time options for Jetzig. +pub const JetzigInitOptions = struct { + zmpl_version: enum { v1, v2 } = .v1, +}; pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigInitOptions) !void { - _ = options; const target = b.host; const optimize = exe.root_module.optimize orelse .Debug; const jetzig_dep = b.dependency( "jetzig", - .{ .optimize = optimize, .target = b.host }, + .{ .optimize = optimize, .target = target, .zmpl_version = options.zmpl_version }, ); const jetzig_module = jetzig_dep.module("jetzig"); const zmpl_module = jetzig_dep.module("zmpl"); @@ -159,5 +179,6 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn exe_static_routes.root_module.addImport("jetzig_app", &exe.root_module); const run_static_routes_cmd = b.addRunArtifact(exe_static_routes); + run_static_routes_cmd.expectExitCode(0); exe.step.dependOn(&run_static_routes_cmd.step); } diff --git a/build.zig.zon b/build.zig.zon index dbb8401..7bb3441 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -7,8 +7,8 @@ .hash = "1220bfc5c29bc930b5a524c210712ef65c6cde6770450899bef01164a3089e6707fa", }, .zmpl = .{ - .url = "https://github.com/jetzig-framework/zmpl/archive/ffdbd3767da28d5ab07c1a786ea778152d2f79f6.tar.gz", - .hash = "12202cf05fd4ba2482a9b4b89c632b435310a76ac501b7a3d87dfd41006748dd138d", + .url = "https://github.com/jetzig-framework/zmpl/archive/d907a96ffa28721477f5c8895732a4a9396276f5.tar.gz", + .hash = "1220e96969db44c451577e6a59a0d99af73b4370781c9361e3ea7e0d6f24c7f13abb", }, .args = .{ .url = "https://github.com/MasterQ32/zig-args/archive/01d72b9a0128c474aeeb9019edd48605fa6d95f7.tar.gz", diff --git a/demo/build.zig b/demo/build.zig index 8f2e0ae..c6eaaec 100644 --- a/demo/build.zig +++ b/demo/build.zig @@ -18,7 +18,7 @@ pub fn build(b: *std.Build) !void { // All dependencies **must** be added to imports above this line. - try jetzig.jetzigInit(b, exe, .{}); + try jetzig.jetzigInit(b, exe, .{ .zmpl_version = .v2 }); b.installArtifact(exe); diff --git a/demo/src/app/views/iguanas/index.zmpl b/demo/src/app/views/iguanas/index.zmpl index 7651164..c07004f 100644 --- a/demo/src/app/views/iguanas/index.zmpl +++ b/demo/src/app/views/iguanas/index.zmpl @@ -1,6 +1,7 @@