mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 14:06:08 +00:00
App initialization
This commit is contained in:
parent
4d2bd0b86b
commit
062ec58e67
39
build.zig
39
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, .{});
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
11
src/init/src/app/views/index.zig
Normal file
11
src/init/src/app/views/index.zig
Normal file
@ -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);
|
||||
}
|
19
src/init/src/main.zig
Normal file
19
src/init/src/main.zig
Normal file
@ -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),
|
||||
);
|
||||
}
|
@ -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;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user