authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-11-24 00:57:57 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-11-24 00:57:57 -08:00
log5a69da98a707e3e638af7204e6dbb80bef0b186c
tree842673b28c35610d635645c265914179dbed72f6
parent0df96b60dee2a9e76bf12ad8b8ab1d47dba52994

util: drop does_folder_exist


4 files changed, 10 insertions(+), 23 deletions(-)

src/cmd/fetch.zig+2-1
......@@ -1,6 +1,7 @@
11const std = @import("std");
22const string = []const u8;
33const ansi = @import("ansi");
4const extras = @import("extras");
45
56const zigmod = @import("../lib.zig");
67const u = @import("./../util/index.zig");
......@@ -201,7 +202,7 @@ const DiffChange = struct {
201202fn diff_lockfile(alloc: std.mem.Allocator) !void {
202203 const max = std.math.maxInt(usize);
203204
204 if (try u.does_folder_exist(".git")) {
205 if (try extras.doesFolderExist(null, ".git")) {
205206 const result = try u.run_cmd_raw(alloc, null, &.{ "git", "diff", "zigmod.lock" });
206207 var stdout = std.io.fixedBufferStream(result.stdout);
207208 const r = stdout.reader();
src/cmd/init.zig+2-2
......@@ -135,7 +135,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {
135135 }
136136
137137 // ask about .gitignore
138 if (try u.does_folder_exist(".git")) {
138 if (try extras.doesFolderExist(null, ".git")) {
139139 const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitignore?", gpa);
140140 if (do) {
141141 const exists = try extras.doesFileExist(null, ".gitignore");
......@@ -155,7 +155,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {
155155 }
156156
157157 // ask about .gitattributes
158 if (try u.does_folder_exist(".git")) {
158 if (try extras.doesFolderExist(null, ".git")) {
159159 const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitattributes?", gpa);
160160 if (do) {
161161 const exists = try extras.doesFileExist(null, ".gitattributes");
src/common.zig+6-6
......@@ -136,7 +136,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
136136 u.fail("fetch: git: version type '{s}' is invalid.", .{vtype});
137137 },
138138 };
139 if (try u.does_folder_exist(pv)) {
139 if (try extras.doesFolderExist(null, pv)) {
140140 if (vers.id == .branch) {
141141 if (options.update) {
142142 try d.type.update(options.alloc, pv, d.path);
......@@ -158,7 +158,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
158158 try setTreeReadOnly(pvd, options.alloc);
159159 return pv;
160160 }
161 if (!try u.does_folder_exist(p)) {
161 if (!try extras.doesFolderExist(null, p)) {
162162 try d.type.pull(options.alloc, d.path, p);
163163 } else {
164164 if (options.update) {
......@@ -168,7 +168,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
168168 return p;
169169 },
170170 .hg => {
171 if (!try u.does_folder_exist(p)) {
171 if (!try extras.doesFolderExist(null, p)) {
172172 try d.type.pull(options.alloc, d.path, p);
173173 } else {
174174 if (options.update) {
......@@ -178,12 +178,12 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
178178 return p;
179179 },
180180 .http => {
181 if (try u.does_folder_exist(pv)) {
181 if (try extras.doesFolderExist(null, pv)) {
182182 return pv;
183183 }
184184 const file_name = try u.last(try u.split(options.alloc, d.path, "/"));
185185 if (d.version.len > 0) {
186 if (try u.does_folder_exist(pv)) {
186 if (try extras.doesFolderExist(null, pv)) {
187187 return pv;
188188 }
189189 const file_path = try std.fs.path.join(options.alloc, &.{ pv, file_name });
......@@ -199,7 +199,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
199199 u.fail("{s} does not match hash {s}", .{ d.path, d.version });
200200 return p;
201201 }
202 if (try u.does_folder_exist(p)) {
202 if (try extras.doesFolderExist(null, p)) {
203203 try std.fs.cwd().deleteTree(p);
204204 }
205205 const file_path = try std.fs.path.resolve(options.alloc, &.{ p, file_name });
src/util/funcs.zig-14
......@@ -46,20 +46,6 @@ pub fn split(alloc: std.mem.Allocator, in: string, delim: string) ![]string {
4646 return list.toOwnedSlice();
4747}
4848
49pub fn does_folder_exist(fpath: string) !bool {
50 const file = std.fs.cwd().openFile(fpath, .{}) catch |e| switch (e) {
51 error.FileNotFound => return false,
52 error.IsDir => return true,
53 else => |ee| return ee,
54 };
55 defer file.close();
56 const s = try file.stat();
57 if (s.kind != .directory) {
58 return false;
59 }
60 return true;
61}
62
6349pub fn trim_suffix(in: string, suffix: string) string {
6450 if (std.mem.endsWith(u8, in, suffix)) {
6551 return in[0 .. in.len - suffix.len];