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 {
5858 pub const _96h80ezrvj7i = cache ++ "/git/github.com/nektro/zig-leven";
5959 pub const _qyrnfg0iwpzl = cache ++ "/git/github.com/nektro/zig-fs-check";
6060 pub const _c1xirp1ota5p = cache ++ "/git/github.com/nektro/zig-inquirer";
61 pub const _u7sysdckdymi = cache ++ "/git/github.com/arqv/ini";
6162 pub const _o6ogpor87xc2 = cache ++ "/git/github.com/marlersoft/zigwin32";
6263};
6364
......@@ -128,9 +129,13 @@ pub const package_data = struct {
128129 .directory = dirs._c1xirp1ota5p,
129130 .pkg = Pkg{ .name = "inquirer", .path = .{ .path = dirs._c1xirp1ota5p ++ "/src/lib.zig" }, .dependencies = &.{ _s84v9o48ucb0.pkg.?, _tnj3qf44tpeq.pkg.? } },
130131 };
132 pub const _u7sysdckdymi = Package{
133 .directory = dirs._u7sysdckdymi,
134 .pkg = Pkg{ .name = "ini", .path = .{ .path = dirs._u7sysdckdymi ++ "/src/ini.zig" }, .dependencies = null },
135 };
131136 pub const _89ujp8gq842x = Package{
132137 .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.? } },
134139 };
135140 pub const _o6ogpor87xc2 = Package{
136141 .directory = dirs._o6ogpor87xc2,
src/cmd/init.zig+58
......@@ -3,11 +3,15 @@ const gpa = std.heap.c_allocator;
33
44const inquirer = @import("inquirer");
55const detectlicense = @import("detect-license");
6const knownfolders = @import("known-folders");
7const ini = @import("ini");
68const u = @import("./../util/index.zig");
79
810//
911//
1012
13const s_in_y = std.time.s_per_week * 52;
14
1115pub fn execute(args: [][]u8) !void {
1216 _ = args;
1317
......@@ -55,6 +59,7 @@ pub fn execute(args: [][]u8) !void {
5559 switch (try inquirer.forConfirm(stdout, stdin, "Is this okay?", gpa)) {
5660 false => {
5761 std.debug.print("okay. quitting...", .{});
62 return;
5863 },
5964 true => {
6065 const file = try cwd.createFile("zig.mod", .{});
......@@ -68,6 +73,50 @@ pub fn execute(args: [][]u8) !void {
6873 u.print("Successfully initialized new package {s}!\n", .{name});
6974 },
7075 }
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 }
71120}
72121
73122pub 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,
86135 try w.print("description: {s}\n", .{description});
87136 try w.print("dependencies:\n", .{});
88137}
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:
3838 - src: git https://github.com/nektro/zig-detect-license
3939 - src: git https://github.com/nektro/zig-fs-check
4040 - src: git https://github.com/nektro/zig-inquirer
41 - src: git https://github.com/arqv/ini
4142
4243dev_dependencies:
4344 - src: git https://github.com/marlersoft/zigwin32
zigmod.lock+2-1
......@@ -10,9 +10,10 @@ git https://github.com/MasterQ32/zig-network commit-b9c91769d8ebd626c8e45b2abb05
1010git https://github.com/MasterQ32/zig-uri commit-52cdd2061bec0579519f0d30280597f3a1db8b75
1111git https://github.com/nektro/zig-json commit-72e555fbc0776f2600aee19b01e5ab1855ebec7a
1212git https://github.com/nektro/zig-range commit-890ca308fe09b3d5c866d5cfb3b3d7a95dbf939f
13git https://github.com/nektro/zig-detect-license commit-0ebd7d7c80b88fd5b606ee2ec57e9c6e23927fbf
13git https://github.com/nektro/zig-detect-license commit-d4544410f811c402b866f21f63588d1aae1b2d90
1414git https://github.com/nektro/zig-licenses-text commit-3c07c6e4eb0965dafd0b029c632f823631b3169c
1515git https://github.com/nektro/zig-leven commit-8e9f827117ab1418578b692a2325754cc3ee692d
1616git https://github.com/nektro/zig-fs-check commit-dcd8da90fcb8bf3c9887e713bf0ec072bc897dd5
1717git https://github.com/nektro/zig-inquirer commit-d36621903868136b2ce347e9be9c937e664e1e74
18git https://github.com/arqv/ini commit-e5296e929513eb3d86a5f815c4a4a5ecd4d8d11c
1819git https://github.com/marlersoft/zigwin32 commit-ca6b8e5c378e31655fa11b886b44b62f703ef090
zigmod.sum+2-1
......@@ -9,9 +9,10 @@ blake3-21f91e48333ac0ca7f4704c96352831c25216e7056d02ce24de95d03fc942246 git/gith
99blake3-030ebb03f1ed21122e681b06786bea6f2f1b810e8eb9f2029d0eee4f4fb3103f git/github.com/MasterQ32/zig-uri
1010blake3-1893709ffc6359c5f9cd2f9409abccf78a94ed37bb2c6dd075c603356d17c94b git/github.com/nektro/zig-json
1111blake3-09698753782139ab4877d08f33235170836f68b73e482b65cdee5637a6addf86 git/github.com/nektro/zig-range
12blake3-e9e11cd5724286e0365ebdd5c17f35bcf75a4c1f6deb0060fc2e142ea0aa47bd git/github.com/nektro/zig-detect-license
12blake3-dbbd8d54afa4f2ba93fe11396ca8137c85b4be358ffd106829668f74c9568739 git/github.com/nektro/zig-detect-license
1313blake3-20ceb9e27bdb93540e93006628fb94e0540f3f69a2304a8f73ad2989b8e27226 git/github.com/nektro/zig-licenses-text
1414blake3-cf68eaad66254b89c8bc18578f58607aa2e87ae59dd5696c2394eb5a0a31c455 git/github.com/nektro/zig-leven
1515blake3-10460da714f5c8436d09c268795597b0b2686ffc1e789c9f50366c2cbb0e1ff3 git/github.com/nektro/zig-fs-check
1616blake3-1cfc14f309d2dc8994bd8e2fc4e029cfee139ea2aef130a139bd996de3a538a8 git/github.com/nektro/zig-inquirer
17blake3-5bb722bdcd68e8d9edd356d6741910f60b24f33758bdd1f529fd8de8561c0d8f git/github.com/arqv/ini
1718blake3-949c3cd2c9e834190feeecb010c59ab5047fa721cfbdf67e0f0fd847194a6dad git/github.com/marlersoft/zigwin32