htzx/build/htzx_build.zig
2023-11-10 16:26:37 -07:00

65 lines
3.0 KiB
Zig

const std = @import("std");
const log = std.log.scoped(.htzx_comp);
pub fn build_htzx(b: *std.Build, exe: *std.Build.Step.Compile) !void {
// Setup comptime parsing for /lib files
var markup_files = std.ArrayList([]const u8).init(b.allocator);
// var zig_files = std.ArrayList([]const u8).init(b.allocator);
defer markup_files.deinit();
// defer zig_files.deinit();
var options = b.addOptions();
// Add all files names in the src folder to `files`
// var iter_dir = try std.fs.cwd().openIterableDir("lib", .{});
const lib_dir_path = "src/lib/";
var lib_dir = try std.fs.cwd().openIterableDir(lib_dir_path, .{});
var lib_walker = try lib_dir.walk(b.allocator);
var walking = true;
while (walking) blk: {
var file = lib_walker.next() catch {
walking = false;
break :blk;
};
if (file == null) {
walking = false;
break :blk;
}
if (file.?.kind == .file) {
const relative_path = file.?.path;
const file_path = try std.mem.concat(b.allocator, u8, &[_][]const u8{lib_dir_path, relative_path});
var split_iter = std.mem.splitBackwardsAny(u8, relative_path, ".");
var extension = split_iter.first();
// if (std.mem.eql(u8, extension, "zig")) {
// std.debug.print("Adding zig file {s}...\n", .{path});
// try zig_files.append(b.dupe(path));
// var arr: [50]u8 = undefined;
// var buf = arr[0..];
// var z_file_path = try std.fmt.bufPrint(buf, "lib/{s}", .{path});
// var z_file = try std.fs.cwd().openFile(z_file_path, .{});
// const zig_contents = try z_file.readToEndAlloc(b.allocator, 64000);
// const index = std.mem.indexOf(u8, zig_contents, "pub fn post");
// if (index) |i| {
// std.debug.print("Found post endpoint in {s}\n", .{z_file_path});
// var routes_file = try src_dir.createFile("routes.zig", .{});
// _ = try routes_file.write(zig_contents[i..]);
// std.debug.print("Wrote {s}... to routes.zig\n", .{zig_contents[0..10]});
// }
// } else
if (std.mem.eql(u8, extension, "html")) {
log.info("Adding html file {s}...", .{file_path});
try markup_files.append(b.dupe(file_path));
} else {
log.info("Unrecognized file extension '.{s}'\t{s}", .{ extension, file_path });
}
}
// std.debug.print("Got entry in `versions:` base: {s}, path: {s}, kind: {any}\n", .{ ver.?.basename, ver.?.path, ver.?.kind });
}
// Add the file names as an option to the exe, making it available
// as a string array at comptime in main.zig
// options.addOption([]const []const u8, "zig_files", zig_files.items);
options.addOption([]const []const u8, "markup_files", markup_files.items);
exe.addOptions("options", options);
}