Streamline project build.zig

Offload as much work as possible into new `jetzigInit` function provided
by Jetzig's `build.zig`. This means that a new Jetzig project's
`build.zig` is now almost completely vanilla with the exception of two
extra lines:

```zig
const jetzig = @import("jetzig");
```

```zig
    try jetzig.jetzigInit(b, exe, .{});
```
This commit is contained in:
Bob Farrell 2024-03-10 10:06:36 +00:00
parent 2a25084de6
commit 97d66aa6be
2 changed files with 47 additions and 51 deletions

View File

@ -67,3 +67,47 @@ pub fn build(b: *std.Build) !void {
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_main_tests.step);
}
/// Placeholder for potential options we may add in future without breaking
/// backward-compatibility.
pub const JetzigInitOptions = struct {};
pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigInitOptions) !void {
_ = options;
const target = b.host;
const optimize = exe.root_module.optimize orelse .Debug;
const jetzig_dep = b.dependency(
"jetzig",
.{ .optimize = optimize, .target = b.host },
);
const jetzig_module = jetzig_dep.module("jetzig");
const zmpl_module = jetzig_dep.module("zmpl");
exe.root_module.addImport("jetzig", jetzig_module);
exe.root_module.addImport("zmpl", zmpl_module);
var generate_routes = try GenerateRoutes.init(b.allocator, "src/app/views");
try generate_routes.generateRoutes();
const write_files = b.addWriteFiles();
const routes_file = write_files.add("routes.zig", generate_routes.buffer.items);
for (generate_routes.static_routes.items) |route| _ = write_files.add(route.path, route.source);
for (generate_routes.dynamic_routes.items) |route| _ = write_files.add(route.path, route.source);
const routes_module = b.createModule(.{ .root_source_file = routes_file });
const exe_static_routes = b.addExecutable(.{
.name = "static",
.root_source_file = jetzig_dep.path("src/compile_static_routes.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("routes", routes_module);
routes_module.addImport("jetzig", jetzig_module);
exe_static_routes.root_module.addImport("routes", routes_module);
exe_static_routes.root_module.addImport("jetzig", jetzig_module);
exe_static_routes.root_module.addImport("zmpl", zmpl_module);
const run_static_routes_cmd = b.addRunArtifact(exe_static_routes);
exe.step.dependOn(&run_static_routes_cmd.step);
}

View File

@ -1,23 +1,9 @@
const std = @import("std");
const jetzig_build = @import("jetzig");
pub const zmpl = jetzig_build.zmpl;
const GenerateRoutes = @import("jetzig").GenerateRoutes;
const jetzig = @import("jetzig");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const jetzig_dep = b.dependency("jetzig", .{ .optimize = optimize, .target = target });
const lib = b.addStaticLibrary(.{
.name = "jetzig-demo",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(lib);
const exe = b.addExecutable(.{
.name = "jetzig-demo",
@ -26,48 +12,14 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
});
const jetzig_module = jetzig_dep.module("jetzig");
const zmpl_module = jetzig_dep.module("zmpl");
exe.root_module.addImport("jetzig", jetzig_module);
lib.root_module.addImport("jetzig", jetzig_module);
exe.root_module.addImport("zmpl", zmpl_module);
lib.root_module.addImport("zmpl", zmpl_module);
try jetzig.jetzigInit(b, exe, .{});
b.installArtifact(exe);
var generate_routes = try GenerateRoutes.init(b.allocator, "src/app/views");
try generate_routes.generateRoutes();
const write_files = b.addWriteFiles();
const routes_file = write_files.add("routes.zig", generate_routes.buffer.items);
for (generate_routes.static_routes.items) |route| _ = write_files.add(route.path, route.source);
for (generate_routes.dynamic_routes.items) |route| _ = write_files.add(route.path, route.source);
const routes_module = b.createModule(.{ .root_source_file = routes_file });
const exe_static_routes = b.addExecutable(.{
.name = "static",
.root_source_file = jetzig_dep.path("src/compile_static_routes.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("routes", routes_module);
lib.root_module.addImport("routes", routes_module);
routes_module.addImport("jetzig", jetzig_module);
exe_static_routes.root_module.addImport("routes", routes_module);
exe_static_routes.root_module.addImport("jetzig", jetzig_module);
exe_static_routes.root_module.addImport("zmpl", zmpl_module);
const run_static_routes_cmd = b.addRunArtifact(exe_static_routes);
exe.step.dependOn(&run_static_routes_cmd.step);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
if (b.args) |args| run_cmd.addArgs(args);
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);