diff --git a/src/cmd_fetch.zig b/src/cmd_fetch.zig index ac2520cf50a8ee4dd0a12784c81de1ae7fc7f8fe..c5ba6c479ed7d928d5b1f0863cb420d49086ff78 100644 --- a/src/cmd_fetch.zig +++ b/src/cmd_fetch.zig @@ -12,7 +12,7 @@ pub fn execute(args: [][]u8) !void { const home = try known_folders.getPath(gpa, .home); const dir = try std.fmt.allocPrint(gpa, "{}{}", .{home, "/.cache/zigmod/deps"}); - try fetch_deps(dir, "./zig.mod"); + const top_module = try fetch_deps(dir, "./zig.mod"); // const f = try std.fs.cwd().createFile("./deps.zig", .{}); @@ -34,12 +34,13 @@ pub fn execute(args: [][]u8) !void { }); try w.print("\n", .{}); try w.print("pub const packages = ", .{}); - try print_deps(w, dir, try u.ModFile.init(gpa, "./zig.mod"), 0); + try print_deps(w, dir, top_module, 0); try w.print(";\n", .{}); } -fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!void { +fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { const m = try u.ModFile.init(gpa, mpath); + const moduledeps = &std.ArrayList(u.Module).init(gpa); for (m.deps) |d| { const p = try std.fmt.allocPrint(gpa, "{}{}{}", .{dir, "/", try d.clean_path()}); switch (d.type) { @@ -56,10 +57,18 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!void { } switch (d.type) { else => { - try fetch_deps(dir, try std.fmt.allocPrint(gpa, "{}{}", .{p, "/zig.mod"})); + var dd = try fetch_deps(dir, try std.fmt.allocPrint(gpa, "{}{}", .{p, "/zig.mod"})); + dd.clean_path = try d.clean_path(); + try moduledeps.append(dd); }, } } + return u.Module{ + .name = m.name, + .main = m.main, + .deps = moduledeps.items, + .clean_path = "", + }; } fn 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 { }; } -fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.ModFile, tabs: i32) anyerror!void { +fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32) anyerror!void { if (m.deps.len == 0 and tabs > 0) { try w.print("null", .{}); return; @@ -80,16 +89,11 @@ fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.ModFile, tabs: i32) a const t = " "; const r = try u.repeat(t, tabs); for (m.deps) |d| { - const dcpath = try d.clean_path(); - const p = try u.concat(&[_][]const u8{dir, "/", dcpath}); - const np = try u.concat(&[_][]const u8{p, "/zig.mod"}); - const n = try u.ModFile.init(gpa, np); - try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,"build.Pkg{"})}); - try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".name = \"",n.name,"\","})}); - try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".path = cache ++ \"/",dcpath,"/",n.main,"\","})}); + try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".name = \"",d.name,"\","})}); + try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".path = cache ++ \"/",d.clean_path,"/",d.main,"\","})}); try w.print("{}", .{try u.concat(&[_][]const u8{r,t,t,".dependencies = "})}); - try print_deps(w, dir, n, tabs+2); + try print_deps(w, dir, d, tabs+2); try w.print("{}\n", .{","}); try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,"},"})}); } diff --git a/src/util/index.zig b/src/util/index.zig index 6f3c03d5e2169d4bfc31bede1fa99c9669d1b0c0..4f8eb0085e548648d4f5f9f3d2e162d07f12108f 100644 --- a/src/util/index.zig +++ b/src/util/index.zig @@ -4,3 +4,4 @@ usingnamespace @import("./funcs.zig"); usingnamespace @import("./dep_type.zig"); usingnamespace @import("./dep.zig"); usingnamespace @import("./modfile.zig"); +usingnamespace @import("./module.zig"); diff --git a/src/util/module.zig b/src/util/module.zig new file mode 100644 index 0000000000000000000000000000000000000000..5ab2d224390b74270e959d41dba54f668624fd22 --- /dev/null +++ b/src/util/module.zig @@ -0,0 +1,12 @@ +const std = @import("std"); + +// +// + +pub const Module = struct { + name: []const u8, + main: []const u8, + + deps: []Module, + clean_path: []const u8, +};