Cli: Reload for fail build

When zig build fails, the `jetzig server` command also exits. This
should be an uninteneded behavior. This commit fixes this by catching
failed build and restart after file change is detected.
Fixes #33
This commit is contained in:
Froxcey 2024-03-25 16:55:12 +08:00
parent 19c04f1482
commit f01abb86f5
No known key found for this signature in database
GPG Key ID: 621379E1D6250388

View File

@ -63,12 +63,15 @@ pub fn run(
);
while (true) {
// TODO: Catch this error instead of propagating
try util.runCommand(
util.runCommand(
allocator,
realpath,
&[_][]const u8{ "zig", "build", "-Djetzig_runner=true", "install", "--color", "on" },
);
) catch {
std.debug.print("Build failed, waiting for file change...\n", .{});
try awaitFileChange(allocator, cwd, &mtime);
continue;
};
const exe_path = try util.locateExecutable(allocator, cwd, .{});
if (exe_path == null) {
@ -98,22 +101,22 @@ pub fn run(
std.process.exit(term.Exited);
}
while (true) {
if (process.term) |_| {
_ = try process.wait();
std.debug.print("Server exited, restarting...\n", .{});
}
// HACK: This currenly doesn't restart the server when it exits, maybe that
// could be implemented in the future.
std.time.sleep(watch_changes_pause_duration);
awaitFileChange(allocator, cwd, mtime);
std.debug.print("Changes detected, restarting server...\n", .{});
_ = try process.kill();
}
}
const new_mtime = try totalMtime(allocator, cwd, "src");
if (new_mtime > mtime) {
std.debug.print("Changes detected, restarting server...\n", .{});
_ = try process.kill();
mtime = new_mtime;
break;
}
fn awaitFileChange(allocator: std.mem.Allocator, cwd: std.fs.Dir, 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;
return;
}
}
}