generate confgen option json ahead of script compile time

This commit is contained in:
LordMZTE 2023-06-01 16:27:35 +02:00
parent e7bd7d0f08
commit 2b9c4a88e0
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
6 changed files with 30 additions and 26 deletions

View file

@ -3,30 +3,17 @@ const std = @import("std");
pub const confgen_json_opt = std.json.ParseOptions{ .ignore_unknown_fields = true };
/// Retrieve some confgen options given a path to the cgfile and a struct type
/// Retrieve some confgen options given a path to the opts.json file and a struct type
/// with a field for each option.
pub fn confgenGet(comptime T: type, cgfile: []const u8, alloc: std.mem.Allocator) !T {
const info = @typeInfo(T);
pub fn confgenGet(comptime T: type, optsjson: []const u8, alloc: std.mem.Allocator) !T {
var file = try std.fs.cwd().openFile(optsjson, .{});
defer file.close();
var buf_reader = std.io.bufferedReader(file.reader());
const field_names = comptime blk: {
var names: [info.Struct.fields.len][]const u8 = undefined;
for (info.Struct.fields, 0..) |f, i|
names[i] = f.name;
var reader = std.json.Reader(1024 * 8, @TypeOf(buf_reader.reader()))
.init(alloc, buf_reader.reader());
defer reader.deinit();
break :blk names;
};
const out = try std.ChildProcess.exec(.{
.allocator = alloc,
.argv = &[_][]const u8{ "confgen", "--json-opt", cgfile } ++ field_names,
});
defer {
alloc.free(out.stdout);
alloc.free(out.stderr);
}
if (!std.meta.eql(out.term, .{ .Exited = 0 }))
return error.UnexpectedExitCode;
return try std.json.parseFromSlice(T, alloc, out.stdout, confgen_json_opt);
return try std.json.parseFromTokenSource(T, alloc, &reader, confgen_json_opt);
//return try std.json.parseFromSlice(T, alloc, out.stdout, confgen_json_opt);
}

View file

@ -21,7 +21,7 @@ pub fn build(b: *std.build.Builder) !void {
const cg_opt = try common.confgenGet(struct {
term_font: []const u8,
}, "../confgen.lua", b.allocator);
}, "../cgout/opts.json", b.allocator);
const opts = b.addOptions();
opts.addOption([]const u8, "font", cg_opt.term_font);

View file

@ -5,4 +5,5 @@
(define (run)
(rm "cgout")
(cmd "confgen" "confgen.lua" "cgout")
(generate-cgopt-json)
null)

View file

@ -10,6 +10,9 @@
(define (run)
(define (bin-path bin)
(build-path (output-bin-path) "bin" bin))
(generate-cgopt-json)
;; Symlink interpreted scripts
(mklink "scripts/map-touch-display.rkt" (bin-path "map-touch-display"))
(mklink "scripts/start-joshuto.sh" (bin-path "start-joshuto"))

View file

@ -4,6 +4,7 @@
(define (run)
(define nvim-config-dir (build-path (find-system-path 'home-dir) ".config" "nvim"))
(generate-cgopt-json)
(install-zig "mzte-nv" "ReleaseSafe")
(rm nvim-config-dir)
(copy "mzte-nv/conf" nvim-config-dir)

View file

@ -6,6 +6,7 @@
cmd
rm
copy
generate-cgopt-json
install-zig
install-rust
install-roswell)
@ -19,9 +20,9 @@
(define (display-function-call func args)
(when (log-calls)
(fprintf (current-error-port)
"\x1b[1;30m(\x1b[1;32m~s \x1b[1;33m~a\x1b[1;30m)\x1b[0m\n"
"\x1b[1;30m(\x1b[1;32m~s\x1b[1;33m~a\x1b[1;30m)\x1b[0m\n"
func
(apply ~a #:separator " " args))))
(string-append (if (null? args) "" " ") (apply ~a #:separator " " args)))))
;; Defines an alias to a function which will log it's parameters on invokation.
(define-syntax-rule (define-logging name func)
@ -52,6 +53,17 @@
"--out-dir"
(build-path (output-bin-path) "bin")))))
(define-logging generate-cgopt-json
(λ ()
(unless (directory-exists? "cgout")
(make-directory "cgout"))
(call-with-output-file*
#:exists 'truncate/replace
"cgout/opts.json"
(λ (outfile)
(parameterize ([log-calls #f] [current-output-port outfile])
(cmd "confgen" "--json-opt" "confgen.lua"))))))
(define-logging
install-roswell
(λ (path)