diff --git a/src/architecture.zig b/src/architecture.zig index 8ad46d3..dfcdb89 100644 --- a/src/architecture.zig +++ b/src/architecture.zig @@ -27,50 +27,36 @@ fn archToString(arch: std.Target.Cpu.Arch) ?[]const u8 { }; } -pub fn detect(allocator: std.mem.Allocator, params: DetectParams) !?[]u8 { - const os_str = osToString(params.os) orelse return error.UnsupportedSystem; - const arch_str = archToString(params.arch) orelse return error.UnsupportedSystem; - - const len = os_str.len + arch_str.len + 1; // +1 for the '-' - const result = try allocator.alloc(u8, len); - - if (params.reverse) { - @memcpy(result[0..arch_str.len], arch_str); - result[arch_str.len] = '-'; - @memcpy(result[arch_str.len + 1 ..], os_str); - } else { - @memcpy(result[0..os_str.len], os_str); - result[os_str.len] = '-'; - @memcpy(result[os_str.len + 1 ..], arch_str); - } - - return result; +/// get platform str +/// +/// in offical zig json +/// use "arch-platform" as key +/// use "platform-arch" as file name +/// +/// for performance, we treat this function as comptime-func +pub fn platform_str(comptime params: DetectParams) !?[]const u8 { + const os_str = comptime osToString(params.os) orelse + return error.UnsupportedSystem; + + const arch_str = comptime archToString(params.arch) orelse + return error.UnsupportedSystem; + + if (params.reverse) + return arch_str ++ "-" ++ os_str; + + return os_str ++ "-" ++ arch_str; } test "detect() Test" { - const allocator = std.testing.allocator; - - { - const result = try detect(allocator, DetectParams{ .os = std.Target.Os.Tag.linux, .arch = std.Target.Cpu.Arch.x86_64 }) orelse unreachable; - defer allocator.free(result); - try std.testing.expectEqualStrings("linux-x86_64", result); - } + const result_1 = try platform_str(DetectParams{ .os = std.Target.Os.Tag.linux, .arch = std.Target.Cpu.Arch.x86_64 }) orelse unreachable; + try std.testing.expectEqualStrings("linux-x86_64", result_1); - { - const result = try detect(allocator, DetectParams{ .os = std.Target.Os.Tag.linux, .arch = std.Target.Cpu.Arch.aarch64, .reverse = true }) orelse unreachable; - defer allocator.free(result); - try std.testing.expectEqualStrings("aarch64-linux", result); - } + const result_2 = try platform_str(DetectParams{ .os = std.Target.Os.Tag.linux, .arch = std.Target.Cpu.Arch.aarch64, .reverse = true }) orelse unreachable; + try std.testing.expectEqualStrings("aarch64-linux", result_2); - { - const result = try detect(allocator, DetectParams{ .os = std.Target.Os.Tag.macos, .arch = std.Target.Cpu.Arch.x86_64 }) orelse unreachable; - defer allocator.free(result); - try std.testing.expectEqualStrings("macos-x86_64", result); - } + const result_3 = try platform_str(DetectParams{ .os = std.Target.Os.Tag.macos, .arch = std.Target.Cpu.Arch.x86_64 }) orelse unreachable; + try std.testing.expectEqualStrings("macos-x86_64", result_3); - { - const result = try detect(allocator, DetectParams{ .os = std.Target.Os.Tag.windows, .arch = std.Target.Cpu.Arch.x86_64 }) orelse unreachable; - defer allocator.free(result); - try std.testing.expectEqualStrings("windows-x86_64", result); - } + const result_4 = try platform_str(DetectParams{ .os = std.Target.Os.Tag.windows, .arch = std.Target.Cpu.Arch.x86_64 }) orelse unreachable; + try std.testing.expectEqualStrings("windows-x86_64", result_4); } diff --git a/src/download.zig b/src/download.zig index de3ddaa..1f3eb2b 100644 --- a/src/download.zig +++ b/src/download.zig @@ -107,16 +107,19 @@ fn download_and_extract( var zvm_dir = try open_or_create_zvm_dir(); defer zvm_dir.close(); - const platform = try architecture.detect(allocator, architecture.DetectParams{ .os = builtin.os.tag, .arch = builtin.cpu.arch, .reverse = false }) orelse unreachable; - defer allocator.free(platform); + const platform_str = try architecture.platform_str(architecture.DetectParams{ + .os = builtin.os.tag, + .arch = builtin.cpu.arch, + .reverse = false, + }) orelse unreachable; - const file_name = try std.mem.concat(allocator, u8, &[_][]const u8{ "zig-", platform, "-", version, ".", archive_ext }); + const file_name = try std.mem.concat(allocator, u8, &[_][]const u8{ "zig-", platform_str, "-", version, ".", archive_ext }); defer allocator.free(file_name); const total_size: usize = @intCast(req.response.content_length orelse 0); var downloaded_bytes: usize = 0; - const download_message = try std.fmt.allocPrint(allocator, "Downloading Zig version {s} for platform {s}...", .{ version, platform }); + const download_message = try std.fmt.allocPrint(allocator, "Downloading Zig version {s} for platform {s}...", .{ version, platform_str }); defer allocator.free(download_message); var download_node = root_node.start(download_message, total_size); diff --git a/src/install.zig b/src/install.zig index 95e7ccb..8b2896b 100644 --- a/src/install.zig +++ b/src/install.zig @@ -97,17 +97,17 @@ fn fetch_version_data(allocator: Allocator, requested_version: []const u8, sub_k } pub fn from_version(version: []const u8) !void { - var allocator = tools.get_allocator(); + const allocator = tools.get_allocator(); - const platform_str = try architecture.detect(allocator, architecture.DetectParams{ + const platform_str = try architecture.platform_str(architecture.DetectParams{ .os = builtin.os.tag, .arch = builtin.cpu.arch, .reverse = true, }) orelse unreachable; - defer allocator.free(platform_str); var arena = std.heap.ArenaAllocator.init(allocator); defer arena.deinit(); + const version_data = try fetch_version_data(arena.allocator(), version, platform_str); if (version_data) |data| { std.debug.print("Install {s}\n", .{data.name});