authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-25 20:25:17-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-26 11:42:04-08:00
log67904e925d2b33c48dda3d4ddaf158328964dc2e
tree35c505758915b7a2473dec2b7165a4fc4c2d5b38
parentea516f0e81d055e0d7504eff36dfde694831bec1

zig init: adjust template lang to allow zig fmt passthrough


4 files changed, 29 insertions(+), 26 deletions(-)

lib/init/build.zig+3-3
......@@ -42,14 +42,14 @@ pub fn build(b: *std.Build) void {
4242 // Modules can depend on one another using the `std.Build.Module.addImport` function.
4343 // This is what allows Zig source code to use `@import("foo")` where 'foo' is not a
4444 // file path. In this case, we set up `exe_mod` to import `lib_mod`.
45 exe_mod.addImport("$n_lib", lib_mod);
45 exe_mod.addImport(".NAME_lib", lib_mod);
4646
4747 // Now, we will create a static library based on the module we created above.
4848 // This creates a `std.Build.Step.Compile`, which is the build step responsible
4949 // for actually invoking the compiler.
5050 const lib = b.addLibrary(.{
5151 .linkage = .static,
52 .name = "$n",
52 .name = ".NAME",
5353 .root_module = lib_mod,
5454 });
5555
......@@ -61,7 +61,7 @@ pub fn build(b: *std.Build) void {
6161 // This creates another `std.Build.Step.Compile`, but this one builds an executable
6262 // rather than a static library.
6363 const exe = b.addExecutable(.{
64 .name = "$n",
64 .name = ".NAME",
6565 .root_module = exe_mod,
6666 });
6767
lib/init/build.zig.zon+3-3
......@@ -6,7 +6,7 @@
66 //
77 // It is redundant to include "zig" in this name because it is already
88 // within the Zig package namespace.
9 .name = .$n,
9 .name = .LITNAME,
1010
1111 // This is a [Semantic Version](https://semver.org/).
1212 // In a future version of Zig it will be used for package deduplication.
......@@ -24,11 +24,11 @@
2424 // original project's identity. Thus it is recommended to leave the comment
2525 // on the following line intact, so that it shows up in code reviews that
2626 // modify the field.
27 .nonce = $i, // Changing this has security and trust implications.
27 .nonce = .NONCE, // Changing this has security and trust implications.
2828
2929 // Tracks the earliest Zig version that the package considers to be a
3030 // supported use case.
31 .minimum_zig_version = "$v",
31 .minimum_zig_version = ".ZIGVER",
3232
3333 // This field is optional.
3434 // This is currently advisory only; Zig does not yet do anything
lib/init/src/main.zig+1-1
......@@ -43,4 +43,4 @@ test "fuzz example" {
4343const std = @import("std");
4444
4545/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.
46const lib = @import("$n_lib");
46const lib = @import(".NAME_lib");
src/main.zig+22-19
......@@ -7543,28 +7543,31 @@ const Templates = struct {
75437543 };
75447544 templates.buffer.clearRetainingCapacity();
75457545 try templates.buffer.ensureUnusedCapacity(contents.len);
7546 var state: enum { start, dollar } = .start;
7547 for (contents) |c| switch (state) {
7548 .start => switch (c) {
7549 '$' => state = .dollar,
7550 else => try templates.buffer.append(c),
7551 },
7552 .dollar => switch (c) {
7553 'n' => {
7546 var i: usize = 0;
7547 while (i < contents.len) {
7548 if (contents[i] == '.') {
7549 if (std.mem.startsWith(u8, contents[i..], ".LITNAME")) {
7550 try templates.buffer.append('.');
75547551 try templates.buffer.appendSlice(root_name);
7555 state = .start;
7556 },
7557 'i' => {
7552 i += ".LITNAME".len;
7553 continue;
7554 } else if (std.mem.startsWith(u8, contents[i..], ".NAME")) {
7555 try templates.buffer.appendSlice(root_name);
7556 i += ".NAME".len;
7557 continue;
7558 } else if (std.mem.startsWith(u8, contents[i..], ".NONCE")) {
75587559 try templates.buffer.writer().print("0x{x}", .{nonce.int()});
7559 state = .start;
7560 },
7561 'v' => {
7560 i += ".NONCE".len;
7561 continue;
7562 } else if (std.mem.startsWith(u8, contents[i..], ".ZIGVER")) {
75627563 try templates.buffer.appendSlice(build_options.version);
7563 state = .start;
7564 },
7565 else => fatal("unknown substitution: ${c}", .{c}),
7566 },
7567 };
7564 i += ".ZIGVER".len;
7565 continue;
7566 }
7567 }
7568 try templates.buffer.append(contents[i]);
7569 i += 1;
7570 }
75687571
75697572 return out_dir.writeFile(.{
75707573 .sub_path = template_path,