| author | |
| committer | |
| log | 4b04e9feee75c7c7383217db811900a280ce0cde |
| tree | 3dc58a89c2bed06bab7dc40e22dd3188db3ea200 |
| parent | 18fe4b25e188b95d6dbd59a5f62ce5142317540f |
2 files changed, 52 insertions(+), 35 deletions(-)
src/cmd/init.zig+14-33| ... | ... | @@ -7,8 +7,20 @@ const u = @import("./../util/index.zig"); |
| 7 | 7 | // |
| 8 | 8 | |
| 9 | 9 | pub fn execute(args: [][]u8) !void { |
| 10 | const name = try detect_pkgname(u.try_index([]const u8, args, 0, "")); | |
| 11 | const mainf = try detct_mainfile(u.try_index([]const u8, args, 1, "")); | |
| 10 | const name = u.detect_pkgname(u.try_index([]const u8, args, 0, ""), null) catch |err| switch (err) { | |
| 11 | error.NoBuildZig => { | |
| 12 | u.assert(false, "init requires a build.zig file", .{}); | |
| 13 | unreachable; | |
| 14 | }, | |
| 15 | else => return err, | |
| 16 | }; | |
| 17 | const mainf = u.detct_mainfile(u.try_index([]const u8, args, 1, ""), null, name) catch |err| switch (err) { | |
| 18 | error.CantFindMain => { | |
| 19 | u.assert(false, "unable to detect package entry point", .{}); | |
| 20 | unreachable; | |
| 21 | }, | |
| 22 | else => return err, | |
| 23 | }; | |
| 12 | 24 | |
| 13 | 25 | const file = try std.fs.cwd().createFile("zig.mod", .{}); |
| 14 | 26 | defer file.close(); |
| ... | ... | @@ -21,34 +33,3 @@ pub fn execute(args: [][]u8) !void { |
| 21 | 33 | |
| 22 | 34 | u.print("Initialized a new package named {s} with entry point {s}", .{ name, mainf }); |
| 23 | 35 | } |
| 24 | ||
| 25 | fn detect_pkgname(def: []const u8) ![]const u8 { | |
| 26 | if (def.len > 0) { | |
| 27 | return def; | |
| 28 | } | |
| 29 | u.assert(try u.does_file_exist("build.zig"), "init requires a build.zig file", .{}); | |
| 30 | const dpath = try std.fs.cwd().realpathAlloc(gpa, "build.zig"); | |
| 31 | const split = try u.split(dpath, std.fs.path.sep_str); | |
| 32 | var name = split[split.len - 2]; | |
| 33 | name = u.trim_prefix(name, "zig-"); | |
| 34 | u.assert(name.len > 0, "package name must not be an empty string", .{}); | |
| 35 | return name; | |
| 36 | } | |
| 37 | ||
| 38 | fn detct_mainfile(def: []const u8) ![]const u8 { | |
| 39 | if (def.len > 0) { | |
| 40 | if (try u.does_file_exist(def)) { | |
| 41 | if (std.mem.endsWith(u8, def, ".zig")) { | |
| 42 | return def; | |
| 43 | } | |
| 44 | } | |
| 45 | } | |
| 46 | if (try u.does_file_exist(try std.fs.path.join(gpa, &.{ "src", "lib.zig" }))) { | |
| 47 | return "src/lib.zig"; | |
| 48 | } | |
| 49 | if (try u.does_file_exist(try std.fs.path.join(gpa, &.{ "src", "main.zig" }))) { | |
| 50 | return "src/main.zig"; | |
| 51 | } | |
| 52 | u.assert(false, "unable to detect package entry point", .{}); | |
| 53 | unreachable; | |
| 54 | } |
src/util/funcs.zig+38-2| ... | ... | @@ -49,8 +49,8 @@ pub fn trim_prefix(in: []const u8, prefix: []const u8) []const u8 { |
| 49 | 49 | return in; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | pub fn does_file_exist(fpath: []const u8) !bool { | |
| 53 | const file = std.fs.cwd().openFile(fpath, .{}) catch |e| switch (e) { | |
| 52 | pub fn does_file_exist(fpath: []const u8, dir: ?std.fs.Dir) !bool { | |
| 53 | const file = (dir orelse std.fs.cwd()).openFile(fpath, .{}) catch |e| switch (e) { | |
| 54 | 54 | error.FileNotFound => return false, |
| 55 | 55 | error.IsDir => return true, |
| 56 | 56 | else => return e, |
| ... | ... | @@ -265,3 +265,39 @@ pub fn slice(comptime T: type, input: []const T, from: usize, to: usize) []const |
| 265 | 265 | const t = std.math.min(to, input.len); |
| 266 | 266 | return input[f..t]; |
| 267 | 267 | } |
| 268 | ||
| 269 | pub fn detect_pkgname(override: []const u8, dir: ?std.fs.Dir) ![]const u8 { | |
| 270 | if (override.len > 0) { | |
| 271 | return override; | |
| 272 | } | |
| 273 | if (!(try does_file_exist("build.zig", dir))) { | |
| 274 | return error.NoBuildZig; | |
| 275 | } | |
| 276 | const dpath = try (dir orelse std.fs.cwd()).realpathAlloc(gpa, "build.zig"); | |
| 277 | const splitP = try split(dpath, std.fs.path.sep_str); | |
| 278 | var name = splitP[splitP.len - 2]; | |
| 279 | name = trim_prefix(name, "zig-"); | |
| 280 | assert(name.len > 0, "package name must not be an empty string", .{}); | |
| 281 | return name; | |
| 282 | } | |
| 283 | ||
| 284 | pub fn detct_mainfile(override: []const u8, dir: ?std.fs.Dir, name: []const u8) ![]const u8 { | |
| 285 | if (override.len > 0) { | |
| 286 | if (try does_file_exist(override, dir)) { | |
| 287 | if (std.mem.endsWith(u8, override, ".zig")) { | |
| 288 | return override; | |
| 289 | } | |
| 290 | } | |
| 291 | } | |
| 292 | const namedotzig = try std.mem.concat(gpa, u8, &.{ name, ".zig" }); | |
| 293 | if (try does_file_exist(namedotzig, dir)) { | |
| 294 | return namedotzig; | |
| 295 | } | |
| 296 | if (try does_file_exist(try std.fs.path.join(gpa, &.{ "src", "lib.zig" }), dir)) { | |
| 297 | return "src/lib.zig"; | |
| 298 | } | |
| 299 | if (try does_file_exist(try std.fs.path.join(gpa, &.{ "src", "main.zig" }), dir)) { | |
| 300 | return "src/main.zig"; | |
| 301 | } | |
| 302 | return error.CantFindMain; | |
| 303 | } |