diff --git a/src/cmd/zpm.zig b/src/cmd/zpm.zig index 6f8591b52fb94c8da7d7ea04d660d48b6cd6e06d..311ced077546161cc5299d66115f147ff103f0d6 100644 --- a/src/cmd/zpm.zig +++ b/src/cmd/zpm.zig @@ -21,10 +21,12 @@ pub const server_root = "https://zig.pm/api"; pub const Package = struct { author: string, name: string, - tags: []string, + tags: []const string, git: string, - root_file: ?string, + root_file: string, description: string, + source: u32, + links: []const ?string, }; pub fn execute(args: [][]u8) !void { @@ -62,3 +64,45 @@ pub fn server_fetch(url: string) !json.Value { const val = try json.parse(gpa, body_content); return val; } + +pub fn server_fetchArray(url: string) ![]const Package { + const val = try server_fetch(url); + var list = std.ArrayList(Package).init(gpa); + errdefer list.deinit(); + + for (val.Array) |item| { + if (item.getT("root_file", .String) == null) continue; + try list.append(Package{ + .name = item.getT("name", .String).?, + .author = item.getT("author", .String).?, + .description = item.getT("description", .String).?, + .tags = try valueStrArray(item.getT("tags", .Array).?), + .git = item.getT("git", .String).?, + .root_file = item.getT("root_file", .String).?, + .source = @intCast(u32, item.getT("source", .Int).?), + .links = try valueLinks(item.get("links").?), + }); + } + return list.toOwnedSlice(); +} + +fn valueStrArray(vals: []json.Value) ![]string { + var list = std.ArrayList(string).init(gpa); + errdefer list.deinit(); + + for (vals) |item| { + if (item != .String) continue; + try list.append(item.String); + } + return list.toOwnedSlice(); +} + +fn valueLinks(vals: json.Value) ![]?string { + var list = std.ArrayList(?string).init(gpa); + errdefer list.deinit(); + + try list.append(vals.getT("github", .String)); + try list.append(vals.getT("aquila", .String)); + try list.append(vals.getT("astrolabe", .String)); + return list.toOwnedSlice(); +} diff --git a/src/cmd/zpm/add.zig b/src/cmd/zpm/add.zig index 3ef9aeec8bd64f0d64df164c47673e19ae3a4630..74b13358273073f477e17b3fc395c9c4eb50874e 100644 --- a/src/cmd/zpm/add.zig +++ b/src/cmd/zpm/add.zig @@ -10,17 +10,8 @@ const zpm = @import("./../zpm.zig"); // pub fn execute(args: [][]u8) !void { - const url = "https://zpm.random-projects.net/api/packages"; - - const req = try zfetch.Request.init(gpa, url, null); - defer req.deinit(); - - try req.do(.GET, null, null); - const r = req.reader(); - - const body_content = try r.readAllAlloc(gpa, std.math.maxInt(usize)); - var stream = std.json.TokenStream.init(body_content); - const val = try std.json.parse([]zpm.Package, &stream, .{ .allocator = gpa }); + const url = try std.mem.join(gpa, "/", &.{ zpm.server_root, "packages" }); + const val = try zpm.server_fetchArray(url); const found = blk: { for (val) |pkg| { @@ -31,8 +22,6 @@ pub fn execute(args: [][]u8) !void { u.fail("no package with name '{s}' found", .{args[0]}); }; - 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 zigmod.ModFile.init(gpa); for (self_module.deps) |dep| { if (std.mem.eql(u8, dep.name, found.name)) { @@ -74,7 +63,7 @@ pub fn execute(args: [][]u8) !void { try file_w.print(" - src: git {s}\n", .{u.trim_suffix(found.git, ".git")}); if (!(has_zigdotmod or has_zigmodyml)) { try file_w.print(" name: {s}\n", .{found.name}); - try file_w.print(" main: {s}\n", .{found.root_file.?[1..]}); + 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/zpm/search.zig b/src/cmd/zpm/search.zig index f381dd4618db98d922a09af7d2ce14b4897f47ca..90279e0678a6f09d335db339e53f33424438a8c3 100644 --- a/src/cmd/zpm/search.zig +++ b/src/cmd/zpm/search.zig @@ -13,25 +13,7 @@ pub fn execute(args: [][]u8) !void { const out = std.io.getStdOut().writer(); const url = try std.mem.join(gpa, "/", &.{ zpm.server_root, "packages" }); - const val = try zpm.server_fetch(url); - - var arr = std.ArrayList(zpm.Package).init(gpa); - defer arr.deinit(); - - for (val.Array) |item| { - if (item.get("root_file")) |_| {} else { - continue; - } - try arr.append(zpm.Package{ - .name = item.getT("name", .String).?, - .author = item.getT("author", .String).?, - .description = item.getT("description", .String).?, - .tags = &.{}, - .git = "", - .root_file = "", - }); - } - const list = arr.items; + const list = try zpm.server_fetchArray(url); const name_col_width = blk: { var w: usize = 4;