authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-07-31 18:57:31 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-07-31 18:57:31 -07:00
log1d667ab10000a662680d6a509bfad95ffec77b70
tree62e0472d046a0e0595a9104b7005a27f6b727c0b
parent984b832113ea2d78587e4be0b427f5fe6db553a0

cmd/init- prompt for creating .gitignore and LICENSE too


5 files changed, 69 insertions(+), 3 deletions(-)

deps.zig+6-1
...@@ -58,6 +58,7 @@ const dirs = struct {...@@ -58,6 +58,7 @@ const dirs = struct {
58 pub const _96h80ezrvj7i = cache ++ "/git/github.com/nektro/zig-leven";58 pub const _96h80ezrvj7i = cache ++ "/git/github.com/nektro/zig-leven";
59 pub const _qyrnfg0iwpzl = cache ++ "/git/github.com/nektro/zig-fs-check";59 pub const _qyrnfg0iwpzl = cache ++ "/git/github.com/nektro/zig-fs-check";
60 pub const _c1xirp1ota5p = cache ++ "/git/github.com/nektro/zig-inquirer";60 pub const _c1xirp1ota5p = cache ++ "/git/github.com/nektro/zig-inquirer";
61 pub const _u7sysdckdymi = cache ++ "/git/github.com/arqv/ini";
61 pub const _o6ogpor87xc2 = cache ++ "/git/github.com/marlersoft/zigwin32";62 pub const _o6ogpor87xc2 = cache ++ "/git/github.com/marlersoft/zigwin32";
62};63};
6364
...@@ -128,9 +129,13 @@ pub const package_data = struct {...@@ -128,9 +129,13 @@ pub const package_data = struct {
128 .directory = dirs._c1xirp1ota5p,129 .directory = dirs._c1xirp1ota5p,
129 .pkg = Pkg{ .name = "inquirer", .path = .{ .path = dirs._c1xirp1ota5p ++ "/src/lib.zig" }, .dependencies = &.{ _s84v9o48ucb0.pkg.?, _tnj3qf44tpeq.pkg.? } },130 .pkg = Pkg{ .name = "inquirer", .path = .{ .path = dirs._c1xirp1ota5p ++ "/src/lib.zig" }, .dependencies = &.{ _s84v9o48ucb0.pkg.?, _tnj3qf44tpeq.pkg.? } },
130 };131 };
132 pub const _u7sysdckdymi = Package{
133 .directory = dirs._u7sysdckdymi,
134 .pkg = Pkg{ .name = "ini", .path = .{ .path = dirs._u7sysdckdymi ++ "/src/ini.zig" }, .dependencies = null },
135 };
131 pub const _89ujp8gq842x = Package{136 pub const _89ujp8gq842x = Package{
132 .directory = dirs._89ujp8gq842x,137 .directory = dirs._89ujp8gq842x,
133 .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.? } },138 .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.? } },
134 };139 };
135 pub const _o6ogpor87xc2 = Package{140 pub const _o6ogpor87xc2 = Package{
136 .directory = dirs._o6ogpor87xc2,141 .directory = dirs._o6ogpor87xc2,
src/cmd/init.zig+58
...@@ -3,11 +3,15 @@ const gpa = std.heap.c_allocator;...@@ -3,11 +3,15 @@ const gpa = std.heap.c_allocator;
33
4const inquirer = @import("inquirer");4const inquirer = @import("inquirer");
5const detectlicense = @import("detect-license");5const detectlicense = @import("detect-license");
6const knownfolders = @import("known-folders");
7const ini = @import("ini");
6const u = @import("./../util/index.zig");8const u = @import("./../util/index.zig");
79
8//10//
9//11//
1012
13const s_in_y = std.time.s_per_week * 52;
14
11pub fn execute(args: [][]u8) !void {15pub fn execute(args: [][]u8) !void {
12 _ = args;16 _ = args;
1317
...@@ -55,6 +59,7 @@ pub fn execute(args: [][]u8) !void {...@@ -55,6 +59,7 @@ pub fn execute(args: [][]u8) !void {
55 switch (try inquirer.forConfirm(stdout, stdin, "Is this okay?", gpa)) {59 switch (try inquirer.forConfirm(stdout, stdin, "Is this okay?", gpa)) {
56 false => {60 false => {
57 std.debug.print("okay. quitting...", .{});61 std.debug.print("okay. quitting...", .{});
62 return;
58 },63 },
59 true => {64 true => {
60 const file = try cwd.createFile("zig.mod", .{});65 const file = try cwd.createFile("zig.mod", .{});
...@@ -68,6 +73,50 @@ pub fn execute(args: [][]u8) !void {...@@ -68,6 +73,50 @@ pub fn execute(args: [][]u8) !void {
68 u.print("Successfully initialized new package {s}!\n", .{name});73 u.print("Successfully initialized new package {s}!\n", .{name});
69 },74 },
70 }75 }
76
77 // ask about .gitignore
78 if (try u.does_folder_exist(".git")) {
79 const do = try inquirer.forConfirm(stdout, stdin, "It appears you're using git. Do you want init to add Zigmod to your .gitignore?", gpa);
80 if (do) {
81 const exists = try u.does_file_exist(".gitignore", null);
82 const file = try (if (exists) cwd.openFile(".gitignore", .{ .read = true, .write = true }) else cwd.createFile(".gitignore", .{}));
83 defer file.close();
84 try file.seekTo(try file.getEndPos());
85 const w = file.writer();
86
87 if (!exists) try w.writeAll("zig-*\n");
88 try w.writeAll(".zigmod\n");
89 try w.writeAll("deps.zig\n");
90 }
91 }
92
93 // ask about LICENSE
94 if (!(try u.does_file_exist("LICENSE", null))) {
95 if (detectlicense.licenses.find(license)) |text| {
96 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)) {
97 var realtext = text;
98 realtext = try std.mem.replaceOwned(u8, gpa, realtext, "<year>", try inquirer.answer(
99 stdout,
100 "year:",
101 []const u8,
102 "{s}",
103 try std.fmt.allocPrint(gpa, "{d}", .{1970 + @divFloor(std.time.timestamp(), s_in_y)}),
104 ));
105 realtext = try std.mem.replaceOwned(u8, gpa, realtext, "<copyright holders>", try inquirer.forString(
106 stdout,
107 stdin,
108 "copyright holder's name:",
109 gpa,
110 try guessCopyrightName(),
111 ));
112
113 const file = try cwd.createFile("LICENSE", .{});
114 defer file.close();
115 const w = file.writer();
116 try w.writeAll(realtext);
117 }
118 }
119 }
71}120}
72121
73pub fn writeExeManifest(w: std.fs.File.Writer, id: []const u8, name: []const u8, license: ?[]const u8, description: ?[]const u8) !void {122pub 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,...@@ -86,3 +135,12 @@ pub fn writeLibManifest(w: std.fs.File.Writer, id: []const u8, name: []const u8,
86 try w.print("description: {s}\n", .{description});135 try w.print("description: {s}\n", .{description});
87 try w.print("dependencies:\n", .{});136 try w.print("dependencies:\n", .{});
88}137}
138
139fn guessCopyrightName() !?[]const u8 {
140 const home = (try knownfolders.open(gpa, .home, .{})).?;
141 if (!(try u.does_file_exist(".gitconfig", home))) return null;
142 const file = try home.openFile(".gitconfig", .{});
143 const content = try file.reader().readAllAlloc(gpa, 1024 * 1024);
144 var iniO = try ini.parseIntoMap(content, gpa);
145 return iniO.map.get("user.name");
146}
zig.mod+1
...@@ -38,6 +38,7 @@ dependencies:...@@ -38,6 +38,7 @@ dependencies:
38 - src: git https://github.com/nektro/zig-detect-license38 - src: git https://github.com/nektro/zig-detect-license
39 - src: git https://github.com/nektro/zig-fs-check39 - src: git https://github.com/nektro/zig-fs-check
40 - src: git https://github.com/nektro/zig-inquirer40 - src: git https://github.com/nektro/zig-inquirer
41 - src: git https://github.com/arqv/ini
4142
42dev_dependencies:43dev_dependencies:
43 - src: git https://github.com/marlersoft/zigwin3244 - src: git https://github.com/marlersoft/zigwin32
zigmod.lock+2-1
...@@ -10,9 +10,10 @@ git https://github.com/MasterQ32/zig-network commit-b9c91769d8ebd626c8e45b2abb05...@@ -10,9 +10,10 @@ git https://github.com/MasterQ32/zig-network commit-b9c91769d8ebd626c8e45b2abb05
10git https://github.com/MasterQ32/zig-uri commit-52cdd2061bec0579519f0d30280597f3a1db8b7510git https://github.com/MasterQ32/zig-uri commit-52cdd2061bec0579519f0d30280597f3a1db8b75
11git https://github.com/nektro/zig-json commit-72e555fbc0776f2600aee19b01e5ab1855ebec7a11git https://github.com/nektro/zig-json commit-72e555fbc0776f2600aee19b01e5ab1855ebec7a
12git https://github.com/nektro/zig-range commit-890ca308fe09b3d5c866d5cfb3b3d7a95dbf939f12git https://github.com/nektro/zig-range commit-890ca308fe09b3d5c866d5cfb3b3d7a95dbf939f
13git https://github.com/nektro/zig-detect-license commit-0ebd7d7c80b88fd5b606ee2ec57e9c6e23927fbf13git https://github.com/nektro/zig-detect-license commit-d4544410f811c402b866f21f63588d1aae1b2d90
14git https://github.com/nektro/zig-licenses-text commit-3c07c6e4eb0965dafd0b029c632f823631b3169c14git https://github.com/nektro/zig-licenses-text commit-3c07c6e4eb0965dafd0b029c632f823631b3169c
15git https://github.com/nektro/zig-leven commit-8e9f827117ab1418578b692a2325754cc3ee692d15git https://github.com/nektro/zig-leven commit-8e9f827117ab1418578b692a2325754cc3ee692d
16git https://github.com/nektro/zig-fs-check commit-dcd8da90fcb8bf3c9887e713bf0ec072bc897dd516git https://github.com/nektro/zig-fs-check commit-dcd8da90fcb8bf3c9887e713bf0ec072bc897dd5
17git https://github.com/nektro/zig-inquirer commit-d36621903868136b2ce347e9be9c937e664e1e7417git https://github.com/nektro/zig-inquirer commit-d36621903868136b2ce347e9be9c937e664e1e74
18git https://github.com/arqv/ini commit-e5296e929513eb3d86a5f815c4a4a5ecd4d8d11c
18git https://github.com/marlersoft/zigwin32 commit-ca6b8e5c378e31655fa11b886b44b62f703ef09019git https://github.com/marlersoft/zigwin32 commit-ca6b8e5c378e31655fa11b886b44b62f703ef090
zigmod.sum+2-1
...@@ -9,9 +9,10 @@ blake3-21f91e48333ac0ca7f4704c96352831c25216e7056d02ce24de95d03fc942246 git/gith...@@ -9,9 +9,10 @@ blake3-21f91e48333ac0ca7f4704c96352831c25216e7056d02ce24de95d03fc942246 git/gith
9blake3-030ebb03f1ed21122e681b06786bea6f2f1b810e8eb9f2029d0eee4f4fb3103f git/github.com/MasterQ32/zig-uri9blake3-030ebb03f1ed21122e681b06786bea6f2f1b810e8eb9f2029d0eee4f4fb3103f git/github.com/MasterQ32/zig-uri
10blake3-1893709ffc6359c5f9cd2f9409abccf78a94ed37bb2c6dd075c603356d17c94b git/github.com/nektro/zig-json10blake3-1893709ffc6359c5f9cd2f9409abccf78a94ed37bb2c6dd075c603356d17c94b git/github.com/nektro/zig-json
11blake3-09698753782139ab4877d08f33235170836f68b73e482b65cdee5637a6addf86 git/github.com/nektro/zig-range11blake3-09698753782139ab4877d08f33235170836f68b73e482b65cdee5637a6addf86 git/github.com/nektro/zig-range
12blake3-e9e11cd5724286e0365ebdd5c17f35bcf75a4c1f6deb0060fc2e142ea0aa47bd git/github.com/nektro/zig-detect-license12blake3-dbbd8d54afa4f2ba93fe11396ca8137c85b4be358ffd106829668f74c9568739 git/github.com/nektro/zig-detect-license
13blake3-20ceb9e27bdb93540e93006628fb94e0540f3f69a2304a8f73ad2989b8e27226 git/github.com/nektro/zig-licenses-text13blake3-20ceb9e27bdb93540e93006628fb94e0540f3f69a2304a8f73ad2989b8e27226 git/github.com/nektro/zig-licenses-text
14blake3-cf68eaad66254b89c8bc18578f58607aa2e87ae59dd5696c2394eb5a0a31c455 git/github.com/nektro/zig-leven14blake3-cf68eaad66254b89c8bc18578f58607aa2e87ae59dd5696c2394eb5a0a31c455 git/github.com/nektro/zig-leven
15blake3-10460da714f5c8436d09c268795597b0b2686ffc1e789c9f50366c2cbb0e1ff3 git/github.com/nektro/zig-fs-check15blake3-10460da714f5c8436d09c268795597b0b2686ffc1e789c9f50366c2cbb0e1ff3 git/github.com/nektro/zig-fs-check
16blake3-1cfc14f309d2dc8994bd8e2fc4e029cfee139ea2aef130a139bd996de3a538a8 git/github.com/nektro/zig-inquirer16blake3-1cfc14f309d2dc8994bd8e2fc4e029cfee139ea2aef130a139bd996de3a538a8 git/github.com/nektro/zig-inquirer
17blake3-5bb722bdcd68e8d9edd356d6741910f60b24f33758bdd1f529fd8de8561c0d8f git/github.com/arqv/ini
17blake3-949c3cd2c9e834190feeecb010c59ab5047fa721cfbdf67e0f0fd847194a6dad git/github.com/marlersoft/zigwin3218blake3-949c3cd2c9e834190feeecb010c59ab5047fa721cfbdf67e0f0fd847194a6dad git/github.com/marlersoft/zigwin32