Auto-Update
updater.zig provides comprehensive auto-update functionality for downloading and installing updates automatically.
AutoUpdater Class
The AutoUpdater class provides a complete update workflow:
zig
const std = @import("std");
const updater = @import("updater");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var auto_updater = updater.AutoUpdater.init(allocator, .{
.provider = updater.providers.github,
.owner = "username",
.repo = "myproject",
.current_version = "1.0.0",
.auto_update = .{
.enabled = true,
.auto_download = true,
.auto_install = false,
.prompt_before_update = true,
.backup_current = true,
.verify_checksum = true,
.install_command = "zig build install",
.pre_update_command = "echo Backing up...",
.post_update_command = "echo Done!",
},
});
defer auto_updater.deinit();
// Check and optionally update
const result = try auto_updater.checkAndUpdate();
if (result.has_update) {
std.debug.print("Update available: {s}\n", .{result.latest_version.?});
}
}AutoUpdateConfig Options
| Option | Default | Description |
|---|---|---|
enabled | false | Enable auto-update functionality |
auto_download | false | Download new version automatically |
auto_install | false | Install after download |
prompt_before_update | true | Ask user before updating |
backup_current | true | Backup current version first |
download_dir | null | Custom download directory |
verify_checksum | true | Verify download checksum |
expected_checksum | null | Expected SHA256 checksum |
restart_after_update | false | Restart app after update |
Custom Commands
| Option | Description |
|---|---|
install_command | Command to install the update |
pre_update_command | Run before updating |
post_update_command | Run after updating |
restart_command | Command to restart the app |
Methods
checkAndUpdate()
Checks for updates and optionally downloads/installs:
zig
const result = try auto_updater.checkAndUpdate();download()
Downloads the update:
zig
try auto_updater.download(result);install()
Installs a downloaded update:
zig
try auto_updater.install();backupCurrent()
Creates a backup of the current executable:
zig
try auto_updater.backupCurrent();rollback()
Restores from backup if update fails:
zig
try auto_updater.rollback();cleanup()
Removes temporary files:
zig
auto_updater.cleanup();Update Workflows
Manual (Default)
User is notified and updates manually:
zig
.auto_update = .{
.enabled = true,
.auto_download = false,
.prompt_before_update = true,
}Semi-Automatic
Downloads automatically but asks before installing:
zig
.auto_update = .{
.enabled = true,
.auto_download = true,
.auto_install = false,
.prompt_before_update = true,
}Fully Automatic
Downloads and installs automatically:
zig
.auto_update = .{
.enabled = true,
.auto_download = true,
.auto_install = true,
.prompt_before_update = false,
.backup_current = true,
}Complete Example
zig
const std = @import("std");
const updater = @import("updater");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var auto_updater = updater.AutoUpdater.init(allocator, .{
.provider = updater.providers.github,
.owner = "muhammad-fiaz",
.repo = "updater.zig",
.current_version = "0.0.1",
.auto_update = .{
.enabled = true,
.auto_download = true,
.auto_install = true,
.backup_current = true,
.verify_checksum = true,
.install_command = "zig build install -Doptimize=ReleaseSafe",
},
});
defer auto_updater.deinit();
const result = auto_updater.checkAndUpdate() catch |err| {
std.debug.print("Update check failed: {}\n", .{err});
return;
};
if (result.has_update) {
std.debug.print("Updated to {s}\n", .{result.latest_version.?});
} else {
std.debug.print("Already on latest version\n", .{});
}
}Checksum Verification
Enable checksum verification for security:
zig
.auto_update = .{
.verify_checksum = true,
.expected_checksum = "abc123...", // Optional SHA256 hash
}If expected_checksum is provided, the download is verified against it. If verification fails, error.ChecksumMismatch is returned.