| ... | ... | @@ -0,0 +1,85 @@ |
| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const gpa = std.heap.c_allocator; |
| 4 | const knownfolders = @import("known-folders"); |
| 5 | const extras = @import("extras"); |
| 6 | |
| 7 | const zigmod = @import("./../lib.zig"); |
| 8 | const u = @import("./../util/funcs.zig"); |
| 9 | const common = @import("./../common.zig"); |
| 10 | |
| 11 | pub fn execute(self_name: []const u8, args: [][:0]u8) !void { |
| 12 | _ = self_name; |
| 13 | |
| 14 | if (args.len < 2) u.fail("usage: zigmod install [git|hg|http] [url]", .{}); |
| 15 | |
| 16 | const homepath = try knownfolders.getPath(gpa, .home) orelse u.fail("failed to read HOME", .{}); |
| 17 | const cache = try knownfolders.getPath(gpa, .cache); |
| 18 | const datapath = try knownfolders.getPath(gpa, .data) orelse u.fail("failed to read XDG_DATA_HOME", .{}); |
| 19 | |
| 20 | const RemoteType = enum { |
| 21 | git, |
| 22 | hg, |
| 23 | http, |
| 24 | }; |
| 25 | const rty_s = args[0]; |
| 26 | const rty = std.meta.stringToEnum(RemoteType, rty_s) orelse u.fail("usage: zigmod install [git|hg|http] [url]", .{}); |
| 27 | const ty: zigmod.Dep.Type = switch (rty) { |
| 28 | .git => .git, |
| 29 | .hg => .hg, |
| 30 | .http => .http, |
| 31 | }; |
| 32 | const dep: zigmod.Dep = .{ |
| 33 | .type = ty, |
| 34 | .path = args[1], |
| 35 | .id = u.random_string(48), |
| 36 | .name = "(name)", |
| 37 | .main = "", |
| 38 | .version = "", |
| 39 | .c_include_dirs = &.{}, |
| 40 | .c_source_flags = &.{}, |
| 41 | .c_source_files = &.{}, |
| 42 | .only_os = &.{}, |
| 43 | .except_os = &.{}, |
| 44 | .yaml = null, |
| 45 | .deps = &.{}, |
| 46 | .keep = false, |
| 47 | .for_build = false, |
| 48 | }; |
| 49 | const clean_path = try dep.clean_path(gpa); |
| 50 | const cachepath = try std.fs.path.join(gpa, &.{ cache.?, "zigmod", "deps" }); |
| 51 | const modpath = try std.fs.path.join(gpa, &.{ cachepath, clean_path }); |
| 52 | std.log.debug("modpath: {s}", .{modpath}); |
| 53 | |
| 54 | if (!try extras.doesFolderExist(null, modpath)) { |
| 55 | try dep.type.pull(gpa, dep.path, modpath); |
| 56 | } else { |
| 57 | try dep.type.update(gpa, modpath, ""); |
| 58 | } |
| 59 | |
| 60 | const moddir = try std.fs.cwd().openDir(modpath, .{}); |
| 61 | const ci = @import("./ci.zig"); |
| 62 | try ci.do(gpa, cachepath, moddir); |
| 63 | |
| 64 | const modfile = try zigmod.ModFile.from_dir(gpa, moddir, modpath); |
| 65 | const zigversion_sv = modfile.min_zig_version orelse u.fail("zigmod manifest requires min_zig_version field", .{}); |
| 66 | const zigversion = try std.fmt.allocPrint(gpa, "{}", .{zigversion_sv}); |
| 67 | const zigpath = try std.fs.path.join(gpa, &.{ datapath, "zig", zigversion, "zig" }); |
| 68 | |
| 69 | // zig build |
| 70 | const argv: []const string = &.{ |
| 71 | zigpath, "build", |
| 72 | "--prefix", try std.fs.path.join(gpa, &.{ homepath, ".zigmod" }), |
| 73 | }; |
| 74 | std.log.debug("argv: {s}", .{argv}); |
| 75 | var proc = std.process.Child.init(argv, gpa); |
| 76 | proc.cwd = modpath; |
| 77 | const term = try proc.spawnAndWait(); |
| 78 | switch (term) { |
| 79 | .Exited => |v| u.assert(v == 0, "zig build failed with exit code: {d}", .{v}), |
| 80 | .Signal => |v| u.fail("zig build was stopped with signal: {d}", .{v}), |
| 81 | .Stopped => |v| u.fail("zig build was stopped with code: {d}", .{v}), |
| 82 | .Unknown => |v| u.fail("zig build encountered unknown: {d}", .{v}), |
| 83 | } |
| 84 | std.log.info("success!", .{}); |
| 85 | } |