| author | |
| committer | |
| log | b84b5b55b06a5117e03acee33ed40b7b90a1fe1c |
| tree | 3a29a2bc0f3a48cbe620fc22a8cfed9c78e45e07 |
| parent | 0637df01d2a87c2b019f8f794ba6625119714515 |
3 files changed, 50 insertions(+), 35 deletions(-)
src/cmd/zpm.zig+46-2| ... | @@ -21,10 +21,12 @@ pub const server_root = "https://zig.pm/api"; | ... | @@ -21,10 +21,12 @@ pub const server_root = "https://zig.pm/api"; |
| 21 | pub const Package = struct { | 21 | pub const Package = struct { |
| 22 | author: string, | 22 | author: string, |
| 23 | name: string, | 23 | name: string, |
| 24 | tags: []string, | 24 | tags: []const string, |
| 25 | git: string, | 25 | git: string, |
| 26 | root_file: ?string, | 26 | root_file: string, |
| 27 | description: string, | 27 | description: string, |
| 28 | source: u32, | ||
| 29 | links: []const ?string, | ||
| 28 | }; | 30 | }; |
| 29 | 31 | ||
| 30 | pub fn execute(args: [][]u8) !void { | 32 | pub fn execute(args: [][]u8) !void { |
| ... | @@ -62,3 +64,45 @@ pub fn server_fetch(url: string) !json.Value { | ... | @@ -62,3 +64,45 @@ pub fn server_fetch(url: string) !json.Value { |
| 62 | const val = try json.parse(gpa, body_content); | 64 | const val = try json.parse(gpa, body_content); |
| 63 | return val; | 65 | return val; |
| 64 | } | 66 | } |
| 67 | |||
| 68 | pub fn server_fetchArray(url: string) ![]const Package { | ||
| 69 | const val = try server_fetch(url); | ||
| 70 | var list = std.ArrayList(Package).init(gpa); | ||
| 71 | errdefer list.deinit(); | ||
| 72 | |||
| 73 | for (val.Array) |item| { | ||
| 74 | if (item.getT("root_file", .String) == null) continue; | ||
| 75 | try list.append(Package{ | ||
| 76 | .name = item.getT("name", .String).?, | ||
| 77 | .author = item.getT("author", .String).?, | ||
| 78 | .description = item.getT("description", .String).?, | ||
| 79 | .tags = try valueStrArray(item.getT("tags", .Array).?), | ||
| 80 | .git = item.getT("git", .String).?, | ||
| 81 | .root_file = item.getT("root_file", .String).?, | ||
| 82 | .source = @intCast(u32, item.getT("source", .Int).?), | ||
| 83 | .links = try valueLinks(item.get("links").?), | ||
| 84 | }); | ||
| 85 | } | ||
| 86 | return list.toOwnedSlice(); | ||
| 87 | } | ||
| 88 | |||
| 89 | fn valueStrArray(vals: []json.Value) ![]string { | ||
| 90 | var list = std.ArrayList(string).init(gpa); | ||
| 91 | errdefer list.deinit(); | ||
| 92 | |||
| 93 | for (vals) |item| { | ||
| 94 | if (item != .String) continue; | ||
| 95 | try list.append(item.String); | ||
| 96 | } | ||
| 97 | return list.toOwnedSlice(); | ||
| 98 | } | ||
| 99 | |||
| 100 | fn valueLinks(vals: json.Value) ![]?string { | ||
| 101 | var list = std.ArrayList(?string).init(gpa); | ||
| 102 | errdefer list.deinit(); | ||
| 103 | |||
| 104 | try list.append(vals.getT("github", .String)); | ||
| 105 | try list.append(vals.getT("aquila", .String)); | ||
| 106 | try list.append(vals.getT("astrolabe", .String)); | ||
| 107 | return list.toOwnedSlice(); | ||
| 108 | } |
src/cmd/zpm/add.zig+3-14| ... | @@ -10,17 +10,8 @@ const zpm = @import("./../zpm.zig"); | ... | @@ -10,17 +10,8 @@ const zpm = @import("./../zpm.zig"); |
| 10 | // | 10 | // |
| 11 | 11 | ||
| 12 | pub fn execute(args: [][]u8) !void { | 12 | pub fn execute(args: [][]u8) !void { |
| 13 | const url = "https://zpm.random-projects.net/api/packages"; | 13 | const url = try std.mem.join(gpa, "/", &.{ zpm.server_root, "packages" }); |
| 14 | 14 | const val = try zpm.server_fetchArray(url); | |
| 15 | const req = try zfetch.Request.init(gpa, url, null); | ||
| 16 | defer req.deinit(); | ||
| 17 | |||
| 18 | try req.do(.GET, null, null); | ||
| 19 | const r = req.reader(); | ||
| 20 | |||
| 21 | const body_content = try r.readAllAlloc(gpa, std.math.maxInt(usize)); | ||
| 22 | var stream = std.json.TokenStream.init(body_content); | ||
| 23 | const val = try std.json.parse([]zpm.Package, &stream, .{ .allocator = gpa }); | ||
| 24 | 15 | ||
| 25 | const found = blk: { | 16 | const found = blk: { |
| 26 | for (val) |pkg| { | 17 | for (val) |pkg| { |
| ... | @@ -31,8 +22,6 @@ pub fn execute(args: [][]u8) !void { | ... | @@ -31,8 +22,6 @@ pub fn execute(args: [][]u8) !void { |
| 31 | u.fail("no package with name '{s}' found", .{args[0]}); | 22 | u.fail("no package with name '{s}' found", .{args[0]}); |
| 32 | }; | 23 | }; |
| 33 | 24 | ||
| 34 | u.assert(found.root_file != null, "package must have an entry point to be able to be added to your dependencies", .{}); | ||
| 35 | |||
| 36 | const self_module = try zigmod.ModFile.init(gpa); | 25 | const self_module = try zigmod.ModFile.init(gpa); |
| 37 | for (self_module.deps) |dep| { | 26 | for (self_module.deps) |dep| { |
| 38 | if (std.mem.eql(u8, dep.name, found.name)) { | 27 | if (std.mem.eql(u8, dep.name, found.name)) { |
| ... | @@ -74,7 +63,7 @@ pub fn execute(args: [][]u8) !void { | ... | @@ -74,7 +63,7 @@ pub fn execute(args: [][]u8) !void { |
| 74 | try file_w.print(" - src: git {s}\n", .{u.trim_suffix(found.git, ".git")}); | 63 | try file_w.print(" - src: git {s}\n", .{u.trim_suffix(found.git, ".git")}); |
| 75 | if (!(has_zigdotmod or has_zigmodyml)) { | 64 | if (!(has_zigdotmod or has_zigmodyml)) { |
| 76 | try file_w.print(" name: {s}\n", .{found.name}); | 65 | try file_w.print(" name: {s}\n", .{found.name}); |
| 77 | try file_w.print(" main: {s}\n", .{found.root_file.?[1..]}); | 66 | try file_w.print(" main: {s}\n", .{found.root_file[1..]}); |
| 78 | } | 67 | } |
| 79 | 68 | ||
| 80 | std.log.info("Successfully added package {s} by {s}", .{ found.name, found.author }); | 69 | std.log.info("Successfully added package {s} by {s}", .{ found.name, found.author }); |
src/cmd/zpm/search.zig+1-19| ... | @@ -13,25 +13,7 @@ pub fn execute(args: [][]u8) !void { | ... | @@ -13,25 +13,7 @@ pub fn execute(args: [][]u8) !void { |
| 13 | const out = std.io.getStdOut().writer(); | 13 | const out = std.io.getStdOut().writer(); |
| 14 | 14 | ||
| 15 | const url = try std.mem.join(gpa, "/", &.{ zpm.server_root, "packages" }); | 15 | const url = try std.mem.join(gpa, "/", &.{ zpm.server_root, "packages" }); |
| 16 | const val = try zpm.server_fetch(url); | 16 | const list = try zpm.server_fetchArray(url); |
| 17 | |||
| 18 | var arr = std.ArrayList(zpm.Package).init(gpa); | ||
| 19 | defer arr.deinit(); | ||
| 20 | |||
| 21 | for (val.Array) |item| { | ||
| 22 | if (item.get("root_file")) |_| {} else { | ||
| 23 | continue; | ||
| 24 | } | ||
| 25 | try arr.append(zpm.Package{ | ||
| 26 | .name = item.getT("name", .String).?, | ||
| 27 | .author = item.getT("author", .String).?, | ||
| 28 | .description = item.getT("description", .String).?, | ||
| 29 | .tags = &.{}, | ||
| 30 | .git = "", | ||
| 31 | .root_file = "", | ||
| 32 | }); | ||
| 33 | } | ||
| 34 | const list = arr.items; | ||
| 35 | 17 | ||
| 36 | const name_col_width = blk: { | 18 | const name_col_width = blk: { |
| 37 | var w: usize = 4; | 19 | var w: usize = 4; |