-
Notifications
You must be signed in to change notification settings - Fork 27
/
build.zig
501 lines (429 loc) · 20 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
const std = @import("std");
pub const Routes = @import("src/Routes.zig");
pub const GenerateMimeTypes = @import("src/GenerateMimeTypes.zig");
const zmpl_build = @import("zmpl");
const Environment = enum { development, testing, production };
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const templates_paths = try zmpl_build.templatesPaths(
b.allocator,
&.{
.{ .prefix = "views", .path = &.{ "src", "app", "views" } },
.{ .prefix = "mailers", .path = &.{ "src", "app", "mailers" } },
},
);
const lib = b.addStaticLibrary(.{
.name = "jetzig",
.root_source_file = b.path("src/jetzig.zig"),
.target = target,
.optimize = optimize,
});
const mime_module = try GenerateMimeTypes.generateMimeModule(b);
const zig_args_dep = b.dependency("args", .{ .target = target, .optimize = optimize });
const jetzig_module = b.addModule("jetzig", .{ .root_source_file = b.path("src/jetzig.zig") });
jetzig_module.addImport("mime_types", mime_module);
lib.root_module.addImport("jetzig", jetzig_module);
const zmpl_dep = b.dependency(
"zmpl",
.{
.target = target,
.optimize = optimize,
.zmpl_templates_paths = templates_paths,
.zmpl_auto_build = false,
.zmpl_markdown_fragments = try generateMarkdownFragments(b),
.zmpl_constants = try zmpl_build.addTemplateConstants(b, struct {
jetzig_view: []const u8,
jetzig_action: []const u8,
}),
},
);
const zmpl_module = zmpl_dep.module("zmpl");
const jetkv_dep = b.dependency("jetkv", .{ .target = target, .optimize = optimize });
const jetquery_dep = b.dependency("jetquery", .{
.target = target,
.optimize = optimize,
.jetquery_migrations_path = @as([]const u8, "src/app/database/migrations"),
.jetquery_config_path = @as([]const u8, "config/database.zig"),
});
const jetcommon_dep = b.dependency("jetcommon", .{ .target = target, .optimize = optimize });
const zmd_dep = b.dependency("zmd", .{ .target = target, .optimize = optimize });
const httpz_dep = b.dependency("httpz", .{ .target = target, .optimize = optimize });
const pg_dep = b.dependency("pg", .{ .target = target, .optimize = optimize });
// This is the way to make it look nice in the zig build script
// If we would do it the other way around, we would have to do
// b.dependency("jetzig",.{}).builder.dependency("zmpl",.{}).module("zmpl");
b.modules.put("zmpl", zmpl_dep.module("zmpl")) catch @panic("Out of memory");
b.modules.put("zmd", zmd_dep.module("zmd")) catch @panic("Out of memory");
b.modules.put("pg", pg_dep.module("pg")) catch @panic("Out of memory");
b.modules.put("jetquery", jetquery_dep.module("jetquery")) catch @panic("Out of memory");
b.modules.put("jetcommon", jetcommon_dep.module("jetcommon")) catch @panic("Out of memory");
b.modules.put("jetquery_migrate", jetquery_dep.module("jetquery_migrate")) catch @panic("Out of memory");
jetquery_dep.module("jetquery").addImport("pg", pg_dep.module("pg"));
const smtp_client_dep = b.dependency("smtp_client", .{
.target = target,
.optimize = optimize,
});
lib.root_module.addImport("zmpl", zmpl_module);
jetzig_module.addImport("zmpl", zmpl_module);
jetzig_module.addImport("args", zig_args_dep.module("args"));
jetzig_module.addImport("zmd", zmd_dep.module("zmd"));
jetzig_module.addImport("jetkv", jetkv_dep.module("jetkv"));
jetzig_module.addImport("jetquery", jetquery_dep.module("jetquery"));
jetzig_module.addImport("jetcommon", jetcommon_dep.module("jetcommon"));
jetzig_module.addImport("smtp", smtp_client_dep.module("smtp_client"));
jetzig_module.addImport("httpz", httpz_dep.module("httpz"));
const main_tests = b.addTest(.{
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
});
const docs_step = b.step("docs", "Generate documentation");
const docs_install = b.addInstallDirectory(.{
.source_dir = lib.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
docs_step.dependOn(&docs_install.step);
main_tests.root_module.addImport("zmpl", zmpl_dep.module("zmpl"));
main_tests.root_module.addImport("jetkv", jetkv_dep.module("jetkv"));
main_tests.root_module.addImport("jetquery", jetquery_dep.module("jetquery"));
main_tests.root_module.addImport("jetcommon", jetcommon_dep.module("jetcommon"));
main_tests.root_module.addImport("httpz", httpz_dep.module("httpz"));
main_tests.root_module.addImport("smtp", smtp_client_dep.module("smtp_client"));
const test_build_options = b.addOptions();
test_build_options.addOption(Environment, "environment", .testing);
test_build_options.addOption(bool, "build_static", true);
test_build_options.addOption(bool, "debug_console", false);
const run_main_tests = b.addRunArtifact(main_tests);
main_tests.root_module.addOptions("build_options", test_build_options);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_main_tests.step);
}
/// Build-time options for Jetzig.
pub const JetzigInitOptions = struct {
zmpl_version: enum { v1, v2 } = .v2,
};
pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigInitOptions) !void {
if (options.zmpl_version == .v1) {
std.debug.print("Zmpl v1 has now been removed. Please upgrade to v2.\n", .{});
return error.ZmplVersionNotSupported;
}
const target = exe.root_module.resolved_target orelse @panic("Unable to detect compile target.");
const optimize = exe.root_module.optimize orelse .Debug;
if (optimize != .Debug) exe.linkLibC();
const environment = b.option(
Environment,
"environment",
"Jetzig server environment.",
) orelse .development;
const build_static = b.option(
bool,
"build_static",
"Pre-render static routes. [default: false in development, true in testing/production]",
) orelse (environment != .development);
const debug_console = b.option(
bool,
"debug_console",
"Render a debug console on error. Default: false.",
) orelse false;
if (debug_console == true and environment != .development) {
std.debug.print("Environment must be `development` when `debug_console` is enabled.", .{});
return error.JetzigBuildError;
}
const jetzig_dep = b.dependency(
"jetzig",
.{ .optimize = optimize, .target = target },
);
const jetquery_dep = jetzig_dep.builder.dependency("jetquery", .{
.target = target,
.optimize = optimize,
.jetquery_migrations_path = @as([]const u8, "src/app/database/migrations"),
.jetquery_config_path = @as([]const u8, "config/database.zig"),
});
const jetzig_module = jetzig_dep.module("jetzig");
const zmpl_module = jetzig_dep.module("zmpl");
const zmd_module = jetzig_dep.module("zmd");
const pg_module = jetzig_dep.module("pg");
const jetquery_module = jetzig_dep.module("jetquery");
const jetcommon_module = jetzig_dep.module("jetcommon");
const jetquery_migrate_module = jetzig_dep.module("jetquery_migrate");
const jetquery_reflect_module = jetquery_dep.module("jetquery_reflect");
const build_options = b.addOptions();
build_options.addOption(Environment, "environment", environment);
build_options.addOption(bool, "build_static", build_static);
build_options.addOption(bool, "debug_console", debug_console);
jetzig_module.addOptions("build_options", build_options);
exe.root_module.addImport("jetzig", jetzig_module);
exe.root_module.addImport("zmpl", zmpl_module);
exe.root_module.addImport("zmd", zmd_module);
exe.root_module.addImport("pg", pg_module);
if (b.option(bool, "jetzig_runner", "Used internally by `jetzig server` command.")) |jetzig_runner| {
if (jetzig_runner) {
const file = try std.fs.cwd().createFile(".jetzig", .{ .truncate = true });
defer file.close();
try file.writeAll(exe.name);
}
}
const root_path = b.build_root.path orelse try std.fs.cwd().realpathAlloc(b.allocator, ".");
const templates_path: []const u8 = try std.fs.path.join(
b.allocator,
&[_][]const u8{ root_path, "src", "app" },
);
const views_path: []const u8 = try std.fs.path.join(
b.allocator,
&[_][]const u8{ root_path, "src", "app", "views" },
);
const jobs_path = try std.fs.path.join(
b.allocator,
&[_][]const u8{ root_path, "src", "app", "jobs" },
);
const mailers_path = try std.fs.path.join(
b.allocator,
&[_][]const u8{ root_path, "src", "app", "mailers" },
);
const exe_routes_file = b.addExecutable(.{
.name = "routes",
.root_source_file = jetzig_dep.path("src/routes_file.zig"),
.target = target,
.optimize = optimize,
});
exe_routes_file.root_module.addImport("jetzig", jetzig_module);
exe_routes_file.root_module.addImport("jetkv", jetzig_module);
exe_routes_file.root_module.addImport("httpz", jetzig_module);
exe_routes_file.root_module.addImport("zmpl", zmpl_module);
const run_routes_file_cmd = b.addRunArtifact(exe_routes_file);
const routes_file_path = run_routes_file_cmd.addOutputFileArg("routes.zig");
run_routes_file_cmd.addArgs(&.{
root_path,
b.pathFromRoot("src"),
templates_path,
views_path,
jobs_path,
mailers_path,
});
const routes_module = b.createModule(.{ .root_source_file = routes_file_path });
routes_module.addImport("jetzig", jetzig_module);
exe.root_module.addImport("routes", routes_module);
const exe_static_routes = b.addExecutable(.{
.name = "static",
.root_source_file = jetzig_dep.path("src/compile_static_routes.zig"),
.target = target,
.optimize = optimize,
});
const main_module = b.createModule(.{ .root_source_file = b.path("src/main.zig") });
exe_static_routes.root_module.addImport("routes", routes_module);
exe_static_routes.root_module.addImport("jetzig", jetzig_module);
exe_static_routes.root_module.addImport("zmpl", zmpl_module);
exe_static_routes.root_module.addImport("main", main_module);
const schema_module = if (try isSourceFile(b, "src/app/database/Schema.zig"))
b.createModule(.{ .root_source_file = b.path("src/app/database/Schema.zig") })
else
jetzig_dep.builder.createModule(
.{ .root_source_file = jetzig_dep.builder.path("src/jetzig/DefaultSchema.zig") },
);
exe_static_routes.root_module.addImport("Schema", schema_module);
exe.root_module.addImport("Schema", schema_module);
schema_module.addImport("jetzig", jetzig_module);
exe_static_routes.root_module.addImport("routes", routes_module);
exe_static_routes.root_module.addImport("jetzig", jetzig_module);
exe_static_routes.root_module.addImport("zmpl", zmpl_module);
const markdown_fragments_write_files = b.addWriteFiles();
const path = markdown_fragments_write_files.add(
"markdown_fragments.zig",
try generateMarkdownFragments(b),
);
const markdown_fragments_module = b.createModule(.{ .root_source_file = path });
exe_static_routes.root_module.addImport("markdown_fragments", markdown_fragments_module);
const run_static_routes_cmd = b.addRunArtifact(exe_static_routes);
const static_outputs_path = run_static_routes_cmd.addOutputFileArg("static.zig");
const static_module = if (build_static)
b.createModule(.{ .root_source_file = static_outputs_path })
else
b.createModule(.{
.root_source_file = jetzig_dep.builder.path("src/jetzig/development_static.zig"),
});
exe.root_module.addImport("static", static_module);
run_static_routes_cmd.expectExitCode(0);
const run_tests_file_cmd = b.addRunArtifact(exe_routes_file);
run_tests_file_cmd.step.dependOn(&run_routes_file_cmd.step);
const tests_file_path = run_tests_file_cmd.addOutputFileArg("tests.zig");
run_tests_file_cmd.addArgs(&.{
root_path,
b.pathFromRoot("src"),
templates_path,
views_path,
jobs_path,
mailers_path,
try randomSeed(b), // Hack to force cache skip - let's find a better way.
});
for (try scanSourceFiles(b)) |sub_path| {
// XXX: We don't use these args, but they help Zig's build system to cache/bust steps.
run_routes_file_cmd.addFileArg(.{ .src_path = .{ .owner = b, .sub_path = sub_path } });
run_tests_file_cmd.addFileArg(.{ .src_path = .{ .owner = b, .sub_path = sub_path } });
}
const exe_unit_tests = b.addTest(.{
.root_source_file = tests_file_path,
.target = target,
.optimize = optimize,
.test_runner = jetzig_dep.path("src/test_runner.zig"),
});
exe_unit_tests.root_module.addImport("jetzig", jetzig_module);
exe_unit_tests.root_module.addImport("static", static_module);
exe_unit_tests.root_module.addImport("routes", routes_module);
exe_unit_tests.root_module.addImport("main", main_module);
var it = exe.root_module.import_table.iterator();
while (it.next()) |import| {
if (std.mem.eql(u8, import.key_ptr.*, "static")) continue;
routes_module.addImport(import.key_ptr.*, import.value_ptr.*);
exe_static_routes.root_module.addImport(import.key_ptr.*, import.value_ptr.*);
exe_unit_tests.root_module.addImport(import.key_ptr.*, import.value_ptr.*);
main_module.addImport(import.key_ptr.*, import.value_ptr.*);
}
if (exe.root_module.link_libc == true) {
exe_static_routes.linkLibC();
exe_unit_tests.linkLibC();
}
for (exe.root_module.link_objects.items) |link_object| {
try exe_static_routes.root_module.link_objects.append(b.allocator, link_object);
try exe_unit_tests.root_module.link_objects.append(b.allocator, link_object);
try main_module.link_objects.append(b.allocator, link_object);
}
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const test_step = b.step("jetzig:test", "Run tests");
test_step.dependOn(&run_exe_unit_tests.step);
run_exe_unit_tests.step.dependOn(&run_static_routes_cmd.step);
run_exe_unit_tests.step.dependOn(&run_routes_file_cmd.step);
const routes_step = b.step("jetzig:routes", "List all routes in your app");
const exe_routes = b.addExecutable(.{
.name = "routes",
.root_source_file = jetzig_dep.path("src/commands/routes.zig"),
.target = target,
.optimize = optimize,
});
const auth_user_create_step = b.step("jetzig:auth:user:create", "List all routes in your app");
const exe_auth = b.addExecutable(.{
.name = "auth",
.root_source_file = jetzig_dep.path("src/commands/auth.zig"),
.target = target,
.optimize = optimize,
});
exe_auth.root_module.addImport("jetquery", jetquery_module);
exe_auth.root_module.addImport("jetzig", jetzig_module);
exe_auth.root_module.addImport("jetcommon", jetcommon_module);
exe_auth.root_module.addImport("Schema", schema_module);
exe_auth.root_module.addImport("main", main_module);
const run_auth_user_create_cmd = b.addRunArtifact(exe_auth);
auth_user_create_step.dependOn(&run_auth_user_create_cmd.step);
run_auth_user_create_cmd.addArg("user:create");
if (b.option([]const u8, "auth_username", "Auth username")) |username| {
run_auth_user_create_cmd.addArg(username);
}
if (b.option([]const u8, "auth_password", "Auth password")) |password| {
run_auth_user_create_cmd.addArg(password);
}
const exe_database = b.addExecutable(.{
.name = "database",
.root_source_file = jetzig_dep.path("src/commands/database.zig"),
.target = target,
.optimize = optimize,
});
exe_database.root_module.addImport("jetquery", jetquery_module);
exe_database.root_module.addImport("jetzig", jetzig_module);
exe_database.root_module.addImport("jetcommon", jetcommon_module);
exe_database.root_module.addImport("jetquery_migrate", jetquery_migrate_module);
exe_database.root_module.addImport("jetquery_reflect", jetquery_reflect_module);
exe_database.root_module.addImport("Schema", schema_module);
registerDatabaseSteps(b, exe_database);
exe_routes.root_module.addImport("jetzig", jetzig_module);
exe_routes.root_module.addImport("routes", routes_module);
exe_routes.root_module.addImport("app", exe.root_module);
const run_routes_cmd = b.addRunArtifact(exe_routes);
routes_step.dependOn(&run_routes_cmd.step);
}
fn registerDatabaseSteps(b: *std.Build, exe_database: *std.Build.Step.Compile) void {
const commands = .{
.{ "migrate", "Migrate your Jetzig app's database." },
.{ "rollback", "Roll back a migration in your Jetzig app's database." },
.{ "create", "Create a database for your Jetzig app." },
.{ "drop", "Drop your Jetzig app's database." },
.{ "reflect", "Read your app's database and generate a JetQuery schema." },
};
inline for (commands) |command| {
const action = command[0];
const description = command[1];
const step = b.step("jetzig:database:" ++ action, description);
const run_cmd = b.addRunArtifact(exe_database);
run_cmd.addArg(action);
step.dependOn(&run_cmd.step);
}
}
fn generateMarkdownFragments(b: *std.Build) ![]const u8 {
const file = std.fs.cwd().openFile(b.pathJoin(&.{ "src", "main.zig" }), .{}) catch |err| {
switch (err) {
error.FileNotFound => return "",
else => return err,
}
};
const stat = try file.stat();
const source = try file.readToEndAllocOptions(b.allocator, @intCast(stat.size), null, @alignOf(u8), 0);
if (try getMarkdownFragmentsSource(b.allocator, source)) |markdown_fragments_source| {
return try std.fmt.allocPrint(b.allocator,
\\const std = @import("std");
\\const zmd = @import("zmd");
\\
\\{s};
\\
, .{markdown_fragments_source});
} else {
return "";
}
}
fn getMarkdownFragmentsSource(allocator: std.mem.Allocator, source: [:0]const u8) !?[]const u8 {
var ast = try std.zig.Ast.parse(allocator, source, .zig);
defer ast.deinit(allocator);
for (ast.nodes.items(.tag), 0..) |tag, index| {
switch (tag) {
.simple_var_decl => {
const decl = ast.simpleVarDecl(@intCast(index));
const identifier = ast.tokenSlice(decl.ast.mut_token + 1);
if (std.mem.eql(u8, identifier, "markdown_fragments")) {
return ast.getNodeSource(@intCast(index));
}
},
else => continue,
}
}
return null;
}
fn isSourceFile(b: *std.Build, path: []const u8) !bool {
const dir = try std.fs.openDirAbsolute(b.build_root.path.?, .{});
const stat = dir.statFile(path) catch |err| {
switch (err) {
error.FileNotFound => return false,
else => return err,
}
};
return stat.kind == .file;
}
fn scanSourceFiles(b: *std.Build) ![]const []const u8 {
var buf = std.ArrayList([]const u8).init(b.allocator);
var src_dir = try std.fs.openDirAbsolute(b.pathFromRoot("src"), .{ .iterate = true });
defer src_dir.close();
var walker = try src_dir.walk(b.allocator);
defer walker.deinit();
while (try walker.next()) |entry| {
if (entry.kind == .file) try buf.append(
try std.fs.path.join(b.allocator, &.{ "src", entry.path }),
);
}
return try buf.toOwnedSlice();
}
fn randomSeed(b: *std.Build) ![]const u8 {
const seed = try b.allocator.alloc(u8, 32);
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (0..32) |index| {
seed[index] = chars[std.crypto.random.intRangeAtMost(u8, 0, chars.len - 1)];
}
return seed;
}