diff --git a/src/cmd/zpm.zig b/src/cmd/zpm.zig index 033689b81098d8d40dfd814d9431c9503c5ce841..35277f6b41d5fe43eb97c5091a0cfd250cf0077a 100644 --- a/src/cmd/zpm.zig +++ b/src/cmd/zpm.zig @@ -10,7 +10,7 @@ const u = @import("./../util/index.zig"); // pub const commands = struct { - pub const add = @import("./zpm_add.zig"); + pub const add = @import("./zpm/add.zig"); pub const showjson = @import("./zpm/showjson.zig"); pub const tags = @import("./zpm/tags.zig"); }; diff --git a/src/cmd/zpm/add.zig b/src/cmd/zpm/add.zig new file mode 100644 index 0000000000000000000000000000000000000000..0c41eaf90b207fc1eec14f75586f8b225fcc9d8c --- /dev/null +++ b/src/cmd/zpm/add.zig @@ -0,0 +1,69 @@ +const std = @import("std"); +const gpa = std.heap.c_allocator; + +const zfetch = @import("zfetch"); + +const u = @import("./../../util/index.zig"); +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 found = blk: { + for (val) |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)) { + std.log.warn("dependency with name '{s}' already exists in your dependencies", .{found.name}); + } + } + for (self_module.devdeps) |dep| { + if (std.mem.eql(u8, dep.name, found.name)) { + std.log.warn("dependency with name '{s}' already exists in your dev_dependencies", .{found.name}); + } + } + + const has_zigdotmod = blk: { + const _url = try std.mem.join(gpa, "/", &.{ found.git, "blob", "HEAD", "zig.mod" }); + const _req = try zfetch.Request.init(gpa, _url, null); + defer _req.deinit(); + try _req.do(.GET, null, null); + break :blk _req.status.code == 200; + }; + + 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", .{std.mem.trimRight(u8, found.git, ".git")}); + if (!has_zigdotmod) { + 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/zpm_add.zig b/src/cmd/zpm_add.zig deleted file mode 100644 index 4a66aed85a5e9f7334cabf345ca8201a21e4e5d7..0000000000000000000000000000000000000000 --- a/src/cmd/zpm_add.zig +++ /dev/null @@ -1,69 +0,0 @@ -const std = @import("std"); -const gpa = std.heap.c_allocator; - -const zfetch = @import("zfetch"); - -const u = @import("./../util/index.zig"); -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 found = blk: { - for (val) |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)) { - std.log.warn("dependency with name '{s}' already exists in your dependencies", .{found.name}); - } - } - for (self_module.devdeps) |dep| { - if (std.mem.eql(u8, dep.name, found.name)) { - std.log.warn("dependency with name '{s}' already exists in your dev_dependencies", .{found.name}); - } - } - - const has_zigdotmod = blk: { - const _url = try std.mem.join(gpa, "/", &.{ found.git, "blob", "HEAD", "zig.mod" }); - const _req = try zfetch.Request.init(gpa, _url, null); - defer _req.deinit(); - try _req.do(.GET, null, null); - break :blk _req.status.code == 200; - }; - - 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", .{std.mem.trimRight(u8, found.git, ".git")}); - if (!has_zigdotmod) { - 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 }); -}