Skip to content

Commit

Permalink
Awesome
Browse files Browse the repository at this point in the history
Co-authored-by: xHyroM <github@xhyrom.dev>
  • Loading branch information
trnxdev and xhyrom committed Aug 21, 2023
1 parent ff67aa4 commit ef70e9a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"zig.zigVersion": "0.12.0-dev.140+e078324db",
"zig.zigPath": "/home/tiramify/zig/0.12.0-dev.140+e078324db/zig"
"zig.zigPath": "/home/tiramify/.zigd/versions/0.12.0-dev.140+e078324db/zig"
}
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

## Commands

### d-install <Version>, installs the <Version> of zig into zigd's cache.

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

## Config

### Now zigd supports a config file, which is located at `~/.zigdconfig`
### Now zigd supports a config file, which is located at `~/.zigd/config`
### The content can be like this:
```
default=0.12.0-dev.108+5395c2786
Expand Down
7 changes: 5 additions & 2 deletions src/conf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ pub fn load(allocator: std.mem.Allocator, home: []const u8) !std.StringHashMap([

var existed = true;

var cfgfile = homedir.readFileAlloc(allocator, ".zigdconfig", 1 << 21) catch |err| blk: {
var z = try homedir.makeOpenPath(".zigd", .{});
z.close();

var cfgfile = homedir.readFileAlloc(allocator, ".zigd/config", 1 << 21) catch |err| blk: {
switch (err) {
error.FileNotFound => {
existed = false;
Expand Down Expand Up @@ -64,7 +67,7 @@ pub fn save(home: []const u8, cfgmap: std.StringHashMap([]const u8)) !void {

var nbuf = buf[0..l];

try homedir.writeFile(".zigdconfig", nbuf);
try homedir.writeFile(".zigd/config", nbuf);
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ pub fn main() !void {
defer if_free(allocator, needtofree_, zig_version);

const zig_binary = try try_get_bin: {
var zig_binary_0 = try fromHome(home, "zig");
var zig_binary_1 = zig_binary_0.openDir(zig_version, .{}) catch {
var zig_binary_0 = try std.fs.path.join(allocator, &.{home, ".zigd", "versions", zig_version});
defer allocator.free(zig_binary_0);
var zig_binary_1 = std.fs.openDirAbsolute(zig_binary_0, .{}) catch {
std.debug.print("Did not find zig binary in zigd cache, installing...\n", .{});
const bin = try zigd.install(allocator, zig_version, home);
break :try_get_bin bin;
};
var zig_binary_a = zig_binary_1.realpathAlloc(allocator, "zig");
zig_binary_1.close();
zig_binary_0.close();
break :try_get_bin zig_binary_a;
};

Expand Down
32 changes: 18 additions & 14 deletions src/zigd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn install(allocator: std.mem.Allocator, version: []const u8, home: []const
const data = try req.reader().readAllAlloc(allocator, 2 << 50);
defer allocator.free(data);

const friendlyname = try std.mem.concat(allocator, u8, &.{ "zigd-", os, "-", arch, "-", version, ".", archive_ext });
const friendlyname = try std.mem.concat(allocator, u8, &.{ "zigd-", os, url_platform, version, ".", archive_ext });
defer allocator.free(friendlyname);

var downloaddir = try fromHome(home, "Downloads");
Expand All @@ -65,13 +65,21 @@ pub fn install(allocator: std.mem.Allocator, version: []const u8, home: []const
// because it doesn't support GNU longnames or PAX headers.
// https://imgur.com/9ZUhkHx

const fx = try std.fmt.allocPrint(allocator, "zig-" ++ url_platform ++ "-" ++ "{s}", .{version});
const fx = try std.fmt.allocPrint(allocator, "zig-{s}-{s}", .{ url_platform, version });
defer allocator.free(fx);

const lastp = try std.fs.path.join(allocator, &.{ home, "zig", version });
const _zigdver = try std.fs.path.join(allocator, &.{ home, ".zigd", "versions" });
defer allocator.free(_zigdver);

const lastp = try std.fs.path.join(allocator, &.{ _zigdver, version });
defer allocator.free(lastp);

// zig-linux-x86_64-0.12.0-dev.126+387b0ac4f -> 0.12.0-dev.126+387b0ac4f
// create .zigd/versions if it doesn't exist
std.fs.makeDirAbsolute(_zigdver) catch {};

// libarchive can't set dest path so it extracts to cwd
// rename here moves the extracted folder to the correct path
// (cwd)/zig-linux-x86_64-0.11.0 -> ~/.zigd/versions/0.11.0
try std.fs.cwd().rename(fx, lastp);

return try std.fs.path.join(allocator, &.{ lastp, "zig" });
Expand All @@ -83,25 +91,21 @@ pub fn setdefault(allocator: std.mem.Allocator, version: []const u8, home: []con

const d = try config.getOrPut("default");

if (!d.found_existing) {
d.key_ptr.* = try allocator.dupe(u8, "default");
} else {
if (!d.found_existing)
d.key_ptr.* = try allocator.dupe(u8, "default")
else
allocator.free(d.value_ptr.*);
}

d.value_ptr.* = try allocator.dupe(u8, version);

const path = try std.fs.path.join(allocator, &.{ home, "zig" });
const path = try std.fs.path.join(allocator, &.{ home, ".zigd", "versions", version });
defer allocator.free(path);

var zigdir = try std.fs.openDirAbsolute(path, .{});
defer zigdir.close();

var z = zigdir.openDir(version, .{}) catch b: {
var z = std.fs.openDirAbsolute(path, .{}) catch b: {
std.debug.print("Did not find zig binary in zigd cache, installing...\n", .{});
const y = try install(allocator, version, home);
allocator.free(y);
break :b zigdir.openDir(version, .{}) catch unreachable;
break :b std.fs.openDirAbsolute(path, .{}) catch unreachable;
};
z.close();

Expand Down

0 comments on commit ef70e9a

Please sign in to comment.