dotfiles/build_common.zig

26 lines
1 KiB
Zig
Raw Normal View History

//! Shared code for script build scripts
const std = @import("std");
pub const confgen_json_opt = std.json.ParseOptions{ .ignore_unknown_fields = true };
2023-06-01 16:38:18 +02:00
/// Retrieve some confgen options given a relative path to the dotfile root and a struct type
/// with a field for each option.
2023-06-01 16:38:18 +02:00
pub fn confgenGet(comptime T: type, root_path: []const u8, alloc: std.mem.Allocator) !T {
const optsjson = try std.fs.path.join(alloc, &.{ root_path, "cgout", "opts.json" });
defer alloc.free(optsjson);
var file = try std.fs.cwd().openFile(optsjson, .{});
defer file.close();
var buf_reader = std.io.bufferedReader(file.reader());
var reader = std.json.Reader(1024 * 8, @TypeOf(buf_reader.reader()))
.init(alloc, buf_reader.reader());
defer reader.deinit();
2023-06-27 23:20:35 +02:00
const ret = try std.json.parseFromTokenSource(T, alloc, &reader, confgen_json_opt);
2023-06-25 23:57:51 +02:00
// We just grab the value from the parse result as this data will almost certainly have been
// allocated with the builder's arena anyways.
2023-06-27 23:20:35 +02:00
return ret.value;
}