zig init: adjust template lang to allow zig fmt passthrough

This commit is contained in:
Andrew Kelley 2025-02-25 20:25:17 -08:00
parent ea516f0e81
commit 67904e925d
4 changed files with 29 additions and 26 deletions

View file

@ -42,14 +42,14 @@ pub fn build(b: *std.Build) void {
// Modules can depend on one another using the `std.Build.Module.addImport` function. // 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 // 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`. // 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. // 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 // This creates a `std.Build.Step.Compile`, which is the build step responsible
// for actually invoking the compiler. // for actually invoking the compiler.
const lib = b.addLibrary(.{ const lib = b.addLibrary(.{
.linkage = .static, .linkage = .static,
.name = "$n", .name = ".NAME",
.root_module = lib_mod, .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 // This creates another `std.Build.Step.Compile`, but this one builds an executable
// rather than a static library. // rather than a static library.
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "$n", .name = ".NAME",
.root_module = exe_mod, .root_module = exe_mod,
}); });

View file

@ -6,7 +6,7 @@
// //
// It is redundant to include "zig" in this name because it is already // It is redundant to include "zig" in this name because it is already
// within the Zig package namespace. // within the Zig package namespace.
.name = .$n, .name = .LITNAME,
// This is a [Semantic Version](https://semver.org/). // This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication. // 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 // 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 // on the following line intact, so that it shows up in code reviews that
// modify the field. // 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 // Tracks the earliest Zig version that the package considers to be a
// supported use case. // supported use case.
.minimum_zig_version = "$v", .minimum_zig_version = ".ZIGVER",
// This field is optional. // This field is optional.
// This is currently advisory only; Zig does not yet do anything // This is currently advisory only; Zig does not yet do anything

View file

@ -43,4 +43,4 @@ test "fuzz example" {
const std = @import("std"); const std = @import("std");
/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details. /// 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");

View file

@ -7543,28 +7543,31 @@ const Templates = struct {
}; };
templates.buffer.clearRetainingCapacity(); templates.buffer.clearRetainingCapacity();
try templates.buffer.ensureUnusedCapacity(contents.len); try templates.buffer.ensureUnusedCapacity(contents.len);
var state: enum { start, dollar } = .start; var i: usize = 0;
for (contents) |c| switch (state) { while (i < contents.len) {
.start => switch (c) { if (contents[i] == '.') {
'$' => state = .dollar, if (std.mem.startsWith(u8, contents[i..], ".LITNAME")) {
else => try templates.buffer.append(c), try templates.buffer.append('.');
},
.dollar => switch (c) {
'n' => {
try templates.buffer.appendSlice(root_name); try templates.buffer.appendSlice(root_name);
state = .start; i += ".LITNAME".len;
}, continue;
'i' => { } 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()}); try templates.buffer.writer().print("0x{x}", .{nonce.int()});
state = .start; i += ".NONCE".len;
}, continue;
'v' => { } else if (std.mem.startsWith(u8, contents[i..], ".ZIGVER")) {
try templates.buffer.appendSlice(build_options.version); try templates.buffer.appendSlice(build_options.version);
state = .start; i += ".ZIGVER".len;
}, continue;
else => fatal("unknown substitution: ${c}", .{c}), }
}, }
}; try templates.buffer.append(contents[i]);
i += 1;
}
return out_dir.writeFile(.{ return out_dir.writeFile(.{
.sub_path = template_path, .sub_path = template_path,