From 73fd32eb28753584edb160fc11b0c16078a2ed6d Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Tue, 22 Jul 2025 06:29:25 +0300 Subject: Update Zig --- build.zig | 27 +++++++++++---------------- build.zig.zon | 4 ++-- src/Config.zig | 7 +++++-- src/install.zig | 2 +- src/list.zig | 33 +++++++++++++++++++-------------- src/main.zig | 16 +++++++++------- src/subcommand.zig | 10 ++++++++-- src/update.zig | 2 +- 8 files changed, 56 insertions(+), 45 deletions(-) diff --git a/build.zig b/build.zig index 20ac769..d19e91e 100644 --- a/build.zig +++ b/build.zig @@ -20,14 +20,18 @@ pub fn build(b: *Build) void { const exe = b.addExecutable(.{ .name = "zup", .version = version, - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "clap", .module = clap.module("clap") }, + .{ .name = "libarchive", .module = libarchive.module("libarchive") }, + .{ .name = "xdg", .module = xdg.module("xdg") }, + }, + }), }); exe.root_module.addOptions("zup-config", config); - exe.root_module.addImport("clap", clap.module("clap")); - exe.root_module.addImport("libarchive", libarchive.module("libarchive")); - exe.root_module.addImport("xdg", xdg.module("xdg")); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); @@ -38,18 +42,9 @@ pub fn build(b: *Build) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - - const exe_tests = b.addTest(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - }); - - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&exe_tests.step); } -const default_version = SemanticVersion.parse("0.5.0") catch unreachable; +const default_version = SemanticVersion.parse("0.7.0-indev") catch unreachable; fn getVersion(b: *Build) SemanticVersion { var out_code: u8 = undefined; diff --git a/build.zig.zon b/build.zig.zon index 53324aa..1f2f04d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -6,8 +6,8 @@ .paths = .{""}, .dependencies = .{ .clap = .{ - .url = "https://git.enes.lv/zig-clap/snapshot/zig-clap-0.10.0.tar.xz", - .hash = "clap-0.10.0-oBajB434AQBDh-Ei3YtoKIRxZacVPF1iSwp3IX_ZB8f0", + .url = "https://git.enes.lv/zig-clap/snapshot/zig-clap-6f103922a8133ba11773e6ad9a52e26e1d99b3e7.tar.xz", + .hash = "clap-0.10.0-oBajB0nsAQAlef8ldBenH89XSJNc_x1rVNkpHK2Y2tHg", }, .libarchive = .{ // .path = "../zig-libarchive", diff --git a/src/Config.zig b/src/Config.zig index ba8f496..1e4a9bd 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -63,7 +63,10 @@ fn readConfig(self: *Config, file: File) !void { supported_targets: [][]u8, }; - var reader = std.json.reader(allocator, file.reader()); + const fileBuf = try allocator.alloc(u8, 4096); + var fileReader = file.reader(fileBuf); + + var reader = std.json.Reader.init(allocator, &fileReader.interface); defer reader.deinit(); const parsed = try std.json.parseFromTokenSourceLeaky( @@ -90,7 +93,7 @@ fn readConfig(self: *Config, file: File) !void { }; const resolved = std.zig.system.resolveTargetQuery(query) catch |e| { - std.log.warn("Failed to resolve '{s}' as a target: {}", .{ target, e}); + std.log.warn("Failed to resolve '{s}' as a target: {}", .{ target, e }); continue; }; diff --git a/src/install.zig b/src/install.zig index 8a16e49..2b6d44f 100644 --- a/src/install.zig +++ b/src/install.zig @@ -51,7 +51,7 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I }; defer allocator.free(installation_dir); - std.log.info("Installing {s}, version {}", .{ name, installation.version }); + std.log.info("Installing {s}, version {f}", .{ name, installation.version }); std.log.info("Downloading from {s}...", .{installation.url_str.?}); diff --git a/src/list.zig b/src/list.zig index 9ff26c2..72f3d91 100644 --- a/src/list.zig +++ b/src/list.zig @@ -4,8 +4,10 @@ const zup = @import("root"); const Allocator = std.mem.Allocator; const ArrayList = std.ArrayList; const Config = zup.Config; +const File = std.fs.File; const Installation = zup.Installation; const Installations = zup.Installations; +const Writer = std.io.Writer; pub const params = \\--active List the active version @@ -33,27 +35,33 @@ pub fn main(comptime Result: type, config: Config, res: Result) !void { list_installed = true; } + const buf = try allocator.alloc(u8, 4096); + defer allocator.free(buf); + + var stdout = File.stdout().writer(buf); + if (list_active) { const active = try Installation.getActiveName(allocator); defer if (active) |s| allocator.free(s); - try printActive(active); + try printActive(&stdout.interface, active); } if (list_installed) { var installed = try Installation.getInstalledList(allocator); defer Installation.deinitMap(allocator, &installed); - try printInstalledList(allocator, installed); + try printInstalledList(allocator, &stdout.interface, installed); } if (list_available) { var available = try Installation.getAvailableList(config); defer Installation.deinitMap(allocator, &available); - try printAvailableList(allocator, available); + try printAvailableList(allocator, &stdout.interface, available); } + + try stdout.interface.flush(); } -fn printActive(active: ?[]const u8) !void { - const writer = std.io.getStdOut().writer(); +fn printActive(writer: *Writer, active: ?[]const u8) !void { try writer.writeAll("Active installation: "); if (active) |act| { try writer.writeAll(act); @@ -64,19 +72,17 @@ fn printActive(active: ?[]const u8) !void { try writer.writeByte('\n'); } -fn printAvailableList(allocator: Allocator, avail: Installations) !void { - const writer = std.io.getStdOut().writer(); +fn printAvailableList(allocator: Allocator, writer: *Writer, avail: Installations) !void { try writer.writeAll("Available versions:\n"); - try printList(allocator, avail); + try printList(allocator, writer, avail); } -fn printInstalledList(allocator: Allocator, installed: Installations) !void { - const writer = std.io.getStdOut().writer(); +fn printInstalledList(allocator: Allocator, writer: *Writer, installed: Installations) !void { try writer.writeAll("Installed versions:\n"); - try printList(allocator, installed); + try printList(allocator, writer, installed); } -fn printList(allocator: Allocator, installations: Installations) !void { +fn printList(allocator: Allocator, writer: *Writer, installations: Installations) !void { const InstallationAndName = struct { name: []const u8, installation: Installation, @@ -98,8 +104,7 @@ fn printList(allocator: Allocator, installations: Installations) !void { std.mem.sort(InstallationAndName, list.items, {}, InstallationAndName.lessThan); - const writer = std.io.getStdOut().writer(); for (list.items) |item| { - try writer.print(" {s}: {}\n", .{ item.name, item.installation.version }); + try writer.print(" {s}: {f}\n", .{ item.name, item.installation.version }); } } diff --git a/src/main.zig b/src/main.zig index c7e3844..b4f06a7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -16,6 +16,7 @@ const std = @import("std"); const zup_config = @import("zup-config"); const ArgIterator = std.process.ArgIterator; +const File = std.fs.File; const GPA = std.heap.GeneralPurposeAllocator(.{}); const StaticStringMap = std.StaticStringMap; const Tuple = std.meta.Tuple; @@ -40,7 +41,10 @@ pub fn main() !void { } pub fn printVersion() !void { - return std.io.getStdErr().writer().print("{}\n", .{zup_config.version}); + var buf: [1024]u8 = undefined; + var stderr = File.stderr().writer(&buf); + try stderr.interface.print("{f}\n", .{zup_config.version}); + return stderr.interface.flush(); } const Command = enum { @@ -106,8 +110,9 @@ const Help = struct { } pub fn mainHelp() !void { - const writer = std.io.getStdErr().writer(); - try writer.writeAll( + var buf: [1024]u8 = undefined; + var stderr = File.stderr().writer(&buf); + try stderr.interface.writeAll( \\USAGE: zup \\ \\These are the common Zup commands: @@ -123,6 +128,7 @@ const Help = struct { \\You can find out more about a command by running `zup help `. \\ ); + return stderr.interface.flush(); } fn unknownHelp(cmd: []const u8) !void { @@ -142,7 +148,3 @@ const Version = struct { return printVersion(); } }; - -test "basic test" { - try std.testing.expectEqual(10, 3 + 7); -} diff --git a/src/subcommand.zig b/src/subcommand.zig index 8fde170..6a6259e 100644 --- a/src/subcommand.zig +++ b/src/subcommand.zig @@ -21,7 +21,9 @@ pub fn SubCommand(comptime template: type) type { ++ template.params); pub fn help(name: []const u8) !void { - const writer = std.io.getStdErr().writer(); + var buf: [1024]u8 = undefined; + var stderr = std.fs.File.stderr().writer(&buf); + const writer = &stderr.interface; try writer.print("USAGE: zup {s} ", .{name}); try clap.usage(writer, clap.Help, ¶ms); try writer.writeAll("\n\n"); @@ -33,6 +35,7 @@ pub fn SubCommand(comptime template: type) type { .spacing_between_parameters = 0, }); try writer.writeAll("\n" ++ template.description ++ "\n"); + return writer.flush(); } pub fn main(name: []const u8, config: Config, args: *ArgIterator) !void { @@ -43,7 +46,10 @@ pub fn SubCommand(comptime template: type) type { .allocator = allocator, .diagnostic = &diag, }) catch |err| { - diag.report(std.io.getStdErr().writer(), err) catch {}; + var buf: [1024]u8 = undefined; + var stderr = std.fs.File.stderr().writer(&buf); + diag.report(&stderr.interface, err) catch {}; + stderr.interface.flush() catch {}; try help(name); return err; }; diff --git a/src/update.zig b/src/update.zig index fc1ab51..6ee7951 100644 --- a/src/update.zig +++ b/src/update.zig @@ -26,7 +26,7 @@ pub fn main(comptime Result: type, config: Config, _: Result) !void { const inst = kv.value_ptr.*; if (available.get(name)) |avail| { if (avail.version.order(inst.version) == .gt) { - std.log.info("Updating {s} from {} to {}...", .{ name, inst.version, avail.version }); + std.log.info("Updating {s} from {f} to {f}...", .{ name, inst.version, avail.version }); try zup.install.base.perform(allocator, name, true, available); } } -- cgit v1.2.3