@@ -50,7 +50,7 @@ pub fn install(allocator: std.mem.Allocator, version: []const u8, home: []const
5050 const data = try req .reader ().readAllAlloc (allocator , 2 << 50 );
5151 defer allocator .free (data );
5252
53- const friendlyname = try std .mem .concat (allocator , u8 , &.{ "zigd-" , os , "-" , arch , "-" , version , "." , archive_ext });
53+ const friendlyname = try std .mem .concat (allocator , u8 , &.{ "zigd-" , os , url_platform , version , "." , archive_ext });
5454 defer allocator .free (friendlyname );
5555
5656 var downloaddir = try fromHome (home , "Downloads" );
@@ -65,13 +65,21 @@ pub fn install(allocator: std.mem.Allocator, version: []const u8, home: []const
6565 // because it doesn't support GNU longnames or PAX headers.
6666 // https://imgur.com/9ZUhkHx
6767
68- const fx = try std .fmt .allocPrint (allocator , "zig-" ++ url_platform ++ "-" ++ " {s}" , .{version });
68+ const fx = try std .fmt .allocPrint (allocator , "zig-{s}- {s}" , .{ url_platform , version });
6969 defer allocator .free (fx );
7070
71- const lastp = try std .fs .path .join (allocator , &.{ home , "zig" , version });
71+ const _zigdver = try std .fs .path .join (allocator , &.{ home , ".zigd" , "versions" });
72+ defer allocator .free (_zigdver );
73+
74+ const lastp = try std .fs .path .join (allocator , &.{ _zigdver , version });
7275 defer allocator .free (lastp );
7376
74- // zig-linux-x86_64-0.12.0-dev.126+387b0ac4f -> 0.12.0-dev.126+387b0ac4f
77+ // create .zigd/versions if it doesn't exist
78+ std .fs .makeDirAbsolute (_zigdver ) catch {};
79+
80+ // libarchive can't set dest path so it extracts to cwd
81+ // rename here moves the extracted folder to the correct path
82+ // (cwd)/zig-linux-x86_64-0.11.0 -> ~/.zigd/versions/0.11.0
7583 try std .fs .cwd ().rename (fx , lastp );
7684
7785 return try std .fs .path .join (allocator , &.{ lastp , "zig" });
@@ -83,25 +91,21 @@ pub fn setdefault(allocator: std.mem.Allocator, version: []const u8, home: []con
8391
8492 const d = try config .getOrPut ("default" );
8593
86- if (! d .found_existing ) {
87- d .key_ptr .* = try allocator .dupe (u8 , "default" );
88- } else {
94+ if (! d .found_existing )
95+ d .key_ptr .* = try allocator .dupe (u8 , "default" )
96+ else
8997 allocator .free (d .value_ptr .* );
90- }
9198
9299 d .value_ptr .* = try allocator .dupe (u8 , version );
93100
94- const path = try std .fs .path .join (allocator , &.{ home , "zig" });
101+ const path = try std .fs .path .join (allocator , &.{ home , ".zigd" , "versions" , version });
95102 defer allocator .free (path );
96103
97- var zigdir = try std .fs .openDirAbsolute (path , .{});
98- defer zigdir .close ();
99-
100- var z = zigdir .openDir (version , .{}) catch b : {
104+ var z = std .fs .openDirAbsolute (path , .{}) catch b : {
101105 std .debug .print ("Did not find zig binary in zigd cache, installing...\n " , .{});
102106 const y = try install (allocator , version , home );
103107 allocator .free (y );
104- break :b zigdir . openDir ( version , .{}) catch unreachable ;
108+ break :b std . fs . openDirAbsolute ( path , .{}) catch unreachable ;
105109 };
106110 z .close ();
107111
0 commit comments