From 426ada3d7ce5491308a61eb366863ae236b19661 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sat, 21 Aug 2021 01:29:20 -0700 Subject: [PATCH] common- refactor collect_deps to accept a Dir instead of a path --- src/cmd/ci.zig | 4 ++-- src/cmd/fetch.zig | 3 ++- src/cmd/license.zig | 3 ++- src/cmd/sum.zig | 5 +++-- src/common.zig | 19 ++++++++++--------- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/cmd/ci.zig b/src/cmd/ci.zig index 203f463b68fcde7aa0b6c88624db4ca5d23290c9..ef65556eed6d430687a93e90ed4ae6b4785d97b7 100644 --- a/src/cmd/ci.zig +++ b/src/cmd/ci.zig @@ -10,15 +10,15 @@ const common = @import("./../common.zig"); pub fn execute(args: [][]u8) !void { _ = args; - const dir = std.fs.cwd(); const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" }); + const dir = std.fs.cwd(); var options = common.CollectOptions{ .log = true, .update = false, .lock = try common.parse_lockfile(dir), }; - const top_module = try common.collect_deps_deep(cachepath, "zig.mod", &options); + const top_module = try common.collect_deps_deep(cachepath, dir, &options); var list = std.ArrayList(u.Module).init(gpa); try common.collect_pkgs(top_module, &list); diff --git a/src/cmd/fetch.zig b/src/cmd/fetch.zig index 101b5d3d666d7836e60484a1edfa8fa405e022d0..ac5fea1840931940c59b91c7c77a75e32c26e3aa 100644 --- a/src/cmd/fetch.zig +++ b/src/cmd/fetch.zig @@ -18,13 +18,14 @@ const bootstrap = if (@hasDecl(build_options, "bootstrap")) build_options.bootst pub fn execute(args: [][]u8) !void { // const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" }); + const dir = std.fs.cwd(); const should_update = !(args.len >= 1 and std.mem.eql(u8, args[0], "--no-update")); var options = common.CollectOptions{ .log = should_update, .update = should_update, }; - const top_module = try common.collect_deps_deep(cachepath, "zig.mod", &options); + const top_module = try common.collect_deps_deep(cachepath, dir, &options); var list = std.ArrayList(u.Module).init(gpa); try common.collect_pkgs(top_module, &list); diff --git a/src/cmd/license.zig b/src/cmd/license.zig index 01bea77c2bf6a5957bdab9f2de0cad05fa3190b1..19e508bbda12a9e5621f8345803db6bdade0e798 100644 --- a/src/cmd/license.zig +++ b/src/cmd/license.zig @@ -18,12 +18,13 @@ pub fn execute(args: [][]u8) !void { _ = args; const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" }); + const dir = std.fs.cwd(); var options = common.CollectOptions{ .log = false, .update = false, }; - const top_module = try common.collect_deps_deep(cachepath, "zig.mod", &options); + const top_module = try common.collect_deps_deep(cachepath, dir, &options); const master_list = &List.init(gpa); try common.collect_pkgs(top_module, master_list); diff --git a/src/cmd/sum.zig b/src/cmd/sum.zig index 242817dda5c6fd4b23908f14d41c9ff5f19e3c5a..f4850f5db53b899039cceacd333b29524443d249 100644 --- a/src/cmd/sum.zig +++ b/src/cmd/sum.zig @@ -11,15 +11,16 @@ pub fn execute(args: [][]u8) !void { _ = args; const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" }); + const dir = std.fs.cwd(); var options = common.CollectOptions{ .log = false, .update = false, }; - const top_module = try common.collect_deps_deep(cachepath, "zig.mod", &options); + const top_module = try common.collect_deps_deep(cachepath, dir, &options); // - const f = try std.fs.cwd().createFile("zigmod.sum", .{}); + const f = try dir.createFile("zigmod.sum", .{}); defer f.close(); const w = f.writer(); diff --git a/src/common.zig b/src/common.zig index 9bb8eaef3ec72db384c4f5f19de153949780e24b..444da0e3055ff8343d8abf4b72115b648493e8a8 100644 --- a/src/common.zig +++ b/src/common.zig @@ -27,8 +27,8 @@ pub const CollectOptions = struct { } }; -pub fn collect_deps_deep(cachepath: []const u8, mpath: []const u8, options: *CollectOptions) !u.Module { - const m = try u.ModFile.init(gpa, mpath); +pub fn collect_deps_deep(cachepath: []const u8, mdir: std.fs.Dir, options: *CollectOptions) !u.Module { + const m = try u.ModFile.from_dir(gpa, mdir); try options.init(); var moduledeps = std.ArrayList(u.Module).init(gpa); defer moduledeps.deinit(); @@ -36,7 +36,7 @@ pub fn collect_deps_deep(cachepath: []const u8, mpath: []const u8, options: *Col try std.fs.cwd().makePath(".zigmod/deps/files"); try moduledeps.append(try add_files_package("root", m.root_files, m.name)); } - try moduledeps.append(try collect_deps(cachepath, mpath, options)); + try moduledeps.append(try collect_deps(cachepath, mdir, options)); for (m.devdeps) |*d| { if (try get_module_from_dep(d, cachepath, m.name, options)) |founddep| { try moduledeps.append(founddep); @@ -54,8 +54,8 @@ pub fn collect_deps_deep(cachepath: []const u8, mpath: []const u8, options: *Col }; } -pub fn collect_deps(cachepath: []const u8, mpath: []const u8, options: *CollectOptions) anyerror!u.Module { - const m = try u.ModFile.init(gpa, mpath); +pub fn collect_deps(cachepath: []const u8, mdir: std.fs.Dir, options: *CollectOptions) anyerror!u.Module { + const m = try u.ModFile.from_dir(gpa, mdir); var moduledeps = std.ArrayList(u.Module).init(gpa); defer moduledeps.deinit(); if (m.files.len > 0) { @@ -92,9 +92,9 @@ pub fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void } } -pub fn get_modpath(basedir: []const u8, d: u.Dep, parent_name: []const u8, options: *CollectOptions) ![]const u8 { - const p = try std.fs.path.join(gpa, &.{ basedir, try d.clean_path() }); - const pv = try std.fs.path.join(gpa, &.{ basedir, try d.clean_path_v() }); +pub fn get_modpath(cachepath: []const u8, d: u.Dep, parent_name: []const u8, options: *CollectOptions) ![]const u8 { + const p = try std.fs.path.join(gpa, &.{ cachepath, try d.clean_path() }); + const pv = try std.fs.path.join(gpa, &.{ cachepath, try d.clean_path_v() }); const nocache = d.type == .local or d.type == .system_lib; if (!nocache and u.list_contains(options.already_fetched.items, p)) return p; @@ -209,6 +209,7 @@ pub fn get_module_from_dep(d: *u.Dep, cachepath: []const u8, parent_name: []cons } } const modpath = try get_modpath(cachepath, d.*, parent_name, options); + const moddir = try std.fs.cwd().openDir(modpath, .{}); const nocache = d.type == .local or d.type == .system_lib; if (!nocache) try options.already_fetched.append(modpath); @@ -229,7 +230,7 @@ pub fn get_module_from_dep(d: *u.Dep, cachepath: []const u8, parent_name: []cons }; }, else => { - var dd = try collect_deps(cachepath, try u.concat(&.{ modpath, "/zig.mod" }), options) catch |e| switch (e) { + var dd = try collect_deps(cachepath, moddir, options) catch |e| switch (e) { error.FileNotFound => { if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) { var mod_from = try u.Module.from(d.*, cachepath, options); -- 2.54.0