diff --git a/build.zig b/build.zig index e591da6..1313727 100644 --- a/build.zig +++ b/build.zig @@ -47,6 +47,11 @@ pub fn build(b: *std.Build) !void { }, ); + const zmpl_steps = zmpl_dep.builder.top_level_steps; + const zmpl_compile_step = zmpl_steps.get("compile").?; + const compile_step = b.step("compile", "Compile Zmpl templates"); + compile_step.dependOn(&zmpl_compile_step.step); + const zmpl_module = zmpl_dep.module("zmpl"); const jetkv_dep = b.dependency("jetkv", .{ .target = target, .optimize = optimize }); @@ -232,7 +237,10 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn exe_routes_file.root_module.addImport("zmpl", zmpl_module); const run_routes_file_cmd = b.addRunArtifact(exe_routes_file); + const source_files = b.addUpdateSourceFiles(); const routes_file_path = run_routes_file_cmd.addOutputFileArg("routes.zig"); + source_files.addCopyFileToSource(routes_file_path, "src/routes.zig"); + run_routes_file_cmd.addArgs(&.{ root_path, b.pathFromRoot("src"), @@ -317,6 +325,13 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn run_tests_file_cmd.addFileArg(.{ .src_path = .{ .owner = b, .sub_path = sub_path } }); } + const jetzig_steps = jetzig_dep.builder.top_level_steps; + const jetzig_compile_step = jetzig_steps.get("compile").?; + const compile_step = b.step("compile", "Compile Zmpl templates"); + compile_step.dependOn(&jetzig_compile_step.step); + // compile_step.dependOn(&run_routes_file_cmd.step); + b.getInstallStep().dependOn(compile_step); + const exe_unit_tests = b.addTest(.{ .root_source_file = tests_file_path, .target = target, diff --git a/cli/commands/server.zig b/cli/commands/server.zig index 034ac78..b1d736e 100644 --- a/cli/commands/server.zig +++ b/cli/commands/server.zig @@ -51,8 +51,6 @@ pub fn run( const realpath = try std.fs.realpathAlloc(allocator, "."); defer allocator.free(realpath); - var mtime = try totalMtime(allocator, cwd, "src"); - std.debug.print( "Launching development server. [reload:{s}]\n", .{ @@ -66,6 +64,10 @@ pub fn run( try argv.appendSlice(&.{ "zig", "build", + "--watch", + "-fincremental", + "--debounce", + "500", util.environmentBuildOption(main_options.options.environment), "-Djetzig_runner=true", }); @@ -77,20 +79,23 @@ pub fn run( "on", }); - while (true) { - util.runCommandInDir( - allocator, - argv.items, - .{ .path = realpath }, - .{ .output = .stream }, - ) catch { - std.debug.print("Build failed, waiting for file change...\n", .{}); - try awaitFileChange(allocator, cwd, &mtime); - std.debug.print("Changes detected, restarting server...\n", .{}); - continue; - }; + var exe_path = try util.locateExecutable(allocator, cwd, .{}); + const stat = try std.fs.cwd().statFile(exe_path.?); + var mtime = stat.mtime; + + util.runCommandInDir( + allocator, + argv.items, + .{ .path = realpath }, + .{ .output = .stream, .wait = false }, + ) catch { + std.debug.print("Build failed, waiting for file change...\n", .{}); + std.process.exit(1); + }; + + while (true) { + exe_path = try util.locateExecutable(allocator, cwd, .{}); - const exe_path = try util.locateExecutable(allocator, cwd, .{}); if (exe_path == null) { std.debug.print("Unable to locate compiled executable. Exiting.\n", .{}); std.process.exit(1); @@ -120,40 +125,41 @@ pub fn run( // HACK: This currenly doesn't restart the server when it exits, maybe that // could be implemented in the future. - try awaitFileChange(allocator, cwd, &mtime); + try awaitFileChange(exe_path.?, &mtime); std.debug.print("Changes detected, restarting server...\n", .{}); _ = try process.kill(); } } -fn awaitFileChange(allocator: std.mem.Allocator, cwd: std.fs.Dir, mtime: *i128) !void { +fn awaitFileChange(path: []const u8, mtime: *i128) !void { while (true) { std.time.sleep(watch_changes_pause_duration); - const new_mtime = try totalMtime(allocator, cwd, "src"); - if (new_mtime > mtime.*) { - mtime.* = new_mtime; + const stat = try std.fs.cwd().statFile(path); + if (stat.mtime > mtime.*) { + mtime.* = stat.mtime; return; } } } fn totalMtime(allocator: std.mem.Allocator, cwd: std.fs.Dir, sub_path: []const u8) !i128 { - var dir = try cwd.openDir(sub_path, .{ .iterate = true }); - defer dir.close(); + // var dir = try cwd.openDir(sub_path, .{ .iterate = true }); + // defer dir.close(); + _ = sub_path; - var walker = try dir.walk(allocator); + var walker = try cwd.walk(allocator); defer walker.deinit(); var sum: i128 = 0; while (try walker.next()) |entry| { if (entry.kind != .file) continue; - const extension = std.fs.path.extension(entry.path); + // const extension = std.fs.path.extension(entry.path); - if (std.mem.eql(u8, extension, ".zig") or std.mem.eql(u8, extension, ".zmpl")) { - const stat = try dir.statFile(entry.path); - sum += stat.mtime; - } + // if (std.mem.eql(u8, extension, ".zig") or std.mem.eql(u8, extension, ".zmpl")) { + const stat = try cwd.statFile(entry.path); + sum += stat.mtime; + // } } return sum; diff --git a/cli/util.zig b/cli/util.zig index 386a0fc..6a1365d 100644 --- a/cli/util.zig +++ b/cli/util.zig @@ -163,6 +163,7 @@ const Dir = union(enum) { pub const RunOptions = struct { output: enum { stream, capture } = .capture, + wait: bool = true, }; /// Runs a command as a child process in the given directory and verifies successful exit code. @@ -201,6 +202,8 @@ pub fn runCommandInDir(allocator: std.mem.Allocator, argv: []const []const u8, d .stream => {}, } + if (!options.wait) return; + const result = std.process.Child.RunResult{ .term = try child.wait(), .stdout = try stdout.toOwnedSlice(), diff --git a/demo/src/app/views/root.zig b/demo/src/app/views/root.zig index 61b3d0f..94d75cd 100644 --- a/demo/src/app/views/root.zig +++ b/demo/src/app/views/root.zig @@ -12,7 +12,9 @@ pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View { try root.put("imported_number", importedFunction(100, 200, 300)); try request.response.headers.append("x-example-header", "example header value"); - std.debug.print("what", .{}); + try root.put("foobar", "helloooo what"); + try request.server.logger.INFO("hello yx", .{}); + std.debug.print("hello\n", .{}); return request.render(.ok); } diff --git a/demo/src/app/views/root/index.zmpl b/demo/src/app/views/root/index.zmpl index 77a388a..da7bffd 100644 --- a/demo/src/app/views/root/index.zmpl +++ b/demo/src/app/views/root/index.zmpl @@ -1,5 +1,6 @@
+ {{$.foobar}}