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 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const ansi = @import("ansi");3const ansi = @import("ansi");
4const extras = @import("extras");
45
5const zigmod = @import("../lib.zig");6const zigmod = @import("../lib.zig");
6const u = @import("./../util/index.zig");7const u = @import("./../util/index.zig");
...@@ -201,7 +202,7 @@ const DiffChange = struct {...@@ -201,7 +202,7 @@ const DiffChange = struct {
201fn diff_lockfile(alloc: std.mem.Allocator) !void {202fn diff_lockfile(alloc: std.mem.Allocator) !void {
202 const max = std.math.maxInt(usize);203 const max = std.math.maxInt(usize);
203204
204 if (try u.does_folder_exist(".git")) {205 if (try extras.doesFolderExist(null, ".git")) {
205 const result = try u.run_cmd_raw(alloc, null, &.{ "git", "diff", "zigmod.lock" });206 const result = try u.run_cmd_raw(alloc, null, &.{ "git", "diff", "zigmod.lock" });
206 var stdout = std.io.fixedBufferStream(result.stdout);207 var stdout = std.io.fixedBufferStream(result.stdout);
207 const r = stdout.reader();208 const r = stdout.reader();
src/cmd/init.zig+2-2
...@@ -135,7 +135,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {...@@ -135,7 +135,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {
135 }135 }
136136
137 // ask about .gitignore137 // ask about .gitignore
138 if (try u.does_folder_exist(".git")) {138 if (try extras.doesFolderExist(null, ".git")) {
139 const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitignore?", gpa);139 const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitignore?", gpa);
140 if (do) {140 if (do) {
141 const exists = try extras.doesFileExist(null, ".gitignore");141 const exists = try extras.doesFileExist(null, ".gitignore");
...@@ -155,7 +155,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {...@@ -155,7 +155,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {
155 }155 }
156156
157 // ask about .gitattributes157 // ask about .gitattributes
158 if (try u.does_folder_exist(".git")) {158 if (try extras.doesFolderExist(null, ".git")) {
159 const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitattributes?", gpa);159 const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitattributes?", gpa);
160 if (do) {160 if (do) {
161 const exists = try extras.doesFileExist(null, ".gitattributes");161 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) !...@@ -136,7 +136,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
136 u.fail("fetch: git: version type '{s}' is invalid.", .{vtype});136 u.fail("fetch: git: version type '{s}' is invalid.", .{vtype});
137 },137 },
138 };138 };
139 if (try u.does_folder_exist(pv)) {139 if (try extras.doesFolderExist(null, pv)) {
140 if (vers.id == .branch) {140 if (vers.id == .branch) {
141 if (options.update) {141 if (options.update) {
142 try d.type.update(options.alloc, pv, d.path);142 try d.type.update(options.alloc, pv, d.path);
...@@ -158,7 +158,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !...@@ -158,7 +158,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
158 try setTreeReadOnly(pvd, options.alloc);158 try setTreeReadOnly(pvd, options.alloc);
159 return pv;159 return pv;
160 }160 }
161 if (!try u.does_folder_exist(p)) {161 if (!try extras.doesFolderExist(null, p)) {
162 try d.type.pull(options.alloc, d.path, p);162 try d.type.pull(options.alloc, d.path, p);
163 } else {163 } else {
164 if (options.update) {164 if (options.update) {
...@@ -168,7 +168,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !...@@ -168,7 +168,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
168 return p;168 return p;
169 },169 },
170 .hg => {170 .hg => {
171 if (!try u.does_folder_exist(p)) {171 if (!try extras.doesFolderExist(null, p)) {
172 try d.type.pull(options.alloc, d.path, p);172 try d.type.pull(options.alloc, d.path, p);
173 } else {173 } else {
174 if (options.update) {174 if (options.update) {
...@@ -178,12 +178,12 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !...@@ -178,12 +178,12 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
178 return p;178 return p;
179 },179 },
180 .http => {180 .http => {
181 if (try u.does_folder_exist(pv)) {181 if (try extras.doesFolderExist(null, pv)) {
182 return pv;182 return pv;
183 }183 }
184 const file_name = try u.last(try u.split(options.alloc, d.path, "/"));184 const file_name = try u.last(try u.split(options.alloc, d.path, "/"));
185 if (d.version.len > 0) {185 if (d.version.len > 0) {
186 if (try u.does_folder_exist(pv)) {186 if (try extras.doesFolderExist(null, pv)) {
187 return pv;187 return pv;
188 }188 }
189 const file_path = try std.fs.path.join(options.alloc, &.{ pv, file_name });189 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) !...@@ -199,7 +199,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
199 u.fail("{s} does not match hash {s}", .{ d.path, d.version });199 u.fail("{s} does not match hash {s}", .{ d.path, d.version });
200 return p;200 return p;
201 }201 }
202 if (try u.does_folder_exist(p)) {202 if (try extras.doesFolderExist(null, p)) {
203 try std.fs.cwd().deleteTree(p);203 try std.fs.cwd().deleteTree(p);
204 }204 }
205 const file_path = try std.fs.path.resolve(options.alloc, &.{ p, file_name });205 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 {...@@ -46,20 +46,6 @@ pub fn split(alloc: std.mem.Allocator, in: string, delim: string) ![]string {
46 return list.toOwnedSlice();46 return list.toOwnedSlice();
47}47}
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
63pub fn trim_suffix(in: string, suffix: string) string {49pub fn trim_suffix(in: string, suffix: string) string {
64 if (std.mem.endsWith(u8, in, suffix)) {50 if (std.mem.endsWith(u8, in, suffix)) {
65 return in[0 .. in.len - suffix.len];51 return in[0 .. in.len - suffix.len];