Commit 67904e925d

Andrew Kelley <andrew@ziglang.org>
2025-02-26 05:25:17
zig init: adjust template lang to allow zig fmt passthrough
1 parent ea516f0
Changed files (4)
lib/init/src/main.zig
@@ -43,4 +43,4 @@ test "fuzz example" {
 const std = @import("std");
 
 /// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.
-const lib = @import("$n_lib");
+const lib = @import(".NAME_lib");
lib/init/build.zig
@@ -42,14 +42,14 @@ pub fn build(b: *std.Build) void {
     // Modules can depend on one another using the `std.Build.Module.addImport` function.
     // This is what allows Zig source code to use `@import("foo")` where 'foo' is not a
     // file path. In this case, we set up `exe_mod` to import `lib_mod`.
-    exe_mod.addImport("$n_lib", lib_mod);
+    exe_mod.addImport(".NAME_lib", lib_mod);
 
     // Now, we will create a static library based on the module we created above.
     // This creates a `std.Build.Step.Compile`, which is the build step responsible
     // for actually invoking the compiler.
     const lib = b.addLibrary(.{
         .linkage = .static,
-        .name = "$n",
+        .name = ".NAME",
         .root_module = lib_mod,
     });
 
@@ -61,7 +61,7 @@ pub fn build(b: *std.Build) void {
     // This creates another `std.Build.Step.Compile`, but this one builds an executable
     // rather than a static library.
     const exe = b.addExecutable(.{
-        .name = "$n",
+        .name = ".NAME",
         .root_module = exe_mod,
     });
 
lib/init/build.zig.zon
@@ -6,7 +6,7 @@
     //
     // It is redundant to include "zig" in this name because it is already
     // within the Zig package namespace.
-    .name = .$n,
+    .name = .LITNAME,
 
     // This is a [Semantic Version](https://semver.org/).
     // In a future version of Zig it will be used for package deduplication.
@@ -24,11 +24,11 @@
     // original project's identity. Thus it is recommended to leave the comment
     // on the following line intact, so that it shows up in code reviews that
     // modify the field.
-    .nonce = $i, // Changing this has security and trust implications.
+    .nonce = .NONCE, // Changing this has security and trust implications.
 
     // Tracks the earliest Zig version that the package considers to be a
     // supported use case.
-    .minimum_zig_version = "$v",
+    .minimum_zig_version = ".ZIGVER",
 
     // This field is optional.
     // This is currently advisory only; Zig does not yet do anything
src/main.zig
@@ -7543,28 +7543,31 @@ const Templates = struct {
         };
         templates.buffer.clearRetainingCapacity();
         try templates.buffer.ensureUnusedCapacity(contents.len);
-        var state: enum { start, dollar } = .start;
-        for (contents) |c| switch (state) {
-            .start => switch (c) {
-                '$' => state = .dollar,
-                else => try templates.buffer.append(c),
-            },
-            .dollar => switch (c) {
-                'n' => {
+        var i: usize = 0;
+        while (i < contents.len) {
+            if (contents[i] == '.') {
+                if (std.mem.startsWith(u8, contents[i..], ".LITNAME")) {
+                    try templates.buffer.append('.');
                     try templates.buffer.appendSlice(root_name);
-                    state = .start;
-                },
-                'i' => {
+                    i += ".LITNAME".len;
+                    continue;
+                } else if (std.mem.startsWith(u8, contents[i..], ".NAME")) {
+                    try templates.buffer.appendSlice(root_name);
+                    i += ".NAME".len;
+                    continue;
+                } else if (std.mem.startsWith(u8, contents[i..], ".NONCE")) {
                     try templates.buffer.writer().print("0x{x}", .{nonce.int()});
-                    state = .start;
-                },
-                'v' => {
+                    i += ".NONCE".len;
+                    continue;
+                } else if (std.mem.startsWith(u8, contents[i..], ".ZIGVER")) {
                     try templates.buffer.appendSlice(build_options.version);
-                    state = .start;
-                },
-                else => fatal("unknown substitution: ${c}", .{c}),
-            },
-        };
+                    i += ".ZIGVER".len;
+                    continue;
+                }
+            }
+            try templates.buffer.append(contents[i]);
+            i += 1;
+        }
 
         return out_dir.writeFile(.{
             .sub_path = template_path,