Skip to content

Commit 81bb954

Browse files
committed
feat: version command
1 parent 4eaca12 commit 81bb954

5 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎build.zig‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ pub fn build(b: *std.Build) void {
2626
.root_source_file = b.path("asset/root.zig"),
2727
});
2828

29+
// Build options module (version info)
30+
const build_options = b.addOptions();
31+
// Read version from build.zig.zon
32+
const build_zon = @import("build.zig.zon");
33+
build_options.addOption([]const u8, "version", build_zon.version);
34+
const build_options_mod = build_options.createModule();
35+
2936
if (cross_compile) {
3037
// Build for all targets
3138
for (cross_targets) |t| {
@@ -44,6 +51,7 @@ pub fn build(b: *std.Build) void {
4451
.imports = &.{
4552
.{ .name = "argzon", .module = argzon_mod },
4653
.{ .name = "asset", .module = asset_mod },
54+
.{ .name = "build_options", .module = build_options_mod },
4755
},
4856
}),
4957
});
@@ -78,6 +86,7 @@ pub fn build(b: *std.Build) void {
7886
.imports = &.{
7987
.{ .name = "argzon", .module = argzon_mod },
8088
.{ .name = "asset", .module = asset_mod },
89+
.{ .name = "build_options", .module = build_options_mod },
8190
},
8291
}),
8392
});

‎build.zig.zon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
.name = .shgit,
1010
// This is a [Semantic Version](https://semver.org/).
1111
// In a future version of Zig it will be used for package deduplication.
12-
.version = "0.0.0",
12+
.version = "0.1.4",
1313
// Together with name, this represents a globally unique package
1414
// identifier. This field is generated by the Zig toolchain when the
1515
// package is first created, and then *never changes*. This allows

‎src/args.zon‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,9 @@
9797
.name = "init",
9898
.description = "Initialize shgit in an existing directory structure",
9999
},
100+
.{
101+
.name = "version",
102+
.description = "Show shgit version",
103+
},
100104
},
101105
}

‎src/commands/version.zig‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const std = @import("std");
2+
const build_options = @import("build_options");
3+
4+
const log = std.log.scoped(.version);
5+
6+
pub fn execute(allocator: std.mem.Allocator, verbose: bool) !void {
7+
_ = allocator;
8+
_ = verbose;
9+
10+
// Get version from build.zig.zon
11+
const version = try std.SemanticVersion.parse(build_options.version);
12+
13+
var stdout_buf: [10 * build_options.version.len]u8 = undefined;
14+
var stdout_buffered = std.fs.File.stdout().writer(&stdout_buf);
15+
const stdout = &stdout_buffered.interface;
16+
17+
try stdout.print("shgit v{}.{}.{}", .{ version.major, version.minor, version.patch });
18+
if (version.pre) |pre| {
19+
try stdout.print("-{s}", .{pre});
20+
}
21+
if (version.build) |build| {
22+
try stdout.print("+{s}", .{build});
23+
}
24+
try stdout.print("\n", .{});
25+
try stdout.flush();
26+
}

‎src/main.zig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const link = @import("commands/link.zig");
77
const worktree = @import("commands/worktree.zig");
88
const sync = @import("commands/sync.zig");
99
const init_cmd = @import("commands/init.zig");
10+
const version = @import("commands/version.zig");
1011

1112
const log = std.log.scoped(.shgit);
1213

@@ -43,6 +44,7 @@ pub fn main() !void {
4344
.worktree => |wt_args| try worktree.execute(gpa, wt_args, verbose),
4445
.sync => try sync.execute(gpa, verbose),
4546
.init => try init_cmd.execute(gpa, verbose),
47+
.version => try version.execute(gpa, verbose),
4648
}
4749
} else {
4850
var stdout_buf: [argzon.MAX_BUF_SIZE]u8 = undefined;

0 commit comments

Comments
 (0)