diff --git a/docs/commands/README.md b/docs/commands/README.md index cd52db37807c4b4096247317b8a1fd541c19526f..071a1249edb22ee2d0d430276e28a8f1ca7d1601 100644 --- a/docs/commands/README.md +++ b/docs/commands/README.md @@ -17,3 +17,4 @@ Running `zigmod` with no commands will print this list. - [`aq`](aq.md) - [`aq add`](aq_add.md) - [`aq showjson`](aq_showjson.md) +- [`aq install`](aq_install.md) diff --git a/docs/commands/aq_install.md b/docs/commands/aq_install.md new file mode 100644 index 0000000000000000000000000000000000000000..b17e26d5dbdec6b300e9dbac339d2f8332018f7b --- /dev/null +++ b/docs/commands/aq_install.md @@ -0,0 +1,12 @@ +## `aq install` command +``` +zigmod aq install +``` + +This command takes `` and installs it for use on your local machine. + +If you're on the details page for a package, the string this command is expecting is the path of the url after the domain name. So for example for the package https://aquila.red/1/nektro/discord-archiver, you would add it using `zigmod aq install 1/nektro/discord-archiver`. + +Adding `~/.zigmod/bin` to your `$PATH` will allow you to reference the commands by name instead of by absolute path. + +The directory `~/.cache/zigmod` is a cache directory and may be deleted at any time. diff --git a/src/cmd/aq.zig b/src/cmd/aq.zig index 61bbedc48cafb22a243c6039b2873c228cd99020..0e823257d3bb7d9838c0f3beaab6ebda772abeb2 100644 --- a/src/cmd/aq.zig +++ b/src/cmd/aq.zig @@ -12,6 +12,7 @@ const u = @import("./../util/index.zig"); pub const commands = struct { pub const add = @import("./aquila/add.zig"); pub const showjson = @import("./aquila/showjson.zig"); + pub const install = @import("./aquila/install.zig"); }; pub const server_root = "https://aquila.red"; @@ -26,6 +27,7 @@ pub fn execute(args: [][]u8) !void { \\The subcommands available are: \\ - add Append this package to your dependencies \\ - showjson Print debug api data to stdout + \\ - install Install a package }); return; } diff --git a/src/cmd/aquila/install.zig b/src/cmd/aquila/install.zig new file mode 100644 index 0000000000000000000000000000000000000000..5137ac70bf9e65581e8fd5b1c1431a4dce06438c --- /dev/null +++ b/src/cmd/aquila/install.zig @@ -0,0 +1,71 @@ +const std = @import("std"); +const gpa = std.heap.c_allocator; + +const knownfolders = @import("known-folders"); + +const u = @import("./../../util/index.zig"); +const common = @import("./../../common.zig"); + +pub fn execute(args: [][]u8) !void { + const home = try knownfolders.getPath(gpa, .home); + const homepath = home.?; + const homedir = try std.fs.cwd().openDir(homepath, .{}); + + if (!(try u.does_file_exist("zig.mod", homedir))) { + const f = try homedir.createFile("zig.mod", .{}); + defer f.close(); + const w = f.writer(); + const init = @import("../init.zig"); + try init.writeExeManifest(w, try u.random_string(48), "zigmod_installation", null, null); + } + + // add to ~/zig.mod for later + const aqadd = @import("./add.zig"); + const pkgurl = aqadd.do(homedir, args[0]) catch |err| switch (err) { + error.AquilaBadResponse => return, + else => return err, + }; + + // get modfile and dep + const m = try u.ModFile.from_dir(gpa, homedir); + var dep: u.Dep = undefined; + for (m.devdeps) |d| { + if (std.mem.eql(u8, d.path, pkgurl)) { + dep = d; + break; + } + } + + // + const cache = try knownfolders.getPath(gpa, .cache); + const cachepath = try std.fs.path.join(gpa, &.{ cache.?, "zigmod", "deps" }); + + // fetch singular pkg + var fetchoptions = common.CollectOptions{ + .log = true, + .update = false, + }; + try fetchoptions.init(); + const modpath = try common.get_modpath(cachepath, dep, "install", &fetchoptions); + const moddir = try std.fs.cwd().openDir(modpath, .{}); + + // zigmod ci + const ci = @import("../ci.zig"); + try ci.do(modpath, moddir); + + // zig build + const argv: []const []const u8 = &.{ + "zig", "build", + "--prefix", try std.fs.path.join(gpa, &.{ homepath, ".zigmod" }), + "--cache-dir", try std.fs.path.join(gpa, &.{ cache.?, "zigmod", "zig" }), + }; + const proc = try std.ChildProcess.init(argv, gpa); + proc.cwd = modpath; + const term = try proc.spawnAndWait(); + switch (term) { + .Exited => |v| u.assert(v == 0, "zig build failed with exit code: {d}", .{v}), + .Signal => |v| std.log.info("zig build was stopped with signal: {d}", .{v}), + .Stopped => |v| std.log.info("zig build was stopped with code: {d}", .{v}), + .Unknown => |v| std.log.info("zig build encountered unknown: {d}", .{v}), + } +}