Skip to content

Commit

Permalink
Streamline project build.zig
Browse files Browse the repository at this point in the history
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, .{});
```
  • Loading branch information
bobf committed Mar 10, 2024
1 parent 2a25084 commit 97d66aa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 51 deletions.
44 changes: 44 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
54 changes: 3 additions & 51 deletions demo/build.zig
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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);
Expand Down

0 comments on commit 97d66aa

Please sign in to comment.