Repository navigation
Expand file tree
/
Copy pathdemo.zig
More file actions
50 lines (47 loc) · 1.99 KB
/
Copy pathdemo.zig
File metadata and controls
50 lines (47 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/// This file demonstrates a self-updating Zig executable.
/// If this Demo is V1 it will replace itself with Demo2 (V2) (expected to be in zig-out/bin/demo2) and continues running the new version.
const std = @import("std");
const replace_exe = @import("replace-exe");
const SEMVER = "V1";
pub fn main() !void {
const native_os = @import("builtin").os.tag;
const allocator = std.heap.page_allocator;
// Register hook for process to do self_delete/replace detection
replace_exe.init(allocator); // Must be called as soon as possible on Windows - If Allocator is not provided, ArenaAllocator(std.heap.page_allocator) is used
var args = try std.process.ArgIterator.initWithAllocator(allocator);
defer args.deinit();
_ = args.next(); // skip exe name
const cmd = args.next() orelse {
std.log.err("Usage: demo <replace|delete> [new_executable_path]", .{});
std.process.exit(1);
};
const d = Demo{};
var updated: bool = false;
d.print("Before:");
if (std.mem.eql(u8, SEMVER, "V1")) {
if (std.mem.eql(u8, cmd, "replace")) {
// Here demo2 is a new exe that must replace demo1 or demo
replace_exe.selfReplace(allocator, args.next() orelse "zig-out/bin/demo2") catch |err| {
std.log.err("Failed to replace executable: {}", .{err});
return err;
};
updated = true;
} else if (std.mem.eql(u8, cmd, "delete")) {
std.log.info("V1: Deleting self...", .{});
try replace_exe.selfDelete(switch (native_os) {
.windows => allocator,
else => null,
});
std.log.info("V1: Self-delete succeeded.", .{});
} else {
return error.InvalidArguments;
}
}
if (updated) std.log.info("Update complete!", .{});
d.print("After:");
}
const Demo = struct {
fn print(_: *const Demo, prefix: []const u8) void {
std.log.info("{s} Demo version: {s}", .{ prefix, SEMVER });
}
};