From 67904e925d2b33c48dda3d4ddaf158328964dc2e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 25 Feb 2025 20:25:17 -0800 Subject: [PATCH] zig init: adjust template lang to allow zig fmt passthrough --- lib/init/build.zig | 6 +++--- lib/init/build.zig.zon | 6 +++--- lib/init/src/main.zig | 2 +- src/main.zig | 41 ++++++++++++++++++++++------------------- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/lib/init/build.zig b/lib/init/build.zig index ec25698c68..4db31713e4 100644 --- a/lib/init/build.zig +++ b/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, }); diff --git a/lib/init/build.zig.zon b/lib/init/build.zig.zon index 061254fd67..b154ddc16b 100644 --- a/lib/init/build.zig.zon +++ b/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 diff --git a/lib/init/src/main.zig b/lib/init/src/main.zig index cc69127d4f..85d8c93551 100644 --- a/lib/init/src/main.zig +++ b/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"); diff --git a/src/main.zig b/src/main.zig index d22d682ded..3dc6155db6 100644 --- a/src/main.zig +++ b/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,