Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: Use %LOCALAPPDATA%/zigup as the default install directory. #118

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn main() !u8 {
try std.fs.cwd().makeDir("scratch");
const bin_dir = "scratch" ++ sep ++ "bin";
try std.fs.cwd().makeDir(bin_dir);
const install_dir = if (builtin.os.tag == .windows) (bin_dir ++ "\\zig") else ("scratch/install");
const install_dir = "scratch/install";
try std.fs.cwd().makeDir(install_dir);

// NOTE: for now we are incorrectly assuming the install dir is CWD/zig-out
Expand All @@ -35,7 +35,7 @@ pub fn main() !u8 {
.{},
);

const install_args = if (builtin.os.tag == .windows) [_][]const u8{} else [_][]const u8{ "--install-dir", install_dir };
const install_args = [_][]const u8{ "--install-dir", install_dir };
const zigup_args = &[_][]const u8{zigup} ++ install_args;

var allocator_store = std.heap.ArenaAllocator.init(std.heap.page_allocator);
Expand Down
44 changes: 27 additions & 17 deletions zigup.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,37 @@ fn ignoreHttpCallback(request: []const u8) void {
_ = request;
}

fn getHomeDir() ![]const u8 {
return std.os.getenv("HOME") orelse {
std.log.err("cannot find install directory, $HOME environment variable is not set", .{});
return error.MissingHomeEnvironmentVariable;
};
}

fn allocInstallDirString(allocator: Allocator) ![]const u8 {
// TODO: maybe support ZIG_INSTALL_DIR environment variable?
// TODO: maybe support a file on the filesystem to configure install dir?
if (builtin.os.tag == .windows) {
const self_exe_dir = try std.fs.selfExeDirPathAlloc(allocator);
defer allocator.free(self_exe_dir);
return std.fs.path.join(allocator, &.{ self_exe_dir, "zig" });
}
const home = try getHomeDir();
if (!std.fs.path.isAbsolute(home)) {
std.log.err("$HOME environment variable '{s}' is not an absolute path", .{home});
return error.BadHomeEnvironmentVariable;

const install_parent_env_var = comptime switch (builtin.os.tag) {
.windows => "LOCALAPPDATA",
else => "HOME",
};
const install_dir_name = comptime switch (builtin.os.tag) {
.windows => "zigup", // Note that %LOCALAPPDATA%/zig is used by zig itself.
else => "zig",
};

const install_parent_path = std.process.getEnvVarOwned(allocator, install_parent_env_var) catch |err| {
std.log.err(
"cannot find install directory: failed to get value for environment variable '{s}'",
.{install_parent_env_var},
);
return err;
};
defer allocator.free(install_parent_path);

if (!std.fs.path.isAbsolute(install_parent_path)) {
std.log.err(
"'{s}' environment variable '{s}' is not set to an absolute path",
.{ install_parent_env_var, install_parent_path },
);
return error.BadEnvironmentVariable;
}
return std.fs.path.join(allocator, &[_][]const u8{ home, "zig" });

return std.fs.path.join(allocator, &[_][]const u8{ install_parent_path, install_dir_name });
}
const GetInstallDirOptions = struct {
create: bool,
Expand Down