diff --git a/src/cmd/fetch.zig b/src/cmd/fetch.zig new file mode 100644 index 0000000000000000000000000000000000000000..2b29162c27ef43d1020eb5fdb5142aaeb23f8f6d --- /dev/null +++ b/src/cmd/fetch.zig @@ -0,0 +1,239 @@ +const std = @import("std"); +const gpa = std.heap.c_allocator; +const fs = std.fs; + +const known_folders = @import("known-folders"); +const u = @import("./../util/index.zig"); +const common = @import("./../common.zig"); + +// +// + +pub fn execute(args: [][]u8) !void { + // + const dir = try fs.path.join(gpa, &.{".zigmod", "deps"}); + + const top_module = try common.collect_deps(dir, "zig.mod", .{ + .log = true, + .update = true, + }); + + // + const f = try fs.cwd().createFile("deps.zig", .{}); + defer f.close(); + + const w = f.writer(); + try w.writeAll("const std = @import(\"std\");\n"); + try w.writeAll("const build = std.build;\n"); + try w.writeAll("\n"); + try w.print("pub const cache = \"{s}\";\n", .{std.zig.fmtEscapes(dir)}); + try w.writeAll("\n"); + try w.print("{s}\n", .{ + \\pub fn addAllTo(exe: *build.LibExeObjStep) void { + \\ @setEvalBranchQuota(1_000_000); + \\ for (packages) |pkg| { + \\ exe.addPackage(pkg); + \\ } + \\ if (c_include_dirs.len > 0 or c_source_files.len > 0) { + \\ exe.linkLibC(); + \\ } + \\ for (c_include_dirs) |dir| { + \\ exe.addIncludeDir(dir); + \\ } + \\ inline for (c_source_files) |fpath| { + \\ exe.addCSourceFile(fpath[1], @field(c_source_flags, fpath[0])); + \\ } + \\ for (system_libs) |lib| { + \\ exe.linkSystemLibrary(lib); + \\ } + \\} + \\ + \\fn get_flags(comptime index: usize) []const u8 { + \\ return @field(c_source_flags, _paths[index]); + \\} + \\ + }); + + const list = &std.ArrayList(u.Module).init(gpa); + try common.collect_pkgs(top_module, list); + + try w.writeAll("pub const _ids = .{\n"); + try print_ids(w, list.items); + try w.writeAll("};\n\n"); + + try w.print("pub const _paths = {s}\n", .{".{"}); + try print_paths(w, list.items); + try w.writeAll("};\n\n"); + + try w.writeAll("pub const package_data = struct {\n"); + const duped = &std.ArrayList(u.Module).init(gpa); + for (list.items) |mod| { + if (mod.main.len > 0 and mod.clean_path.len > 0) { + try duped.append(mod); + } + } + try print_pkg_data_to(w, duped, &std.ArrayList(u.Module).init(gpa)); + try w.writeAll("};\n\n"); + + try w.writeAll("pub const packages = "); + try print_deps(w, dir, top_module, 0, true); + try w.writeAll(";\n\n"); + + try w.writeAll("pub const pkgs = "); + try print_deps(w, dir, top_module, 0, false); + try w.writeAll(";\n\n"); + + try w.writeAll("pub const c_include_dirs = &[_][]const u8{\n"); + try print_incl_dirs_to(w, list.items); + try w.writeAll("};\n\n"); + + try w.writeAll("pub const c_source_flags = struct {\n"); + try print_csrc_flags_to(w, list.items); + try w.writeAll("};\n\n"); + + try w.writeAll("pub const c_source_files = &[_][2][]const u8{\n"); + try print_csrc_dirs_to(w, list.items); + try w.writeAll("};\n\n"); + + try w.writeAll("pub const system_libs = &[_][]const u8{\n"); + try print_sys_libs_to(w, list.items, &std.ArrayList([]const u8).init(gpa)); + try w.writeAll("};\n\n"); +} + +fn print_ids(w: fs.File.Writer, list: []u.Module) !void { + for (list) |mod| { + if (mod.is_sys_lib) { + continue; + } + try w.print(" \"{s}\",\n", .{mod.id}); + } +} + +fn print_paths(w: fs.File.Writer, list: []u.Module) !void { + for (list) |mod| { + if (mod.is_sys_lib) { + continue; + } + if (mod.clean_path.len == 0) { + try w.print(" \"\",\n", .{}); + } else { + const s = std.fs.path.sep_str; + try w.print(" \"{s}{s}{s}\",\n", .{std.zig.fmtEscapes(s), std.zig.fmtEscapes(mod.clean_path), std.zig.fmtEscapes(s)}); + } + } +} + +fn print_deps(w: fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32, array: bool) anyerror!void { + if (m.has_no_zig_deps() and tabs > 0) { + try w.print("null", .{}); + return; + } + if (array) { + try u.print_all(w, .{"&[_]build.Pkg{"}, true); + } else { + try u.print_all(w, .{"struct {"}, true); + } + const t = " "; + const r = try u.repeat(t, tabs); + for (m.deps) |d, i| { + if (d.main.len == 0) { + continue; + } + if (!array) { + try w.print(" pub const {s} = packages[{}];\n", .{std.mem.replaceOwned(u8, gpa, d.name, "-", "_"), i}); + } + else { + try w.print(" package_data._{s},\n", .{d.id}); + } + } + try w.print("{s}", .{try u.concat(&.{r,"}"})}); +} + +fn print_incl_dirs_to(w: fs.File.Writer, list: []u.Module) !void { + for (list) |mod, i| { + if (mod.is_sys_lib) { + continue; + } + for (mod.c_include_dirs) |it| { + if (i > 0) { + try w.print(" cache ++ _paths[{}] ++ \"{s}\",\n", .{i, std.zig.fmtEscapes(it)}); + } else { + try w.print(" \"{s}\",\n", .{std.zig.fmtEscapes(it)}); + } + } + } +} + +fn print_csrc_dirs_to(w: fs.File.Writer, list: []u.Module) !void { + for (list) |mod, i| { + if (mod.is_sys_lib) { + continue; + } + for (mod.c_source_files) |it| { + if (i > 0) { + try w.print(" {s}_ids[{}], cache ++ _paths[{}] ++ \"{s}\"{s},\n", .{"[_][]const u8{", i, i, it, "}"}); + } else { + try w.print(" {s}_ids[{}], \".{s}/{s}\"{s},\n", .{"[_][]const u8{", i, std.zig.fmtEscapes(mod.clean_path), it, "}"}); + } + } + } +} + +fn print_csrc_flags_to(w: fs.File.Writer, list: []u.Module) !void { + for (list) |mod, i| { + if (mod.is_sys_lib) { + continue; + } + if (mod.c_source_flags.len == 0 and mod.c_source_files.len == 0) { + continue; + } + try w.print(" pub const @\"{s}\" = {s}", .{mod.id, "&.{"}); + for (mod.c_source_flags) |it| { + try w.print("\"{s}\",", .{std.zig.fmtEscapes(it)}); + } + try w.print("{s};\n", .{"}"}); + + } +} + +fn print_sys_libs_to(w: fs.File.Writer, list: []u.Module, list2: *std.ArrayList([]const u8)) !void { + for (list) |mod| { + if (!mod.is_sys_lib) { + continue; + } + try w.print(" \"{s}\",\n", .{mod.name}); + } +} + +fn print_pkg_data_to(w: fs.File.Writer, list: *std.ArrayList(u.Module), list2: *std.ArrayList(u.Module)) anyerror!void { + var i: usize = 0; + while (i < list.items.len) : (i += 1) { + const mod = list.items[i]; + if (contains_all(mod.deps, list2)) { + try w.print(" pub const _{s} = build.Pkg{{ .name = \"{s}\", .path = cache ++ \"/{s}/{s}\", .dependencies = &[_]build.Pkg{{", .{mod.id, mod.name, std.zig.fmtEscapes(mod.clean_path), mod.main}); + for (mod.deps) |d| { + if (d.main.len > 0) { + try w.print(" _{s},", .{d.id}); + } + } + try w.print(" }} }};\n", .{}); + + try list2.append(mod); + _ = list.orderedRemove(i); + break; + } + } + if (list.items.len > 0) { + try print_pkg_data_to(w, list, list2); + } +} + +/// returns if all of the zig modules in needles are in haystack +fn contains_all(needles: []u.Module, haystack: *std.ArrayList(u.Module)) bool { + for (needles) |item| { + if (item.main.len > 0 and !u.list_contains_gen(u.Module, haystack, item)) { + return false; + } + } + return true; +} diff --git a/src/cmd/init.zig b/src/cmd/init.zig new file mode 100644 index 0000000000000000000000000000000000000000..1a7823384615f3a611001ab1d78adad9cce23561 --- /dev/null +++ b/src/cmd/init.zig @@ -0,0 +1,53 @@ +const std = @import("std"); +const gpa = std.heap.c_allocator; + +const u = @import("./../util/index.zig"); + +// +// + +pub fn execute(args: [][]u8) !void { + const name = try detect_pkgname(u.try_index([]const u8, args, 0, "")); + const mainf = try detct_mainfile(u.try_index([]const u8, args, 1, "")); + + const file = try std.fs.cwd().createFile("zig.mod", .{}); + defer file.close(); + + 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", .{}); + + u.print("Initialized a new package named {s} with entry point {s}", .{name, mainf}); +} + +fn detect_pkgname(def: []const u8) ![]const u8 { + if (def.len > 0) { + return def; + } + const dpath = try std.fs.cwd().realpathAlloc(gpa, "build.zig"); + const split = try u.split(dpath, std.fs.path.sep_str); + var name = split[split.len-2]; + name = u.trim_prefix(name, "zig-"); + u.assert(name.len > 0, "package name must not be an empty string", .{}); + return name; +} + +fn detct_mainfile(def: []const u8) ![]const u8 { + if (def.len > 0) { + if (try u.does_file_exist(def)) { + if (std.mem.endsWith(u8, def, ".zig")) { + return def; + } + } + } + if (try u.does_file_exist(try std.fs.path.join(gpa, &.{"src", "lib.zig"}))) { + return "src/lib.zig"; + } + if (try u.does_file_exist(try std.fs.path.join(gpa, &.{"src", "main.zig"}))) { + return "src/main.zig"; + } + u.assert(false, "unable to detect package entry point", .{}); + unreachable; +} diff --git a/src/cmd/sum.zig b/src/cmd/sum.zig new file mode 100644 index 0000000000000000000000000000000000000000..fe22b9e729eb57778a6d76ac97a074a7f16c83dd --- /dev/null +++ b/src/cmd/sum.zig @@ -0,0 +1,46 @@ +const std = @import("std"); +const gpa = std.heap.c_allocator; + +const known_folders = @import("known-folders"); +const u = @import("./../util/index.zig"); +const common = @import("./../common.zig"); + +// +// + +pub fn execute(args: [][]u8) !void { + // + const dir = try std.fs.path.join(gpa, &.{".zigmod", "deps"}); + + const top_module = try common.collect_deps(dir, "zig.mod", .{ + .log = false, + .update = false, + }); + + // + const f = try std.fs.cwd().createFile("zig.sum", .{}); + defer f.close(); + const w = f.writer(); + + // + const module_list = &std.ArrayList(u.Module).init(gpa); + try dedupe_mod_list(module_list, top_module); + + for (module_list.items) |m| { + if (m.clean_path.len == 0) { continue; } + const hash = try m.get_hash(dir); + try w.print("{s} {s}\n", .{hash, m.clean_path}); + } +} + +fn dedupe_mod_list(list: *std.ArrayList(u.Module), module: u.Module) anyerror!void { + if (u.list_contains_gen(u.Module, list, module)) { + return; + } + if (module.clean_path.len > 0) { + try list.append(module); + } + for (module.deps) |m| { + try dedupe_mod_list(list, m); + } +} diff --git a/src/cmd/zpm.zig b/src/cmd/zpm.zig new file mode 100644 index 0000000000000000000000000000000000000000..4f2f7c95858083568de54770c3d2cb7ece7cb6ad --- /dev/null +++ b/src/cmd/zpm.zig @@ -0,0 +1,34 @@ +const std = @import("std"); +const gpa = std.heap.c_allocator; + +const u = @import("./../util/index.zig"); + +// +// + +pub const commands = struct { + pub const add = @import("./zpm_add.zig"); +}; + +pub fn execute(args: [][]u8) !void { + if (args.len == 0) { + std.debug.warn("{s}\n", .{ + \\This is a subcommand for use with https://github.com/zigtools/zpm-server instances but has no default behavior on its own aside from showing you this nice help text. + \\ + \\The default remote is https://zpm.random-projects.net/. + \\ + \\The subcommands available are: + \\ - add Append this package to your dependencies + }); + return; + } + + inline for (std.meta.declarations(commands)) |decl| { + if (std.mem.eql(u8, args[0], decl.name)) { + const cmd = @field(commands, decl.name); + try cmd.execute(args[1..]); + return; + } + } + std.debug.panic("error: unknown command \"{s}\" for \"zigmod zpm\"", .{args[0]}); +} diff --git a/src/cmd/zpm_add.zig b/src/cmd/zpm_add.zig new file mode 100644 index 0000000000000000000000000000000000000000..8c5c4a241fbd4c17cac0ff394c37d453d8674a3a --- /dev/null +++ b/src/cmd/zpm_add.zig @@ -0,0 +1,90 @@ +const std = @import("std"); +const gpa = std.heap.c_allocator; + +const zuri = @import("zuri"); +const iguanatls = @import("iguanatls"); +const u = @import("./../util/index.zig"); + +// +// + +pub const Zpm = struct { + pub const Package = struct { + author: []const u8, + name: []const u8, + tags: [][]const u8, + git: []const u8, + root_file: ?[]const u8, + description: []const u8, + }; +}; + +pub fn execute(args: [][]u8) !void { + const url = try zuri.Uri.parse("https://zpm.random-projects.net:443/api/packages", true); + + const sock = try std.net.tcpConnectToHost(gpa, url.host.name, url.port.?); + defer sock.close(); + + var client = try iguanatls.client_connect(.{ + .reader = sock.reader(), + .writer = sock.writer(), + .cert_verifier = .none, + .temp_allocator = gpa, + .ciphersuites = iguanatls.ciphersuites.all, + }, url.host.name); + defer client.close_notify() catch {}; + + const w = client.writer(); + try w.print("GET {s} HTTP/1.1\r\n", .{url.path}); + try w.print("Host: {s}:{}\r\n", .{url.host.name, url.port.?}); + try w.writeAll("Accept: application/json; charset=UTF-8\r\n"); + try w.writeAll("Connection: close\r\n"); + try w.writeAll("\r\n"); + + const r = client.reader(); + var buf: [1]u8 = undefined; + const data = &std.ArrayList(u8).init(gpa); + while (true) { + const len = try r.read(&buf); + if (len == 0) { + break; + } + try data.appendSlice(buf[0..len]); + } + + const index = std.mem.indexOf(u8, data.items, "\r\n\r\n").?; + const html_contents = data.items[index..]; + + var stream = std.json.TokenStream.init(html_contents[4..]); + const res = try std.json.parse([]Zpm.Package, &stream, .{ .allocator = gpa, }); + + const found = blk: { + for (res) |pkg| { + if (std.mem.eql(u8, pkg.name, args[0])) { + break :blk pkg; + } + } + u.assert(false, "no package with name '{s}' found", .{args[0]}); + unreachable; + }; + + u.assert(found.root_file != null, "package must have an entry point to be able to be added to your dependencies", .{}); + + const self_module = try u.ModFile.init(gpa, "zig.mod"); + for (self_module.deps) |dep| { + if (std.mem.eql(u8, dep.name, found.name)) { + u.assert(false, "dependency with name '{s}' already exists in your dependencies", .{found.name}); + } + } + + const file = try std.fs.cwd().openFile("zig.mod", .{ .read=true, .write=true }); + try file.seekTo(try file.getEndPos()); + + const file_w = file.writer(); + try file_w.print("\n", .{}); + try file_w.print(" - src: git {s}\n", .{found.git}); + try file_w.print(" name: {s}\n", .{found.name}); + try file_w.print(" main: {s}\n", .{found.root_file.?[1..]}); + + std.log.info("Successfully added package {s} by {s}", .{found.name, found.author}); +} diff --git a/src/cmd_fetch.zig b/src/cmd_fetch.zig deleted file mode 100644 index 6fb09c25a49caba30e35bbcd0866479e8139ff52..0000000000000000000000000000000000000000 --- a/src/cmd_fetch.zig +++ /dev/null @@ -1,239 +0,0 @@ -const std = @import("std"); -const gpa = std.heap.c_allocator; -const fs = std.fs; - -const known_folders = @import("known-folders"); -const u = @import("./util/index.zig"); -const common = @import("./common.zig"); - -// -// - -pub fn execute(args: [][]u8) !void { - // - const dir = try fs.path.join(gpa, &.{".zigmod", "deps"}); - - const top_module = try common.collect_deps(dir, "zig.mod", .{ - .log = true, - .update = true, - }); - - // - const f = try fs.cwd().createFile("deps.zig", .{}); - defer f.close(); - - const w = f.writer(); - try w.writeAll("const std = @import(\"std\");\n"); - try w.writeAll("const build = std.build;\n"); - try w.writeAll("\n"); - try w.print("pub const cache = \"{s}\";\n", .{std.zig.fmtEscapes(dir)}); - try w.writeAll("\n"); - try w.print("{s}\n", .{ - \\pub fn addAllTo(exe: *build.LibExeObjStep) void { - \\ @setEvalBranchQuota(1_000_000); - \\ for (packages) |pkg| { - \\ exe.addPackage(pkg); - \\ } - \\ if (c_include_dirs.len > 0 or c_source_files.len > 0) { - \\ exe.linkLibC(); - \\ } - \\ for (c_include_dirs) |dir| { - \\ exe.addIncludeDir(dir); - \\ } - \\ inline for (c_source_files) |fpath| { - \\ exe.addCSourceFile(fpath[1], @field(c_source_flags, fpath[0])); - \\ } - \\ for (system_libs) |lib| { - \\ exe.linkSystemLibrary(lib); - \\ } - \\} - \\ - \\fn get_flags(comptime index: usize) []const u8 { - \\ return @field(c_source_flags, _paths[index]); - \\} - \\ - }); - - const list = &std.ArrayList(u.Module).init(gpa); - try common.collect_pkgs(top_module, list); - - try w.writeAll("pub const _ids = .{\n"); - try print_ids(w, list.items); - try w.writeAll("};\n\n"); - - try w.print("pub const _paths = {s}\n", .{".{"}); - try print_paths(w, list.items); - try w.writeAll("};\n\n"); - - try w.writeAll("pub const package_data = struct {\n"); - const duped = &std.ArrayList(u.Module).init(gpa); - for (list.items) |mod| { - if (mod.main.len > 0 and mod.clean_path.len > 0) { - try duped.append(mod); - } - } - try print_pkg_data_to(w, duped, &std.ArrayList(u.Module).init(gpa)); - try w.writeAll("};\n\n"); - - try w.writeAll("pub const packages = "); - try print_deps(w, dir, top_module, 0, true); - try w.writeAll(";\n\n"); - - try w.writeAll("pub const pkgs = "); - try print_deps(w, dir, top_module, 0, false); - try w.writeAll(";\n\n"); - - try w.writeAll("pub const c_include_dirs = &[_][]const u8{\n"); - try print_incl_dirs_to(w, list.items); - try w.writeAll("};\n\n"); - - try w.writeAll("pub const c_source_flags = struct {\n"); - try print_csrc_flags_to(w, list.items); - try w.writeAll("};\n\n"); - - try w.writeAll("pub const c_source_files = &[_][2][]const u8{\n"); - try print_csrc_dirs_to(w, list.items); - try w.writeAll("};\n\n"); - - try w.writeAll("pub const system_libs = &[_][]const u8{\n"); - try print_sys_libs_to(w, list.items, &std.ArrayList([]const u8).init(gpa)); - try w.writeAll("};\n\n"); -} - -fn print_ids(w: fs.File.Writer, list: []u.Module) !void { - for (list) |mod| { - if (mod.is_sys_lib) { - continue; - } - try w.print(" \"{s}\",\n", .{mod.id}); - } -} - -fn print_paths(w: fs.File.Writer, list: []u.Module) !void { - for (list) |mod| { - if (mod.is_sys_lib) { - continue; - } - if (mod.clean_path.len == 0) { - try w.print(" \"\",\n", .{}); - } else { - const s = std.fs.path.sep_str; - try w.print(" \"{s}{s}{s}\",\n", .{std.zig.fmtEscapes(s), std.zig.fmtEscapes(mod.clean_path), std.zig.fmtEscapes(s)}); - } - } -} - -fn print_deps(w: fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32, array: bool) anyerror!void { - if (m.has_no_zig_deps() and tabs > 0) { - try w.print("null", .{}); - return; - } - if (array) { - try u.print_all(w, .{"&[_]build.Pkg{"}, true); - } else { - try u.print_all(w, .{"struct {"}, true); - } - const t = " "; - const r = try u.repeat(t, tabs); - for (m.deps) |d, i| { - if (d.main.len == 0) { - continue; - } - if (!array) { - try w.print(" pub const {s} = packages[{}];\n", .{std.mem.replaceOwned(u8, gpa, d.name, "-", "_"), i}); - } - else { - try w.print(" package_data._{s},\n", .{d.id}); - } - } - try w.print("{s}", .{try u.concat(&.{r,"}"})}); -} - -fn print_incl_dirs_to(w: fs.File.Writer, list: []u.Module) !void { - for (list) |mod, i| { - if (mod.is_sys_lib) { - continue; - } - for (mod.c_include_dirs) |it| { - if (i > 0) { - try w.print(" cache ++ _paths[{}] ++ \"{s}\",\n", .{i, std.zig.fmtEscapes(it)}); - } else { - try w.print(" \"{s}\",\n", .{std.zig.fmtEscapes(it)}); - } - } - } -} - -fn print_csrc_dirs_to(w: fs.File.Writer, list: []u.Module) !void { - for (list) |mod, i| { - if (mod.is_sys_lib) { - continue; - } - for (mod.c_source_files) |it| { - if (i > 0) { - try w.print(" {s}_ids[{}], cache ++ _paths[{}] ++ \"{s}\"{s},\n", .{"[_][]const u8{", i, i, it, "}"}); - } else { - try w.print(" {s}_ids[{}], \".{s}/{s}\"{s},\n", .{"[_][]const u8{", i, std.zig.fmtEscapes(mod.clean_path), it, "}"}); - } - } - } -} - -fn print_csrc_flags_to(w: fs.File.Writer, list: []u.Module) !void { - for (list) |mod, i| { - if (mod.is_sys_lib) { - continue; - } - if (mod.c_source_flags.len == 0 and mod.c_source_files.len == 0) { - continue; - } - try w.print(" pub const @\"{s}\" = {s}", .{mod.id, "&.{"}); - for (mod.c_source_flags) |it| { - try w.print("\"{s}\",", .{std.zig.fmtEscapes(it)}); - } - try w.print("{s};\n", .{"}"}); - - } -} - -fn print_sys_libs_to(w: fs.File.Writer, list: []u.Module, list2: *std.ArrayList([]const u8)) !void { - for (list) |mod| { - if (!mod.is_sys_lib) { - continue; - } - try w.print(" \"{s}\",\n", .{mod.name}); - } -} - -fn print_pkg_data_to(w: fs.File.Writer, list: *std.ArrayList(u.Module), list2: *std.ArrayList(u.Module)) anyerror!void { - var i: usize = 0; - while (i < list.items.len) : (i += 1) { - const mod = list.items[i]; - if (contains_all(mod.deps, list2)) { - try w.print(" pub const _{s} = build.Pkg{{ .name = \"{s}\", .path = cache ++ \"/{s}/{s}\", .dependencies = &[_]build.Pkg{{", .{mod.id, mod.name, std.zig.fmtEscapes(mod.clean_path), mod.main}); - for (mod.deps) |d| { - if (d.main.len > 0) { - try w.print(" _{s},", .{d.id}); - } - } - try w.print(" }} }};\n", .{}); - - try list2.append(mod); - _ = list.orderedRemove(i); - break; - } - } - if (list.items.len > 0) { - try print_pkg_data_to(w, list, list2); - } -} - -/// returns if all of the zig modules in needles are in haystack -fn contains_all(needles: []u.Module, haystack: *std.ArrayList(u.Module)) bool { - for (needles) |item| { - if (item.main.len > 0 and !u.list_contains_gen(u.Module, haystack, item)) { - return false; - } - } - return true; -} diff --git a/src/cmd_init.zig b/src/cmd_init.zig deleted file mode 100644 index 536451134fff37afa7191886ad9b8658789bc545..0000000000000000000000000000000000000000 --- a/src/cmd_init.zig +++ /dev/null @@ -1,53 +0,0 @@ -const std = @import("std"); -const gpa = std.heap.c_allocator; - -const u = @import("./util/index.zig"); - -// -// - -pub fn execute(args: [][]u8) !void { - const name = try detect_pkgname(u.try_index([]const u8, args, 0, "")); - const mainf = try detct_mainfile(u.try_index([]const u8, args, 1, "")); - - const file = try std.fs.cwd().createFile("zig.mod", .{}); - defer file.close(); - - 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", .{}); - - u.print("Initialized a new package named {s} with entry point {s}", .{name, mainf}); -} - -fn detect_pkgname(def: []const u8) ![]const u8 { - if (def.len > 0) { - return def; - } - const dpath = try std.fs.cwd().realpathAlloc(gpa, "build.zig"); - const split = try u.split(dpath, std.fs.path.sep_str); - var name = split[split.len-2]; - name = u.trim_prefix(name, "zig-"); - u.assert(name.len > 0, "package name must not be an empty string", .{}); - return name; -} - -fn detct_mainfile(def: []const u8) ![]const u8 { - if (def.len > 0) { - if (try u.does_file_exist(def)) { - if (std.mem.endsWith(u8, def, ".zig")) { - return def; - } - } - } - if (try u.does_file_exist(try std.fs.path.join(gpa, &.{"src", "lib.zig"}))) { - return "src/lib.zig"; - } - if (try u.does_file_exist(try std.fs.path.join(gpa, &.{"src", "main.zig"}))) { - return "src/main.zig"; - } - u.assert(false, "unable to detect package entry point", .{}); - unreachable; -} diff --git a/src/cmd_sum.zig b/src/cmd_sum.zig deleted file mode 100644 index 7a8da597d9a4600026e7e7ee17abc9ba1defee5b..0000000000000000000000000000000000000000 --- a/src/cmd_sum.zig +++ /dev/null @@ -1,46 +0,0 @@ -const std = @import("std"); -const gpa = std.heap.c_allocator; - -const known_folders = @import("known-folders"); -const u = @import("./util/index.zig"); -const common = @import("./common.zig"); - -// -// - -pub fn execute(args: [][]u8) !void { - // - const dir = try std.fs.path.join(gpa, &.{".zigmod", "deps"}); - - const top_module = try common.collect_deps(dir, "zig.mod", .{ - .log = false, - .update = false, - }); - - // - const f = try std.fs.cwd().createFile("zig.sum", .{}); - defer f.close(); - const w = f.writer(); - - // - const module_list = &std.ArrayList(u.Module).init(gpa); - try dedupe_mod_list(module_list, top_module); - - for (module_list.items) |m| { - if (m.clean_path.len == 0) { continue; } - const hash = try m.get_hash(dir); - try w.print("{s} {s}\n", .{hash, m.clean_path}); - } -} - -fn dedupe_mod_list(list: *std.ArrayList(u.Module), module: u.Module) anyerror!void { - if (u.list_contains_gen(u.Module, list, module)) { - return; - } - if (module.clean_path.len > 0) { - try list.append(module); - } - for (module.deps) |m| { - try dedupe_mod_list(list, m); - } -} diff --git a/src/cmd_zpm.zig b/src/cmd_zpm.zig deleted file mode 100644 index bc96bc7ff6b7084495875270caedeb0d79cef9eb..0000000000000000000000000000000000000000 --- a/src/cmd_zpm.zig +++ /dev/null @@ -1,34 +0,0 @@ -const std = @import("std"); -const gpa = std.heap.c_allocator; - -const u = @import("./util/index.zig"); - -// -// - -pub const commands = struct { - pub const add = @import("./cmd_zpm_add.zig"); -}; - -pub fn execute(args: [][]u8) !void { - if (args.len == 0) { - std.debug.warn("{s}\n", .{ - \\This is a subcommand for use with https://github.com/zigtools/zpm-server instances but has no default behavior on its own aside from showing you this nice help text. - \\ - \\The default remote is https://zpm.random-projects.net/. - \\ - \\The subcommands available are: - \\ - add Append this package to your dependencies - }); - return; - } - - inline for (std.meta.declarations(commands)) |decl| { - if (std.mem.eql(u8, args[0], decl.name)) { - const cmd = @field(commands, decl.name); - try cmd.execute(args[1..]); - return; - } - } - std.debug.panic("error: unknown command \"{s}\" for \"zigmod zpm\"", .{args[0]}); -} diff --git a/src/cmd_zpm_add.zig b/src/cmd_zpm_add.zig deleted file mode 100644 index 273af78f8478f7bdc104614f91440cbd82ca3ba5..0000000000000000000000000000000000000000 --- a/src/cmd_zpm_add.zig +++ /dev/null @@ -1,90 +0,0 @@ -const std = @import("std"); -const gpa = std.heap.c_allocator; - -const zuri = @import("zuri"); -const iguanatls = @import("iguanatls"); -const u = @import("./util/index.zig"); - -// -// - -pub const Zpm = struct { - pub const Package = struct { - author: []const u8, - name: []const u8, - tags: [][]const u8, - git: []const u8, - root_file: ?[]const u8, - description: []const u8, - }; -}; - -pub fn execute(args: [][]u8) !void { - const url = try zuri.Uri.parse("https://zpm.random-projects.net:443/api/packages", true); - - const sock = try std.net.tcpConnectToHost(gpa, url.host.name, url.port.?); - defer sock.close(); - - var client = try iguanatls.client_connect(.{ - .reader = sock.reader(), - .writer = sock.writer(), - .cert_verifier = .none, - .temp_allocator = gpa, - .ciphersuites = iguanatls.ciphersuites.all, - }, url.host.name); - defer client.close_notify() catch {}; - - const w = client.writer(); - try w.print("GET {s} HTTP/1.1\r\n", .{url.path}); - try w.print("Host: {s}:{}\r\n", .{url.host.name, url.port.?}); - try w.writeAll("Accept: application/json; charset=UTF-8\r\n"); - try w.writeAll("Connection: close\r\n"); - try w.writeAll("\r\n"); - - const r = client.reader(); - var buf: [1]u8 = undefined; - const data = &std.ArrayList(u8).init(gpa); - while (true) { - const len = try r.read(&buf); - if (len == 0) { - break; - } - try data.appendSlice(buf[0..len]); - } - - const index = std.mem.indexOf(u8, data.items, "\r\n\r\n").?; - const html_contents = data.items[index..]; - - var stream = std.json.TokenStream.init(html_contents[4..]); - const res = try std.json.parse([]Zpm.Package, &stream, .{ .allocator = gpa, }); - - const found = blk: { - for (res) |pkg| { - if (std.mem.eql(u8, pkg.name, args[0])) { - break :blk pkg; - } - } - u.assert(false, "no package with name '{s}' found", .{args[0]}); - unreachable; - }; - - u.assert(found.root_file != null, "package must have an entry point to be able to be added to your dependencies", .{}); - - const self_module = try u.ModFile.init(gpa, "zig.mod"); - for (self_module.deps) |dep| { - if (std.mem.eql(u8, dep.name, found.name)) { - u.assert(false, "dependency with name '{s}' already exists in your dependencies", .{found.name}); - } - } - - const file = try std.fs.cwd().openFile("zig.mod", .{ .read=true, .write=true }); - try file.seekTo(try file.getEndPos()); - - const file_w = file.writer(); - try file_w.print("\n", .{}); - try file_w.print(" - src: git {s}\n", .{found.git}); - try file_w.print(" name: {s}\n", .{found.name}); - try file_w.print(" main: {s}\n", .{found.root_file.?[1..]}); - - std.log.info("Successfully added package {s} by {s}", .{found.name, found.author}); -} diff --git a/src/main.zig b/src/main.zig index 409348975844c9473da1d1ae35e1d914d083883e..ebadb4563e1e88f8851d8e0bfd26efa9e88b34e7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -9,14 +9,14 @@ pub const common = @import("./common.zig"); // pub const commands_to_bootstrap = struct { - pub const fetch = @import("./cmd_fetch.zig"); + pub const fetch = @import("./cmd/fetch.zig"); }; pub const commands = struct { - pub const init = @import("./cmd_init.zig"); - pub const fetch = @import("./cmd_fetch.zig"); - pub const sum = @import("./cmd_sum.zig"); - pub const zpm = @import("./cmd_zpm.zig"); + pub const init = @import("./cmd/init.zig"); + pub const fetch = @import("./cmd/fetch.zig"); + pub const sum = @import("./cmd/sum.zig"); + pub const zpm = @import("./cmd/zpm.zig"); pub const license = @import("./cmd/license.zig"); };