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 {...@@ -12,7 +12,7 @@ pub fn execute(args: [][]u8) !void {
12 const home = try known_folders.getPath(gpa, .home);12 const home = try known_folders.getPath(gpa, .home);
13 const dir = try std.fmt.allocPrint(gpa, "{}{}", .{home, "/.cache/zigmod/deps"});13 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
17 //17 //
18 const f = try std.fs.cwd().createFile("./deps.zig", .{});18 const f = try std.fs.cwd().createFile("./deps.zig", .{});
...@@ -34,12 +34,13 @@ pub fn execute(args: [][]u8) !void {...@@ -34,12 +34,13 @@ pub fn execute(args: [][]u8) !void {
34 });34 });
35 try w.print("\n", .{});35 try w.print("\n", .{});
36 try w.print("pub const packages = ", .{});36 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);
38 try w.print(";\n", .{});38 try w.print(";\n", .{});
39}39}
4040
41fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!void {41fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
42 const m = try u.ModFile.init(gpa, mpath);42 const m = try u.ModFile.init(gpa, mpath);
43 const moduledeps = &std.ArrayList(u.Module).init(gpa);
43 for (m.deps) |d| {44 for (m.deps) |d| {
44 const p = try std.fmt.allocPrint(gpa, "{}{}{}", .{dir, "/", try d.clean_path()});45 const p = try std.fmt.allocPrint(gpa, "{}{}{}", .{dir, "/", try d.clean_path()});
45 switch (d.type) {46 switch (d.type) {
...@@ -56,10 +57,18 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!void {...@@ -56,10 +57,18 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!void {
56 }57 }
57 switch (d.type) {58 switch (d.type) {
58 else => {59 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);
60 },63 },
61 }64 }
62 }65 }
66 return u.Module{
67 .name = m.name,
68 .main = m.main,
69 .deps = moduledeps.items,
70 .clean_path = "",
71 };
63}72}
6473
65fn run_cmd(dir: ?[]const u8, args: []const []const u8) !void {74fn 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 {...@@ -71,7 +80,7 @@ fn run_cmd(dir: ?[]const u8, args: []const []const u8) !void {
71 };80 };
72}81}
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 {
75 if (m.deps.len == 0 and tabs > 0) {84 if (m.deps.len == 0 and tabs > 0) {
76 try w.print("null", .{});85 try w.print("null", .{});
77 return;86 return;
...@@ -80,16 +89,11 @@ fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.ModFile, tabs: i32) a...@@ -80,16 +89,11 @@ fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.ModFile, tabs: i32) a
80 const t = " ";89 const t = " ";
81 const r = try u.repeat(t, tabs);90 const r = try u.repeat(t, tabs);
82 for (m.deps) |d| {91 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
88 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,"build.Pkg{"})});92 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,"\","})});93 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".name = \"",d.name,"\","})});
90 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".path = cache ++ \"/",dcpath,"/",n.main,"\","})});94 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,t,".path = cache ++ \"/",d.clean_path,"/",d.main,"\","})});
91 try w.print("{}", .{try u.concat(&[_][]const u8{r,t,t,".dependencies = "})});95 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);
93 try w.print("{}\n", .{","});97 try w.print("{}\n", .{","});
94 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,"},"})});98 try w.print("{}\n", .{try u.concat(&[_][]const u8{r,t,"},"})});
95 }99 }
src/util/index.zig+1
...@@ -4,3 +4,4 @@ usingnamespace @import("./funcs.zig");...@@ -4,3 +4,4 @@ usingnamespace @import("./funcs.zig");
4usingnamespace @import("./dep_type.zig");4usingnamespace @import("./dep_type.zig");
5usingnamespace @import("./dep.zig");5usingnamespace @import("./dep.zig");
6usingnamespace @import("./modfile.zig");6usingnamespace @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};