authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-07-07 12:54:18 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-07-07 12:54:18 -07:00
log4bb41fdcee156ecc69fdac3b21a59ac6b1ac6ce9
treecd8a5e9d236b61b5f87e631bf925f147034eacb3
parent7395668d0b99e4702a35823da1a188afc3d4c409

cmd/fetch- deduplicate dependency list when downloading


6 files changed, 34 insertions(+), 15 deletions(-)

src/cmd/ci.zig+3-2
...@@ -12,11 +12,12 @@ pub fn execute(args: [][]u8) !void {...@@ -12,11 +12,12 @@ pub fn execute(args: [][]u8) !void {
1212
13 const dir = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });13 const dir = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });
1414
15 const top_module = try common.collect_deps_deep(dir, "zig.mod", .{15 var options = common.CollectOptions{
16 .log = true,16 .log = true,
17 .update = false,17 .update = false,
18 .lock = try common.parse_lockfile("zigmod.lock"),18 .lock = try common.parse_lockfile("zigmod.lock"),
19 });19 };
20 const top_module = try common.collect_deps_deep(dir, "zig.mod", &options);
2021
21 const list = &std.ArrayList(u.Module).init(gpa);22 const list = &std.ArrayList(u.Module).init(gpa);
22 try common.collect_pkgs(top_module, list);23 try common.collect_pkgs(top_module, list);
src/cmd/fetch.zig+3-2
...@@ -14,10 +14,11 @@ pub fn execute(args: [][]u8) !void {...@@ -14,10 +14,11 @@ pub fn execute(args: [][]u8) !void {
14 const dir = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });14 const dir = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });
15 const should_update = !(args.len >= 1 and std.mem.eql(u8, args[0], "--no-update"));15 const should_update = !(args.len >= 1 and std.mem.eql(u8, args[0], "--no-update"));
1616
17 const top_module = try common.collect_deps_deep(dir, "zig.mod", .{17 var options = common.CollectOptions{
18 .log = should_update,18 .log = should_update,
19 .update = should_update,19 .update = should_update,
20 });20 };
21 const top_module = try common.collect_deps_deep(dir, "zig.mod", &options);
2122
22 const list = &std.ArrayList(u.Module).init(gpa);23 const list = &std.ArrayList(u.Module).init(gpa);
23 try common.collect_pkgs(top_module, list);24 try common.collect_pkgs(top_module, list);
src/cmd/license.zig+3-2
...@@ -19,10 +19,11 @@ pub fn execute(args: [][]u8) !void {...@@ -19,10 +19,11 @@ pub fn execute(args: [][]u8) !void {
1919
20 const dir = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });20 const dir = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });
2121
22 const top_module = try common.collect_deps_deep(dir, "zig.mod", .{22 var options = common.CollectOptions{
23 .log = false,23 .log = false,
24 .update = false,24 .update = false,
25 });25 };
26 const top_module = try common.collect_deps_deep(dir, "zig.mod", &options);
2627
27 const master_list = &List.init(gpa);28 const master_list = &List.init(gpa);
28 try common.collect_pkgs(top_module, master_list);29 try common.collect_pkgs(top_module, master_list);
src/cmd/sum.zig+3-2
...@@ -14,10 +14,11 @@ pub fn execute(args: [][]u8) !void {...@@ -14,10 +14,11 @@ pub fn execute(args: [][]u8) !void {
1414
15 const dir = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });15 const dir = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });
1616
17 const top_module = try common.collect_deps_deep(dir, "zig.mod", .{17 var options = common.CollectOptions{
18 .log = false,18 .log = false,
19 .update = false,19 .update = false,
20 });20 };
21 const top_module = try common.collect_deps_deep(dir, "zig.mod", &options);
2122
22 //23 //
23 const f = try std.fs.cwd().createFile("zigmod.sum", .{});24 const f = try std.fs.cwd().createFile("zigmod.sum", .{});
src/common.zig+21-6
...@@ -3,6 +3,7 @@ const gpa = std.heap.c_allocator;...@@ -3,6 +3,7 @@ const gpa = std.heap.c_allocator;
33
4const u = @import("./util/index.zig");4const u = @import("./util/index.zig");
5const yaml = @import("./util/yaml.zig");5const yaml = @import("./util/yaml.zig");
6const string = []const u8;
67
7//8//
8//9//
...@@ -11,10 +12,18 @@ pub const CollectOptions = struct {...@@ -11,10 +12,18 @@ pub const CollectOptions = struct {
11 log: bool,12 log: bool,
12 update: bool,13 update: bool,
13 lock: ?[]const [4][]const u8 = null,14 lock: ?[]const [4][]const u8 = null,
15 alloc: *std.mem.Allocator = gpa,
16 already_fetched: *std.ArrayList(string) = undefined,
17
18 pub fn init(self: *CollectOptions) !void {
19 self.already_fetched = try self.alloc.create(std.ArrayList(string));
20 self.already_fetched.* = std.ArrayList(string).init(self.alloc);
21 }
14};22};
1523
16pub fn collect_deps_deep(dir: []const u8, mpath: []const u8, options: CollectOptions) !u.Module {24pub fn collect_deps_deep(dir: []const u8, mpath: []const u8, options: *CollectOptions) !u.Module {
17 const m = try u.ModFile.init(gpa, mpath);25 const m = try u.ModFile.init(gpa, mpath);
26 try options.init();
18 const moduledeps = &std.ArrayList(u.Module).init(gpa);27 const moduledeps = &std.ArrayList(u.Module).init(gpa);
19 defer moduledeps.deinit();28 defer moduledeps.deinit();
20 try std.fs.cwd().makePath(".zigmod/deps/files");29 try std.fs.cwd().makePath(".zigmod/deps/files");
...@@ -39,7 +48,7 @@ pub fn collect_deps_deep(dir: []const u8, mpath: []const u8, options: CollectOpt...@@ -39,7 +48,7 @@ pub fn collect_deps_deep(dir: []const u8, mpath: []const u8, options: CollectOpt
39 };48 };
40}49}
4150
42pub fn collect_deps(dir: []const u8, mpath: []const u8, options: CollectOptions) anyerror!u.Module {51pub fn collect_deps(dir: []const u8, mpath: []const u8, options: *CollectOptions) anyerror!u.Module {
43 const m = try u.ModFile.init(gpa, mpath);52 const m = try u.ModFile.init(gpa, mpath);
44 const moduledeps = &std.ArrayList(u.Module).init(gpa);53 const moduledeps = &std.ArrayList(u.Module).init(gpa);
45 defer moduledeps.deinit();54 defer moduledeps.deinit();
...@@ -76,9 +85,13 @@ pub fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void...@@ -76,9 +85,13 @@ pub fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void
76 }85 }
77}86}
7887
79fn get_moddir(basedir: []const u8, d: u.Dep, parent_name: []const u8, options: CollectOptions) ![]const u8 {88fn get_moddir(basedir: []const u8, d: u.Dep, parent_name: []const u8, options: *CollectOptions) ![]const u8 {
80 const p = try std.fs.path.join(gpa, &.{ basedir, try d.clean_path() });89 const p = try std.fs.path.join(gpa, &.{ basedir, try d.clean_path() });
81 const pv = try std.fs.path.join(gpa, &.{ basedir, try d.clean_path_v() });90 const pv = try std.fs.path.join(gpa, &.{ basedir, try d.clean_path_v() });
91
92 if (u.list_contains(options.already_fetched.items, p)) return p;
93 if (u.list_contains(options.already_fetched.items, pv)) return pv;
94
82 const tempdir = try std.fs.path.join(gpa, &.{ basedir, "temp" });95 const tempdir = try std.fs.path.join(gpa, &.{ basedir, "temp" });
83 if (options.log and d.type != .local) {96 if (options.log and d.type != .local) {
84 u.print("fetch: {s}: {s}: {s}", .{ parent_name, @tagName(d.type), d.path });97 u.print("fetch: {s}: {s}: {s}", .{ parent_name, @tagName(d.type), d.path });
...@@ -174,7 +187,7 @@ fn get_moddir(basedir: []const u8, d: u.Dep, parent_name: []const u8, options: C...@@ -174,7 +187,7 @@ fn get_moddir(basedir: []const u8, d: u.Dep, parent_name: []const u8, options: C
174 }187 }
175}188}
176189
177pub fn get_module_from_dep(d: *u.Dep, dir: []const u8, parent_name: []const u8, options: CollectOptions) anyerror!?u.Module {190pub fn get_module_from_dep(d: *u.Dep, dir: []const u8, parent_name: []const u8, options: *CollectOptions) anyerror!?u.Module {
178 if (options.lock) |lock| {191 if (options.lock) |lock| {
179 for (lock) |item| {192 for (lock) |item| {
180 if (std.mem.eql(u8, item[0], try d.clean_path())) {193 if (std.mem.eql(u8, item[0], try d.clean_path())) {
...@@ -186,6 +199,7 @@ pub fn get_module_from_dep(d: *u.Dep, dir: []const u8, parent_name: []const u8,...@@ -186,6 +199,7 @@ pub fn get_module_from_dep(d: *u.Dep, dir: []const u8, parent_name: []const u8,
186 }199 }
187 }200 }
188 const moddir = try get_moddir(dir, d.*, parent_name, options);201 const moddir = try get_moddir(dir, d.*, parent_name, options);
202 try options.already_fetched.append(moddir);
189 switch (d.type) {203 switch (d.type) {
190 .system_lib => {204 .system_lib => {
191 return u.Module{205 return u.Module{
...@@ -286,10 +300,11 @@ fn add_files_package(pkg_name: []const u8, dirs: []const []const u8, parent_name...@@ -286,10 +300,11 @@ fn add_files_package(pkg_name: []const u8, dirs: []const []const u8, parent_name
286 .yaml = null,300 .yaml = null,
287 .deps = &.{},301 .deps = &.{},
288 };302 };
289 return (try get_module_from_dep(&d, destination, parent_name, .{303 var options = CollectOptions{
290 .log = false,304 .log = false,
291 .update = false,305 .update = false,
292 })).?;306 };
307 return (try get_module_from_dep(&d, destination, parent_name, &options)).?;
293}308}
294309
295pub fn parse_lockfile(path: []const u8) ![]const [4][]const u8 {310pub fn parse_lockfile(path: []const u8) ![]const [4][]const u8 {
src/util/module.zig+1-1
...@@ -24,7 +24,7 @@ pub const Module = struct {...@@ -24,7 +24,7 @@ pub const Module = struct {
24 clean_path: []const u8,24 clean_path: []const u8,
25 dep: ?u.Dep,25 dep: ?u.Dep,
2626
27 pub fn from(dep: u.Dep, dir: []const u8, options: common.CollectOptions) !Module {27 pub fn from(dep: u.Dep, dir: []const u8, options: *common.CollectOptions) !Module {
28 const moddeps = &std.ArrayList(Module).init(gpa);28 const moddeps = &std.ArrayList(Module).init(gpa);
29 defer moddeps.deinit();29 defer moddeps.deinit();
30 for (dep.deps) |*d| {30 for (dep.deps) |*d| {