diff --git a/build.zig b/build.zig index 4f04a2d..ffd5430 100644 --- a/build.zig +++ b/build.zig @@ -33,6 +33,17 @@ pub fn build(b: *std.Build) void { 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(.{ .name = "nightwatch", .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); @@ -86,3 +98,14 @@ pub fn build(b: *std.Build) void { 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 "" }); +} diff --git a/src/backend/FSEvents.zig b/src/backend/FSEvents.zig index d72e37a..5e27554 100644 --- a/src/backend/FSEvents.zig +++ b/src/backend/FSEvents.zig @@ -238,8 +238,8 @@ pub fn add_watch(self: *@This(), allocator: std.mem.Allocator, path: []const u8) 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); self.stop_stream(allocator); - self.arm(allocator) catch {}; + self.arm(allocator) catch return error.WatchFailed; } diff --git a/src/main.zig b/src/main.zig index 02fc402..d322b14 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,6 +1,7 @@ const std = @import("std"); const builtin = @import("builtin"); const nightwatch = @import("nightwatch"); +const build_options = @import("build_options"); const Watcher = switch (builtin.os.tag) { .linux => nightwatch.Create(.polling), @@ -170,6 +171,27 @@ fn usage(out: std.fs.File) !void { 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 { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); @@ -182,11 +204,17 @@ pub fn main() !void { try usage(std.fs.File.stderr()); std.process.exit(1); } + if (std.mem.eql(u8, args[1], "-h") or std.mem.eql(u8, args[1], "--help")) { try usage(std.fs.File.stdout()); return; } + if (std.mem.eql(u8, args[1], "--version")) { + try version(std.fs.File.stdout()); + return; + } + var buf: [4096]u8 = undefined; var stderr = std.fs.File.stderr().writer(&buf); defer stderr.interface.flush() catch {}; @@ -254,10 +282,7 @@ pub fn main() !void { .ignore = ignore_list.items, }; - var watcher = switch (builtin.os.tag) { - .linux => try nightwatch.Create(.polling).init(allocator, &cli_handler.handler), - else => try nightwatch.Default.init(allocator, &cli_handler.handler), - }; + var watcher = try get_nightwatch().init(allocator, &cli_handler.handler); defer watcher.deinit();