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

Use cova #3

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{});

const zip = b.dependency("zip", .{});
const cova = b.dependency("cova", .{});

const common = b.createModule(.{ .root_source_file = b.path("src/common/root.zig") });
const default_os = target.result.os.tag;
Expand All @@ -32,6 +33,7 @@ pub fn build(b: *std.Build) !void {

zigverm.root_module.addImport("common", common);
zigverm.root_module.addImport("zip", zip.module("zip"));
zigverm.root_module.addImport("cova", cova.module("cova"));
zig.root_module.addImport("common", common);
b.installArtifact(zigverm);
b.installArtifact(zig);
Expand Down
4 changes: 4 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
.url = "https://github.com/AMythicDev/zip.zig/archive/refs/tags/v0.1.0.tar.gz",
.hash = "1220b5e3ecda108ccae1749479ec7e02f1f2ddcbb8102a2b5a7cd92bc8aebb4d153d",
},
.cova = .{
.url = "https://github.com/00JCIV00/cova/archive/refs/tags/v0.10.1-beta.tar.gz",
.hash = "12202782cc66e88f0c15b9acea9d6d1d50dbe941d68b5297927243f6d0377dbb1123",
},
},

.paths = .{
Expand Down
196 changes: 75 additions & 121 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,126 +4,80 @@ const streql = @import("common").streql;
const Version = @import("main.zig").Version;
const utils = @import("utils.zig");

const helptext =
\\zigverm - A version manager for Zig
\\
\\zigverm [options] [command]
\\
\\Commands:
\\ install <version> Install a specific version. Version can be any valid semantic version
\\ or master or stable
\\ override [DIRECTORY] <version> Override the version of zig used under DIRECTORY. DIRECTORY can be
\\ - Path to a directory, under which to override
\\ - "default", to change the default version
\\ - ommited to use the current directory
\\ override-rm <DIRECTORY> Override the version of zig used under DIRECTORY. DIRECTORY should
\\ be path to a directory, for which to remove override
\\ update [VERSION] Update version to its latest available point release, If [VERSION] is
\\ not provided, it will update all installed versions
\\ update-self Update zigverm itself
\\ std [VERSION] Open the standard library documentation in the default web browser.
\\ If [VERSION] is specified, it will open the documentation for that version
\\ otherwise it will default to of the active version on the current directory.
\\ reference [VERSION] Open the language reference in the default web browser.
\\ If [VERSION] is specified, it will open the reference for that version
\\ otherwise it will default to of the active version on the current directory.
\\ remove <version> Remove a already installed specific version. Version can be any
\\ valid semantic version or master or stable
\\ info Show information about installations
\\
\\Options:
\\ -h --help Show this help message
\\ -V --version Print version info
;
const cova = @import("cova");
pub const CommandT = cova.Command.Base();
pub const OptionT = CommandT.OptionT;
pub const ValueT = CommandT.ValueT;

pub const OverrideArrgs = struct {
version: []const u8,
directory: ?[]const u8,
pub const Cli = CommandT{
.name = "zigverm",
.description = "A version manager for the Zig programming language",
.sub_cmds = &.{
.{
.name = "install",
.description = "Install a specific version",
.vals = &.{
ValueT.ofType([]const u8, .{ .name = "version", .description = "Version can be any valid semantic version or master or stable" }),
},
},
.{
.name = "remove",
.description = "Remove a already installed specific version.",
.vals = &.{
ValueT.ofType([]const u8, .{ .name = "version", .description = "Version can be any valid semantic version or master or stable" }),
},
},
.{
.name = "override",
.description = "Override the version of zig used",
.opts = &.{OptionT{
.name = "directory",
.short_name = 'd',
.long_name = "dir",
.description =
\\ DIRECTORY can be
\\ - Path to a directory, under which to override
\\ - "default", to change the default version
\\ - ommited to use the current directory
,
.val = ValueT.ofType([]const u8, .{
.name = "directory",
}),
}},
.vals = &.{
ValueT.ofType([]const u8, .{ .name = "version", .description = "Version can be any valid semantic version or master or stable" }),
},
},
.{
.name = "override-rm",
.description = "Override the version of zig used",
.vals = &.{
ValueT.ofType([]const u8, .{ .name = "directory", .description = "DIRECTORY should be path to a directory, for which to remove override" }),
},
},
.{
.name = "std",
.description = "Open the standard library documentation in the default web browser.",
.vals = &.{
ValueT.ofType([]const u8, .{ .name = "version", .description = "Opens for this VERSION. If not specified, opens for the active Zig version in the current directory.", .default_val = "" }),
},
.vals_mandatory = false,
},
.{
.name = "reference",
.description = "Open the language reference in the default web browser.",
.vals = &.{
ValueT.ofType([]const u8, .{ .name = "version", .description = "Opens for this VERSION. If not specified, opens for the active version on the current directory.", .default_val = "" }),
},
.vals_mandatory = false,
},
.{
.name = "info",
.description = "Show information about installations",
},
.{
.name = "update-self",
.description = "Update zigverm itself",
},
},
};

pub const Cli = union(enum) {
install: []const u8,
remove: []const u8,
show,
update_self,
override: OverrideArrgs,
override_rm: ?[]const u8,
update: ?[]const u8,
std: ?[]const u8,
reference: ?[]const u8,

pub fn read_args(alloc: Allocator) anyerror!Cli {
var arg_iter = try std.process.argsWithAllocator(alloc);
defer arg_iter.deinit();

_ = arg_iter.next();

var command: Cli = undefined;

var cmd: []const u8 = undefined;

{
const trycmd = arg_iter.next();
if (trycmd) |c|
cmd = c
else
incorrectUsage(null);
}

if (streql(cmd, "install")) {
const rel = arg_iter.next().?;
command = Cli{ .install = try alloc.dupe(u8, rel) };
} else if (streql(cmd, "remove")) {
const rel = arg_iter.next().?;
command = Cli{ .remove = try alloc.dupe(u8, rel) };
} else if (streql(cmd, "update")) {
const rel = arg_iter.next();
command = Cli{ .update = if (rel) |r| try alloc.dupe(u8, r) else null };
} else if (streql(cmd, "std")) {
const rel = arg_iter.next();
command = Cli{ .std = if (rel) |r| try alloc.dupe(u8, r) else null };
} else if (streql(cmd, "reference")) {
const rel = arg_iter.next();
command = Cli{ .reference = if (rel) |r| try alloc.dupe(u8, r) else null };
} else if (streql(cmd, "info")) {
command = Cli.show;
} else if (streql(cmd, "-h") or streql(cmd, "--help")) {
utils.printStdOut("{s}\n", .{helptext});
std.process.exit(0);
} else if (streql(cmd, "-V") or streql(cmd, "--version")) {
utils.printStdOut("{s}\n", .{Version});
std.process.exit(0);
} else if (streql(cmd, "update-self")) {
command = Cli.update_self;
} else if (streql(cmd, "override")) {
const rel_or_directory = arg_iter.next() orelse return incorrectUsage(null);
const rel = arg_iter.next();

var override_args = OverrideArrgs{ .version = undefined, .directory = null };

if (rel != null) {
override_args.version = try alloc.dupe(u8, rel.?);
override_args.directory = try alloc.dupe(u8, rel_or_directory);
} else {
override_args.version = try alloc.dupe(u8, rel_or_directory);
}

command = Cli{ .override = override_args };
} else if (streql(cmd, "override-rm")) {
const directory = arg_iter.next() orelse return incorrectUsage(null);
command = Cli{ .override_rm = directory };
} else incorrectUsage(cmd);

return command;
}
};

fn incorrectUsage(cmd: ?[]const u8) noreturn {
if (cmd != null) {
std.debug.print("Incorrect usage for '{s}'. Please see help by using --help\n", .{cmd.?});
std.process.exit(1);
} else {
std.debug.print("Incorrect usage. Please see help by using --help\n", .{});
std.process.exit(1);
}
}
88 changes: 47 additions & 41 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const std = @import("std");
const utils = @import("utils.zig");
const builtin = @import("builtin");
const Cli = @import("cli.zig").Cli;
const cli = @import("cli.zig");
const common = @import("common");
const update_self = @import("update-self.zig");
const cova = @import("cova");

const Cli = cli.Cli;
const CommandT = cli.CommandT;
const Client = std.http.Client;
const http = std.http;
const paths = common.paths;
Expand All @@ -22,50 +25,53 @@ pub fn main() !void {
var aa = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const alloc = aa.allocator();

const command = try Cli.read_args(alloc);
const command = try Cli.init(alloc, .{});
var args_iter = try cova.ArgIteratorGeneric.init(alloc);
const stdout = std.io.getStdOut().writer();

try cova.parseArgs(&args_iter, CommandT, command, stdout, .{});
var cp = try CommonPaths.resolve(alloc);
defer cp.close();

switch (command) {
Cli.install => |version| {
var rel = try Release.releasefromVersion(version);
if (cp.install_dir.openDir(try common.release_name(alloc, rel), .{})) |_| {
std.log.err("Version already installled. Quitting", .{});
std.process.exit(0);
} else |_| {}

var client = Client{ .allocator = alloc };
defer client.deinit();

const resp = try install.get_json_dslist(&client);
const releases = try json.parseFromSliceLeaky(json.Value, alloc, resp.body[0..resp.length], .{});

try install.install_release(alloc, &client, releases, &rel, cp);
},
Cli.remove => |version| {
const rel = try Release.releasefromVersion(version);
try remove_release(alloc, rel, cp);
},
Cli.show => try show_info(alloc, cp),
Cli.std => |ver| try open_std(alloc, cp, ver),
Cli.reference => |ver| try open_reference(alloc, cp, ver),
Cli.override => |oa| {
var override_args = oa;
const rel = try Release.releasefromVersion(override_args.version);
if (override_args.directory != null and !streql(override_args.directory.?, "default")) {
override_args.directory = try std.fs.realpathAlloc(alloc, override_args.directory.?);
}
const directory = override_args.directory orelse try std.process.getCwdAlloc(alloc);
try override(alloc, cp, rel, directory);
},
Cli.override_rm => |dir| {
const directory = dir orelse try std.process.getCwdAlloc(alloc);
try override_rm(alloc, cp, directory);
},
Cli.update_self => try update_self.update_self(alloc, cp),
Cli.update => |version_possible| try update_zig_installation(alloc, cp, version_possible),
}
// switch (command) {
// Cli.install => |version| {
// var rel = try Release.releasefromVersion(version);
// if (cp.install_dir.openDir(try common.release_name(alloc, rel), .{})) |_| {
// std.log.err("Version already installled. Quitting", .{});
// std.process.exit(0);
// } else |_| {}
//
// var client = Client{ .allocator = alloc };
// defer client.deinit();
//
// const resp = try install.get_json_dslist(&client);
// const releases = try json.parseFromSliceLeaky(json.Value, alloc, resp.body[0..resp.length], .{});
//
// try install.install_release(alloc, &client, releases, &rel, cp);
// },
// Cli.remove => |version| {
// const rel = try Release.releasefromVersion(version);
// try remove_release(alloc, rel, cp);
// },
// Cli.show => try show_info(alloc, cp),
// Cli.std => |ver| try open_std(alloc, cp, ver),
// Cli.reference => |ver| try open_reference(alloc, cp, ver),
// Cli.override => |oa| {
// var override_args = oa;
// const rel = try Release.releasefromVersion(override_args.version);
// if (override_args.directory != null and !streql(override_args.directory.?, "default")) {
// override_args.directory = try std.fs.realpathAlloc(alloc, override_args.directory.?);
// }
// const directory = override_args.directory orelse try std.process.getCwdAlloc(alloc);
// try override(alloc, cp, rel, directory);
// },
// Cli.override_rm => |dir| {
// const directory = dir orelse try std.process.getCwdAlloc(alloc);
// try override_rm(alloc, cp, directory);
// },
// Cli.update_self => try update_self.update_self(alloc, cp),
// Cli.update => |version_possible| try update_zig_installation(alloc, cp, version_possible),
// }
}

fn remove_release(alloc: Allocator, rel: Release, cp: CommonPaths) !void {
Expand Down
Loading