Skip to content

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

OptionDefaultDescription
enabledfalseEnable auto-update functionality
auto_downloadfalseDownload new version automatically
auto_installfalseInstall after download
prompt_before_updatetrueAsk user before updating
backup_currenttrueBackup current version first
download_dirnullCustom download directory
verify_checksumtrueVerify download checksum
expected_checksumnullExpected SHA256 checksum
restart_after_updatefalseRestart app after update

Custom Commands

OptionDescription
install_commandCommand to install the update
pre_update_commandRun before updating
post_update_commandRun after updating
restart_commandCommand 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.

Released under the MIT License.