authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-05-07 19:40:31 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-05-07 19:40:31 -07:00
log29d32b0e9a88c0dc9a2120417540abf986c22ae6
treed01c3f2ab7153695e9243bfa1412884f629429ff
parent46a869c8cba9ee4d45c284528bc928887b90f0ed

cmd/zpm: move add command to zpm folder


3 files changed, 70 insertions(+), 70 deletions(-)

src/cmd/zpm.zig+1-1
...@@ -10,7 +10,7 @@ const u = @import("./../util/index.zig");...@@ -10,7 +10,7 @@ const u = @import("./../util/index.zig");
10//10//
1111
12pub const commands = struct {12pub const commands = struct {
13 pub const add = @import("./zpm_add.zig");13 pub const add = @import("./zpm/add.zig");
14 pub const showjson = @import("./zpm/showjson.zig");14 pub const showjson = @import("./zpm/showjson.zig");
15 pub const tags = @import("./zpm/tags.zig");15 pub const tags = @import("./zpm/tags.zig");
16};16};
src/cmd/zpm/add.zig created+69
...@@ -0,0 +1,69 @@
1const std = @import("std");
2const gpa = std.heap.c_allocator;
3
4const zfetch = @import("zfetch");
5
6const u = @import("./../../util/index.zig");
7const zpm = @import("./../zpm.zig");
8
9//
10//
11
12pub fn execute(args: [][]u8) !void {
13 const url = "https://zpm.random-projects.net/api/packages";
14
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
25 const found = blk: {
26 for (val) |pkg| {
27 if (std.mem.eql(u8, pkg.name, args[0])) {
28 break :blk pkg;
29 }
30 }
31 u.assert(false, "no package with name '{s}' found", .{args[0]});
32 unreachable;
33 };
34
35 u.assert(found.root_file != null, "package must have an entry point to be able to be added to your dependencies", .{});
36
37 const self_module = try u.ModFile.init(gpa, "zig.mod");
38 for (self_module.deps) |dep| {
39 if (std.mem.eql(u8, dep.name, found.name)) {
40 std.log.warn("dependency with name '{s}' already exists in your dependencies", .{found.name});
41 }
42 }
43 for (self_module.devdeps) |dep| {
44 if (std.mem.eql(u8, dep.name, found.name)) {
45 std.log.warn("dependency with name '{s}' already exists in your dev_dependencies", .{found.name});
46 }
47 }
48
49 const has_zigdotmod = blk: {
50 const _url = try std.mem.join(gpa, "/", &.{ found.git, "blob", "HEAD", "zig.mod" });
51 const _req = try zfetch.Request.init(gpa, _url, null);
52 defer _req.deinit();
53 try _req.do(.GET, null, null);
54 break :blk _req.status.code == 200;
55 };
56
57 const file = try std.fs.cwd().openFile("zig.mod", .{ .read = true, .write = true });
58 try file.seekTo(try file.getEndPos());
59
60 const file_w = file.writer();
61 try file_w.print("\n", .{});
62 try file_w.print(" - src: git {s}\n", .{std.mem.trimRight(u8, found.git, ".git")});
63 if (!has_zigdotmod) {
64 try file_w.print(" name: {s}\n", .{found.name});
65 try file_w.print(" main: {s}\n", .{found.root_file.?[1..]});
66 }
67
68 std.log.info("Successfully added package {s} by {s}", .{ found.name, found.author });
69}
src/cmd/zpm_add.zig deleted-69
...@@ -1,69 +0,0 @@
1const std = @import("std");
2const gpa = std.heap.c_allocator;
3
4const zfetch = @import("zfetch");
5
6const u = @import("./../util/index.zig");
7const zpm = @import("./zpm.zig");
8
9//
10//
11
12pub fn execute(args: [][]u8) !void {
13 const url = "https://zpm.random-projects.net/api/packages";
14
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
25 const found = blk: {
26 for (val) |pkg| {
27 if (std.mem.eql(u8, pkg.name, args[0])) {
28 break :blk pkg;
29 }
30 }
31 u.assert(false, "no package with name '{s}' found", .{args[0]});
32 unreachable;
33 };
34
35 u.assert(found.root_file != null, "package must have an entry point to be able to be added to your dependencies", .{});
36
37 const self_module = try u.ModFile.init(gpa, "zig.mod");
38 for (self_module.deps) |dep| {
39 if (std.mem.eql(u8, dep.name, found.name)) {
40 std.log.warn("dependency with name '{s}' already exists in your dependencies", .{found.name});
41 }
42 }
43 for (self_module.devdeps) |dep| {
44 if (std.mem.eql(u8, dep.name, found.name)) {
45 std.log.warn("dependency with name '{s}' already exists in your dev_dependencies", .{found.name});
46 }
47 }
48
49 const has_zigdotmod = blk: {
50 const _url = try std.mem.join(gpa, "/", &.{ found.git, "blob", "HEAD", "zig.mod" });
51 const _req = try zfetch.Request.init(gpa, _url, null);
52 defer _req.deinit();
53 try _req.do(.GET, null, null);
54 break :blk _req.status.code == 200;
55 };
56
57 const file = try std.fs.cwd().openFile("zig.mod", .{ .read = true, .write = true });
58 try file.seekTo(try file.getEndPos());
59
60 const file_w = file.writer();
61 try file_w.print("\n", .{});
62 try file_w.print(" - src: git {s}\n", .{std.mem.trimRight(u8, found.git, ".git")});
63 if (!has_zigdotmod) {
64 try file_w.print(" name: {s}\n", .{found.name});
65 try file_w.print(" main: {s}\n", .{found.root_file.?[1..]});
66 }
67
68 std.log.info("Successfully added package {s} by {s}", .{ found.name, found.author });
69}