From 67caed05c15bee558beb29dd5f33d38cac874e89 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 18 Sep 2022 11:11:05 -0700 Subject: [PATCH] dry: u.does_file_exist -> extras.doesFileExist --- src/cmd/aquila/install.zig | 3 ++- src/cmd/aquila/update.zig | 3 ++- src/cmd/init.zig | 9 +++++---- src/common.zig | 2 +- src/util/funcs.zig | 20 +++++--------------- 5 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/cmd/aquila/install.zig b/src/cmd/aquila/install.zig index 65f5cbac85c4cf21907ca68ef02392b61dbe5f9e..41075079f56115138cbe9df6f942ebdc420a7add 100644 --- a/src/cmd/aquila/install.zig +++ b/src/cmd/aquila/install.zig @@ -2,6 +2,7 @@ const std = @import("std"); const string = []const u8; const gpa = std.heap.c_allocator; const knownfolders = @import("known-folders"); +const extras = @import("extras"); const zigmod = @import("../../lib.zig"); const u = @import("./../../util/index.zig"); @@ -12,7 +13,7 @@ pub fn execute(args: [][]u8) !void { const homepath = home.?; const homedir = try std.fs.cwd().openDir(homepath, .{}); - if (!(try u.does_file_exist(homedir, "zigmod.yml"))) { + if (!(try extras.doesFileExist(homedir, "zigmod.yml"))) { const f = try homedir.createFile("zigmod.yml", .{}); defer f.close(); const w = f.writer(); diff --git a/src/cmd/aquila/update.zig b/src/cmd/aquila/update.zig index 511e4664972784c027b82b05d2958e2853080d0d..45bcc89a751a36adce9e83ef14807c847d9a1051 100644 --- a/src/cmd/aquila/update.zig +++ b/src/cmd/aquila/update.zig @@ -2,6 +2,7 @@ const std = @import("std"); const string = []const u8; const gpa = std.heap.c_allocator; const knownfolders = @import("known-folders"); +const extras = @import("extras"); const zigmod = @import("../../lib.zig"); const u = @import("./../../util/index.zig"); @@ -12,7 +13,7 @@ pub fn execute(args: [][]u8) !void { const homepath = home.?; const homedir = try std.fs.cwd().openDir(homepath, .{}); - if (!(try u.does_file_exist(homedir, "zigmod.yml"))) { + if (!(try extras.doesFileExist(homedir, "zigmod.yml"))) { const f = try homedir.createFile("zigmod.yml", .{}); defer f.close(); const w = f.writer(); diff --git a/src/cmd/init.zig b/src/cmd/init.zig index 12bf2daec26045405fdaa051df6330de2e7e81c3..7f2bf6373a770df88ff5f870eac4bb0ec8c23c86 100644 --- a/src/cmd/init.zig +++ b/src/cmd/init.zig @@ -6,6 +6,7 @@ const detectlicense = @import("detect-license"); const knownfolders = @import("known-folders"); const ini = @import("ini"); const time = @import("time"); +const extras = @import("extras"); const u = @import("./../util/index.zig"); @@ -72,7 +73,7 @@ pub fn execute(args: [][]u8) !void { } // ask about LICENSE - if (!(try u.does_file_exist(null, "LICENSE"))) { + if (!(try extras.doesFileExist(null, "LICENSE"))) { if (detectlicense.licenses.find(license)) |text| { 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)) { var realtext = text; @@ -134,7 +135,7 @@ pub fn execute(args: [][]u8) !void { if (try u.does_folder_exist(".git")) { const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitignore?", gpa); if (do) { - const exists = try u.does_file_exist(null, ".gitignore"); + const exists = try extras.doesFileExist(null, ".gitignore"); const file: std.fs.File = try (if (exists) cwd.openFile(".gitignore", .{ .mode = .read_write }) else cwd.createFile(".gitignore", .{})); defer file.close(); const len = try file.getEndPos(); @@ -153,7 +154,7 @@ pub fn execute(args: [][]u8) !void { if (try u.does_folder_exist(".git")) { const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitattributes?", gpa); if (do) { - const exists = try u.does_file_exist(null, ".gitattributes"); + const exists = try extras.doesFileExist(null, ".gitattributes"); const file: std.fs.File = try (if (exists) cwd.openFile(".gitattributes", .{ .mode = .read_write }) else cwd.createFile(".gitattributes", .{})); defer file.close(); const len = try file.getEndPos(); @@ -188,7 +189,7 @@ pub fn writeLibManifest(w: std.fs.File.Writer, id: string, name: string, entry: fn guessCopyrightName() !?string { const home = (try knownfolders.open(gpa, .home, .{})).?; - if (!(try u.does_file_exist(home, ".gitconfig"))) return null; + if (!(try extras.doesFileExist(home, ".gitconfig"))) return null; const file = try home.openFile(".gitconfig", .{}); const content = try file.reader().readAllAlloc(gpa, 1024 * 1024); var iniO = try ini.parseIntoMap(content, gpa); diff --git a/src/common.zig b/src/common.zig index 74a395bff880a7bb804bb2c1e50de2e3979cc699..0fea28a9a286a79df078ef7885f69b4d465eb0de 100644 --- a/src/common.zig +++ b/src/common.zig @@ -346,7 +346,7 @@ pub fn add_files_package(alloc: std.mem.Allocator, cachepath: string, pkg_name: pub fn parse_lockfile(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]const [4]string { var list = std.ArrayList([4]string).init(alloc); const max = std.math.maxInt(usize); - if (!try u.does_file_exist(dir, "zigmod.lock")) return &[_][4]string{}; + if (!try extras.doesFileExist(dir, "zigmod.lock")) return &[_][4]string{}; const f = try dir.openFile("zigmod.lock", .{}); const r = f.reader(); var i: usize = 0; diff --git a/src/util/funcs.zig b/src/util/funcs.zig index dda8f4ee0bf52660a5808100874bd628f1d1cd04..8cb0b073c0e6412dab0c3f77fdc7ec748ffa0aba 100644 --- a/src/util/funcs.zig +++ b/src/util/funcs.zig @@ -45,16 +45,6 @@ pub fn split(alloc: std.mem.Allocator, in: string, delim: string) ![]string { return list.toOwnedSlice(); } -pub fn does_file_exist(dir: ?std.fs.Dir, fpath: string) !bool { - const file = (dir orelse std.fs.cwd()).openFile(fpath, .{}) catch |e| switch (e) { - error.FileNotFound => return false, - error.IsDir => return true, - else => return e, - }; - defer file.close(); - return true; -} - pub fn does_folder_exist(fpath: string) !bool { const file = std.fs.cwd().openFile(fpath, .{}) catch |e| switch (e) { error.FileNotFound => return false, @@ -243,7 +233,7 @@ pub fn detect_pkgname(alloc: std.mem.Allocator, override: string, dir: string) ! return override; } const dirO = if (dir.len == 0) std.fs.cwd() else try std.fs.cwd().openDir(dir, .{}); - if (!(try does_file_exist(dirO, "build.zig"))) { + if (!(try extras.doesFileExist(dirO, "build.zig"))) { return error.NoBuildZig; } 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) ! pub fn detct_mainfile(alloc: std.mem.Allocator, override: string, dir: ?std.fs.Dir, name: string) !string { if (override.len > 0) { - if (try does_file_exist(dir, override)) { + if (try extras.doesFileExist(dir, override)) { if (std.mem.endsWith(u8, override, ".zig")) { return override; } } } const namedotzig = try std.mem.concat(alloc, u8, &.{ name, ".zig" }); - if (try does_file_exist(dir, namedotzig)) { + if (try extras.doesFileExist(dir, namedotzig)) { return namedotzig; } - if (try does_file_exist(dir, try std.fs.path.join(alloc, &.{ "src", "lib.zig" }))) { + if (try extras.doesFileExist(dir, try std.fs.path.join(alloc, &.{ "src", "lib.zig" }))) { return "src/lib.zig"; } - if (try does_file_exist(dir, try std.fs.path.join(alloc, &.{ "src", "main.zig" }))) { + if (try extras.doesFileExist(dir, try std.fs.path.join(alloc, &.{ "src", "main.zig" }))) { return "src/main.zig"; } return error.CantFindMain; -- 2.54.0