Skip to content

Commit

Permalink
wip: add std.progress
Browse files Browse the repository at this point in the history
  • Loading branch information
hendriknielaender committed Nov 26, 2024
1 parent d544c84 commit 0c2411d
Showing 1 changed file with 46 additions and 22 deletions.
68 changes: 46 additions & 22 deletions src/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const util_extract = @import("util/extract.zig");
const util_tool = @import("util/tool.zig");
const util_http = @import("util/http.zig");
const util_minisign = @import("util/minisign.zig");
const Progress = std.Progress;

const Version = struct {
name: []const u8,
Expand Down Expand Up @@ -42,84 +43,107 @@ fn install_zig(version: []const u8) !void {

const arena_allocator = arena.allocator();

// Get version path
// Determine the number of steps
const total_steps = 6;

// Initialize progress root node
const root_node = std.Progress.start(.{
.root_name = "Installing Zig",
.estimated_total_items = total_steps,
});

var items_done: usize = 0;

// Step 1: Get version path
const version_path = try util_data.get_zvm_zig_version(arena_allocator);
// Get extract path
items_done += 1;
root_node.setCompletedItems(items_done);

// Step 2: Get extract path
const extract_path = try std.fs.path.join(arena_allocator, &.{ version_path, version });
items_done += 1;
root_node.setCompletedItems(items_done);

// Get version data
// Step 3: Get version data
const version_data: meta.Zig.VersionData = blk: {
const res = try util_http.http_get(arena_allocator, config.zig_url);
var zig_meta = try meta.Zig.init(res, arena_allocator);
const tmp_val = try zig_meta.get_version_data(version, platform_str, arena_allocator);
break :blk tmp_val orelse return error.UnsupportedVersion;
};
items_done += 1;
root_node.setCompletedItems(items_done);

if (util_tool.does_path_exist(extract_path)) {
try alias.set_version(version, false);
root_node.end();
return;
}

// Step 4: Download the tarball
const file_name = std.fs.path.basename(version_data.tarball);

const parsed_uri = std.Uri.parse(version_data.tarball) catch unreachable;

// Download the tarball
// Create a child progress node for the download
const download_node = root_node.start("Downloading Zig tarball", version_data.size);
const tarball_file = try util_http.download(parsed_uri, file_name, version_data.shasum, version_data.size);
defer tarball_file.close();
download_node.end();
items_done += 1;

root_node.setCompletedItems(items_done);

// Derive signature URI by appending ".minisig" to the tarball URL
// Step 5: Download the signature file
var signature_uri_buffer: [1024]u8 = undefined;
const signature_uri_buf = try std.fmt.bufPrint(
&signature_uri_buffer,
"{s}.minisig",
.{version_data.tarball}, // Use the original tarball URL
.{version_data.tarball},
);

const signature_uri = try std.Uri.parse(signature_uri_buffer[0..signature_uri_buf.len]);

// Define signature file name
const signature_file_name = try std.mem.concat(
arena_allocator,
u8,
&.{ file_name, ".minisig" },
);

// Download the signature file
// Create a child progress node for the signature download
const sig_download_node = root_node.start("Downloading Signature File", 0);
const minisig_file = try util_http.download(signature_uri, signature_file_name, null, null);
defer minisig_file.close();
sig_download_node.end();
items_done += 1;
root_node.setCompletedItems(items_done);

// Get paths to the tarball and signature files
// Step 6: Perform Minisign Verification
const zvm_store_path = try util_data.get_zvm_path_segment(allocator, "store");
defer allocator.free(zvm_store_path);
const tarball_path = try std.fs.path.join(arena_allocator, &.{ zvm_store_path, file_name });
const sig_path = try std.fs.path.join(arena_allocator, &.{ zvm_store_path, signature_file_name });

// Perform Minisign Verification
try util_minisign.verify(
&allocator,
sig_path,
config.ZIG_MINISIGN_PUBLIC_KEY,
tarball_path,
);
items_done += 1;
root_node.setCompletedItems(items_done);

// Proceed with extraction after successful verification
const extract_node = root_node.start("Extracting Zig tarball", 0);
try util_tool.try_create_path(extract_path);
const extract_dir = try std.fs.openDirAbsolute(extract_path, .{});

try util_extract.extract(extract_dir, tarball_file, if (builtin.os.tag == .windows) .zip else .tarxz, false);
extract_node.end();
items_done += 1;
root_node.setCompletedItems(items_done);

//TODO: not needed (macOS) still needed for unix and windows?
// const sub_path = try std.fs.path.join(arena_allocator, &.{
// extract_path, try std.mem.concat(
// arena_allocator,
// u8,
// &.{},
// ),
// });
//
//try util_tool.copy_dir(sub_path, extract_path);
root_node.end();

// Set the version alias
try alias.set_version(version, false);
}

Expand Down

0 comments on commit 0c2411d

Please sign in to comment.