mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 22:16:08 +00:00
Updated the api for the Build system, Added a step for the compilation of views
This commit is contained in:
parent
50f518806a
commit
6ee3b79884
89
build.zig
89
build.zig
@ -3,6 +3,8 @@ const std = @import("std");
|
|||||||
pub fn build(b: *std.Build) !void {
|
pub fn build(b: *std.Build) !void {
|
||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
const template_path = b.option([]const u8, "zmpl_templates_path", "Path to templates") orelse "src/app/views/";
|
||||||
|
const manifest : []const u8 = b.pathJoin(&.{ template_path, "zmpl.manifest.zig" });
|
||||||
|
|
||||||
const lib = b.addStaticLibrary(.{
|
const lib = b.addStaticLibrary(.{
|
||||||
.name = "jetzig",
|
.name = "jetzig",
|
||||||
@ -22,38 +24,22 @@ pub fn build(b: *std.Build) !void {
|
|||||||
|
|
||||||
b.installArtifact(exe);
|
b.installArtifact(exe);
|
||||||
|
|
||||||
const jetzig_module = b.createModule(.{ .source_file = .{ .path = "src/jetzig.zig" } });
|
const jetzig_module = b.addModule("jetzig",.{ .root_source_file = .{ .path = "src/jetzig.zig" } });
|
||||||
exe.addModule("jetzig", jetzig_module);
|
exe.root_module.addImport("jetzig", jetzig_module);
|
||||||
lib.addModule("jetzig", jetzig_module);
|
lib.root_module.addImport("jetzig", jetzig_module);
|
||||||
try b.modules.put("jetzig", jetzig_module);
|
|
||||||
|
|
||||||
const zmpl_dep = b.dependency(
|
const zmpl_dep = b.dependency(
|
||||||
"zmpl",
|
"zmpl",
|
||||||
.{
|
.{ .target = target, .optimize = optimize, .zmpl_templates_path = template_path, .zmpl_manifest_path = manifest},
|
||||||
.target = target,
|
|
||||||
.optimize = optimize,
|
|
||||||
.zmpl_templates_path = @as([]const u8, "src/app/views/"),
|
|
||||||
.zmpl_manifest_path = @as([]const u8, "src/app/views/zmpl.manifest.zig"),
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
lib.addModule("zmpl", zmpl_dep.module("zmpl"));
|
lib.root_module.addImport("zmpl", zmpl_dep.module("zmpl"));
|
||||||
exe.addModule("zmpl", zmpl_dep.module("zmpl"));
|
exe.root_module.addImport("zmpl", zmpl_dep.module("zmpl"));
|
||||||
try b.modules.put("zmpl", zmpl_dep.module("zmpl"));
|
jetzig_module.addImport("zmpl", zmpl_dep.module("zmpl"));
|
||||||
try jetzig_module.dependencies.put("zmpl", zmpl_dep.module("zmpl"));
|
|
||||||
|
|
||||||
var dir = std.fs.cwd();
|
// This is the way to make it look nice in the zig build script
|
||||||
var views_dir = try dir.makeOpenPath("src/app/views", .{});
|
// If we would do it the other way around, we would have to do b.dependency("jetzig",.{}).builder.dependency("zmpl",.{}).module("zmpl");
|
||||||
var file = try views_dir.createFile("routes.zig", .{ .truncate = true });
|
b.modules.put("zmpl", zmpl_dep.module("zmpl")) catch @panic("Out of memory");
|
||||||
try file.writeAll("pub const routes = .{\n");
|
|
||||||
const views = try findViews(b.allocator);
|
|
||||||
for (views.items) |view| {
|
|
||||||
try file.writeAll(try std.fmt.allocPrint(b.allocator, " @import(\"{s}\"),\n", .{view.path}));
|
|
||||||
std.debug.print("[jetzig] Imported view: {s}\n", .{view.path});
|
|
||||||
}
|
|
||||||
|
|
||||||
try file.writeAll("};\n");
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
const run_cmd = b.addRunArtifact(exe);
|
const run_cmd = b.addRunArtifact(exe);
|
||||||
|
|
||||||
@ -69,7 +55,7 @@ pub fn build(b: *std.Build) !void {
|
|||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
|
||||||
main_tests.addModule("zmpl", zmpl_dep.module("zmpl"));
|
main_tests.root_module.addImport("zmpl", zmpl_dep.module("zmpl"));
|
||||||
const run_main_tests = b.addRunArtifact(main_tests);
|
const run_main_tests = b.addRunArtifact(main_tests);
|
||||||
|
|
||||||
const test_step = b.step("test", "Run library tests");
|
const test_step = b.step("test", "Run library tests");
|
||||||
@ -81,9 +67,36 @@ const ViewItem = struct {
|
|||||||
name: []const u8,
|
name: []const u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn findViews(allocator: std.mem.Allocator) !std.ArrayList(*ViewItem) {
|
pub const CompileViewsStepOptions = struct {
|
||||||
|
template_path: []const u8 = "src/app/views/",
|
||||||
|
max_rss: usize = 0,
|
||||||
|
};
|
||||||
|
pub const CompileViewsStep = struct {
|
||||||
|
step: std.Build.Step,
|
||||||
|
template_path: []const u8,
|
||||||
|
|
||||||
|
fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void {
|
||||||
|
const self = @fieldParentPtr(CompileViewsStep, "step", step);
|
||||||
|
try compileViews(step.owner, self.template_path);
|
||||||
|
prog_node.completeOne();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create(owner: *std.Build, options: CompileViewsStepOptions) *CompileViewsStep {
|
||||||
|
const step = std.Build.Step.init(.{
|
||||||
|
.id = std.Build.Step.Id.custom,
|
||||||
|
.name = "Compile views",
|
||||||
|
.owner = owner,
|
||||||
|
.max_rss = options.max_rss,
|
||||||
|
.makeFn = &make,
|
||||||
|
});
|
||||||
|
const compile_step_view = owner.allocator.create(CompileViewsStep) catch @panic("Out of memory");
|
||||||
|
compile_step_view.* = .{ .step = step, .template_path = options.template_path, };
|
||||||
|
return compile_step_view;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn findViews(allocator: std.mem.Allocator, template_path: []const u8) !std.ArrayList(*ViewItem) {
|
||||||
var array = std.ArrayList(*ViewItem).init(allocator);
|
var array = std.ArrayList(*ViewItem).init(allocator);
|
||||||
const dir = try std.fs.cwd().openIterableDir("src/app/views", .{});
|
const dir = try std.fs.cwd().openDir(template_path, .{ .iterate = true });
|
||||||
var walker = try dir.walk(allocator);
|
var walker = try dir.walk(allocator);
|
||||||
defer walker.deinit();
|
defer walker.deinit();
|
||||||
while (try walker.next()) |entry| {
|
while (try walker.next()) |entry| {
|
||||||
@ -107,4 +120,20 @@ fn findViews(allocator: std.mem.Allocator) !std.ArrayList(*ViewItem) {
|
|||||||
try array.append(ptr);
|
try array.append(ptr);
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn compileViews(b: *std.Build, template_path: []const u8) !void {
|
||||||
|
var dir = b.build_root.handle;
|
||||||
|
var views_dir = try dir.makeOpenPath(template_path, .{});
|
||||||
|
var file = try views_dir.createFile("routes.zig", .{ .truncate = true });
|
||||||
|
try file.writeAll("pub const routes = .{\n");
|
||||||
|
const views = try findViews(b.allocator, template_path);
|
||||||
|
for (views.items) |view| {
|
||||||
|
try file.writeAll(try std.fmt.allocPrint(b.allocator, " @import(\"{s}\"),\n", .{view.path}));
|
||||||
|
std.debug.print("[jetzig] Imported view: {s}\n", .{view.path});
|
||||||
|
}
|
||||||
|
|
||||||
|
try file.writeAll("};\n");
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user