| ... | @@ -15,93 +15,8 @@ pub const CollectOptions = struct { | ... | @@ -15,93 +15,8 @@ pub const CollectOptions = struct { |
| 15 | pub fn collect_deps(dir: []const u8, mpath: []const u8, comptime options: CollectOptions) anyerror!u.Module { | 15 | pub fn collect_deps(dir: []const u8, mpath: []const u8, comptime options: CollectOptions) anyerror!u.Module { |
| 16 | const m = try u.ModFile.init(gpa, mpath); | 16 | const m = try u.ModFile.init(gpa, mpath); |
| 17 | const moduledeps = &std.ArrayList(u.Module).init(gpa); | 17 | const moduledeps = &std.ArrayList(u.Module).init(gpa); |
| 18 | const tempdir = try fs.path.join(gpa, &.{dir, "temp"}); | | |
| 19 | var moddir: []const u8 = undefined; | | |
| 20 | for (m.deps) |d| { | 18 | for (m.deps) |d| { |
| 21 | const p = try fs.path.join(gpa, &.{dir, try d.clean_path()}); | 19 | const moddir = try get_moddir(dir, d, m.name, options); |
| 22 | const pv = try fs.path.join(gpa, &.{dir, try d.clean_path_v()}); | | |
| 23 | if (options.log) { u.print("fetch: {s}: {s}: {s}", .{m.name, @tagName(d.type), d.path}); } | | |
| 24 | moddir = p; | | |
| 25 | switch (d.type) { | | |
| 26 | .system_lib => { | | |
| 27 | // no op | | |
| 28 | }, | | |
| 29 | .git => blk: { | | |
| 30 | if (d.version.len > 0) { | | |
| 31 | const vers = u.parse_split(u.GitVersionType, "-").do(d.version) catch |e| switch (e) { | | |
| 32 | error.IterEmpty => unreachable, | | |
| 33 | error.NoMemberFound => { | | |
| 34 | const vtype = d.version[0..std.mem.indexOf(u8, d.version, "-").?]; | | |
| 35 | u.assert(false, "fetch: git: version type '{s}' is invalid.", .{vtype}); | | |
| 36 | unreachable; | | |
| 37 | }, | | |
| 38 | }; | | |
| 39 | if (try u.does_folder_exist(pv)) { | | |
| 40 | if (vers.id == .branch) { | | |
| 41 | if (options.update) { try d.type.update(pv, d.path); } | | |
| 42 | } | | |
| 43 | moddir = pv; | | |
| 44 | break :blk; | | |
| 45 | } | | |
| 46 | try d.type.pull(d.path, tempdir); | | |
| 47 | if ((try u.run_cmd(tempdir, &.{"git", "checkout", vers.string})) > 0) { | | |
| 48 | u.assert(false, "fetch: git: {s}: {s} {s} does not exist", .{d.path, @tagName(vers.id), vers.string}); | | |
| 49 | } | | |
| 50 | const td_fd = try fs.cwd().openDir(dir, .{}); | | |
| 51 | try u.mkdir_all(pv); | | |
| 52 | try td_fd.rename("temp", try d.clean_path_v()); | | |
| 53 | if (vers.id != .branch) { | | |
| 54 | const pvd = try std.fs.cwd().openDir(pv, .{}); | | |
| 55 | try pvd.deleteTree(".git"); | | |
| 56 | } | | |
| 57 | moddir = pv; | | |
| 58 | break :blk; | | |
| 59 | } | | |
| 60 | if (!try u.does_folder_exist(p)) { | | |
| 61 | try d.type.pull(d.path, p); | | |
| 62 | } | | |
| 63 | else { | | |
| 64 | if (options.update) { try d.type.update(p, d.path); } | | |
| 65 | } | | |
| 66 | }, | | |
| 67 | .hg => { | | |
| 68 | if (!try u.does_folder_exist(p)) { | | |
| 69 | try d.type.pull(d.path, p); | | |
| 70 | } | | |
| 71 | else { | | |
| 72 | if (options.update) { try d.type.update(p, d.path); } | | |
| 73 | } | | |
| 74 | }, | | |
| 75 | .http => blk: { | | |
| 76 | if (try u.does_folder_exist(pv)) { | | |
| 77 | moddir = pv; | | |
| 78 | break :blk; | | |
| 79 | } | | |
| 80 | const file_name = try u.last(try u.split(d.path, "/")); | | |
| 81 | if (d.version.len > 0) { | | |
| 82 | if (try u.does_folder_exist(pv)) { | | |
| 83 | moddir = pv; | | |
| 84 | break :blk; | | |
| 85 | } | | |
| 86 | const file_path = try std.fs.path.join(gpa, &.{pv, file_name}); | | |
| 87 | try d.type.pull(d.path, pv); | | |
| 88 | if (try u.validate_hash(d.version, file_path)) { | | |
| 89 | try std.fs.cwd().deleteFile(file_path); | | |
| 90 | moddir = pv; | | |
| 91 | break :blk; | | |
| 92 | } | | |
| 93 | try u.rm_recv(pv); | | |
| 94 | u.assert(false, "{s} does not match hash {s}", .{d.path, d.version}); | | |
| 95 | break :blk; | | |
| 96 | } | | |
| 97 | if (try u.does_folder_exist(p)) { | | |
| 98 | try u.rm_recv(p); | | |
| 99 | } | | |
| 100 | const file_path = try std.fs.path.join(gpa, &.{p, file_name}); | | |
| 101 | try d.type.pull(d.path, p); | | |
| 102 | try std.fs.deleteFileAbsolute(file_path); | | |
| 103 | }, | | |
| 104 | } | | |
| 105 | switch (d.type) { | 20 | switch (d.type) { |
| 106 | .system_lib => { | 21 | .system_lib => { |
| 107 | if (d.is_for_this()) try moduledeps.append(u.Module{ | 22 | if (d.is_for_this()) try moduledeps.append(u.Module{ |
| ... | @@ -172,3 +87,89 @@ pub fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void | ... | @@ -172,3 +87,89 @@ pub fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void |
| 172 | try collect_pkgs(d, list); | 87 | try collect_pkgs(d, list); |
| 173 | } | 88 | } |
| 174 | } | 89 | } |
| | 90 | |
| | 91 | fn get_moddir(basedir: []const u8, d: u.Dep, parent_name: []const u8, comptime options: CollectOptions) ![]const u8 { |
| | 92 | const p = try fs.path.join(gpa, &.{basedir, try d.clean_path()}); |
| | 93 | const pv = try fs.path.join(gpa, &.{basedir, try d.clean_path_v()}); |
| | 94 | const tempdir = try fs.path.join(gpa, &.{basedir, "temp"}); |
| | 95 | if (options.log) { u.print("fetch: {s}: {s}: {s}", .{parent_name, @tagName(d.type), d.path}); } |
| | 96 | switch (d.type) { |
| | 97 | .system_lib => { |
| | 98 | // no op |
| | 99 | return ""; |
| | 100 | }, |
| | 101 | .git => { |
| | 102 | if (d.version.len > 0) { |
| | 103 | const vers = u.parse_split(u.GitVersionType, "-").do(d.version) catch |e| switch (e) { |
| | 104 | error.IterEmpty => unreachable, |
| | 105 | error.NoMemberFound => { |
| | 106 | const vtype = d.version[0..std.mem.indexOf(u8, d.version, "-").?]; |
| | 107 | u.assert(false, "fetch: git: version type '{s}' is invalid.", .{vtype}); |
| | 108 | unreachable; |
| | 109 | }, |
| | 110 | }; |
| | 111 | if (try u.does_folder_exist(pv)) { |
| | 112 | if (vers.id == .branch) { |
| | 113 | if (options.update) { try d.type.update(pv, d.path); } |
| | 114 | } |
| | 115 | return pv; |
| | 116 | } |
| | 117 | try d.type.pull(d.path, tempdir); |
| | 118 | if ((try u.run_cmd(tempdir, &.{"git", "checkout", vers.string})) > 0) { |
| | 119 | u.assert(false, "fetch: git: {s}: {s} {s} does not exist", .{d.path, @tagName(vers.id), vers.string}); |
| | 120 | } |
| | 121 | const td_fd = try fs.cwd().openDir(basedir, .{}); |
| | 122 | try u.mkdir_all(pv); |
| | 123 | try td_fd.rename("temp", try d.clean_path_v()); |
| | 124 | if (vers.id != .branch) { |
| | 125 | const pvd = try std.fs.cwd().openDir(pv, .{}); |
| | 126 | try pvd.deleteTree(".git"); |
| | 127 | } |
| | 128 | return pv; |
| | 129 | } |
| | 130 | if (!try u.does_folder_exist(p)) { |
| | 131 | try d.type.pull(d.path, p); |
| | 132 | } |
| | 133 | else { |
| | 134 | if (options.update) { try d.type.update(p, d.path); } |
| | 135 | } |
| | 136 | return p; |
| | 137 | }, |
| | 138 | .hg => { |
| | 139 | if (!try u.does_folder_exist(p)) { |
| | 140 | try d.type.pull(d.path, p); |
| | 141 | } |
| | 142 | else { |
| | 143 | if (options.update) { try d.type.update(p, d.path); } |
| | 144 | } |
| | 145 | return p; |
| | 146 | }, |
| | 147 | .http => { |
| | 148 | if (try u.does_folder_exist(pv)) { |
| | 149 | return pv; |
| | 150 | } |
| | 151 | const file_name = try u.last(try u.split(d.path, "/")); |
| | 152 | if (d.version.len > 0) { |
| | 153 | if (try u.does_folder_exist(pv)) { |
| | 154 | return pv; |
| | 155 | } |
| | 156 | const file_path = try std.fs.path.join(gpa, &.{pv, file_name}); |
| | 157 | try d.type.pull(d.path, pv); |
| | 158 | if (try u.validate_hash(d.version, file_path)) { |
| | 159 | try std.fs.cwd().deleteFile(file_path); |
| | 160 | return pv; |
| | 161 | } |
| | 162 | try u.rm_recv(pv); |
| | 163 | u.assert(false, "{s} does not match hash {s}", .{d.path, d.version}); |
| | 164 | return p; |
| | 165 | } |
| | 166 | if (try u.does_folder_exist(p)) { |
| | 167 | try u.rm_recv(p); |
| | 168 | } |
| | 169 | const file_path = try std.fs.path.join(gpa, &.{p, file_name}); |
| | 170 | try d.type.pull(d.path, p); |
| | 171 | try std.fs.deleteFileAbsolute(file_path); |
| | 172 | return p; |
| | 173 | }, |
| | 174 | } |
| | 175 | } |