diff --git a/deps.zig b/deps.zig index 4a480398d4b937c5fe5eed08113fe67c33699be8..e0043525db3b8f449dd7facaa38669fbe776032c 100644 --- a/deps.zig +++ b/deps.zig @@ -58,6 +58,7 @@ const dirs = struct { pub const _96h80ezrvj7i = cache ++ "/git/github.com/nektro/zig-leven"; pub const _qyrnfg0iwpzl = cache ++ "/git/github.com/nektro/zig-fs-check"; pub const _c1xirp1ota5p = cache ++ "/git/github.com/nektro/zig-inquirer"; + pub const _u7sysdckdymi = cache ++ "/git/github.com/arqv/ini"; pub const _o6ogpor87xc2 = cache ++ "/git/github.com/marlersoft/zigwin32"; }; @@ -128,9 +129,13 @@ pub const package_data = struct { .directory = dirs._c1xirp1ota5p, .pkg = Pkg{ .name = "inquirer", .path = .{ .path = dirs._c1xirp1ota5p ++ "/src/lib.zig" }, .dependencies = &.{ _s84v9o48ucb0.pkg.?, _tnj3qf44tpeq.pkg.? } }, }; + pub const _u7sysdckdymi = Package{ + .directory = dirs._u7sysdckdymi, + .pkg = Pkg{ .name = "ini", .path = .{ .path = dirs._u7sysdckdymi ++ "/src/ini.zig" }, .dependencies = null }, + }; pub const _89ujp8gq842x = Package{ .directory = dirs._89ujp8gq842x, - .pkg = Pkg{ .name = "zigmod", .path = .{ .path = dirs._89ujp8gq842x ++ "/src/lib.zig" }, .dependencies = &.{ _s84v9o48ucb0.pkg.?, _2ta738wrqbaq.pkg.?, _0npcrzfdlrvk.pkg.?, _ejw82j2ipa0e.pkg.?, _ocmr9rtohgcc.pkg.?, _tnj3qf44tpeq.pkg.?, _2ovav391ivak.pkg.?, _qyrnfg0iwpzl.pkg.?, _c1xirp1ota5p.pkg.? } }, + .pkg = Pkg{ .name = "zigmod", .path = .{ .path = dirs._89ujp8gq842x ++ "/src/lib.zig" }, .dependencies = &.{ _s84v9o48ucb0.pkg.?, _2ta738wrqbaq.pkg.?, _0npcrzfdlrvk.pkg.?, _ejw82j2ipa0e.pkg.?, _ocmr9rtohgcc.pkg.?, _tnj3qf44tpeq.pkg.?, _2ovav391ivak.pkg.?, _qyrnfg0iwpzl.pkg.?, _c1xirp1ota5p.pkg.?, _u7sysdckdymi.pkg.? } }, }; pub const _o6ogpor87xc2 = Package{ .directory = dirs._o6ogpor87xc2, diff --git a/src/cmd/init.zig b/src/cmd/init.zig index 2efb3f6ecf7a718b2a8bcfaa5bbd835eea45038c..7fae6b395c406b9d98dde4326235d7555df8dbc3 100644 --- a/src/cmd/init.zig +++ b/src/cmd/init.zig @@ -3,11 +3,15 @@ const gpa = std.heap.c_allocator; const inquirer = @import("inquirer"); const detectlicense = @import("detect-license"); +const knownfolders = @import("known-folders"); +const ini = @import("ini"); const u = @import("./../util/index.zig"); // // +const s_in_y = std.time.s_per_week * 52; + pub fn execute(args: [][]u8) !void { _ = args; @@ -55,6 +59,7 @@ pub fn execute(args: [][]u8) !void { switch (try inquirer.forConfirm(stdout, stdin, "Is this okay?", gpa)) { false => { std.debug.print("okay. quitting...", .{}); + return; }, true => { const file = try cwd.createFile("zig.mod", .{}); @@ -68,6 +73,50 @@ pub fn execute(args: [][]u8) !void { u.print("Successfully initialized new package {s}!\n", .{name}); }, } + + // ask about .gitignore + 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(".gitignore", null); + const file = try (if (exists) cwd.openFile(".gitignore", .{ .read = true, .write = true }) else cwd.createFile(".gitignore", .{})); + defer file.close(); + try file.seekTo(try file.getEndPos()); + const w = file.writer(); + + if (!exists) try w.writeAll("zig-*\n"); + try w.writeAll(".zigmod\n"); + try w.writeAll("deps.zig\n"); + } + } + + // ask about LICENSE + if (!(try u.does_file_exist("LICENSE", null))) { + 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; + realtext = try std.mem.replaceOwned(u8, gpa, realtext, "", try inquirer.answer( + stdout, + "year:", + []const u8, + "{s}", + try std.fmt.allocPrint(gpa, "{d}", .{1970 + @divFloor(std.time.timestamp(), s_in_y)}), + )); + realtext = try std.mem.replaceOwned(u8, gpa, realtext, "", try inquirer.forString( + stdout, + stdin, + "copyright holder's name:", + gpa, + try guessCopyrightName(), + )); + + const file = try cwd.createFile("LICENSE", .{}); + defer file.close(); + const w = file.writer(); + try w.writeAll(realtext); + } + } + } } pub fn writeExeManifest(w: std.fs.File.Writer, id: []const u8, name: []const u8, license: ?[]const u8, description: ?[]const u8) !void { @@ -86,3 +135,12 @@ pub fn writeLibManifest(w: std.fs.File.Writer, id: []const u8, name: []const u8, try w.print("description: {s}\n", .{description}); try w.print("dependencies:\n", .{}); } + +fn guessCopyrightName() !?[]const u8 { + const home = (try knownfolders.open(gpa, .home, .{})).?; + if (!(try u.does_file_exist(".gitconfig", home))) return null; + const file = try home.openFile(".gitconfig", .{}); + const content = try file.reader().readAllAlloc(gpa, 1024 * 1024); + var iniO = try ini.parseIntoMap(content, gpa); + return iniO.map.get("user.name"); +} diff --git a/zig.mod b/zig.mod index b2691298a670fab8f78c5bf97c71b670e9a32a4e..3d7e94dd2a318ee3921731145b9bd4406549ae0f 100644 --- a/zig.mod +++ b/zig.mod @@ -38,6 +38,7 @@ dependencies: - src: git https://github.com/nektro/zig-detect-license - src: git https://github.com/nektro/zig-fs-check - src: git https://github.com/nektro/zig-inquirer + - src: git https://github.com/arqv/ini dev_dependencies: - src: git https://github.com/marlersoft/zigwin32 diff --git a/zigmod.lock b/zigmod.lock index 181dceac6ff9e700adc91a71f5ab4c88f62be001..e3cc3b72d60b7a2ea7398f9d0fa9b981712d090d 100644 --- a/zigmod.lock +++ b/zigmod.lock @@ -10,9 +10,10 @@ git https://github.com/MasterQ32/zig-network commit-b9c91769d8ebd626c8e45b2abb05 git https://github.com/MasterQ32/zig-uri commit-52cdd2061bec0579519f0d30280597f3a1db8b75 git https://github.com/nektro/zig-json commit-72e555fbc0776f2600aee19b01e5ab1855ebec7a git https://github.com/nektro/zig-range commit-890ca308fe09b3d5c866d5cfb3b3d7a95dbf939f -git https://github.com/nektro/zig-detect-license commit-0ebd7d7c80b88fd5b606ee2ec57e9c6e23927fbf +git https://github.com/nektro/zig-detect-license commit-d4544410f811c402b866f21f63588d1aae1b2d90 git https://github.com/nektro/zig-licenses-text commit-3c07c6e4eb0965dafd0b029c632f823631b3169c git https://github.com/nektro/zig-leven commit-8e9f827117ab1418578b692a2325754cc3ee692d git https://github.com/nektro/zig-fs-check commit-dcd8da90fcb8bf3c9887e713bf0ec072bc897dd5 git https://github.com/nektro/zig-inquirer commit-d36621903868136b2ce347e9be9c937e664e1e74 +git https://github.com/arqv/ini commit-e5296e929513eb3d86a5f815c4a4a5ecd4d8d11c git https://github.com/marlersoft/zigwin32 commit-ca6b8e5c378e31655fa11b886b44b62f703ef090 diff --git a/zigmod.sum b/zigmod.sum index de85b592905718cb7216dd17414c9c265640be07..b1d286cdd8be25f6690de30fd9ef59751d4d3b9b 100644 --- a/zigmod.sum +++ b/zigmod.sum @@ -9,9 +9,10 @@ blake3-21f91e48333ac0ca7f4704c96352831c25216e7056d02ce24de95d03fc942246 git/gith blake3-030ebb03f1ed21122e681b06786bea6f2f1b810e8eb9f2029d0eee4f4fb3103f git/github.com/MasterQ32/zig-uri blake3-1893709ffc6359c5f9cd2f9409abccf78a94ed37bb2c6dd075c603356d17c94b git/github.com/nektro/zig-json blake3-09698753782139ab4877d08f33235170836f68b73e482b65cdee5637a6addf86 git/github.com/nektro/zig-range -blake3-e9e11cd5724286e0365ebdd5c17f35bcf75a4c1f6deb0060fc2e142ea0aa47bd git/github.com/nektro/zig-detect-license +blake3-dbbd8d54afa4f2ba93fe11396ca8137c85b4be358ffd106829668f74c9568739 git/github.com/nektro/zig-detect-license blake3-20ceb9e27bdb93540e93006628fb94e0540f3f69a2304a8f73ad2989b8e27226 git/github.com/nektro/zig-licenses-text blake3-cf68eaad66254b89c8bc18578f58607aa2e87ae59dd5696c2394eb5a0a31c455 git/github.com/nektro/zig-leven blake3-10460da714f5c8436d09c268795597b0b2686ffc1e789c9f50366c2cbb0e1ff3 git/github.com/nektro/zig-fs-check blake3-1cfc14f309d2dc8994bd8e2fc4e029cfee139ea2aef130a139bd996de3a538a8 git/github.com/nektro/zig-inquirer +blake3-5bb722bdcd68e8d9edd356d6741910f60b24f33758bdd1f529fd8de8561c0d8f git/github.com/arqv/ini blake3-949c3cd2c9e834190feeecb010c59ab5047fa721cfbdf67e0f0fd847194a6dad git/github.com/marlersoft/zigwin32