From 09d633b1405c5cbd4e02f4c86cb34f0368bf321f Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 24 Nov 2023 00:17:14 -0800 Subject: [PATCH] cmd: when running zigmod inside deps folder, use parent deps folder as cachepath --- src/cmd/ci.zig | 3 ++- src/cmd/fetch.zig | 3 +-- src/cmd/generate.zig | 2 +- src/cmd/license.zig | 3 ++- src/cmd/sum.zig | 3 ++- src/util/funcs.zig | 11 +++++++++++ 6 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/cmd/ci.zig b/src/cmd/ci.zig index d27b3d0caa2b39b904f92728bfeacfd66605c48a..49979be23481205d03457a272ad6aaf7e07769ca 100644 --- a/src/cmd/ci.zig +++ b/src/cmd/ci.zig @@ -2,6 +2,7 @@ const std = @import("std"); const string = []const u8; const zigmod = @import("../lib.zig"); +const u = @import("./../util/index.zig"); const common = @import("./../common.zig"); // Inspired by: @@ -12,7 +13,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void { _ = args; const gpa = std.heap.c_allocator; - const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" }); + const cachepath = try u.find_cachepath(); const dir = std.fs.cwd(); try do(gpa, cachepath, dir); } diff --git a/src/cmd/fetch.zig b/src/cmd/fetch.zig index 4af122f4afc250e43b240e2ee4d4204b1a2c3265..08233f0ba0eec2b43d78881ac408bbb19eaaf90d 100644 --- a/src/cmd/fetch.zig +++ b/src/cmd/fetch.zig @@ -13,9 +13,8 @@ const license = @import("./license.zig"); pub fn execute(self_name: []const u8, args: [][]u8) !void { _ = self_name; - // const gpa = std.heap.c_allocator; - const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" }); + const cachepath = try u.find_cachepath(); const dir = std.fs.cwd(); const should_update = !(args.len >= 1 and std.mem.eql(u8, args[0], "--no-update")); diff --git a/src/cmd/generate.zig b/src/cmd/generate.zig index d0282e7048e0ee9667d9ee2fb583e09023988f13..5dcf4363ff5d085d6810c6cac8225987853ab4da 100644 --- a/src/cmd/generate.zig +++ b/src/cmd/generate.zig @@ -14,7 +14,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void { // const gpa = std.heap.c_allocator; - const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" }); + const cachepath = try u.find_cachepath(); const dir = std.fs.cwd(); var options = common.CollectOptions{ diff --git a/src/cmd/license.zig b/src/cmd/license.zig index a3ae38dc96478b4ee4250c7c90c27acd19b24592..9ba4d58b39bafb40977a6ef7b04a5a11f3dd5a05 100644 --- a/src/cmd/license.zig +++ b/src/cmd/license.zig @@ -6,6 +6,7 @@ const licenses = @import("licenses"); const extras = @import("extras"); const zigmod = @import("../lib.zig"); +const u = @import("./../util/index.zig"); const common = @import("./../common.zig"); const List = std.ArrayList(zigmod.Module); @@ -18,7 +19,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void { _ = self_name; _ = args; - const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" }); + const cachepath = try u.find_cachepath(); const dir = std.fs.cwd(); var options = common.CollectOptions{ diff --git a/src/cmd/sum.zig b/src/cmd/sum.zig index 6e550663a28adbc5ef3f6e4608541cd5d3b705e7..da91051c127e5fd0bdf61c983867a789d9a47397 100644 --- a/src/cmd/sum.zig +++ b/src/cmd/sum.zig @@ -2,6 +2,7 @@ const std = @import("std"); const gpa = std.heap.c_allocator; const zigmod = @import("../lib.zig"); +const u = @import("./../util/index.zig"); const common = @import("./../common.zig"); // @@ -11,7 +12,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void { _ = self_name; _ = args; - const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" }); + const cachepath = try u.find_cachepath(); const dir = std.fs.cwd(); var options = common.CollectOptions{ diff --git a/src/util/funcs.zig b/src/util/funcs.zig index df25a417eae4ef877aeeaf2d7a30d5b792d7a82d..11f6e007bb2b1cd38511945736ef621f37fbf00f 100644 --- a/src/util/funcs.zig +++ b/src/util/funcs.zig @@ -1,5 +1,6 @@ const std = @import("std"); const string = []const u8; +const gpa = std.heap.c_allocator; const extras = @import("extras"); const u = @import("index.zig"); @@ -250,3 +251,13 @@ pub fn indexOfAfter(haystack: string, needle: u8, after: usize) ?usize { } return null; } + +pub fn find_cachepath() !string { + const haystack = try std.fs.cwd().realpathAlloc(gpa, "."); + const needle = "/.zigmod/deps"; + + if (std.mem.indexOf(u8, haystack, needle)) |index| { + return haystack[0 .. index + needle.len]; + } + return try std.fs.path.join(gpa, &.{ ".zigmod", "deps" }); +} -- 2.54.0