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 {
1212
1313 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{
1616 .log = true,
1717 .update = false,
1818 .lock = try common.parse_lockfile("zigmod.lock"),
19 });
19 };
20 const top_module = try common.collect_deps_deep(dir, "zig.mod", &options);
2021
2122 const list = &std.ArrayList(u.Module).init(gpa);
2223 try common.collect_pkgs(top_module, list);
src/cmd/fetch.zig+3-2
......@@ -14,10 +14,11 @@ pub fn execute(args: [][]u8) !void {
1414 const dir = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });
1515 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{
1818 .log = should_update,
1919 .update = should_update,
20 });
20 };
21 const top_module = try common.collect_deps_deep(dir, "zig.mod", &options);
2122
2223 const list = &std.ArrayList(u.Module).init(gpa);
2324 try common.collect_pkgs(top_module, list);
src/cmd/license.zig+3-2
......@@ -19,10 +19,11 @@ pub fn execute(args: [][]u8) !void {
1919
2020 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{
2323 .log = false,
2424 .update = false,
25 });
25 };
26 const top_module = try common.collect_deps_deep(dir, "zig.mod", &options);
2627
2728 const master_list = &List.init(gpa);
2829 try common.collect_pkgs(top_module, master_list);
src/cmd/sum.zig+3-2
......@@ -14,10 +14,11 @@ pub fn execute(args: [][]u8) !void {
1414
1515 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{
1818 .log = false,
1919 .update = false,
20 });
20 };
21 const top_module = try common.collect_deps_deep(dir, "zig.mod", &options);
2122
2223 //
2324 const f = try std.fs.cwd().createFile("zigmod.sum", .{});
src/common.zig+21-6
......@@ -3,6 +3,7 @@ const gpa = std.heap.c_allocator;
33
44const u = @import("./util/index.zig");
55const yaml = @import("./util/yaml.zig");
6const string = []const u8;
67
78//
89//
......@@ -11,10 +12,18 @@ pub const CollectOptions = struct {
1112 log: bool,
1213 update: bool,
1314 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 }
1422};
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 {
1725 const m = try u.ModFile.init(gpa, mpath);
26 try options.init();
1827 const moduledeps = &std.ArrayList(u.Module).init(gpa);
1928 defer moduledeps.deinit();
2029 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
3948 };
4049}
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 {
4352 const m = try u.ModFile.init(gpa, mpath);
4453 const moduledeps = &std.ArrayList(u.Module).init(gpa);
4554 defer moduledeps.deinit();
......@@ -76,9 +85,13 @@ pub fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void
7685 }
7786}
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 {
8089 const p = try std.fs.path.join(gpa, &.{ basedir, try d.clean_path() });
8190 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
8295 const tempdir = try std.fs.path.join(gpa, &.{ basedir, "temp" });
8396 if (options.log and d.type != .local) {
8497 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
174187 }
175188}
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 {
178191 if (options.lock) |lock| {
179192 for (lock) |item| {
180193 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,
186199 }
187200 }
188201 const moddir = try get_moddir(dir, d.*, parent_name, options);
202 try options.already_fetched.append(moddir);
189203 switch (d.type) {
190204 .system_lib => {
191205 return u.Module{
......@@ -286,10 +300,11 @@ fn add_files_package(pkg_name: []const u8, dirs: []const []const u8, parent_name
286300 .yaml = null,
287301 .deps = &.{},
288302 };
289 return (try get_module_from_dep(&d, destination, parent_name, .{
303 var options = CollectOptions{
290304 .log = false,
291305 .update = false,
292 })).?;
306 };
307 return (try get_module_from_dep(&d, destination, parent_name, &options)).?;
293308}
294309
295310pub fn parse_lockfile(path: []const u8) ![]const [4][]const u8 {
src/util/module.zig+1-1
......@@ -24,7 +24,7 @@ pub const Module = struct {
2424 clean_path: []const u8,
2525 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 {
2828 const moddeps = &std.ArrayList(Module).init(gpa);
2929 defer moddeps.deinit();
3030 for (dep.deps) |*d| {