feat: add --version

This commit is contained in:
CJ van den Berg 2026-03-14 18:01:32 +01:00
parent f68fdfe013
commit 8662a3f7fd
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 54 additions and 6 deletions

View file

@ -33,6 +33,17 @@ pub fn build(b: *std.Build) void {
mod.linkFramework("CoreFoundation", .{}); mod.linkFramework("CoreFoundation", .{});
} }
var version: std.ArrayList(u8) = .empty;
defer version.deinit(b.allocator);
gen_version(b, version.writer(b.allocator)) catch |e| {
if (b.release_mode != .off)
std.debug.panic("gen_version failed: {any}", .{e});
version.clearAndFree(b.allocator);
version.appendSlice(b.allocator, "unknown") catch {};
};
const write_file_step = b.addWriteFiles();
const version_file = write_file_step.add("version", version.items);
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "nightwatch", .name = "nightwatch",
.root_module = b.createModule(.{ .root_module = b.createModule(.{
@ -45,6 +56,7 @@ pub fn build(b: *std.Build) void {
}, },
}), }),
}); });
exe.root_module.addImport("version", b.createModule(.{ .root_source_file = version_file }));
b.installArtifact(exe); b.installArtifact(exe);
@ -86,3 +98,14 @@ pub fn build(b: *std.Build) void {
b.installArtifact(tests); b.installArtifact(tests);
} }
} }
fn gen_version(b: *std.Build, writer: anytype) !void {
var code: u8 = 0;
const describe = try b.runAllowFail(&[_][]const u8{ "git", "describe", "--always", "--tags" }, &code, .Ignore);
const diff_ = try b.runAllowFail(&[_][]const u8{ "git", "diff", "--stat", "--patch", "HEAD" }, &code, .Ignore);
const diff = std.mem.trimRight(u8, diff_, "\r\n ");
const version = std.mem.trimRight(u8, describe, "\r\n ");
try writer.print("{s}{s}", .{ version, if (diff.len > 0) "-dirty" else "" });
}

View file

@ -238,8 +238,8 @@ pub fn add_watch(self: *@This(), allocator: std.mem.Allocator, path: []const u8)
self.arm(allocator) catch return error.WatchFailed; self.arm(allocator) catch return error.WatchFailed;
} }
pub fn remove_watch(self: *@This(), allocator: std.mem.Allocator, path: []const u8) void { pub fn remove_watch(self: *@This(), allocator: std.mem.Allocator, path: []const u8) error{WatchFailed}!void {
if (self.watches.fetchSwapRemove(path)) |entry| allocator.free(entry.key); if (self.watches.fetchSwapRemove(path)) |entry| allocator.free(entry.key);
self.stop_stream(allocator); self.stop_stream(allocator);
self.arm(allocator) catch {}; self.arm(allocator) catch return error.WatchFailed;
} }

View file

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const nightwatch = @import("nightwatch"); const nightwatch = @import("nightwatch");
const build_options = @import("build_options");
const Watcher = switch (builtin.os.tag) { const Watcher = switch (builtin.os.tag) {
.linux => nightwatch.Create(.polling), .linux => nightwatch.Create(.polling),
@ -170,6 +171,27 @@ fn usage(out: std.fs.File) !void {
try writer.interface.flush(); try writer.interface.flush();
} }
fn version(out: std.fs.File) !void {
var buf: [4096]u8 = undefined;
var writer = out.writer(&buf);
try writer.interface.print(
\\nightwatch version {s}
\\using: {s}
\\
, .{
@embedFile("version"),
@typeName(get_nightwatch().Backend),
});
try writer.interface.flush();
}
fn get_nightwatch() type {
return switch (builtin.os.tag) {
.linux => nightwatch.Create(.polling),
else => nightwatch.Default,
};
}
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
@ -182,11 +204,17 @@ pub fn main() !void {
try usage(std.fs.File.stderr()); try usage(std.fs.File.stderr());
std.process.exit(1); std.process.exit(1);
} }
if (std.mem.eql(u8, args[1], "-h") or std.mem.eql(u8, args[1], "--help")) { if (std.mem.eql(u8, args[1], "-h") or std.mem.eql(u8, args[1], "--help")) {
try usage(std.fs.File.stdout()); try usage(std.fs.File.stdout());
return; return;
} }
if (std.mem.eql(u8, args[1], "--version")) {
try version(std.fs.File.stdout());
return;
}
var buf: [4096]u8 = undefined; var buf: [4096]u8 = undefined;
var stderr = std.fs.File.stderr().writer(&buf); var stderr = std.fs.File.stderr().writer(&buf);
defer stderr.interface.flush() catch {}; defer stderr.interface.flush() catch {};
@ -254,10 +282,7 @@ pub fn main() !void {
.ignore = ignore_list.items, .ignore = ignore_list.items,
}; };
var watcher = switch (builtin.os.tag) { var watcher = try get_nightwatch().init(allocator, &cli_handler.handler);
.linux => try nightwatch.Create(.polling).init(allocator, &cli_handler.handler),
else => try nightwatch.Default.init(allocator, &cli_handler.handler),
};
defer watcher.deinit(); defer watcher.deinit();