diff --git a/build.zig b/build.zig index 0895aec..0355bfc 100644 --- a/build.zig +++ b/build.zig @@ -23,11 +23,11 @@ pub fn build(b: *std.Build) !void { b.installArtifact(exe); const jetzig_module = b.createModule(.{ .source_file = .{ .path = "src/jetzig.zig" } }); - // exe.addModule("jetzig", jetzig_module); - // lib.addModule("jetzig", jetzig_module); + exe.addModule("jetzig", jetzig_module); + lib.addModule("jetzig", jetzig_module); try b.modules.put("jetzig", jetzig_module); - const zmpl_module = b.dependency( + const zmpl_dep = b.dependency( "zmpl", .{ .target = target, @@ -37,11 +37,14 @@ pub fn build(b: *std.Build) !void { }, ); - lib.addModule("zmpl", zmpl_module.module("zmpl")); - exe.addModule("zmpl", zmpl_module.module("zmpl")); + lib.addModule("zmpl", zmpl_dep.module("zmpl")); + exe.addModule("zmpl", zmpl_dep.module("zmpl")); + try b.modules.put("zmpl", zmpl_dep.module("zmpl")); + try jetzig_module.dependencies.put("zmpl", zmpl_dep.module("zmpl")); var dir = std.fs.cwd(); - var file = try dir.createFile("src/app/views/routes.zig", .{ .truncate = true }); + var views_dir = try dir.makeOpenPath("src/app/views", .{}); + var file = try views_dir.createFile("routes.zig", .{ .truncate = true }); try file.writeAll("pub const routes = .{\n"); const views = try findViews(b.allocator); for (views.items) |view| { @@ -52,6 +55,9 @@ pub fn build(b: *std.Build) !void { try file.writeAll("};\n"); file.close(); + const init = b.option(bool, "jetzig_init", "Pass -Djetzig_init=true to generate a new Jetzig project.") orelse false; + if (init) try initializeNewProject(b); + const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); @@ -66,7 +72,7 @@ pub fn build(b: *std.Build) !void { .optimize = optimize, }); - main_tests.addModule("zmpl", zmpl_module.module("zmpl")); + main_tests.addModule("zmpl", zmpl_dep.module("zmpl")); const run_main_tests = b.addRunArtifact(main_tests); const test_step = b.step("test", "Run library tests"); @@ -105,3 +111,22 @@ fn findViews(allocator: std.mem.Allocator) !std.ArrayList(*ViewItem) { } return array; } + +fn initializeNewProject(b: *std.Build) !void { + try copySourceFile(b, "main.zig"); + try copySourceFile(b, "app/views/index.zig"); +} + +fn copySourceFile(b: *std.Build, path: []const u8) !void { + const cwd = std.fs.cwd(); + + var dest_dir = try cwd.makeOpenPath("src", .{}); + defer dest_dir.close(); + const dest_path = b.pathJoin(&[_][]const u8{ try dest_dir.realpathAlloc(b.allocator, "."), path }); + + var src_dir = try cwd.makeOpenPath(b.pathFromRoot("src/init"), .{}); + defer src_dir.close(); + const src_path = b.pathJoin(&[_][]const u8{ try src_dir.realpathAlloc(b.allocator, "."), path }); + + try std.fs.copyFileAbsolute(src_path, dest_path, .{}); +} diff --git a/src/app/views/index.zig b/src/app/views/index.zig index ad1eb80..a7dbd34 100644 --- a/src/app/views/index.zig +++ b/src/app/views/index.zig @@ -1,12 +1,11 @@ const std = @import("std"); - -const jetzig = @import("root").jetzig; +const jetzig = @import("jetzig"); const Request = jetzig.http.Request; const Data = jetzig.data.Data; const View = jetzig.views.View; -pub fn index(request: *Request, data: *Data) anyerror!View { +pub fn index(request: *jetzig.http.Request, data: *jetzig.data.Data) anyerror!jetzig.views.View { var object = try data.object(); - try object.put("foo", data.string("hello")); + try object.put("message", data.string("Welcome to Jetzig!")); return request.render(.ok); } diff --git a/src/init/src/app/views/index.zig b/src/init/src/app/views/index.zig new file mode 100644 index 0000000..a7dbd34 --- /dev/null +++ b/src/init/src/app/views/index.zig @@ -0,0 +1,11 @@ +const std = @import("std"); +const jetzig = @import("jetzig"); +const Request = jetzig.http.Request; +const Data = jetzig.data.Data; +const View = jetzig.views.View; + +pub fn index(request: *jetzig.http.Request, data: *jetzig.data.Data) anyerror!jetzig.views.View { + var object = try data.object(); + try object.put("message", data.string("Welcome to Jetzig!")); + return request.render(.ok); +} diff --git a/src/init/src/main.zig b/src/init/src/main.zig new file mode 100644 index 0000000..72b2899 --- /dev/null +++ b/src/init/src/main.zig @@ -0,0 +1,19 @@ +const std = @import("std"); + +pub const jetzig = @import("jetzig"); +pub const templates = @import("app/views/zmpl.manifest.zig").templates; +pub const routes = @import("app/views/routes.zig").routes; + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer std.debug.assert(gpa.deinit() == .ok); + const allocator = gpa.allocator(); + + const app = try jetzig.init(allocator); + defer app.deinit(); + + try app.start( + comptime jetzig.route(routes), + comptime jetzig.loadTemplates(templates), + ); +} diff --git a/src/main.zig b/src/main.zig index 74482fb..72b2899 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,6 +1,6 @@ const std = @import("std"); -pub const jetzig = @import("jetzig.zig"); +pub const jetzig = @import("jetzig"); pub const templates = @import("app/views/zmpl.manifest.zig").templates; pub const routes = @import("app/views/routes.zig").routes;