mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 14:06:08 +00:00
Merge branch 'app-init'
This commit is contained in:
commit
3223146e30
17
build.zig
17
build.zig
@ -23,11 +23,11 @@ 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.createModule(.{ .source_file = .{ .path = "src/jetzig.zig" } });
|
||||||
// exe.addModule("jetzig", jetzig_module);
|
exe.addModule("jetzig", jetzig_module);
|
||||||
// lib.addModule("jetzig", jetzig_module);
|
lib.addModule("jetzig", jetzig_module);
|
||||||
try b.modules.put("jetzig", jetzig_module);
|
try b.modules.put("jetzig", jetzig_module);
|
||||||
|
|
||||||
const zmpl_module = b.dependency(
|
const zmpl_dep = b.dependency(
|
||||||
"zmpl",
|
"zmpl",
|
||||||
.{
|
.{
|
||||||
.target = target,
|
.target = target,
|
||||||
@ -37,11 +37,14 @@ pub fn build(b: *std.Build) !void {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
lib.addModule("zmpl", zmpl_module.module("zmpl"));
|
lib.addModule("zmpl", zmpl_dep.module("zmpl"));
|
||||||
exe.addModule("zmpl", zmpl_module.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 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");
|
try file.writeAll("pub const routes = .{\n");
|
||||||
const views = try findViews(b.allocator);
|
const views = try findViews(b.allocator);
|
||||||
for (views.items) |view| {
|
for (views.items) |view| {
|
||||||
@ -66,7 +69,7 @@ pub fn build(b: *std.Build) !void {
|
|||||||
.optimize = optimize,
|
.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 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");
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
// `hash`, otherwise you are communicating that you expect to find the old hash at
|
// `hash`, otherwise you are communicating that you expect to find the old hash at
|
||||||
// the new URL.
|
// 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",
|
.hash = "12204256376f262a58935d66a2a0b41ac0447299b7e63a4c6ff160ddcef6572cd3c7",
|
||||||
|
|
||||||
// This is computed from the file contents of the directory of files that is
|
// This is computed from the file contents of the directory of files that is
|
||||||
|
42
init.bash
42
init.bash
@ -25,24 +25,48 @@ echo "Initializing new project in: ${project_path}"
|
|||||||
mkdir -p "${project_path}"
|
mkdir -p "${project_path}"
|
||||||
|
|
||||||
do_exit () {
|
do_exit () {
|
||||||
echo "Error fetching $1 - exiting."
|
echo "Error fetching '$1':"
|
||||||
|
echo "$2"
|
||||||
|
echo "Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
remote_base=https://raw.githubusercontent.com/jetzig-framework/jetzig/main/src/init
|
remote_base=https://raw.githubusercontent.com/jetzig-framework/jetzig/main/src/init
|
||||||
|
|
||||||
objects=(
|
objects=(
|
||||||
build.zig
|
'build.zig'
|
||||||
build.zig.zon
|
'build.zig.zon'
|
||||||
src/main.zig
|
'src/main.zig'
|
||||||
src/app/views/index.zig
|
'src/app/views/index.zig'
|
||||||
|
'src/app/views/index.zmpl'
|
||||||
)
|
)
|
||||||
|
|
||||||
for object in "${objects[$@]}"
|
for object in "${objects[@]}"
|
||||||
do
|
do
|
||||||
echo "Creating output: ${object}"
|
printf "Creating output: ${object} "
|
||||||
url="${remote_base}/${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
|
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
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const jetzig = @import("jetzig");
|
||||||
const jetzig = @import("root").jetzig;
|
|
||||||
const Request = jetzig.http.Request;
|
const Request = jetzig.http.Request;
|
||||||
const Data = jetzig.data.Data;
|
const Data = jetzig.data.Data;
|
||||||
const View = jetzig.views.View;
|
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();
|
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);
|
return request.render(.ok);
|
||||||
}
|
}
|
||||||
|
41
src/init/build.zig
Normal file
41
src/init/build.zig
Normal 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
14
src/init/build.zig.zon
Normal 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 = .{
|
||||||
|
"",
|
||||||
|
},
|
||||||
|
}
|
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");
|
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 templates = @import("app/views/zmpl.manifest.zig").templates;
|
||||||
pub const routes = @import("app/views/routes.zig").routes;
|
pub const routes = @import("app/views/routes.zig").routes;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user