Merge branch 'app-init'

This commit is contained in:
Bob Farrell 2024-01-20 19:42:10 +00:00
commit 3223146e30
9 changed files with 133 additions and 22 deletions

View File

@ -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| {
@ -66,7 +69,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");

View File

@ -21,7 +21,7 @@
// `hash`, otherwise you are communicating that you expect to find the old hash at
// the new URL.
//
.url = "https://github.com/bobf/zmpl/archive/refs/tags/0.0.1.tar.gz",
.url = "https://github.com/jetzig-framework/zmpl/archive/refs/tags/0.0.1.tar.gz",
.hash = "12204256376f262a58935d66a2a0b41ac0447299b7e63a4c6ff160ddcef6572cd3c7",
// This is computed from the file contents of the directory of files that is

View File

@ -25,24 +25,48 @@ echo "Initializing new project in: ${project_path}"
mkdir -p "${project_path}"
do_exit () {
echo "Error fetching $1 - exiting."
echo "Error fetching '$1':"
echo "$2"
echo "Exiting."
exit 1
}
remote_base=https://raw.githubusercontent.com/jetzig-framework/jetzig/main/src/init
objects=(
build.zig
build.zig.zon
src/main.zig
src/app/views/index.zig
'build.zig'
'build.zig.zon'
'src/main.zig'
'src/app/views/index.zig'
'src/app/views/index.zmpl'
)
for object in "${objects[$@]}"
for object in "${objects[@]}"
do
echo "Creating output: ${object}"
printf "Creating output: ${object} "
url="${remote_base}/${object}"
curl -qs --fail --output "${project_path}/${object}" "${url}" || do_exit "${url}"
mkdir -p "$(dirname "${project_path}/${object}")"
set +e
output=$(curl -s --fail --output "${project_path}/${object}" "${url}" 2>&1)
set -e
if (($?))
then
do_exit "${url}" "${output}"
else
echo "✅"
fi
done
echo "Project initialization complete. Welcome to Jetzig. ✈️🦎 "
sed -i.bak -e "s,%%project_name%%,${project},g" 'src/build.zig' && rm build.zig.bak
sed -i.bak -e "s,%%project_name%%,${project},g" 'src/build.zig.zon' && rm build.zig.zon.bak
echo
echo "Finished creating new project in: ${project_path}"
echo
echo "Run your new project:"
echo
echo " cd '${project_path}'"
echo ' zig build run'
echo
echo "Welcome to Jetzig. ✈️🦎 "
echo

View File

@ -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);
}

41
src/init/build.zig Normal file
View File

@ -0,0 +1,41 @@
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "%%project_name%%",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(exe);
const jetzig = b.dependency("jetzig", .{ .optimize = optimize, .target = target });
exe.addModule("jetzig", jetzig.module("jetzig"));
try b.modules.put("jetzig", jetzig.module("jetzig"));
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
}

14
src/init/build.zig.zon Normal file
View File

@ -0,0 +1,14 @@
.{
.name = "%%project_name%%",
.version = "0.0.0",
.dependencies = .{
.jetzig = .{
.url = "https://github.com/jetzig-framework/jetzig/archive/refs/tags/dev.tar.gz",
.hash = "122097efa94bcbc548aa170ea1c58afe6f21101032f8436a9324ea0435adbaa227ef",
},
},
.paths = .{
"",
},
}

View 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
View 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),
);
}

View File

@ -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;