Skip to content

Commit 84be780

Browse files
committed
Implement zigd setup
1 parent e794922 commit 84be780

File tree

7 files changed

+119
-23
lines changed

7 files changed

+119
-23
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99

1010
### Commands:
1111

12-
| Command | What does it do? |
13-
| :---------------: | :-----------------------------------------------: |
14-
| help | Help screen |
15-
| version | Outputs zigd version |
16-
| install [version] | Installs a zig version |
17-
| exists [version] | Check if a zig version is installed on the system |
12+
| Command | What does it do? |
13+
| :---------------: | :----------------------------------------------------------: |
14+
| install [version] | Installs a zig version |
15+
| setup [version] | Installs a zig version and sets it as default in the config. |
16+
| exists [version] | Check if a zig version is installed on the system |
17+
| help | Help screen |
18+
| version | Outputs zigd version |
1819

1920
## zigdemu
2021

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.{
22
.name = "zigd",
3-
.version = "0.0.2",
3+
.version = "0.0.3",
44
.minimum_zig_version = "0.13.0",
55
.paths = .{
66
"build.zig",

src/utils.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const builtin = @import("builtin");
33

44
pub const cpu_arch: []const u8 = switch (builtin.cpu.arch) {
55
.x86, .powerpc64le, .x86_64, .aarch64, .riscv64 => |e| @tagName(e),
6-
else => @compileError("Unsupported CPU Architecture"),
6+
else => @compileError("Unsupported CPU Architecture, if you think your CPU Architecture is supported file an issue on github."),
77
};
88
pub const os: []const u8 = switch (os_tag) {
99
.windows, .linux, .macos => |e| @tagName(e),
10-
else => @compileError("Unsupported OS, if you think your OS supported file an issue on github."),
10+
else => @compileError("Unsupported OS, if you think your OS is supported file an issue on github."),
1111
};
1212
pub const os_tag = builtin.os.tag;
1313
pub const url_platform = os ++ "-" ++ cpu_arch;

src/zigd.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.2
1+
0.0.3

src/zigdcli.zig

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ const zigdcore = @import("zigdcore.zig");
33
const utils = @import("utils.zig");
44

55
const Command = enum {
6-
help,
76
install,
8-
version,
7+
setup,
98
exists,
9+
help,
10+
version,
1011
};
1112

1213
pub fn main() !void {
@@ -25,6 +26,7 @@ pub fn main() !void {
2526
if (std.meta.stringToEnum(Command, args[1])) |command| {
2627
return try switch (command) {
2728
.install => install(allocator, args),
29+
.setup => setup(allocator, args),
2830
.exists => exists(allocator, args),
2931
.help => help_menu(),
3032
.version => version(),
@@ -39,10 +41,11 @@ fn help_menu() !void {
3941
try std.io.getStdOut().writer().print(
4042
\\> zigd ({s}) cli: Manage zigd stuff
4143
\\
42-
\\help - Outputs this help Menu
43-
\\version - Outputs zigd version
4444
\\install [version] - Install a zig version
45+
\\setup [version] - First time setup (creates a config and installs the version)
4546
\\exists [version] - Check if a zig version is installed on the system
47+
\\help - Outputs this help Menu
48+
\\version - Outputs zigd version
4649
\\
4750
, .{utils.zigd_version});
4851
return;
@@ -77,7 +80,78 @@ fn install(allocator: std.mem.Allocator, args: []const []const u8) !void {
7780
const zigd_path = try zigdcore.getZigdPath(allocator);
7881
defer allocator.free(zigd_path);
7982

80-
try zigdcore.install_zig(allocator, download_url, zigd_path, zig_version.as_string);
83+
if (!try zigdcore.install_zig(allocator, download_url, zigd_path, zig_version)) {
84+
std.log.err("Installation failed!", .{});
85+
}
86+
87+
return;
88+
}
89+
90+
fn setup(allocator: std.mem.Allocator, args: []const []const u8) !void {
91+
if (args.len <= 2) {
92+
std.log.err("Wrong Usage!\n", .{});
93+
try std.io.getStdOut().writer().print(
94+
\\Usage: zigd setup [version]
95+
\\For more help use zigd help
96+
\\
97+
, .{});
98+
return;
99+
}
100+
101+
const zigd_path = try zigdcore.getZigdPath(allocator);
102+
defer allocator.free(zigd_path);
103+
104+
const config_path = try std.fs.path.join(allocator, &.{ zigd_path, "config" });
105+
defer allocator.free(config_path);
106+
107+
if (try utils.isFile(config_path)) {
108+
o: while (true) {
109+
std.log.warn("A config file already exists, overwrite (y/n?)", .{});
110+
const byte = try std.io.getStdIn().reader().readByte();
111+
112+
switch (byte) {
113+
'y' => break :o,
114+
'n' => return,
115+
else => continue :o,
116+
}
117+
}
118+
}
119+
120+
var zig_version = try zigdcore.ZigVersion.parse(allocator, args[2], &user_arg, false);
121+
defer zig_version.deinitIfMasterOrZigverOrZonver(allocator);
122+
123+
if (zig_version.source == .Master) {
124+
o: while (true) {
125+
std.log.warn("It is not recommended to setup with master, are you sure you want to set master as default in the config? (y/n)", .{});
126+
const byte = try std.io.getStdIn().reader().readByte();
127+
128+
switch (byte) {
129+
'y' => break :o,
130+
'n' => return,
131+
else => continue :o,
132+
}
133+
}
134+
}
135+
136+
const download_url = try zigdcore.downloadUrlFromVersion(allocator, zig_version.as_string, zig_version.source == .Master);
137+
defer allocator.free(download_url);
138+
139+
try std.io.getStdOut().writer().print("Installing version {s}...\n", .{zig_version});
140+
_ = try zigdcore.install_zig(allocator, download_url, zigd_path, zig_version);
141+
142+
try std.io.getStdOut().writer().print("Creating a config...\n", .{});
143+
const config_file = try std.fs.createFileAbsolute(config_path, .{
144+
.truncate = true,
145+
});
146+
defer config_file.close();
147+
148+
try config_file.writer().writeAll("# Generated by `zigd setup`, btw :)\n");
149+
try config_file.writer().writeAll("default=");
150+
switch (zig_version.source) {
151+
.Master => try config_file.writeAll("master"),
152+
else => try config_file.writeAll(zig_version.as_string),
153+
}
154+
try config_file.writer().writeByte('\n');
81155
}
82156

83157
fn exists(allocator: std.mem.Allocator, args: []const []const u8) !void {

src/zigdcore.zig

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

44
/// Version cannot be master!
5-
/// Returns the path to the zig binary
6-
pub fn install_zig(allocator: std.mem.Allocator, download_url: []const u8, install_path: []const u8, version: []const u8) !void {
5+
/// Returns a bool if it installed/reinstalled zig
6+
pub fn install_zig(allocator: std.mem.Allocator, download_url: []const u8, install_path: []const u8, zig_version: ZigVersion) !bool {
7+
const final_destination = try std.fs.path.join(allocator, &.{ install_path, "versions", zig_version.as_string });
8+
defer allocator.free(final_destination);
9+
10+
if (try utils.isDirectory(final_destination)) {
11+
o: while (true) {
12+
const byte = try std.io.getStdIn().reader().readByte();
13+
std.log.warn("Version {} is already installed on your system! Re-install? (y/n)", .{zig_version});
14+
15+
switch (byte) {
16+
'y' => {
17+
try std.fs.deleteTreeAbsolute(final_destination);
18+
break :o;
19+
},
20+
'n' => return false,
21+
else => continue :o,
22+
}
23+
}
24+
}
25+
726
var client = std.http.Client{ .allocator = allocator };
827
defer client.deinit();
928

@@ -23,10 +42,7 @@ pub fn install_zig(allocator: std.mem.Allocator, download_url: []const u8, insta
2342
if (req.response.status != .ok)
2443
return error.ResponseWasNotOk;
2544

26-
const final_destination = try std.fs.path.join(allocator, &.{ install_path, "versions", version });
27-
defer allocator.free(final_destination);
28-
29-
try utils.createDirectoryIfNotExist(final_destination);
45+
try utils.createDirectoryIgnoreExist(final_destination);
3046

3147
if (utils.os_tag == .windows) {
3248
// std.zip has no strip components :( so we have to do this mess...
@@ -64,7 +80,8 @@ pub fn install_zig(allocator: std.mem.Allocator, download_url: []const u8, insta
6480

6581
try std.tar.pipeToFileSystem(final_dest_dir, xz_decompressor.reader(), .{ .strip_components = 1 });
6682
}
67-
return;
83+
84+
return true;
6885
}
6986

7087
pub fn masterFromIndex(allocator: std.mem.Allocator) ![]const u8 {

src/zigdemu.zig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ pub fn main() !void {
7878
const download_url = try zigdcore.downloadUrlFromVersion(allocator, zig_version.as_string, zig_version.source == .Master);
7979
defer allocator.free(download_url);
8080

81-
try zigdcore.install_zig(allocator, download_url, zigd_path, zig_version.as_string);
81+
if (!try zigdcore.install_zig(allocator, download_url, zigd_path, zig_version)) {
82+
std.log.err("Installation failed! Exiting...", .{});
83+
return;
84+
}
85+
8286
break :o;
8387
}
8488

0 commit comments

Comments
 (0)