From 180e8442af0c29924e021aed327bb4070665af65 Mon Sep 17 00:00:00 2001 From: Loris Cro Date: Sat, 14 Jun 2025 00:31:29 +0200 Subject: [PATCH] zig init: simplify templating logic (#24170) and also rename `advancedPrint` to `bufferedPrint` in the zig init templates These are left overs from my previous changes to zig init. The new templating system removes LITNAME because the new restrictions on package names make it redundant with NAME, and the use of underscores for marking templated identifiers lets us template variable names while still keeping zig fmt happy. --- lib/init/build.zig | 10 +++++----- lib/init/build.zig.zon | 4 ++-- lib/init/src/main.zig | 4 ++-- lib/init/src/root.zig | 3 ++- src/main.zig | 29 +++++++++++------------------ 5 files changed, 22 insertions(+), 28 deletions(-) diff --git a/lib/init/build.zig b/lib/init/build.zig index 969c315c2ea66820a7267c10248cad79735b01d2..8a1c03819b3d694f5cf77e9c3884e15b1059e2ed 100644 --- a/lib/init/build.zig +++ b/lib/init/build.zig @@ -29,7 +29,7 @@ pub fn build(b: *std.Build) void { // to our consumers. We must give it a name because a Zig package can expose // multiple modules and consumers will need to be able to specify which // module they want to access. - const mod = b.addModule(".NAME", .{ + const mod = b.addModule("_NAME", .{ // The root source file is the "entry point" of this module. Users of // this module will only be able to access public declarations contained // in this file, which means that if you have declarations that you @@ -59,7 +59,7 @@ pub fn build(b: *std.Build) void { // If neither case applies to you, feel free to delete the declaration you // don't need and to put everything under a single module. const exe = b.addExecutable(.{ - .name = ".NAME", + .name = "_NAME", .root_module = b.createModule(.{ // b.createModule defines a new module just like b.addModule but, // unlike b.addModule, it does not expose the module to consumers of @@ -74,12 +74,12 @@ pub fn build(b: *std.Build) void { // List of modules available for import in source files part of the // root module. .imports = &.{ - // Here ".NAME" is the name you will use in your source code to - // import this module (e.g. `@import(".NAME")`). The name is + // Here "_NAME" is the name you will use in your source code to + // import this module (e.g. `@import("_NAME")`). The name is // repeated because you are allowed to rename your imports, which // can be extremely useful in case of collisions (which can happen // importing modules from different packages). - .{ .name = ".NAME", .module = mod }, + .{ .name = "_NAME", .module = mod }, }, }), }); diff --git a/lib/init/build.zig.zon b/lib/init/build.zig.zon index ea1e6cc8f48b23f9eaedf8d1200ece05b62238b7..80f300bf4b1875a01cd38ddcb8b29f487d286476 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 = .LITNAME, + .name = ._NAME, // This is a [Semantic Version](https://semver.org/). // In a future version of Zig it will be used for package deduplication. .version = "0.0.0", @@ -25,7 +25,7 @@ .fingerprint = .FINGERPRINT, // 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 = ".ZIGVER", + .minimum_zig_version = "_ZIGVER", // This field is optional. // Each dependency must either provide a `url` and `hash`, or a `path`. // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. diff --git a/lib/init/src/main.zig b/lib/init/src/main.zig index da79c4081c8d33459d3fb13cfbfae7e5d6bed600..2d233b52cf162f800a49591a2e9019dbae974396 100644 --- a/lib/init/src/main.zig +++ b/lib/init/src/main.zig @@ -1,10 +1,10 @@ const std = @import("std"); -const _LITNAME = @import(".NAME"); +const _NAME = @import(".NAME"); pub fn main() !void { // Prints to stderr, ignoring potential errors. std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); - try .NAME.advancedPrint(); + try _NAME.bufferedPrint(); } test "simple test" { diff --git a/lib/init/src/root.zig b/lib/init/src/root.zig index 2c06f1f87cf9aabd0d421230004f3eff3fd770ed..70f4bd8d82ed8ac1faf78e24770a975432d05032 100644 --- a/lib/init/src/root.zig +++ b/lib/init/src/root.zig @@ -1,11 +1,12 @@ //! By convention, root.zig is the root source file when making a library. const std = @import("std"); -pub fn advancedPrint() !void { +pub fn bufferedPrint() !void { // Stdout is for the actual output of your application, for example if you // are implementing gzip, then only the compressed bytes should be sent to // stdout, not any debugging messages. const stdout_file = std.io.getStdOut().writer(); + // Buffering can improve performance significantly in print-heavy programs. var bw = std.io.bufferedWriter(stdout_file); const stdout = bw.writer(); diff --git a/src/main.zig b/src/main.zig index 3821fafb80e1ba3099b66df995bee22ad34c9b33..00954b2564ab7e80339bdd9e4e1ab5634eebfa4d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -7290,34 +7290,27 @@ const Templates = struct { new_line = false; } } + if (templates.strip and contents[i] == '\n') { new_line = true; - } else if (contents[i] == '_') { - if (std.mem.startsWith(u8, contents[i..], "_LITNAME")) { + } else if (contents[i] == '_' or contents[i] == '.') { + // Both '_' and '.' are allowed because depending on the context + // one prefix will be valid, while the other might not. + if (std.mem.startsWith(u8, contents[i + 1 ..], "NAME")) { try templates.buffer.appendSlice(root_name); - i += "_LITNAME".len; + i += "_NAME".len; continue; - } - } else if (contents[i] == '.') { - if (std.mem.startsWith(u8, contents[i..], ".LITNAME")) { - try templates.buffer.append('.'); - try templates.buffer.appendSlice(root_name); - 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..], ".FINGERPRINT")) { + } else if (std.mem.startsWith(u8, contents[i + 1 ..], "FINGERPRINT")) { try templates.buffer.writer().print("0x{x}", .{fingerprint.int()}); - i += ".FINGERPRINT".len; + i += "_FINGERPRINT".len; continue; - } else if (std.mem.startsWith(u8, contents[i..], ".ZIGVER")) { + } else if (std.mem.startsWith(u8, contents[i + 1 ..], "ZIGVER")) { try templates.buffer.appendSlice(build_options.version); - i += ".ZIGVER".len; + i += "_ZIGVER".len; continue; } } + try templates.buffer.append(contents[i]); i += 1; } -- 2.54.0