authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-09-18 11:11:05 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-09-18 11:11:05 -07:00
log67caed05c15bee558beb29dd5f33d38cac874e89
treeca7e3a29b230ff2a9a7440db0f9c94d70a353bb3
parentd2169e0ca9f2477017df633e2018d95986c696bb

dry: u.does_file_exist -> extras.doesFileExist


5 files changed, 15 insertions(+), 22 deletions(-)

src/cmd/aquila/install.zig+2-1
......@@ -2,6 +2,7 @@ const std = @import("std");
22const string = []const u8;
33const gpa = std.heap.c_allocator;
44const knownfolders = @import("known-folders");
5const extras = @import("extras");
56
67const zigmod = @import("../../lib.zig");
78const u = @import("./../../util/index.zig");
......@@ -12,7 +13,7 @@ pub fn execute(args: [][]u8) !void {
1213 const homepath = home.?;
1314 const homedir = try std.fs.cwd().openDir(homepath, .{});
1415
15 if (!(try u.does_file_exist(homedir, "zigmod.yml"))) {
16 if (!(try extras.doesFileExist(homedir, "zigmod.yml"))) {
1617 const f = try homedir.createFile("zigmod.yml", .{});
1718 defer f.close();
1819 const w = f.writer();
src/cmd/aquila/update.zig+2-1
......@@ -2,6 +2,7 @@ const std = @import("std");
22const string = []const u8;
33const gpa = std.heap.c_allocator;
44const knownfolders = @import("known-folders");
5const extras = @import("extras");
56
67const zigmod = @import("../../lib.zig");
78const u = @import("./../../util/index.zig");
......@@ -12,7 +13,7 @@ pub fn execute(args: [][]u8) !void {
1213 const homepath = home.?;
1314 const homedir = try std.fs.cwd().openDir(homepath, .{});
1415
15 if (!(try u.does_file_exist(homedir, "zigmod.yml"))) {
16 if (!(try extras.doesFileExist(homedir, "zigmod.yml"))) {
1617 const f = try homedir.createFile("zigmod.yml", .{});
1718 defer f.close();
1819 const w = f.writer();
src/cmd/init.zig+5-4
......@@ -6,6 +6,7 @@ const detectlicense = @import("detect-license");
66const knownfolders = @import("known-folders");
77const ini = @import("ini");
88const time = @import("time");
9const extras = @import("extras");
910
1011const u = @import("./../util/index.zig");
1112
......@@ -72,7 +73,7 @@ pub fn execute(args: [][]u8) !void {
7273 }
7374
7475 // ask about LICENSE
75 if (!(try u.does_file_exist(null, "LICENSE"))) {
76 if (!(try extras.doesFileExist(null, "LICENSE"))) {
7677 if (detectlicense.licenses.find(license)) |text| {
7778 if (try inquirer.forConfirm(stdout, stdin, "It appears you don't have a LICENSE file defined, would you like init to add it for you?", gpa)) {
7879 var realtext = text;
......@@ -134,7 +135,7 @@ pub fn execute(args: [][]u8) !void {
134135 if (try u.does_folder_exist(".git")) {
135136 const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitignore?", gpa);
136137 if (do) {
137 const exists = try u.does_file_exist(null, ".gitignore");
138 const exists = try extras.doesFileExist(null, ".gitignore");
138139 const file: std.fs.File = try (if (exists) cwd.openFile(".gitignore", .{ .mode = .read_write }) else cwd.createFile(".gitignore", .{}));
139140 defer file.close();
140141 const len = try file.getEndPos();
......@@ -153,7 +154,7 @@ pub fn execute(args: [][]u8) !void {
153154 if (try u.does_folder_exist(".git")) {
154155 const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitattributes?", gpa);
155156 if (do) {
156 const exists = try u.does_file_exist(null, ".gitattributes");
157 const exists = try extras.doesFileExist(null, ".gitattributes");
157158 const file: std.fs.File = try (if (exists) cwd.openFile(".gitattributes", .{ .mode = .read_write }) else cwd.createFile(".gitattributes", .{}));
158159 defer file.close();
159160 const len = try file.getEndPos();
......@@ -188,7 +189,7 @@ pub fn writeLibManifest(w: std.fs.File.Writer, id: string, name: string, entry:
188189
189190fn guessCopyrightName() !?string {
190191 const home = (try knownfolders.open(gpa, .home, .{})).?;
191 if (!(try u.does_file_exist(home, ".gitconfig"))) return null;
192 if (!(try extras.doesFileExist(home, ".gitconfig"))) return null;
192193 const file = try home.openFile(".gitconfig", .{});
193194 const content = try file.reader().readAllAlloc(gpa, 1024 * 1024);
194195 var iniO = try ini.parseIntoMap(content, gpa);
src/common.zig+1-1
......@@ -346,7 +346,7 @@ pub fn add_files_package(alloc: std.mem.Allocator, cachepath: string, pkg_name:
346346pub fn parse_lockfile(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]const [4]string {
347347 var list = std.ArrayList([4]string).init(alloc);
348348 const max = std.math.maxInt(usize);
349 if (!try u.does_file_exist(dir, "zigmod.lock")) return &[_][4]string{};
349 if (!try extras.doesFileExist(dir, "zigmod.lock")) return &[_][4]string{};
350350 const f = try dir.openFile("zigmod.lock", .{});
351351 const r = f.reader();
352352 var i: usize = 0;
src/util/funcs.zig+5-15
......@@ -45,16 +45,6 @@ pub fn split(alloc: std.mem.Allocator, in: string, delim: string) ![]string {
4545 return list.toOwnedSlice();
4646}
4747
48pub fn does_file_exist(dir: ?std.fs.Dir, fpath: string) !bool {
49 const file = (dir orelse std.fs.cwd()).openFile(fpath, .{}) catch |e| switch (e) {
50 error.FileNotFound => return false,
51 error.IsDir => return true,
52 else => return e,
53 };
54 defer file.close();
55 return true;
56}
57
5848pub fn does_folder_exist(fpath: string) !bool {
5949 const file = std.fs.cwd().openFile(fpath, .{}) catch |e| switch (e) {
6050 error.FileNotFound => return false,
......@@ -243,7 +233,7 @@ pub fn detect_pkgname(alloc: std.mem.Allocator, override: string, dir: string) !
243233 return override;
244234 }
245235 const dirO = if (dir.len == 0) std.fs.cwd() else try std.fs.cwd().openDir(dir, .{});
246 if (!(try does_file_exist(dirO, "build.zig"))) {
236 if (!(try extras.doesFileExist(dirO, "build.zig"))) {
247237 return error.NoBuildZig;
248238 }
249239 const dpath = try std.fs.realpathAlloc(alloc, try std.fs.path.join(alloc, &.{ dir, "build.zig" }));
......@@ -256,20 +246,20 @@ pub fn detect_pkgname(alloc: std.mem.Allocator, override: string, dir: string) !
256246
257247pub fn detct_mainfile(alloc: std.mem.Allocator, override: string, dir: ?std.fs.Dir, name: string) !string {
258248 if (override.len > 0) {
259 if (try does_file_exist(dir, override)) {
249 if (try extras.doesFileExist(dir, override)) {
260250 if (std.mem.endsWith(u8, override, ".zig")) {
261251 return override;
262252 }
263253 }
264254 }
265255 const namedotzig = try std.mem.concat(alloc, u8, &.{ name, ".zig" });
266 if (try does_file_exist(dir, namedotzig)) {
256 if (try extras.doesFileExist(dir, namedotzig)) {
267257 return namedotzig;
268258 }
269 if (try does_file_exist(dir, try std.fs.path.join(alloc, &.{ "src", "lib.zig" }))) {
259 if (try extras.doesFileExist(dir, try std.fs.path.join(alloc, &.{ "src", "lib.zig" }))) {
270260 return "src/lib.zig";
271261 }
272 if (try does_file_exist(dir, try std.fs.path.join(alloc, &.{ "src", "main.zig" }))) {
262 if (try extras.doesFileExist(dir, try std.fs.path.join(alloc, &.{ "src", "main.zig" }))) {
273263 return "src/main.zig";
274264 }
275265 return error.CantFindMain;