diff --git a/deps.zig b/deps.zig index d98f466f68b50b720824bfe9f770dda1b9f944df..0cc1e31390024480fdb2a16a4e58d682c9b7132d 100644 --- a/deps.zig +++ b/deps.zig @@ -53,6 +53,11 @@ const dirs = struct { pub const _u9w9dpp6p804 = cache ++ "/git/github.com/MasterQ32/zig-uri"; pub const _ocmr9rtohgcc = cache ++ "/git/github.com/nektro/zig-json"; pub const _tnj3qf44tpeq = cache ++ "/git/github.com/nektro/zig-range"; + pub const _2ovav391ivak = cache ++ "/git/github.com/nektro/zig-detect-license"; + pub const _pt88y5d80m25 = cache ++ "/git/github.com/nektro/zig-licenses-text"; + 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 _o6ogpor87xc2 = cache ++ "/git/github.com/marlersoft/zigwin32"; }; @@ -114,9 +119,34 @@ pub const package_data = struct { .pkg = Pkg{ .name = "range", .path = .{ .path = dirs._tnj3qf44tpeq ++ "/src/lib.zig" }, .dependencies = null }, }; + pub const _pt88y5d80m25 = Package{ + .directory = dirs._pt88y5d80m25, + .pkg = Pkg{ .name = "licenses-text", .path = .{ .path = dirs._pt88y5d80m25 ++ "/src/lib.zig" }, .dependencies = null }, + }; + + pub const _96h80ezrvj7i = Package{ + .directory = dirs._96h80ezrvj7i, + .pkg = Pkg{ .name = "leven", .path = .{ .path = dirs._96h80ezrvj7i ++ "/src/lib.zig" }, .dependencies = &.{_tnj3qf44tpeq.pkg.?} }, + }; + + pub const _qyrnfg0iwpzl = Package{ + .directory = dirs._qyrnfg0iwpzl, + .pkg = Pkg{ .name = "fs-check", .path = .{ .path = dirs._qyrnfg0iwpzl ++ "/src/lib.zig" }, .dependencies = null }, + }; + + pub const _2ovav391ivak = Package{ + .directory = dirs._2ovav391ivak, + .pkg = Pkg{ .name = "detect-license", .path = .{ .path = dirs._2ovav391ivak ++ "/src/lib.zig" }, .dependencies = &.{ _pt88y5d80m25.pkg.?, _96h80ezrvj7i.pkg.?, _qyrnfg0iwpzl.pkg.? } }, + }; + + pub const _c1xirp1ota5p = Package{ + .directory = dirs._c1xirp1ota5p, + .pkg = Pkg{ .name = "inquirer", .path = .{ .path = dirs._c1xirp1ota5p ++ "/src/lib.zig" }, .dependencies = &.{ _s84v9o48ucb0.pkg.?, _tnj3qf44tpeq.pkg.? } }, + }; + 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.? } }, + .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.? } }, }; pub const _o6ogpor87xc2 = Package{ diff --git a/src/cmd/init.zig b/src/cmd/init.zig index 596ff7a552b36fab51b3ccd430d4692c0bfc3f69..c772269cc11f4c401ca384ab45c0147695ee7122 100644 --- a/src/cmd/init.zig +++ b/src/cmd/init.zig @@ -1,35 +1,90 @@ const std = @import("std"); const gpa = std.heap.c_allocator; +const inquirer = @import("inquirer"); +const detectlicense = @import("detect-license"); const u = @import("./../util/index.zig"); // // pub fn execute(args: [][]u8) !void { - const name = u.detect_pkgname(u.try_index([]const u8, args, 0, ""), "") catch |err| switch (err) { + _ = args; + + std.debug.print("This utility will walk you through creating a zig.mod file.\n", .{}); + std.debug.print("That will give a good launching off point to get your next project started.\n", .{}); + std.debug.print("Use `zigmod aq add ` to add a dependency from https://aquila.red/\n", .{}); + std.debug.print("Press ^C at any time to quit.\n", .{}); + std.debug.print("\n", .{}); + + const stdout = std.io.getStdOut().writer(); + const stdin = std.io.getStdIn().reader(); + + const id = try inquirer.answer(stdout, "ID (this gets autogenerated):", []const u8, "{s}", try u.random_string(48)); + + const ptype = try inquirer.forEnum(stdout, stdin, "Are you making an application or a library?", gpa, enum { exe, lib }, null); + + const name = try inquirer.forString(stdout, stdin, "package name:", gpa, u.detect_pkgname(u.try_index([]const u8, args, 0, ""), "") catch |err| switch (err) { error.NoBuildZig => { u.assert(false, "init requires a build.zig file", .{}); unreachable; }, else => return err, - }; - const mainf = u.detct_mainfile(u.try_index([]const u8, args, 1, ""), null, name) catch |err| switch (err) { + }); + + const entry = if (ptype == .lib) try inquirer.forString(stdout, stdin, "package entry point:", gpa, u.detct_mainfile(u.try_index([]const u8, args, 1, ""), null, name) catch |err| switch (err) { error.CantFindMain => { u.assert(false, "unable to detect package entry point", .{}); unreachable; }, else => return err, - }; + }) else null; - const file = try std.fs.cwd().createFile("zig.mod", .{}); - defer file.close(); + const license = try inquirer.forString(stdout, stdin, "license:", gpa, try detectlicense.detectInDir(gpa, std.fs.cwd())); - const fwriter = file.writer(); - try fwriter.print("id: {s}\n", .{u.random_string(48)}); - try fwriter.print("name: {s}\n", .{name}); - try fwriter.print("main: {s}\n", .{mainf}); - try fwriter.print("dependencies:\n", .{}); + const description = try inquirer.forString(stdout, stdin, "description:", gpa, null); - u.print("Initialized a new package named {s} with entry point {s}", .{ name, mainf }); + std.debug.print("\n", .{}); + std.debug.print("About to write local zig.mod:\n", .{}); + + std.debug.print("\n", .{}); + switch (ptype) { + .exe => try writeExeManifest(stdout, id, name, license, description), + .lib => try writeLibManifest(stdout, id, name, entry.?, license, description), + } + + std.debug.print("\n", .{}); + switch (try inquirer.forConfirm(stdout, stdin, "Is this okay?", gpa)) { + false => { + std.debug.print("okay. quitting...", .{}); + }, + true => { + const file = try std.fs.cwd().createFile("zig.mod", .{}); + defer file.close(); + const w = file.writer(); + switch (ptype) { + .exe => try writeExeManifest(w, id, name, license, description), + .lib => try writeLibManifest(w, id, name, entry.?, license, description), + } + std.debug.print("\n", .{}); + u.print("Successfully initialized new package {s}!\n", .{name}); + }, + } +} + +fn writeExeManifest(w: std.fs.File.Writer, id: []const u8, name: []const u8, license: []const u8, description: []const u8) !void { + try w.print("id: {s}\n", .{id}); + try w.print("name: {s}\n", .{name}); + try w.print("license: {s}\n", .{license}); + try w.print("description: {s}\n", .{description}); + try w.print("dev_dependencies:\n", .{}); +} + +fn writeLibManifest(w: std.fs.File.Writer, id: []const u8, name: []const u8, entry: []const u8, license: []const u8, description: []const u8) !void { + try w.print("id: {s}\n", .{id}); + try w.print("name: {s}\n", .{name}); + try w.print("main: {s}\n", .{entry}); + try w.print("license: {s}\n", .{license}); + try w.print("description: {s}\n", .{description}); + try w.print("dependencies:\n", .{}); } diff --git a/zig.mod b/zig.mod index e9e52d41d9763aab9262100e66cabfd52d3aa071..b2691298a670fab8f78c5bf97c71b670e9a32a4e 100644 --- a/zig.mod +++ b/zig.mod @@ -35,6 +35,9 @@ dependencies: - src: git https://github.com/truemedian/zfetch - src: git https://github.com/nektro/zig-json - src: git https://github.com/nektro/zig-range + - 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 dev_dependencies: - src: git https://github.com/marlersoft/zigwin32 diff --git a/zigmod.lock b/zigmod.lock index 8f6aa87426627c7212c5a55521c7030f25c313ea..181dceac6ff9e700adc91a71f5ab4c88f62be001 100644 --- a/zigmod.lock +++ b/zigmod.lock @@ -1,6 +1,6 @@ 2 git https://github.com/yaml/libyaml tag-0.2.5 -git https://github.com/nektro/zig-ansi commit-8ffadb65efdb424c6fe07a804107a775d55ebfe5 +git https://github.com/nektro/zig-ansi commit-d4a53bcac5b87abecc65491109ec22aaf5f3dc2f git https://github.com/ziglibs/known-folders commit-f5e5fb8370de41fbc220c0c0770d62bdf00530ea git https://github.com/nektro/zig-licenses commit-c9b8cbf3565675a056ad4e9b57cb4f84020e7680 git https://github.com/truemedian/zfetch commit-8bbc7b34cd417794841e1432585334bc969dfe83 @@ -10,4 +10,9 @@ 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-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/marlersoft/zigwin32 commit-ca6b8e5c378e31655fa11b886b44b62f703ef090 diff --git a/zigmod.sum b/zigmod.sum index 5ece5fb4e5756abb2dc421923f7be6c207259eb2..de85b592905718cb7216dd17414c9c265640be07 100644 --- a/zigmod.sum +++ b/zigmod.sum @@ -1,5 +1,5 @@ blake3-22472b867734926b202c055892fb0abb03f91556cd88998e2fe77addb003b1dd v/git/github.com/yaml/libyaml/tag-0.2.5 -blake3-f4e0cc5432c5d4532d7f69f935c0517b5e315934cdaff40e514b5375546f5756 git/github.com/nektro/zig-ansi +blake3-3c76436cde156ef2b92eca8e2a38cc2e07f23055422cfe903585bab8bcc47dd9 git/github.com/nektro/zig-ansi blake3-35a1c330c9999876e71418a7d43ad24ca7d1e23c3b5576e5cb75667e3392cc10 git/github.com/ziglibs/known-folders blake3-42c3fae77ef41d2074e74de2d91cfc66bc591170494b10262dd2aeaea0b60cb5 git/github.com/nektro/zig-licenses blake3-e3072f7fb47e86d53c9a1879e254ba1af55941153fd5f6752ec659b2f14854c9 git/github.com/truemedian/zfetch @@ -9,4 +9,9 @@ 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-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-949c3cd2c9e834190feeecb010c59ab5047fa721cfbdf67e0f0fd847194a6dad git/github.com/marlersoft/zigwin32