Skip to content

Commit 9bc51ec

Browse files
committed
add persistent install-dir setting
1 parent a9b48e4 commit 9bc51ec

File tree

3 files changed

+80
-19
lines changed

3 files changed

+80
-19
lines changed

build.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,13 @@ fn addTests(
228228
.name = "test-usage-h",
229229
.argv = &.{"-h"},
230230
.check = .{ .expect_stderr_match = "Usage" },
231+
.use_test_install_dir = false,
231232
});
232233
tests.addWithClean(.{
233234
.name = "test-usage-help",
234235
.argv = &.{"--help"},
235236
.check = .{ .expect_stderr_match = "Usage" },
237+
.use_test_install_dir = false,
236238
});
237239

238240
tests.addWithClean(.{
@@ -243,6 +245,7 @@ fn addTests(
243245
.{ .expect_stdout_match = "version" },
244246
.{ .expect_stdout_match = "0.13.0" },
245247
},
248+
.use_test_install_dir = false,
246249
});
247250

248251
tests.addWithClean(.{
@@ -625,6 +628,7 @@ const TestOptions = struct {
625628
argv: []const []const u8,
626629
check: ?std.Build.Step.Run.StdIo.Check = null,
627630
checks: []const std.Build.Step.Run.StdIo.Check = &.{},
631+
use_test_install_dir: bool = true,
628632
};
629633

630634
const Tests = struct {
@@ -652,6 +656,7 @@ const Tests = struct {
652656
run.addArtifactArg(tests.runtest_exe);
653657
run.addArg(opt.name);
654658
run.addArg(if (opt.add_path) "--with-path" else "--no-path");
659+
run.addArg(if (opt.use_test_install_dir) "--with-test-install-dir" else "--no-test-install-dir");
655660
if (opt.env) |env| {
656661
run.addDirectoryArg(env.dir);
657662
} else {

runtest.zig

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ pub fn main() !void {
1717

1818
const test_name = all_args[1];
1919
const add_path_option = all_args[2];
20-
const in_env_dir = all_args[3];
21-
const with_compilers = compilersArg(all_args[4]);
22-
const keep_compilers = compilersArg(all_args[5]);
23-
const out_env_dir = all_args[6];
24-
const setup_option = all_args[7];
25-
const zigup_exe = all_args[8];
26-
const zigup_args = all_args[9..];
20+
const install_dir_option = all_args[3];
21+
const in_env_dir = all_args[4];
22+
const with_compilers = compilersArg(all_args[5]);
23+
const keep_compilers = compilersArg(all_args[6]);
24+
const out_env_dir = all_args[7];
25+
const setup_option = all_args[8];
26+
const zigup_exe = all_args[9];
27+
const zigup_args = all_args[10..];
2728

2829
const add_path = blk: {
2930
if (std.mem.eql(u8, add_path_option, "--with-path")) break :blk true;
@@ -32,6 +33,13 @@ pub fn main() !void {
3233
std.process.exit(0xff);
3334
};
3435

36+
const use_test_install_dir = blk: {
37+
if (std.mem.eql(u8, install_dir_option, "--with-test-install-dir")) break :blk true;
38+
if (std.mem.eql(u8, install_dir_option, "--no-test-install-dir")) break :blk false;
39+
std.log.err("expected '--with-test-install-dir' or '--no-test-install-dir' but got '{s}'", .{install_dir_option});
40+
std.process.exit(0xff);
41+
};
42+
3543
try fixdeletetree.deleteTree(std.fs.cwd(), out_env_dir);
3644
try std.fs.cwd().makeDir(out_env_dir);
3745

@@ -94,10 +102,18 @@ pub fn main() !void {
94102
try argv.append(zigup_exe);
95103
try argv.append("--path-link");
96104
try argv.append(path_link);
97-
try argv.append("--install-dir");
98-
try argv.append(install_dir);
105+
if (use_test_install_dir) {
106+
try argv.append("--install-dir");
107+
try argv.append(install_dir);
108+
}
99109
try argv.appendSlice(zigup_args);
100110

111+
try std.io.getStdErr().writer().writeAll("ZIGUP EXEC:");
112+
for (argv.items) |arg| {
113+
try std.io.getStdErr().writer().print(" {s}", .{arg});
114+
}
115+
try std.io.getStdErr().writer().writeAll("\n");
116+
101117
var child = std.process.Child.init(argv.items, arena);
102118

103119
if (add_path) {

zigup.zig

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn ignoreHttpCallback(request: []const u8) void {
131131
_ = request;
132132
}
133133

134-
fn allocInstallDirStringXdg(allocator: Allocator) ![]const u8 {
134+
fn allocInstallDirStringXdg(allocator: Allocator) error{AlreadyReported}![]const u8 {
135135
// see https://specifications.freedesktop.org/basedir-spec/latest/#variables
136136
// try $XDG_DATA_HOME/zigup first
137137
xdg_var: {
@@ -141,7 +141,7 @@ fn allocInstallDirStringXdg(allocator: Allocator) ![]const u8 {
141141
std.log.err("$XDG_DATA_HOME environment variable '{s}' is not an absolute path", .{xdg_data_home});
142142
return error.AlreadyReported;
143143
}
144-
return std.fs.path.join(allocator, &[_][]const u8{ xdg_data_home, "zigup" });
144+
return std.fs.path.join(allocator, &[_][]const u8{ xdg_data_home, "zigup" }) catch |e| oom(e);
145145
}
146146
// .. then fallback to $HOME/.local/share/zigup
147147
const home = std.posix.getenv("HOME") orelse {
@@ -152,14 +152,47 @@ fn allocInstallDirStringXdg(allocator: Allocator) ![]const u8 {
152152
std.log.err("$HOME environment variable '{s}' is not an absolute path", .{home});
153153
return error.AlreadyReported;
154154
}
155-
return std.fs.path.join(allocator, &[_][]const u8{ home, ".local", "share", "zigup" });
155+
return std.fs.path.join(allocator, &[_][]const u8{ home, ".local", "share", "zigup" }) catch |e| oom(e);
156156
}
157157

158-
fn allocInstallDirString(allocator: Allocator) ![]const u8 {
158+
fn getSettingsDir(allocator: Allocator) ?[]const u8 {
159+
return std.fs.getAppDataDir(allocator, "zigup") catch |err| switch (err) {
160+
error.OutOfMemory => |e| oom(e),
161+
error.AppDataDirUnavailable => return null,
162+
};
163+
}
164+
165+
fn readInstallDir(allocator: Allocator) !?[]const u8 {
166+
const settings_dir = getSettingsDir(allocator) orelse return null;
167+
defer allocator.free(settings_dir);
168+
169+
const setting_path = std.fs.path.join(
170+
allocator,
171+
&.{ settings_dir, "install-dir" },
172+
) catch |e| oom(e);
173+
defer allocator.free(setting_path);
174+
var file = std.fs.cwd().openFile(setting_path, .{}) catch |err| switch (err) {
175+
error.FileNotFound => return null,
176+
else => |e| {
177+
std.log.err("open '{s}' failed with {s}", .{ setting_path, @errorName(e) });
178+
return error.AlreadyReported;
179+
},
180+
};
181+
defer file.close();
182+
183+
std.debug.panic("TODO: read install-dir from '{s}'", .{setting_path});
184+
}
185+
186+
fn allocInstallDirString(allocator: Allocator) error{AlreadyReported}![]const u8 {
159187
// TODO: maybe support ZIG_INSTALL_DIR environment variable?
188+
if (try readInstallDir(allocator)) |d| return d;
189+
160190
// TODO: maybe support a file on the filesystem to configure install dir?
161191
if (builtin.os.tag == .windows) {
162-
const self_exe_dir = try std.fs.selfExeDirPathAlloc(allocator);
192+
const self_exe_dir = std.fs.selfExeDirPathAlloc(allocator) catch |e| {
193+
std.log.err("failed to get exe dir path with {s}", .{@errorName(e)});
194+
return error.AlreadyReported;
195+
};
163196
defer allocator.free(self_exe_dir);
164197
return std.fs.path.join(allocator, &.{ self_exe_dir, "zig" });
165198
}
@@ -205,8 +238,12 @@ fn toAbsolute(allocator: Allocator, path: []const u8) ![]u8 {
205238
return std.fs.path.join(allocator, &[_][]const u8{ cwd, path });
206239
}
207240

208-
fn help() void {
209-
std.io.getStdErr().writeAll(
241+
fn help(allocator: Allocator) !void {
242+
const default_install_dir = allocInstallDirString(allocator) catch |err| switch (err) {
243+
error.AlreadyReported => "unknown (see error printed above)",
244+
};
245+
246+
try std.io.getStdErr().writer().print(
210247
\\Download and manage zig compilers.
211248
\\
212249
\\Common Usage:
@@ -226,6 +263,7 @@ fn help() void {
226263
\\
227264
\\Common Options:
228265
\\ --install-dir DIR override the default install location
266+
\\ default: {s}
229267
\\ --path-link PATH path to the `zig` symlink that points to the default compiler
230268
\\ this will typically be a file path within a PATH directory so
231269
\\ that the user can just run `zig`
@@ -234,7 +272,9 @@ fn help() void {
234272
++ " " ++ default_index_url ++
235273
\\
236274
\\
237-
) catch unreachable;
275+
,
276+
.{default_install_dir},
277+
);
238278
}
239279

240280
fn getCmdOpt(args: [][:0]u8, i: *usize) ![]const u8 {
@@ -287,7 +327,7 @@ pub fn main2() !u8 {
287327
} else if (std.mem.eql(u8, "--index", arg)) {
288328
index_url = try getCmdOpt(args, &i);
289329
} else if (std.mem.eql(u8, "-h", arg) or std.mem.eql(u8, "--help", arg)) {
290-
help();
330+
try help(allocator);
291331
return 0;
292332
} else {
293333
if (newlen == 0 and std.mem.eql(u8, "run", arg)) {
@@ -300,7 +340,7 @@ pub fn main2() !u8 {
300340
args = args[0..newlen];
301341
}
302342
if (args.len == 0) {
303-
help();
343+
try help(allocator);
304344
return 1;
305345
}
306346
if (std.mem.eql(u8, "fetch-index", args[0])) {

0 commit comments

Comments
 (0)