authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-16 18:48:09 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-16 18:48:09 -08:00
logbab6aa2485ac75b17e3026d233e4016cb542a19f
treeba820328081ff190cc4180fcaa12019160db5c45
parent4ab3cb0116c71961be7b72b840504721307ed554

fetch: add module struct so fs is only searched once


3 files changed, 30 insertions(+), 13 deletions(-)

src/cmd_fetch.zig+17-13
......@@ -12,7 +12,7 @@ pub fn execute(args: [][]u8) !void {
1212 const home = try known_folders.getPath(gpa, .home);
1313 const dir = try std.fmt.allocPrint(gpa, "{}{}", .{home, "/.cache/zigmod/deps"});
1414
15 try fetch_deps(dir, "./zig.mod");
15 const top_module = try fetch_deps(dir, "./zig.mod");
1616
1717 //
1818 const f = try std.fs.cwd().createFile("./deps.zig", .{});
......@@ -34,12 +34,13 @@ pub fn execute(args: [][]u8) !void {
3434 });
3535 try w.print("\n", .{});
3636 try w.print("pub const packages = ", .{});
37 try print_deps(w, dir, try u.ModFile.init(gpa, "./zig.mod"), 0);
37 try print_deps(w, dir, top_module, 0);
3838 try w.print(";\n", .{});
3939}
4040
41fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!void {
41fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
4242 const m = try u.ModFile.init(gpa, mpath);
43 const moduledeps = &std.ArrayList(u.Module).init(gpa);
4344 for (m.deps) |d| {
4445 const p = try std.fmt.allocPrint(gpa, "{}{}{}", .{dir, "/", try d.clean_path()});
4546 switch (d.type) {
......@@ -56,10 +57,18 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!void {
5657 }
5758 switch (d.type) {
5859 else => {
59 try fetch_deps(dir, try std.fmt.allocPrint(gpa, "{}{}", .{p, "/zig.mod"}));
60 var dd = try fetch_deps(dir, try std.fmt.allocPrint(gpa, "{}{}", .{p, "/zig.mod"}));
61 dd.clean_path = try d.clean_path();
62 try moduledeps.append(dd);
6063 },
6164 }
6265 }
66 return u.Module{
67 .name = m.name,
68 .main = m.main,
69 .deps = moduledeps.items,
70 .clean_path = "",
71 };
6372}
6473
6574fn run_cmd(dir: ?[]const u8, args: []const []const u8) !void {
......@@ -71,7 +80,7 @@ fn run_cmd(dir: ?[]const u8, args: []const []const u8) !void {
7180 };
7281}
7382
74fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.ModFile, tabs: i32) anyerror!void {
83fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32) anyerror!void {
7584 if (m.deps.len == 0 and tabs > 0) {
7685 try w.print("null", .{});
7786 return;
......@@ -80,16 +89,11 @@ fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.ModFile, tabs: i32) a
8089 const t = " ";
8190 const r = try u.repeat(t, tabs);
8291 for (m.deps) |d| {
83 const dcpath = try d.clean_path();
84 const p = try u.concat(&[_][]const u8{dir, "/", dcpath});
85 const np = try u.concat(&[_][]const u8{p, "/zig.mod"});
86 const n = try u.ModFile.init(gpa, np);
87
8892 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,"build.Pkg{"})});
89 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".name = \"",n.name,"\","})});
90 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".path = cache ++ \"/",dcpath,"/",n.main,"\","})});
93 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".name = \"",d.name,"\","})});
94 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".path = cache ++ \"/",d.clean_path,"/",d.main,"\","})});
9195 try w.print("{}", .{try u.concat(&[_][]const u8{r,t,t,".dependencies = "})});
92 try print_deps(w, dir, n, tabs+2);
96 try print_deps(w, dir, d, tabs+2);
9397 try w.print("{}\n", .{","});
9498 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,"},"})});
9599 }
src/util/index.zig+1
......@@ -4,3 +4,4 @@ usingnamespace @import("./funcs.zig");
44usingnamespace @import("./dep_type.zig");
55usingnamespace @import("./dep.zig");
66usingnamespace @import("./modfile.zig");
7usingnamespace @import("./module.zig");
src/util/module.zig created+12
......@@ -0,0 +1,12 @@
1const std = @import("std");
2
3//
4//
5
6pub const Module = struct {
7 name: []const u8,
8 main: []const u8,
9
10 deps: []Module,
11 clean_path: []const u8,
12};