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 {...@@ -42,14 +42,14 @@ pub fn build(b: *std.Build) void {
42 // Modules can depend on one another using the `std.Build.Module.addImport` function.42 // Modules can depend on one another using the `std.Build.Module.addImport` function.
43 // This is what allows Zig source code to use `@import("foo")` where 'foo' is not a43 // This is what allows Zig source code to use `@import("foo")` where 'foo' is not a
44 // file path. In this case, we set up `exe_mod` to import `lib_mod`.44 // 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
47 // Now, we will create a static library based on the module we created above.47 // Now, we will create a static library based on the module we created above.
48 // This creates a `std.Build.Step.Compile`, which is the build step responsible48 // This creates a `std.Build.Step.Compile`, which is the build step responsible
49 // for actually invoking the compiler.49 // for actually invoking the compiler.
50 const lib = b.addLibrary(.{50 const lib = b.addLibrary(.{
51 .linkage = .static,51 .linkage = .static,
52 .name = "$n",52 .name = ".NAME",
53 .root_module = lib_mod,53 .root_module = lib_mod,
54 });54 });
5555
...@@ -61,7 +61,7 @@ pub fn build(b: *std.Build) void {...@@ -61,7 +61,7 @@ pub fn build(b: *std.Build) void {
61 // This creates another `std.Build.Step.Compile`, but this one builds an executable61 // This creates another `std.Build.Step.Compile`, but this one builds an executable
62 // rather than a static library.62 // rather than a static library.
63 const exe = b.addExecutable(.{63 const exe = b.addExecutable(.{
64 .name = "$n",64 .name = ".NAME",
65 .root_module = exe_mod,65 .root_module = exe_mod,
66 });66 });
6767
lib/init/build.zig.zon+3-3
...@@ -6,7 +6,7 @@...@@ -6,7 +6,7 @@
6 //6 //
7 // It is redundant to include "zig" in this name because it is already7 // It is redundant to include "zig" in this name because it is already
8 // within the Zig package namespace.8 // within the Zig package namespace.
9 .name = .$n,9 .name = .LITNAME,
1010
11 // This is a [Semantic Version](https://semver.org/).11 // This is a [Semantic Version](https://semver.org/).
12 // In a future version of Zig it will be used for package deduplication.12 // In a future version of Zig it will be used for package deduplication.
...@@ -24,11 +24,11 @@...@@ -24,11 +24,11 @@
24 // original project's identity. Thus it is recommended to leave the comment24 // original project's identity. Thus it is recommended to leave the comment
25 // on the following line intact, so that it shows up in code reviews that25 // on the following line intact, so that it shows up in code reviews that
26 // modify the field.26 // modify the field.
27 .nonce = $i, // Changing this has security and trust implications.27 .nonce = .NONCE, // Changing this has security and trust implications.
2828
29 // Tracks the earliest Zig version that the package considers to be a29 // Tracks the earliest Zig version that the package considers to be a
30 // supported use case.30 // supported use case.
31 .minimum_zig_version = "$v",31 .minimum_zig_version = ".ZIGVER",
3232
33 // This field is optional.33 // This field is optional.
34 // This is currently advisory only; Zig does not yet do anything34 // 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" {...@@ -43,4 +43,4 @@ test "fuzz example" {
43const std = @import("std");43const std = @import("std");
4444
45/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.45/// 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 {...@@ -7543,28 +7543,31 @@ const Templates = struct {
7543 };7543 };
7544 templates.buffer.clearRetainingCapacity();7544 templates.buffer.clearRetainingCapacity();
7545 try templates.buffer.ensureUnusedCapacity(contents.len);7545 try templates.buffer.ensureUnusedCapacity(contents.len);
7546 var state: enum { start, dollar } = .start;7546 var i: usize = 0;
7547 for (contents) |c| switch (state) {7547 while (i < contents.len) {
7548 .start => switch (c) {7548 if (contents[i] == '.') {
7549 '$' => state = .dollar,7549 if (std.mem.startsWith(u8, contents[i..], ".LITNAME")) {
7550 else => try templates.buffer.append(c),7550 try templates.buffer.append('.');
7551 },
7552 .dollar => switch (c) {
7553 'n' => {
7554 try templates.buffer.appendSlice(root_name);7551 try templates.buffer.appendSlice(root_name);
7555 state = .start;7552 i += ".LITNAME".len;
7556 },7553 continue;
7557 'i' => {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")) {
7558 try templates.buffer.writer().print("0x{x}", .{nonce.int()});7559 try templates.buffer.writer().print("0x{x}", .{nonce.int()});
7559 state = .start;7560 i += ".NONCE".len;
7560 },7561 continue;
7561 'v' => {7562 } else if (std.mem.startsWith(u8, contents[i..], ".ZIGVER")) {
7562 try templates.buffer.appendSlice(build_options.version);7563 try templates.buffer.appendSlice(build_options.version);
7563 state = .start;7564 i += ".ZIGVER".len;
7564 },7565 continue;
7565 else => fatal("unknown substitution: ${c}", .{c}),7566 }
7566 },7567 }
7567 };7568 try templates.buffer.append(contents[i]);
7569 i += 1;
7570 }
75687571
7569 return out_dir.writeFile(.{7572 return out_dir.writeFile(.{
7570 .sub_path = template_path,7573 .sub_path = template_path,