authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-14 02:13:34 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-14 02:13:34 -08:00
log09c5634a8c29bc40965a91f8db5bcc1a40c8455a
treedf5fde37012506fdcc8d70df8453da81fa483298
parent77ae99d8fff5ed9c4c7b95b3ade1d80529b3a6b0

zig: add cmd_add


2 files changed, 50 insertions(+), 0 deletions(-)

src/cmd_add.zig created+49
...@@ -0,0 +1,49 @@
1const std = @import("std");
2const gpa = std.heap.c_allocator;
3
4const u = @import("./util/index.zig");
5
6//
7//
8
9pub fn execute(args: [][]u8) !void {
10 //
11 u.assert(args.len >= 1, "missing package <type> parameter", .{});
12 u.assert(args.len >= 2, "missing package <path> parameter", .{});
13
14 const dept = args[0];
15 const path = args[1];
16
17 const dep_type = std.meta.stringToEnum(u.DepType, dept);
18 u.assert(dep_type != null, "provided <type> parameter \"{}\" is not a valid dependency type", .{dept});
19
20 const m = try u.ModFile.init(gpa, "./zig.mod");
21 for (m.deps) |d| {
22 u.assert(!(d.type == dep_type.? and std.mem.eql(u8, d.path, path)), "dependency already added, skipping!", .{});
23 }
24
25 const ndl = &std.ArrayList(u.Dep).init(gpa);
26 for (m.deps) |d| {
27 try ndl.append(d);
28 }
29 try ndl.append(u.Dep{
30 .type = dep_type.?,
31 .path = path,
32 });
33
34 //
35 const f = try std.fs.cwd().createFile("./zig.mod", .{});
36 defer f.close();
37
38 const w = f.writer();
39 try w.print("name: {}\n", .{m.name});
40 try w.print("main: {}\n", .{m.main});
41 try w.print("dependencies:\n", .{});
42
43 for (ndl.items) |d| {
44 try w.print("- type: {}\n", .{@tagName(d.type)});
45 try w.print(" path: {}\n", .{d.path});
46 }
47
48 u.print("Successfully added {}", .{path});
49}
src/main.zig+1
...@@ -8,6 +8,7 @@ const u = @import("./util/index.zig");...@@ -8,6 +8,7 @@ const u = @import("./util/index.zig");
88
9const commands = struct {9const commands = struct {
10 const init = @import("./cmd_init.zig");10 const init = @import("./cmd_init.zig");
11 const add = @import("./cmd_add.zig");
11};12};
1213
13pub fn main() !void {14pub fn main() !void {