authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-28 04:00:16 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-28 04:00:16 -07:00
log291b6a07014a8e7b144643495c4425f6b472baf5
tree147dba845abe510bf6f04dc8a80633be97e3d826
parentdd3c55d7db9340f09256a5f5515a86ecfcedb991

remove global allocator from u.detect_pkgname and u.detct_mainfile


3 files changed, 11 insertions(+), 12 deletions(-)

src/cmd/init.zig+2-2
......@@ -30,14 +30,14 @@ pub fn execute(args: [][]u8) !void {
3030
3131 const ptype = try inquirer.forEnum(stdout, stdin, "Are you making an application or a library?", gpa, enum { exe, lib }, null);
3232
33 const name = try inquirer.forString(stdout, stdin, "package name:", gpa, u.detect_pkgname(u.try_index(string, args, 0, ""), "") catch |err| switch (err) {
33 const name = try inquirer.forString(stdout, stdin, "package name:", gpa, u.detect_pkgname(gpa, u.try_index(string, args, 0, ""), "") catch |err| switch (err) {
3434 error.NoBuildZig => {
3535 u.fail("init requires a build.zig file", .{});
3636 },
3737 else => return err,
3838 });
3939
40 const entry = if (ptype == .lib) try inquirer.forString(stdout, stdin, "package entry point:", gpa, u.detct_mainfile(u.try_index(string, args, 1, ""), null, name) catch |err| switch (err) {
40 const entry = if (ptype == .lib) try inquirer.forString(stdout, stdin, "package entry point:", gpa, u.detct_mainfile(gpa, u.try_index(string, args, 1, ""), null, name) catch |err| switch (err) {
4141 error.CantFindMain => null,
4242 else => return err,
4343 }) else null;
src/common.zig+2-2
......@@ -243,8 +243,8 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
243243 return null;
244244 }
245245 const moddirO = try std.fs.cwd().openDir(modpath, .{});
246 const tryname = try u.detect_pkgname("", modpath);
247 const trymain = u.detct_mainfile("", moddirO, tryname) catch |err| switch (err) {
246 const tryname = try u.detect_pkgname(gpa, "", modpath);
247 const trymain = u.detct_mainfile(gpa, "", moddirO, tryname) catch |err| switch (err) {
248248 error.CantFindMain => null,
249249 else => return err,
250250 };
src/util/funcs.zig+7-8
......@@ -1,6 +1,5 @@
11const std = @import("std");
22const string = []const u8;
3const gpa = std.heap.c_allocator;
43
54const u = @import("index.zig");
65
......@@ -249,7 +248,7 @@ pub fn slice(comptime T: type, input: []const T, from: usize, to: usize) []const
249248 return input[f..t];
250249}
251250
252pub fn detect_pkgname(override: string, dir: string) !string {
251pub fn detect_pkgname(alloc: *std.mem.Allocator, override: string, dir: string) !string {
253252 if (override.len > 0) {
254253 return override;
255254 }
......@@ -257,15 +256,15 @@ pub fn detect_pkgname(override: string, dir: string) !string {
257256 if (!(try does_file_exist(dirO, "build.zig"))) {
258257 return error.NoBuildZig;
259258 }
260 const dpath = try std.fs.realpathAlloc(gpa, try std.mem.concat(gpa, u8, &.{ dir, "build.zig" }));
261 const splitP = try split(gpa, dpath, std.fs.path.sep_str);
259 const dpath = try std.fs.realpathAlloc(alloc, try std.mem.concat(alloc, u8, &.{ dir, "build.zig" }));
260 const splitP = try split(alloc, dpath, std.fs.path.sep_str);
262261 var name = splitP[splitP.len - 2];
263262 name = trim_prefix(name, "zig-");
264263 assert(name.len > 0, "package name must not be an empty string", .{});
265264 return name;
266265}
267266
268pub fn detct_mainfile(override: string, dir: ?std.fs.Dir, name: string) !string {
267pub fn detct_mainfile(alloc: *std.mem.Allocator, override: string, dir: ?std.fs.Dir, name: string) !string {
269268 if (override.len > 0) {
270269 if (try does_file_exist(dir, override)) {
271270 if (std.mem.endsWith(u8, override, ".zig")) {
......@@ -273,14 +272,14 @@ pub fn detct_mainfile(override: string, dir: ?std.fs.Dir, name: string) !string
273272 }
274273 }
275274 }
276 const namedotzig = try std.mem.concat(gpa, u8, &.{ name, ".zig" });
275 const namedotzig = try std.mem.concat(alloc, u8, &.{ name, ".zig" });
277276 if (try does_file_exist(dir, namedotzig)) {
278277 return namedotzig;
279278 }
280 if (try does_file_exist(dir, try std.fs.path.join(gpa, &.{ "src", "lib.zig" }))) {
279 if (try does_file_exist(dir, try std.fs.path.join(alloc, &.{ "src", "lib.zig" }))) {
281280 return "src/lib.zig";
282281 }
283 if (try does_file_exist(dir, try std.fs.path.join(gpa, &.{ "src", "main.zig" }))) {
282 if (try does_file_exist(dir, try std.fs.path.join(alloc, &.{ "src", "main.zig" }))) {
284283 return "src/main.zig";
285284 }
286285 return error.CantFindMain;