| ... | @@ -4,6 +4,7 @@ const fs = std.fs; | ... | @@ -4,6 +4,7 @@ const fs = std.fs; |
| 4 | | 4 | |
| 5 | const known_folders = @import("known-folders"); | 5 | const known_folders = @import("known-folders"); |
| 6 | const u = @import("./util/index.zig"); | 6 | const u = @import("./util/index.zig"); |
| | 7 | const common = @import("./common.zig"); |
| 7 | | 8 | |
| 8 | // | 9 | // |
| 9 | // | 10 | // |
| ... | @@ -12,7 +13,10 @@ pub fn execute(args: [][]u8) !void { | ... | @@ -12,7 +13,10 @@ pub fn execute(args: [][]u8) !void { |
| 12 | // | 13 | // |
| 13 | const dir = try fs.path.join(gpa, &[_][]const u8{".zigmod", "deps"}); | 14 | const dir = try fs.path.join(gpa, &[_][]const u8{".zigmod", "deps"}); |
| 14 | | 15 | |
| 15 | const top_module = try fetch_deps(dir, "zig.mod"); | 16 | const top_module = try common.collect_deps(dir, "zig.mod", .{ |
| | 17 | .log = true, |
| | 18 | .update = true, |
| | 19 | }); |
| 16 | | 20 | |
| 17 | // | 21 | // |
| 18 | const f = try fs.cwd().createFile("deps.zig", .{}); | 22 | const f = try fs.cwd().createFile("deps.zig", .{}); |
| ... | @@ -96,149 +100,6 @@ pub fn execute(args: [][]u8) !void { | ... | @@ -96,149 +100,6 @@ pub fn execute(args: [][]u8) !void { |
| 96 | try w.writeAll("};\n\n"); | 100 | try w.writeAll("};\n\n"); |
| 97 | } | 101 | } |
| 98 | | 102 | |
| 99 | fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { | | |
| 100 | const m = try u.ModFile.init(gpa, mpath); | | |
| 101 | const moduledeps = &std.ArrayList(u.Module).init(gpa); | | |
| 102 | var moddir: []const u8 = undefined; | | |
| 103 | for (m.deps) |d| { | | |
| 104 | const p = try fs.path.join(gpa, &[_][]const u8{dir, try d.clean_path()}); | | |
| 105 | const pv = try fs.path.join(gpa, &[_][]const u8{dir, try d.clean_path_v()}); | | |
| 106 | u.print("fetch: {s}: {s}: {s}", .{m.name, @tagName(d.type), d.path}); | | |
| 107 | moddir = p; | | |
| 108 | switch (d.type) { | | |
| 109 | .system_lib => { | | |
| 110 | // no op | | |
| 111 | }, | | |
| 112 | .git => blk: { | | |
| 113 | if (!try u.does_folder_exist(p)) { | | |
| 114 | try d.type.pull(d.path, p); | | |
| 115 | } | | |
| 116 | else { | | |
| 117 | try d.type.update(p, d.path); | | |
| 118 | } | | |
| 119 | if (d.version.len > 0) { | | |
| 120 | const vers = u.parse_split(u.GitVersionType, "-").do(d.version) catch |e| switch (e) { | | |
| 121 | error.IterEmpty => unreachable, | | |
| 122 | error.NoMemberFound => { | | |
| 123 | const vtype = d.version[0..std.mem.indexOf(u8, d.version, "-").?]; | | |
| 124 | u.assert(false, "fetch: git: version type '{s}' is invalid.", .{vtype}); | | |
| 125 | unreachable; | | |
| 126 | }, | | |
| 127 | }; | | |
| 128 | if (try u.does_folder_exist(pv)) { | | |
| 129 | if (vers.id == .branch) { | | |
| 130 | try d.type.update(p, d.path); | | |
| 131 | } | | |
| 132 | moddir = pv; | | |
| 133 | break :blk; | | |
| 134 | } | | |
| 135 | if ((try u.run_cmd(p, &[_][]const u8{"git", "checkout", vers.string})) > 0) { | | |
| 136 | u.assert(false, "fetch: git: {s}: {s} {s} does not exist", .{d.path, @tagName(vers.id), vers.string}); | | |
| 137 | } else { | | |
| 138 | _ = try u.run_cmd(p, &[_][]const u8{"git", "checkout", "-"}); | | |
| 139 | } | | |
| 140 | try d.type.pull(d.path, pv); | | |
| 141 | _ = try u.run_cmd(pv, &[_][]const u8{"git", "checkout", vers.string}); | | |
| 142 | if (vers.id != .branch) { | | |
| 143 | const pvd = try std.fs.cwd().openDir(pv, .{}); | | |
| 144 | try pvd.deleteTree(".git"); | | |
| 145 | } | | |
| 146 | moddir = pv; | | |
| 147 | } | | |
| 148 | }, | | |
| 149 | .hg => { | | |
| 150 | if (!try u.does_folder_exist(p)) { | | |
| 151 | try d.type.pull(d.path, p); | | |
| 152 | } | | |
| 153 | else { | | |
| 154 | try d.type.update(p, d.path); | | |
| 155 | } | | |
| 156 | }, | | |
| 157 | .http => blk: { | | |
| 158 | if (try u.does_folder_exist(pv)) { | | |
| 159 | moddir = pv; | | |
| 160 | break :blk; | | |
| 161 | } | | |
| 162 | const file_name = try u.last(try u.split(d.path, "/")); | | |
| 163 | if (d.version.len > 0) { | | |
| 164 | const file_path = try std.fs.path.join(gpa, &[_][]const u8{pv, file_name}); | | |
| 165 | try d.type.pull(d.path, pv); | | |
| 166 | if (try u.validate_hash(try u.last(try u.split(pv, "/")), file_path)) { | | |
| 167 | try std.fs.deleteFileAbsolute(file_path); | | |
| 168 | moddir = pv; | | |
| 169 | break :blk; | | |
| 170 | } | | |
| 171 | try u.rm_recv(pv); | | |
| 172 | u.assert(false, "{s} does not match hash {s}", .{d.path, d.version}); | | |
| 173 | break :blk; | | |
| 174 | } | | |
| 175 | if (try u.does_folder_exist(p)) { | | |
| 176 | try u.rm_recv(p); | | |
| 177 | } | | |
| 178 | const file_path = try std.fs.path.join(gpa, &[_][]const u8{p, file_name}); | | |
| 179 | try d.type.pull(d.path, p); | | |
| 180 | try std.fs.deleteFileAbsolute(file_path); | | |
| 181 | }, | | |
| 182 | } | | |
| 183 | switch (d.type) { | | |
| 184 | .system_lib => { | | |
| 185 | if (d.is_for_this()) try moduledeps.append(u.Module{ | | |
| 186 | .is_sys_lib = true, | | |
| 187 | .id = "", | | |
| 188 | .name = d.path, | | |
| 189 | .only_os = d.only_os, | | |
| 190 | .except_os = d.except_os, | | |
| 191 | .main = "", | | |
| 192 | .c_include_dirs = &[_][]const u8{}, | | |
| 193 | .c_source_flags = &[_][]const u8{}, | | |
| 194 | .c_source_files = &[_][]const u8{}, | | |
| 195 | .deps = &[_]u.Module{}, | | |
| 196 | .clean_path = d.path, | | |
| 197 | }); | | |
| 198 | }, | | |
| 199 | else => blk: { | | |
| 200 | var dd = try fetch_deps(dir, try u.concat(&[_][]const u8{moddir, "/zig.mod"})) catch |e| switch (e) { | | |
| 201 | error.FileNotFound => { | | |
| 202 | if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) { | | |
| 203 | var mod_from = try u.Module.from(d); | | |
| 204 | mod_from.id = try u.random_string(48); | | |
| 205 | mod_from.clean_path = u.trim_prefix(moddir, dir)[1..]; | | |
| 206 | if (mod_from.is_for_this()) try moduledeps.append(mod_from); | | |
| 207 | } | | |
| 208 | break :blk; | | |
| 209 | }, | | |
| 210 | else => e, | | |
| 211 | }; | | |
| 212 | dd.clean_path = u.trim_prefix(moddir, dir)[1..]; | | |
| 213 | | | |
| 214 | if (dd.id.len == 0) dd.id = try u.random_string(48); | | |
| 215 | if (d.name.len > 0) dd.name = d.name; | | |
| 216 | if (d.main.len > 0) dd.main = d.main; | | |
| 217 | if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs; | | |
| 218 | if (d.c_source_flags.len > 0) dd.c_source_flags = d.c_source_flags; | | |
| 219 | if (d.c_source_files.len > 0) dd.c_source_files = d.c_source_files; | | |
| 220 | if (d.only_os.len > 0) dd.only_os = d.only_os; | | |
| 221 | if (d.except_os.len > 0) dd.except_os = d.except_os; | | |
| 222 | | | |
| 223 | if (dd.is_for_this()) try moduledeps.append(dd); | | |
| 224 | }, | | |
| 225 | } | | |
| 226 | } | | |
| 227 | return u.Module{ | | |
| 228 | .is_sys_lib = false, | | |
| 229 | .id = m.id, | | |
| 230 | .name = m.name, | | |
| 231 | .main = m.main, | | |
| 232 | .c_include_dirs = m.c_include_dirs, | | |
| 233 | .c_source_flags = m.c_source_flags, | | |
| 234 | .c_source_files = m.c_source_files, | | |
| 235 | .deps = moduledeps.items, | | |
| 236 | .clean_path = "", | | |
| 237 | .only_os = &[_][]const u8{}, | | |
| 238 | .except_os = &[_][]const u8{}, | | |
| 239 | }; | | |
| 240 | } | | |
| 241 | | | |
| 242 | fn print_ids(w: fs.File.Writer, list: []u.Module) !void { | 103 | fn print_ids(w: fs.File.Writer, list: []u.Module) !void { |
| 243 | for (list) |mod| { | 104 | for (list) |mod| { |
| 244 | if (mod.is_sys_lib) { | 105 | if (mod.is_sys_lib) { |