| ... | ... | @@ -7,24 +7,100 @@ const u = @import("./util/index.zig"); |
| 7 | 7 | // |
| 8 | 8 | // |
| 9 | 9 | |
| 10 | | pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { |
| 10 | pub const CollectOptions = struct { |
| 11 | log: bool, |
| 12 | update: bool, |
| 13 | }; |
| 14 | |
| 15 | pub fn collect_deps(dir: []const u8, mpath: []const u8, comptime options: CollectOptions) anyerror!u.Module { |
| 11 | 16 | const m = try u.ModFile.init(gpa, mpath); |
| 12 | 17 | const moduledeps = &std.ArrayList(u.Module).init(gpa); |
| 13 | 18 | var moddir: []const u8 = undefined; |
| 14 | 19 | for (m.deps) |d| { |
| 15 | 20 | const p = try fs.path.join(gpa, &[_][]const u8{dir, try d.clean_path()}); |
| 16 | | const pv = try fs.path.join(gpa, &[_][]const u8{dir, "v", try d.clean_path(), d.version}); |
| 17 | | if (try u.does_file_exist(pv)) { |
| 18 | | moddir = pv; |
| 19 | | } else { |
| 20 | | u.assert(try u.does_file_exist(p), "unable to find dep: {s} {s}, please run zigmod fetch", .{d.path, d.version}); |
| 21 | | moddir = p; |
| 21 | const pv = try fs.path.join(gpa, &[_][]const u8{dir, try d.clean_path_v()}); |
| 22 | if (options.log) { u.print("fetch: {s}: {s}: {s}", .{m.name, @tagName(d.type), d.path}); } |
| 23 | moddir = p; |
| 24 | switch (d.type) { |
| 25 | .system_lib => { |
| 26 | // no op |
| 27 | }, |
| 28 | .git => blk: { |
| 29 | if (!try u.does_folder_exist(p)) { |
| 30 | try d.type.pull(d.path, p); |
| 31 | } |
| 32 | else { |
| 33 | if (options.update) { try d.type.update(p, d.path); } |
| 34 | } |
| 35 | if (d.version.len > 0) { |
| 36 | const vers = u.parse_split(u.GitVersionType, "-").do(d.version) catch |e| switch (e) { |
| 37 | error.IterEmpty => unreachable, |
| 38 | error.NoMemberFound => { |
| 39 | const vtype = d.version[0..std.mem.indexOf(u8, d.version, "-").?]; |
| 40 | u.assert(false, "fetch: git: version type '{s}' is invalid.", .{vtype}); |
| 41 | unreachable; |
| 42 | }, |
| 43 | }; |
| 44 | if (try u.does_folder_exist(pv)) { |
| 45 | if (vers.id == .branch) { |
| 46 | if (options.update) { try d.type.update(p, d.path); } |
| 47 | } |
| 48 | moddir = pv; |
| 49 | break :blk; |
| 50 | } |
| 51 | if ((try u.run_cmd(p, &[_][]const u8{"git", "checkout", vers.string})) > 0) { |
| 52 | u.assert(false, "fetch: git: {s}: {s} {s} does not exist", .{d.path, @tagName(vers.id), vers.string}); |
| 53 | } else { |
| 54 | _ = try u.run_cmd(p, &[_][]const u8{"git", "checkout", "-"}); |
| 55 | } |
| 56 | try d.type.pull(d.path, pv); |
| 57 | _ = try u.run_cmd(pv, &[_][]const u8{"git", "checkout", vers.string}); |
| 58 | if (vers.id != .branch) { |
| 59 | const pvd = try std.fs.cwd().openDir(pv, .{}); |
| 60 | try pvd.deleteTree(".git"); |
| 61 | } |
| 62 | moddir = pv; |
| 63 | } |
| 64 | }, |
| 65 | .hg => { |
| 66 | if (!try u.does_folder_exist(p)) { |
| 67 | try d.type.pull(d.path, p); |
| 68 | } |
| 69 | else { |
| 70 | if (options.update) { try d.type.update(p, d.path); } |
| 71 | } |
| 72 | }, |
| 73 | .http => blk: { |
| 74 | if (try u.does_folder_exist(pv)) { |
| 75 | moddir = pv; |
| 76 | break :blk; |
| 77 | } |
| 78 | const file_name = try u.last(try u.split(d.path, "/")); |
| 79 | if (d.version.len > 0) { |
| 80 | const file_path = try std.fs.path.join(gpa, &[_][]const u8{pv, file_name}); |
| 81 | try d.type.pull(d.path, pv); |
| 82 | if (try u.validate_hash(try u.last(try u.split(pv, "/")), file_path)) { |
| 83 | try std.fs.deleteFileAbsolute(file_path); |
| 84 | moddir = pv; |
| 85 | break :blk; |
| 86 | } |
| 87 | try u.rm_recv(pv); |
| 88 | u.assert(false, "{s} does not match hash {s}", .{d.path, d.version}); |
| 89 | break :blk; |
| 90 | } |
| 91 | if (try u.does_folder_exist(p)) { |
| 92 | try u.rm_recv(p); |
| 93 | } |
| 94 | const file_path = try std.fs.path.join(gpa, &[_][]const u8{p, file_name}); |
| 95 | try d.type.pull(d.path, p); |
| 96 | try std.fs.deleteFileAbsolute(file_path); |
| 97 | }, |
| 22 | 98 | } |
| 23 | 99 | switch (d.type) { |
| 24 | 100 | .system_lib => { |
| 25 | 101 | if (d.is_for_this()) try moduledeps.append(u.Module{ |
| 26 | 102 | .is_sys_lib = true, |
| 27 | | .id = d.id, |
| 103 | .id = "", |
| 28 | 104 | .name = d.path, |
| 29 | 105 | .only_os = d.only_os, |
| 30 | 106 | .except_os = d.except_os, |
| ... | ... | @@ -33,13 +109,13 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { |
| 33 | 109 | .c_source_flags = &[_][]const u8{}, |
| 34 | 110 | .c_source_files = &[_][]const u8{}, |
| 35 | 111 | .deps = &[_]u.Module{}, |
| 36 | | .clean_path = "", |
| 112 | .clean_path = d.path, |
| 37 | 113 | }); |
| 38 | 114 | }, |
| 39 | 115 | else => blk: { |
| 40 | | var dd = try collect_deps(dir, try u.concat(&[_][]const u8{moddir, "/zig.mod"})) catch |e| switch (e) { |
| 116 | var dd = try collect_deps(dir, try u.concat(&[_][]const u8{moddir, "/zig.mod"}), options) catch |e| switch (e) { |
| 41 | 117 | error.FileNotFound => { |
| 42 | | if (d.c_include_dirs.len > 0 or d.c_source_files.len > 0) { |
| 118 | if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) { |
| 43 | 119 | var mod_from = try u.Module.from(d); |
| 44 | 120 | mod_from.id = try u.random_string(48); |
| 45 | 121 | mod_from.clean_path = u.trim_prefix(moddir, dir)[1..]; |