Skip to content

Commit f09eee2

Browse files
committed
🪟: Zigdows
1 parent 1aadc9a commit f09eee2

File tree

5 files changed

+58
-20
lines changed

5 files changed

+58
-20
lines changed

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
zig-out
22
zig-cache
3-
.zig-cache
3+
.zig-cache
4+
DO_NOT_MODIFY_U_STINKY-zigd-install-temp

‎src/utils.zig‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ pub const cpu_arch: []const u8 = switch (builtin.cpu.arch) {
55
.x86, .powerpc64le, .x86_64, .aarch64, .riscv64 => |e| @tagName(e),
66
else => @compileError("Unsupported CPU Architecture"),
77
};
8-
pub const os: []const u8 = switch (builtin.os.tag) {
9-
.linux, .macos => |e| @tagName(e),
10-
else => @compileError("Unsupported OS"), // Windows too
8+
pub const os: []const u8 = switch (os_tag) {
9+
.windows, .linux, .macos => |e| @tagName(e),
10+
else => @compileError("Unsupported OS, if you think your OS supported file an issue on github."),
1111
};
12+
pub const os_tag = builtin.os.tag;
1213
pub const url_platform = os ++ "-" ++ cpu_arch;
13-
pub const archive_ext = if (builtin.os.tag == .windows) "zip" else "tar.xz"; // Maybe Windows support in future?
14+
pub const archive_ext = if (os_tag == .windows) "zip" else "tar.xz"; // Maybe Windows support in future?
1415
pub const index_url: []const u8 = "https://ziglang.org/download/index.json";
1516
pub const download_base_url: []const u8 = "https://ziglang.org/download";
1617
pub const download_base_master_url: []const u8 = "https://ziglang.org/builds"; // Master builds have another url for some unknown reason
1718
pub const zigd_version = @embedFile("zigd.version");
1819
pub const custom_env_path_key_for_zigd = "ZIGD_DIRECTORY";
20+
pub const binary_ext = if (os_tag == .windows) ".exe" else "";
1921
pub const InDebug = builtin.mode == .Debug;
2022

2123
// == File System Stuff

‎src/zigdcli.zig‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const std = @import("std");
22
const zigdcore = @import("zigdcore.zig");
33
const utils = @import("utils.zig");
44

5-
const stdout = std.io.getStdOut().writer();
65
const Command = enum {
76
help,
87
install,
@@ -37,7 +36,7 @@ pub fn main() !void {
3736
}
3837

3938
fn help_menu() !void {
40-
try stdout.print(
39+
try std.io.getStdOut().writer().print(
4140
\\> zigd ({s}) cli: Manage zigd stuff
4241
\\
4342
\\help - Outputs this help Menu
@@ -50,7 +49,7 @@ fn help_menu() !void {
5049
}
5150

5251
fn version() !void {
53-
try stdout.print("{s}\n", .{utils.zigd_version});
52+
try std.io.getStdOut().writer().print("{s}\n", .{utils.zigd_version});
5453
return;
5554
}
5655

@@ -59,7 +58,7 @@ var user_arg: zigdcore.ZigVersion.Source = .UserArg;
5958
fn install(allocator: std.mem.Allocator, args: []const []const u8) !void {
6059
if (args.len <= 2) {
6160
std.log.err("Wrong Usage!\n", .{});
62-
try stdout.print(
61+
try std.io.getStdOut().writer().print(
6362
\\Usage: zigd install [version]
6463
\\For more help use zigd help
6564
\\
@@ -70,7 +69,7 @@ fn install(allocator: std.mem.Allocator, args: []const []const u8) !void {
7069
var zig_version = try zigdcore.ZigVersion.parse(allocator, args[2], &user_arg, false);
7170
defer zig_version.deinitIfMasterOrZigver(allocator);
7271

73-
try stdout.print("Installing zig version {s}\n", .{zig_version});
72+
try std.io.getStdOut().writer().print("Installing zig version {s}\n", .{zig_version});
7473

7574
const download_url = try zigdcore.downloadUrlFromVersion(allocator, zig_version.as_string, zig_version.source == .Master);
7675
defer allocator.free(download_url);
@@ -84,7 +83,7 @@ fn install(allocator: std.mem.Allocator, args: []const []const u8) !void {
8483
fn exists(allocator: std.mem.Allocator, args: []const []const u8) !void {
8584
if (args.len <= 2) {
8685
std.log.err("Wrong Usage!\n", .{});
87-
try stdout.print(
86+
try std.io.getStdOut().writer().print(
8887
\\Usage: zigd exists [version]
8988
\\For more help use zigd help
9089
\\
@@ -102,8 +101,8 @@ fn exists(allocator: std.mem.Allocator, args: []const []const u8) !void {
102101
defer allocator.free(version_path);
103102

104103
if (try utils.isDirectory(version_path)) {
105-
try stdout.writeAll("Yes!\n");
104+
try std.io.getStdOut().writer().writeAll("Yes!\n");
106105
} else {
107-
try stdout.writeAll("No!\n");
106+
try std.io.getStdOut().writer().writeAll("No!\n");
108107
}
109108
}

‎src/zigdcore.zig‎

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,42 @@ pub fn install_zig(allocator: std.mem.Allocator, download_url: []const u8, insta
2828

2929
try utils.createDirectoryIfNotExist(final_destination);
3030

31-
var final_dest = try std.fs.openDirAbsolute(final_destination, .{});
32-
defer final_dest.close();
31+
if (utils.os_tag == .windows) {
32+
// std.zip has no strip components :( so we have to do this mess...
33+
// TODO: It's kinda slow but it's std.zip's fault... yea, maybe we can do something to speed it up.
34+
const temp_name = "DO_NOT_MODIFY_U_STINKY-zigd-install-temp";
3335

34-
var xz_decompressor = try std.compress.xz.decompress(allocator, req.reader());
35-
defer xz_decompressor.deinit();
36+
var temporary_storage = try std.fs.cwd().makeOpenPath(temp_name, .{
37+
.iterate = true,
38+
});
3639

37-
try std.tar.pipeToFileSystem(final_dest, xz_decompressor.reader(), .{ .strip_components = 1 });
40+
const buf = try req.reader().readAllAlloc(allocator, std.math.maxInt(usize));
41+
defer allocator.free(buf);
42+
43+
var fbs = std.io.fixedBufferStream(buf);
44+
try std.zip.extract(temporary_storage, fbs.seekableStream(), .{});
45+
46+
var tempstrg = temporary_storage.iterate();
47+
const w_path = try tempstrg.next() orelse return error.ZigInstallationWasLost__Oops;
48+
49+
const w_path_duped = try allocator.dupe(u8, w_path.name);
50+
defer allocator.free(w_path_duped);
51+
52+
if ((try tempstrg.next()) != null)
53+
return error.InstallationWasSabotaged;
54+
55+
try temporary_storage.rename(w_path_duped, final_destination);
56+
temporary_storage.close();
57+
try std.fs.cwd().deleteDir(temp_name);
58+
} else {
59+
var final_dest_dir = try std.fs.openDirAbsolute(final_destination, .{});
60+
defer final_dest_dir.close();
61+
62+
var xz_decompressor = try std.compress.xz.decompress(allocator, req.reader());
63+
defer xz_decompressor.deinit();
64+
65+
try std.tar.pipeToFileSystem(final_dest_dir, xz_decompressor.reader(), .{ .strip_components = 1 });
66+
}
3867
return;
3968
}
4069

@@ -150,8 +179,15 @@ pub fn getZigdPath(allocator: std.mem.Allocator) ![]u8 {
150179

151180
var custom_zigd: bool = true;
152181
const zigd_directory = env_map.get(utils.custom_env_path_key_for_zigd) orelse v: {
182+
custom_zigd = false;
183+
184+
if (utils.os_tag == .windows) {
185+
if (env_map.get("USERPROFILE")) |userprofile| {
186+
break :v try std.fs.path.join(allocator, &.{ userprofile, ".zigd" });
187+
}
188+
}
189+
153190
if (env_map.get("HOME")) |home| {
154-
custom_zigd = false;
155191
break :v try std.fs.path.join(allocator, &.{ home, ".zigd" });
156192
}
157193

‎src/zigdemu.zig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn main() !void {
5656
zig_version = try zigdcore.ZigVersion.parse(allocator, zig_version.as_string, &zig_version.source, true);
5757
defer zig_version.deinitIfMasterOrZigver(allocator);
5858

59-
const zig_binary_path = try std.fs.path.join(allocator, &.{ zigd_path, "versions", zig_version.as_string, "zig" });
59+
const zig_binary_path = try std.fs.path.join(allocator, &.{ zigd_path, "versions", zig_version.as_string, "zig" ++ utils.binary_ext });
6060
defer allocator.free(zig_binary_path);
6161

6262
if (!(try utils.isFile(zig_binary_path))) o: {

0 commit comments

Comments
 (0)