Skip to content

Commit ef70e9a

Browse files
trnxdevxhyrom
andcommitted
Awesome
Co-authored-by: xHyroM <[email protected]>
1 parent ff67aa4 commit ef70e9a

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"zig.zigVersion": "0.12.0-dev.140+e078324db",
3-
"zig.zigPath": "/home/tiramify/zig/0.12.0-dev.140+e078324db/zig"
3+
"zig.zigPath": "/home/tiramify/.zigd/versions/0.12.0-dev.140+e078324db/zig"
44
}

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99

1010
## Commands
1111

12-
### d-install <Version>, installs the <Version> of zig into zigd's cache.
13-
12+
### d-install <Version>, installs the <Version> of zig into zigd's cache (which is located at `~/.zigd/versions`).
1413

1514
## Config
1615

17-
### Now zigd supports a config file, which is located at `~/.zigdconfig`
16+
### Now zigd supports a config file, which is located at `~/.zigd/config`
1817
### The content can be like this:
1918
```
2019
default=0.12.0-dev.108+5395c2786

src/conf.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ pub fn load(allocator: std.mem.Allocator, home: []const u8) !std.StringHashMap([
99

1010
var existed = true;
1111

12-
var cfgfile = homedir.readFileAlloc(allocator, ".zigdconfig", 1 << 21) catch |err| blk: {
12+
var z = try homedir.makeOpenPath(".zigd", .{});
13+
z.close();
14+
15+
var cfgfile = homedir.readFileAlloc(allocator, ".zigd/config", 1 << 21) catch |err| blk: {
1316
switch (err) {
1417
error.FileNotFound => {
1518
existed = false;
@@ -64,7 +67,7 @@ pub fn save(home: []const u8, cfgmap: std.StringHashMap([]const u8)) !void {
6467

6568
var nbuf = buf[0..l];
6669

67-
try homedir.writeFile(".zigdconfig", nbuf);
70+
try homedir.writeFile(".zigd/config", nbuf);
6871
return;
6972
}
7073

src/main.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ pub fn main() !void {
8181
defer if_free(allocator, needtofree_, zig_version);
8282

8383
const zig_binary = try try_get_bin: {
84-
var zig_binary_0 = try fromHome(home, "zig");
85-
var zig_binary_1 = zig_binary_0.openDir(zig_version, .{}) catch {
84+
var zig_binary_0 = try std.fs.path.join(allocator, &.{home, ".zigd", "versions", zig_version});
85+
defer allocator.free(zig_binary_0);
86+
var zig_binary_1 = std.fs.openDirAbsolute(zig_binary_0, .{}) catch {
8687
std.debug.print("Did not find zig binary in zigd cache, installing...\n", .{});
8788
const bin = try zigd.install(allocator, zig_version, home);
8889
break :try_get_bin bin;
8990
};
9091
var zig_binary_a = zig_binary_1.realpathAlloc(allocator, "zig");
9192
zig_binary_1.close();
92-
zig_binary_0.close();
9393
break :try_get_bin zig_binary_a;
9494
};
9595

src/zigd.zig

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)