Working initial version with linux and firefox limitation, added ref and std subcommands

This commit is contained in:
Nathan Anderson 2023-10-24 17:56:49 -06:00
parent ecb19ad647
commit 9198cb7642
2 changed files with 97 additions and 22 deletions

View File

@ -10,12 +10,14 @@ pub const ZIM_BUILD = 1;
/// The availble arguments for the ZiM program /// The availble arguments for the ZiM program
const Args = enum(u8) { const Args = enum(u8) {
gir,
help, help,
init, init,
install, install,
list, list,
ref,
std,
use, use,
gir,
version, version,
}; };
@ -41,6 +43,8 @@ fn get_arg_parse_type(arg: Args) ArgType {
.init => ArgType.single, .init => ArgType.single,
.install => ArgType.expectsOneParam, .install => ArgType.expectsOneParam,
.list => ArgType.expectsOneParam, .list => ArgType.expectsOneParam,
.ref => ArgType.single,
.std => ArgType.single,
.use => ArgType.expectsOneParam, .use => ArgType.expectsOneParam,
.version => ArgType.single, .version => ArgType.single,
}; };
@ -54,8 +58,10 @@ fn command_desc(arg: Args) string {
.init => " init\t\tAdds the necessary PATH to the system env", .init => " init\t\tAdds the necessary PATH to the system env",
.install => " install\tInstall the zig version", .install => " install\tInstall the zig version",
.list => " list\t\tLists all versions available on the system", .list => " list\t\tLists all versions available on the system",
.ref => " ref\t\tOpens the zig lang reference",
.std => " std\t\tOpens the std docs for the current zig version",
.use => " use\t\tSets the active version of zig to use", .use => " use\t\tSets the active version of zig to use",
.version => " version\t\tPrint out the zim version you are using", .version => " version\tPrint out the zim version you are using",
}; };
} }
@ -106,7 +112,7 @@ fn show_context_help(arg: Args) void {
\\ \\
, .{}); , .{});
}, },
.gir, .help, .init, .version => { .gir, .help, .init, .ref, .std, .version => {
return; return;
}, },
} }
@ -124,24 +130,10 @@ fn do_arg_action(allocator: std.mem.Allocator, arg: Args, param: ?[]const u8) vo
.init => { .init => {
zim.init(allocator) catch |err| { zim.init(allocator) catch |err| {
std.debug.print("Error running `init`\n{any}\n", .{err}); std.debug.print("Error running `init`\n{any}\n", .{err});
};
std.os.exit(1); std.os.exit(1);
return;
},
.version => {
const ver = versionStr(allocator) catch {
std.debug.print("Error getting version\n", .{});
return;
}; };
defer allocator.free(ver); std.os.exit(0);
return;
if (param) |command| {
std.debug.print("{s}\nRunning ZiM Version {s}\n", .{ command, ver });
} else {
std.debug.print("Running ZiM Version {s}\n", .{ver});
}
utils.printGir();
}, },
.install => { .install => {
if (param == null) { if (param == null) {
@ -181,6 +173,21 @@ fn do_arg_action(allocator: std.mem.Allocator, arg: Args, param: ?[]const u8) vo
std.os.exit(1); std.os.exit(1);
}; };
}, },
.ref => {
zim.open_ref(allocator) catch |err| {
std.debug.print("Error running `ref`\n{any}\n", .{err});
std.os.exit(1);
};
std.os.exit(0);
return;
},
.std => {
zim.open_std(allocator) catch |err| {
std.debug.print("Error running `ref`\n{any}\n", .{err});
std.os.exit(1);
};
std.os.exit(0);
},
.use => { .use => {
if (param == null) { if (param == null) {
std.debug.print("Error: Expected version or index to follow.\nRun `zim use help` for details.\n\n", .{}); std.debug.print("Error: Expected version or index to follow.\nRun `zim use help` for details.\n\n", .{});
@ -197,6 +204,21 @@ fn do_arg_action(allocator: std.mem.Allocator, arg: Args, param: ?[]const u8) vo
std.os.exit(1); std.os.exit(1);
}; };
}, },
.version => {
const ver = versionStr(allocator) catch {
std.debug.print("Error getting version\n", .{});
return;
};
defer allocator.free(ver);
if (param) |command| {
std.debug.print("{s}\nRunning ZiM Version {s}\n", .{ command, ver });
} else {
std.debug.print("Running ZiM Version {s}\n", .{ver});
}
utils.printGir();
},
} }
} }

View File

@ -115,7 +115,7 @@ fn getLocalVersionsList(
fn getRemoteVersionsSlice(allocator: std.mem.Allocator) ZimError![]const ZigVersion { fn getRemoteVersionsSlice(allocator: std.mem.Allocator) ZimError![]const ZigVersion {
// Check local file first for chached network call first // Check local file first for chached network call first
const zim_dir = try openZimDir(allocator); const zim_dir = try openZimDir(allocator);
const now = @as(i128, @intCast(std.time.timestamp())) * 1000 * 1000; const now = @as(i128, @intCast(std.time.timestamp())) * 1000 * 1000 * 1000;
const yesterday = now - (24 * 60 * 60 * 1000 * 1000); const yesterday = now - (24 * 60 * 60 * 1000 * 1000);
var remote_versions_file = zim_dir.createFile("versions/remote_versions.json", .{ .read = true, .truncate = false }) catch { var remote_versions_file = zim_dir.createFile("versions/remote_versions.json", .{ .read = true, .truncate = false }) catch {
return ZimError.BadIO; return ZimError.BadIO;
@ -129,14 +129,16 @@ fn getRemoteVersionsSlice(allocator: std.mem.Allocator) ZimError![]const ZigVers
return ZimError.BadIO; return ZimError.BadIO;
}; };
// std.debug.print("Got times modified: {any}, yesterday: {any}\n", .{file_metadata.modified(), yesterday});
if (file_metadata.modified() > yesterday) { if (file_metadata.modified() > yesterday) {
// std.debug.print("Getting cached result\n", .{});
body = remote_versions_file.readToEndAlloc(allocator, SIXTY_FOUR_KILOBYTES) catch |err| { body = remote_versions_file.readToEndAlloc(allocator, SIXTY_FOUR_KILOBYTES) catch |err| {
std.debug.print("Could not read remote versions file: {any}\n", .{err}); std.debug.print("Could not read remote versions file: {any}\n", .{err});
return ZimError.Unexpected; return ZimError.Unexpected;
}; };
} }
if (body.len == 0) { if (body.len == 0) {
std.debug.print("No cached versions, getting remote versions\n", .{}); // std.debug.print("No cached versions, getting remote versions\n", .{});
// Otherwise, make network call and parse // Otherwise, make network call and parse
const versions_json_url = "https://ziglang.org/download/index.json"; const versions_json_url = "https://ziglang.org/download/index.json";
@ -506,7 +508,7 @@ pub fn list(allocator: std.mem.Allocator, param: string) !void {
std.debug.print(" [{d}]\tzig-{s}\n", .{ index, remote_version.version_string }); std.debug.print(" [{d}]\tzig-{s}\n", .{ index, remote_version.version_string });
} }
} }
std.debug.print("\nZig versions can be installed using `zim install <version or index>`\n\n", .{}); std.debug.print("\nZig versions can be installed using `zim install <index>`\n\n", .{});
return; return;
}, },
} }
@ -517,3 +519,54 @@ pub fn init(allocator: std.mem.Allocator) !void {
_ = allocator; _ = allocator;
return ZimError.NotImplemented; return ZimError.NotImplemented;
} }
/// Runs the `ref` subcommand
pub fn open_ref(allocator: std.mem.Allocator) !void {
const ref_path = "~/.config/zim/bin/zig/doc/langref.html";
var zig_ver_res = std.ChildProcess.exec(.{.allocator = allocator, .argv = &.{ "zig", "version" }, .max_output_bytes = 100},) catch |err| {
std.debug.print("Error executing `zig version`: {any}\n", .{err});
return ZimError.Unexpected;
};
defer allocator.free(zig_ver_res.stdout);
defer allocator.free(zig_ver_res.stderr);
if (zig_ver_res.term != .Exited) {
std.debug.print("Expected `zig version` to exit, instead got {any}\n", .{zig_ver_res.term});
return ZimError.Unexpected;
}
const zig_ver = zig_ver_res.stdout[0..zig_ver_res.stdout.len - 1];
std.debug.print("Opening language reference for zig-{s}...\n", .{zig_ver});
var proc = std.ChildProcess.init(&.{ "firefox", ref_path }, allocator);
_ = proc.spawn() catch |err| {
std.debug.print("Error while opening browser with local language reference\n{any}\n", .{err});
return ZimError.Unexpected;
};
return;
}
pub fn open_std(allocator: std.mem.Allocator) !void {
const std_path = "~/.config/zim/bin/zig/doc/std/index.html";
var zig_ver_res = std.ChildProcess.exec(.{.allocator = allocator, .argv = &.{ "zig", "version" }, .max_output_bytes = 100},) catch |err| {
std.debug.print("Error executing `zig version`: {any}\n", .{err});
return ZimError.Unexpected;
};
defer allocator.free(zig_ver_res.stdout);
defer allocator.free(zig_ver_res.stderr);
if (zig_ver_res.term != .Exited) {
std.debug.print("Expected `zig version` to exit, instead got {any}\n", .{zig_ver_res.term});
return ZimError.Unexpected;
}
const zig_ver = zig_ver_res.stdout[0..zig_ver_res.stdout.len - 1];
std.debug.print("Opening zig-{s} std lib docs...\n", .{zig_ver});
var proc = std.ChildProcess.init(&.{ "firefox", std_path }, allocator);
_ = proc.spawn() catch |err| {
std.debug.print("Error while opening browser with local language reference\n{any}\n", .{err});
return ZimError.Unexpected;
};
return;
}