authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2021-01-11 01:53:01 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2021-01-11 01:53:01 -08:00
logc756359635d8dfc436ac52a2708e625a331e2e94
tree92e2c7867172902d141da0b773e80dd256c7d102
parenta8aa99bf4186ddaf3cdeda1c52fb2ed23543a07d

fetch: print zig packages flatly instead of in tree


1 files changed, 48 insertions(+), 12 deletions(-)

src/cmd_fetch.zig+48-12
...@@ -61,6 +61,16 @@ pub fn execute(args: [][]u8) !void {...@@ -61,6 +61,16 @@ pub fn execute(args: [][]u8) !void {
61 try print_paths(w, list.items);61 try print_paths(w, list.items);
62 try w.writeAll("};\n");62 try w.writeAll("};\n");
63 try w.writeAll("\n");63 try w.writeAll("\n");
64 try w.writeAll("pub const package_data = struct {\n");
65 const duped = &std.ArrayList(u.Module).init(gpa);
66 for (list.items) |mod| {
67 if (mod.main.len > 0 and mod.clean_path.len > 0) {
68 try duped.append(mod);
69 }
70 }
71 try print_pkg_data_to(w, duped, &std.ArrayList(u.Module).init(gpa));
72 try w.writeAll("};\n");
73 try w.writeAll("\n");
64 try w.writeAll("pub const packages = ");74 try w.writeAll("pub const packages = ");
65 try print_deps(w, dir, top_module, 0, true);75 try print_deps(w, dir, top_module, 0, true);
66 try w.writeAll(";\n");76 try w.writeAll(";\n");
...@@ -268,23 +278,16 @@ fn print_deps(w: fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32, array:...@@ -268,23 +278,16 @@ fn print_deps(w: fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32, array:
268 }278 }
269 const t = " ";279 const t = " ";
270 const r = try u.repeat(t, tabs);280 const r = try u.repeat(t, tabs);
271 var c: usize = 0;281 for (m.deps) |d, i| {
272 for (m.deps) |d| {
273 if (d.main.len == 0) {282 if (d.main.len == 0) {
274 continue;283 continue;
275 }284 }
276 c += 1;
277 if (!array) {285 if (!array) {
278 try w.print(" pub const {} = packages[{}];\n", .{d.name, c-1});286 try w.print(" pub const {} = packages[{}];\n", .{d.name, i});
279 continue;287 }
288 else {
289 try w.print(" package_data._{},\n", .{d.id});
280 }290 }
281 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,"build.Pkg{"})});
282 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".name = \"",d.name,"\","})});
283 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".path = cache ++ \"/",d.clean_path,"/",d.main,"\","})});
284 try w.print("{}", .{try u.concat(&[_][]const u8{r,t,t,".dependencies = "})});
285 try print_deps(w, dir, d, tabs+2, array);
286 try w.print("{}\n", .{","});
287 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,"},"})});
288 }291 }
289 try w.print("{}", .{try u.concat(&[_][]const u8{r,"}"})});292 try w.print("{}", .{try u.concat(&[_][]const u8{r,"}"})});
290}293}
...@@ -355,3 +358,36 @@ fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void {...@@ -355,3 +358,36 @@ fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void {
355 try collect_pkgs(d, list);358 try collect_pkgs(d, list);
356 }359 }
357}360}
361
362fn print_pkg_data_to(w: fs.File.Writer, list: *std.ArrayList(u.Module), list2: *std.ArrayList(u.Module)) anyerror!void {
363 var i: usize = 0;
364 while (i < list.items.len) : (i += 1) {
365 const mod = list.items[i];
366 if (contains_all(mod.deps, list2)) {
367 try w.print(" pub const _{} = build.Pkg{{ .name = \"{}\", .path = cache ++ \"/{Z}/{}\", .dependencies = &[_]build.Pkg{{", .{mod.id, mod.name, mod.clean_path, mod.main});
368 for (mod.deps) |d| {
369 if (d.main.len > 0) {
370 try w.print(" _{},", .{d.id});
371 }
372 }
373 try w.print(" }} }};\n", .{});
374
375 try list2.append(mod);
376 _ = list.orderedRemove(i);
377 break;
378 }
379 }
380 if (list.items.len > 0) {
381 try print_pkg_data_to(w, list, list2);
382 }
383}
384
385/// returns if all of the zig modules in needles are in haystack
386fn contains_all(needles: []u.Module, haystack: *std.ArrayList(u.Module)) bool {
387 for (needles) |item| {
388 if (item.main.len > 0 and !u.list_contains_gen(u.Module, haystack, item)) {
389 return false;
390 }
391 }
392 return true;
393}