srctree

Gregory Mullen parent c8d59f3b cb73b4ee
refactor templates and code gen a bit

build.zig added: 86, removed: 58, total 28
@@ -12,6 +12,8 @@ pub fn build(b: *std.Build) void {
var binaries = std.ArrayList(*std.Build.Step.Compile).init(b.allocator);
defer binaries.clearAndFree();
 
const templates_compiled = try compileTemplates(b);
 
// srctree bin
const exe = b.addExecutable(.{
.name = "tree",
@@ -21,6 +23,7 @@ pub fn build(b: *std.Build) void {
});
b.installArtifact(exe);
binaries.append(exe) catch unreachable;
//exe.root_module.addImport("templates-compiled", templates_compiled);
 
// Run commands
const run_cmd = b.addRunArtifact(exe);
@@ -40,7 +43,7 @@ pub fn build(b: *std.Build) void {
binaries.append(unit_tests) catch unreachable;
 
for (binaries.items) |ex| {
addSrcTemplates(ex);
ex.root_module.addImport("templates-compiled", templates_compiled);
ex.root_module.addOptions("config", options);
if (enable_libcurl) {
ex.linkSystemLibrary2("curl", .{ .preferred_link_mode = .Static });
@@ -54,6 +57,31 @@ pub fn build(b: *std.Build) void {
test_step.dependOn(&run_unit_tests.step);
}
 
fn compileTemplates(b: *std.Build) !*std.Build.Module {
const compiled = b.addModule("templates-compiled", .{
.root_source_file = .{
.path = "src/template-compiled.zig",
},
});
 
const list = buildSrcTemplates(b) catch @panic("unable to build src files");
const found = b.addOptions();
found.addOption(
[]const []const u8,
"names",
list,
);
compiled.addOptions("found_templates", found);
 
for (list) |file| {
_ = compiled.addAnonymousImport(file, .{
.root_source_file = .{ .path = file },
});
}
 
return compiled;
}
 
var template_list: ?[][]const u8 = null;
 
fn buildSrcTemplates(b: *std.Build) ![][]const u8 {
@@ -74,23 +102,3 @@ fn buildSrcTemplates(b: *std.Build) ![][]const u8 {
template_list = try arrlist.toOwnedSlice();
return template_list.?;
}
 
/// eventually I'd love to make this comptime but alas
fn addSrcTemplates(cs: *std.Build.Step.Compile) void {
var b = cs.step.owner;
 
const list = buildSrcTemplates(b) catch @panic("unable to build src files");
const templates = b.addOptions();
templates.addOption(
[]const []const u8,
"names",
list,
);
cs.root_module.addOptions("templates", templates);
 
for (list) |file| {
_ = cs.root_module.addAnonymousImport(file, .{
.root_source_file = .{ .path = file },
});
}
}
 
src/endpoints/repos/deltas.zig added: 86, removed: 58, total 28
@@ -82,8 +82,8 @@ fn newPost(ctx: *Context) Error!void {
desc.value,
action.name,
});
var data = Patch.loadRemote(ctx.alloc, src.value) catch unreachable;
var filename = std.fmt.allocPrint(
const data = Patch.loadRemote(ctx.alloc, src.value) catch unreachable;
const filename = std.fmt.allocPrint(
ctx.alloc,
"data/patch/{s}.{x}.patch",
.{ rd.name, delta.index },
@@ -116,7 +116,7 @@ fn newComment(ctx: *Context) Error!void {
if (msg.value.len < 2) return ctx.response.redirect(loc, true) catch unreachable;
 
var diff = Deltas.open(ctx.alloc, rd.name, diff_index) catch unreachable orelse return error.Unrouteable;
var c = Comments.new("name", msg.value) catch unreachable;
const c = Comments.new("name", msg.value) catch unreachable;
 
diff.addComment(ctx.alloc, c) catch {};
diff.writeOut() catch unreachable;
 
src/endpoints/repos/diffs.zig added: 86, removed: 58, total 28
@@ -53,7 +53,7 @@ pub fn router(ctx: *Context) Error!Endpoint.Router.Callable {
fn new(ctx: *Context) Error!void {
var tmpl = comptime Template.find("diff-new.html");
tmpl.init(ctx.alloc);
ctx.sendTemplate(&tmpl) catch unreachable;
try ctx.sendTemplate(&tmpl);
}
 
fn inNetwork(str: []const u8) bool {
@@ -102,7 +102,7 @@ fn newPost(ctx: *Context) Error!void {
var tmpl = Template.find("diff-new.html");
tmpl.init(ctx.alloc);
try tmpl.addVar("diff", "new data attempting");
ctx.sendTemplate(&tmpl) catch unreachable;
try ctx.sendTemplate(&tmpl);
}
 
fn newComment(ctx: *Context) Error!void {
@@ -202,7 +202,7 @@ fn view(ctx: *Context) Error!void {
f.close();
} else try tmpl.addString("patch", "Patch not found");
 
ctx.sendTemplate(&tmpl) catch unreachable;
try ctx.sendTemplate(&tmpl);
}
 
fn list(ctx: *Context) Error!void {
@@ -236,5 +236,5 @@ fn list(ctx: *Context) Error!void {
var tmpl = Template.find("deltalist.html");
tmpl.init(ctx.alloc);
try tmpl.ctx.?.putBlock("list", tmpl_ctx[0..end]);
ctx.sendTemplate(&tmpl) catch return error.Unknown;
try ctx.sendTemplate(&tmpl);
}
 
src/response.zig added: 86, removed: 58, total 28
@@ -4,7 +4,7 @@ const Allocator = std.mem.Allocator;
const Request = @import("request.zig");
const Headers = @import("headers.zig");
const UserData = @import("user-data.zig");
const Template = @import("template.zig").Template;
const Template = @import("template").Template;
 
const Response = @This();
 
 
filename was Deleted added: 86, removed: 58, total 28
@@ -0,0 +1,20 @@
const found = @import("found_templates");
 
const TEMPLATE_PATH = "templates/";
 
pub const FileData = struct {
path: []const u8,
blob: []const u8,
};
 
pub const data: [found.names.len]FileData = blk: {
@setEvalBranchQuota(5000);
var t: [found.names.len]FileData = undefined;
for (found.names, &t) |file, *dst| {
dst.* = FileData{
.path = file,
.blob = @embedFile(file),
};
}
break :blk t;
};
 
src/template.zig added: 86, removed: 58, total 28
@@ -1,10 +1,9 @@
const std = @import("std");
const bldtmpls = @import("templates");
const compiled = @import("templates-compiled");
 
const Allocator = std.mem.Allocator;
 
const HTML = @import("html.zig");
const Bleach = @import("bleach.zig");
 
const MAX_BYTES = 2 <<| 15;
const TEMPLATE_PATH = "templates/";
@@ -95,8 +94,7 @@ pub const Context = struct {
pub const Template = struct {
alloc: ?Allocator = null,
ctx: ?Context = null,
path: []const u8,
/// expected to be a pointer to path.
// path: []const u8,
name: []const u8,
blob: []const u8,
parent: ?*const Template = null,
@@ -329,7 +327,7 @@ pub const Directive = struct {
pub fn loop(self: Verb, block: *const Context, out: anytype) anyerror!void {
var t = Template{
.name = self.vari,
.path = "/dev/null",
//.path = "/dev/null",
// would be nice not to have to do a mov here
.ctx = block.*,
.blob = self.blob,
@@ -353,15 +351,15 @@ fn tail(path: []const u8) []const u8 {
return path[0..0];
}
 
pub const builtin: [bldtmpls.names.len]Template = blk: {
pub const builtin: [compiled.data.len]Template = blk: {
@setEvalBranchQuota(5000);
var t: [bldtmpls.names.len]Template = undefined;
for (bldtmpls.names, &t) |file, *dst| {
var t: [compiled.data.len]Template = undefined;
for (compiled.data, &t) |filedata, *dst| {
dst.* = Template{
.alloc = null,
.path = file,
.name = tail(file),
.blob = @embedFile(file),
//.path = filedata.path,
.name = tail(filedata.path),
.blob = filedata.blob,
};
}
break :blk t;
@@ -386,10 +384,11 @@ fn load(a: Allocator) !void {
file.name,
});
defer a.free(name);
const path = try a.dupe(u8, file.name);
const tail_ = tail(file.name);
const name_ = try a.dupe(u8, tail_);
try list.append(.{
.path = path,
.name = tail(path),
//.path = path,
.name = name_,
.blob = try cwd.readFileAlloc(a, name, MAX_BYTES),
});
}
@@ -402,7 +401,8 @@ pub fn init(a: Allocator) void {
 
pub fn raze(a: Allocator) void {
for (dynamic) |t| {
a.free(t.path);
// leaks?
a.free(t.name);
a.free(t.blob);
}
a.free(dynamic);
@@ -436,8 +436,8 @@ test "build.zig included templates" {
};
 
names: for (names) |name| {
for (bldtmpls.names) |bld| {
if (std.mem.eql(u8, name, bld)) continue :names;
for (compiled.data) |bld| {
if (std.mem.eql(u8, name, bld.path)) continue :names;
} else return error.TemplateMissing;
}
}
@@ -449,7 +449,7 @@ test "load templates" {
 
//try std.testing.expectEqual(3, builtin.len);
for (builtin) |bi| {
if (std.mem.eql(u8, bi.path, "templates/index.html")) {
if (std.mem.eql(u8, bi.name, "index.html")) {
try std.testing.expectEqualStrings("index.html", bi.name);
try std.testing.expectEqualStrings("<!DOCTYPE html>", bi.blob[0..15]);
break;
@@ -470,7 +470,7 @@ test "directive nothing" {
var a = std.testing.allocator;
var t = Template{
.alloc = null,
.path = "/dev/null",
//.path = "/dev/null",
.name = "test",
.blob = "<!-- nothing -->",
};
@@ -484,7 +484,7 @@ test "directive ORELSE" {
var a = std.testing.allocator;
var t = Template{
.alloc = null,
.path = "/dev/null",
//.path = "/dev/null",
.name = "test",
.blob = "<!-- this ORELSE string until end -->",
};
@@ -498,7 +498,7 @@ test "directive ORNULL" {
var a = std.testing.allocator;
var t = Template{
.alloc = null,
.path = "/dev/null",
//.path = "/dev/null",
.name = "test",
// Invalid because 'string until end' is known to be unreachable
.blob = "<!-- this ORNULL string until end -->",
@@ -510,7 +510,7 @@ test "directive ORNULL" {
 
t = Template{
.alloc = null,
.path = "/dev/null",
//.path = "/dev/null",
.name = "test",
.blob = "<!-- this ORNULL -->",
};
@@ -547,7 +547,7 @@ test "directive FOREACH" {
 
var t = Template{
.alloc = null,
.path = "/dev/null",
//.path = "/dev/null",
.name = "test",
.blob = blob,
};