| ... | @@ -0,0 +1,46 @@ |
| 1 | const std = @import("std"); |
| 2 | const gpa = std.heap.c_allocator; |
| 3 | |
| 4 | const u = @import("./util/index.zig"); |
| 5 | |
| 6 | // |
| 7 | // |
| 8 | |
| 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, "")); |
| 12 | |
| 13 | const file = try std.fs.cwd().createFile("./zig.mod", .{}); |
| 14 | defer file.close(); |
| 15 | |
| 16 | const fwriter = file.writer(); |
| 17 | try fwriter.print("name: {}\n", .{name}); |
| 18 | try fwriter.print("main: {}\n", .{mainf}); |
| 19 | try fwriter.print("dependencies:\n", .{}); |
| 20 | |
| 21 | u.print("Initialized a new package named {} with entry point {}", .{name, mainf}); |
| 22 | } |
| 23 | |
| 24 | fn detect_pkgname(def: []const u8) ![]const u8 { |
| 25 | if (def.len > 0) { |
| 26 | return def; |
| 27 | } |
| 28 | const dpath = try std.fs.realpathAlloc(gpa, "./"); |
| 29 | const split = try u.split(dpath, "/"); |
| 30 | var name = split[split.len-1]; |
| 31 | name = u.trim_prefix(name, "zig-"); |
| 32 | std.debug.assert(name.len > 0); |
| 33 | return name; |
| 34 | } |
| 35 | |
| 36 | fn detct_mainfile(def: []const u8) ![]const u8 { |
| 37 | if (def.len > 0) { |
| 38 | if (u.does_file_exist(def)) { |
| 39 | if (std.mem.endsWith(u8, def, ".zig")) { |
| 40 | return def; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | std.debug.assert(u.does_file_exist("./src/main.zig")); |
| 45 | return "src/main.zig"; |
| 46 | } |