| 1 | const std = @import("std"); |
| 2 | const gpa = std.heap.c_allocator; |
| 3 | const extras = @import("extras"); |
| 4 | const nfs = @import("nfs"); |
| 5 | const root = @import("root"); |
| 6 | |
| 7 | const zigmod = @import("../../lib.zig"); |
| 8 | const u = @import("./../../util/funcs.zig"); |
| 9 | const zpm = @import("./../zpm.zig"); |
| 10 | |
| 11 | // |
| 12 | // |
| 13 | |
| 14 | pub fn execute(self_name: []const u8, args: []const [:0]const u8) !void { |
| 15 | _ = self_name; |
| 16 | |
| 17 | const url = try std.mem.join(gpa, "/", &.{ zpm.server_root, "packages" }); |
| 18 | const val = try zpm.server_fetchArray(url); |
| 19 | |
| 20 | const found = blk: { |
| 21 | for (val) |pkg| { |
| 22 | if (std.mem.eql(u8, pkg.name, args[0])) { |
| 23 | break :blk pkg; |
| 24 | } |
| 25 | } |
| 26 | u.fail("no package with name '{s}' found", .{args[0]}); |
| 27 | }; |
| 28 | |
| 29 | const self_module = try zigmod.ModFile.init(gpa); |
| 30 | for (self_module.deps) |dep| { |
| 31 | if (std.mem.eql(u8, dep.name, found.name)) { |
| 32 | std.log.warn("dependency with name '{s}' already exists in your dependencies", .{found.name}); |
| 33 | } |
| 34 | } |
| 35 | for (self_module.rootdeps) |dep| { |
| 36 | if (std.mem.eql(u8, dep.name, found.name)) { |
| 37 | std.log.warn("dependency with name '{s}' already exists in your root_dependencies", .{found.name}); |
| 38 | } |
| 39 | } |
| 40 | for (self_module.builddeps) |dep| { |
| 41 | if (std.mem.eql(u8, dep.name, found.name)) { |
| 42 | std.log.warn("dependency with name '{s}' already exists in your build_dependencies", .{found.name}); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | const io = root.io; |
| 47 | // var buf: [4096]u8 = @splat(0); |
| 48 | var client: std.http.Client = .{ .io = io, .allocator = gpa }; |
| 49 | defer client.deinit(); |
| 50 | |
| 51 | const has_zigdotmod = blk: { |
| 52 | const _url = try std.mem.join(gpa, "/", &.{ found.git, "blob", "HEAD", "zig.mod" }); |
| 53 | var _req = try client.request(.GET, try std.Uri.parse(_url), .{ |
| 54 | .headers = .{ .accept_encoding = .{ .override = "identity" } }, |
| 55 | }); |
| 56 | defer _req.deinit(); |
| 57 | try _req.sendBodiless(); |
| 58 | const resp = try _req.receiveHead(&.{}); |
| 59 | break :blk resp.head.status == .ok; |
| 60 | }; |
| 61 | const has_zigmodyml = blk: { |
| 62 | const _url = try std.mem.join(gpa, "/", &.{ found.git, "blob", "HEAD", "zigmod.yml" }); |
| 63 | var _req = try client.request(.GET, try std.Uri.parse(_url), .{ |
| 64 | .headers = .{ .accept_encoding = .{ .override = "identity" } }, |
| 65 | }); |
| 66 | defer _req.deinit(); |
| 67 | try _req.sendBodiless(); |
| 68 | const resp = try _req.receiveHead(&.{}); |
| 69 | break :blk resp.head.status == .ok; |
| 70 | }; |
| 71 | |
| 72 | _, const file = try zigmod.ModFile.openFile(nfs.cwd(), .{ .mode = .read_write }); |
| 73 | defer file.close(); |
| 74 | try file.seekTo(try file.getEndPos()); |
| 75 | |
| 76 | const file_w = file; |
| 77 | try file_w.writeAll("\n"); |
| 78 | try file_w.print(" - src: git {s}\n", .{extras.trimSuffix(found.git, ".git")}); |
| 79 | if (!(has_zigdotmod or has_zigmodyml)) { |
| 80 | try file_w.print(" name: {s}\n", .{found.name}); |
| 81 | try file_w.print(" main: {s}\n", .{found.root_file[1..]}); |
| 82 | } |
| 83 | |
| 84 | std.log.info("Successfully added package {s} by {s}", .{ found.name, found.author }); |
| 85 | } |