feat: add --version
This commit is contained in:
parent
f68fdfe013
commit
8662a3f7fd
3 changed files with 54 additions and 6 deletions
23
build.zig
23
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 "" });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
33
src/main.zig
33
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();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue