| ... | @@ -0,0 +1,55 @@ |
| 1 | const std = @import("std"); |
| 2 | |
| 3 | const u = @import("index.zig"); |
| 4 | const yaml = @import("./yaml.zig"); |
| 5 | |
| 6 | // |
| 7 | // |
| 8 | |
| 9 | const b = 1; |
| 10 | const kb = b * 1024; |
| 11 | const mb = kb * 1024; |
| 12 | |
| 13 | pub const ModFile = struct { |
| 14 | const Self = @This(); |
| 15 | |
| 16 | alloc: *std.mem.Allocator, |
| 17 | name: []const u8, |
| 18 | main: []const u8, |
| 19 | deps: []u.Dep, |
| 20 | |
| 21 | pub fn init(alloc: *std.mem.Allocator, fpath: []const u8) !Self { |
| 22 | // |
| 23 | const mpath = try std.fs.realpathAlloc(alloc, fpath); |
| 24 | const file = try std.fs.openFileAbsolute(mpath, .{}); |
| 25 | defer file.close(); |
| 26 | const input = try file.reader().readAllAlloc(alloc, mb); |
| 27 | const doc = try yaml.parse(alloc, input); |
| 28 | |
| 29 | const name = doc.mapping.get("name").?.string; |
| 30 | const main = doc.mapping.get("main").?.string; |
| 31 | |
| 32 | const dep_list = &std.ArrayList(u.Dep).init(alloc); |
| 33 | if (doc.mapping.get("dependencies")) |dep_seq| { |
| 34 | if (dep_seq == .sequence) { |
| 35 | for (dep_seq.sequence) |item| { |
| 36 | const dtype = item.mapping.get("type").?.string; |
| 37 | const path = item.mapping.get("path").?.string; |
| 38 | const dep_type = std.meta.stringToEnum(u.DepType, dtype).?; |
| 39 | |
| 40 | try dep_list.append(u.Dep{ |
| 41 | .type = dep_type, |
| 42 | .path = path, |
| 43 | }); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return Self{ |
| 49 | .alloc = alloc, |
| 50 | .name = name, |
| 51 | .main = main, |
| 52 | .deps = dep_list.items, |
| 53 | }; |
| 54 | } |
| 55 | }; |