Skip to content

Commit 180e844

Browse files
authored
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.
1 parent a74119a commit 180e844

File tree

5 files changed

+22
-28
lines changed

5 files changed

+22
-28
lines changed

lib/init/build.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn build(b: *std.Build) void {
2929
// to our consumers. We must give it a name because a Zig package can expose
3030
// multiple modules and consumers will need to be able to specify which
3131
// module they want to access.
32-
const mod = b.addModule(".NAME", .{
32+
const mod = b.addModule("_NAME", .{
3333
// The root source file is the "entry point" of this module. Users of
3434
// this module will only be able to access public declarations contained
3535
// in this file, which means that if you have declarations that you
@@ -59,7 +59,7 @@ pub fn build(b: *std.Build) void {
5959
// If neither case applies to you, feel free to delete the declaration you
6060
// don't need and to put everything under a single module.
6161
const exe = b.addExecutable(.{
62-
.name = ".NAME",
62+
.name = "_NAME",
6363
.root_module = b.createModule(.{
6464
// b.createModule defines a new module just like b.addModule but,
6565
// unlike b.addModule, it does not expose the module to consumers of
@@ -74,12 +74,12 @@ pub fn build(b: *std.Build) void {
7474
// List of modules available for import in source files part of the
7575
// root module.
7676
.imports = &.{
77-
// Here ".NAME" is the name you will use in your source code to
78-
// import this module (e.g. `@import(".NAME")`). The name is
77+
// Here "_NAME" is the name you will use in your source code to
78+
// import this module (e.g. `@import("_NAME")`). The name is
7979
// repeated because you are allowed to rename your imports, which
8080
// can be extremely useful in case of collisions (which can happen
8181
// importing modules from different packages).
82-
.{ .name = ".NAME", .module = mod },
82+
.{ .name = "_NAME", .module = mod },
8383
},
8484
}),
8585
});

lib/init/build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// It is redundant to include "zig" in this name because it is already
88
// within the Zig package namespace.
9-
.name = .LITNAME,
9+
.name = ._NAME,
1010
// This is a [Semantic Version](https://semver.org/).
1111
// In a future version of Zig it will be used for package deduplication.
1212
.version = "0.0.0",
@@ -25,7 +25,7 @@
2525
.fingerprint = .FINGERPRINT, // Changing this has security and trust implications.
2626
// Tracks the earliest Zig version that the package considers to be a
2727
// supported use case.
28-
.minimum_zig_version = ".ZIGVER",
28+
.minimum_zig_version = "_ZIGVER",
2929
// This field is optional.
3030
// Each dependency must either provide a `url` and `hash`, or a `path`.
3131
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.

lib/init/src/main.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const std = @import("std");
2-
const _LITNAME = @import(".NAME");
2+
const _NAME = @import(".NAME");
33

44
pub fn main() !void {
55
// Prints to stderr, ignoring potential errors.
66
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
7-
try .NAME.advancedPrint();
7+
try _NAME.bufferedPrint();
88
}
99

1010
test "simple test" {

lib/init/src/root.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! By convention, root.zig is the root source file when making a library.
22
const std = @import("std");
33

4-
pub fn advancedPrint() !void {
4+
pub fn bufferedPrint() !void {
55
// Stdout is for the actual output of your application, for example if you
66
// are implementing gzip, then only the compressed bytes should be sent to
77
// stdout, not any debugging messages.
88
const stdout_file = std.io.getStdOut().writer();
9+
// Buffering can improve performance significantly in print-heavy programs.
910
var bw = std.io.bufferedWriter(stdout_file);
1011
const stdout = bw.writer();
1112

src/main.zig

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7290,34 +7290,27 @@ const Templates = struct {
72907290
new_line = false;
72917291
}
72927292
}
7293+
72937294
if (templates.strip and contents[i] == '\n') {
72947295
new_line = true;
7295-
} else if (contents[i] == '_') {
7296-
if (std.mem.startsWith(u8, contents[i..], "_LITNAME")) {
7297-
try templates.buffer.appendSlice(root_name);
7298-
i += "_LITNAME".len;
7299-
continue;
7300-
}
7301-
} else if (contents[i] == '.') {
7302-
if (std.mem.startsWith(u8, contents[i..], ".LITNAME")) {
7303-
try templates.buffer.append('.');
7296+
} else if (contents[i] == '_' or contents[i] == '.') {
7297+
// Both '_' and '.' are allowed because depending on the context
7298+
// one prefix will be valid, while the other might not.
7299+
if (std.mem.startsWith(u8, contents[i + 1 ..], "NAME")) {
73047300
try templates.buffer.appendSlice(root_name);
7305-
i += ".LITNAME".len;
7301+
i += "_NAME".len;
73067302
continue;
7307-
} else if (std.mem.startsWith(u8, contents[i..], ".NAME")) {
7308-
try templates.buffer.appendSlice(root_name);
7309-
i += ".NAME".len;
7310-
continue;
7311-
} else if (std.mem.startsWith(u8, contents[i..], ".FINGERPRINT")) {
7303+
} else if (std.mem.startsWith(u8, contents[i + 1 ..], "FINGERPRINT")) {
73127304
try templates.buffer.writer().print("0x{x}", .{fingerprint.int()});
7313-
i += ".FINGERPRINT".len;
7305+
i += "_FINGERPRINT".len;
73147306
continue;
7315-
} else if (std.mem.startsWith(u8, contents[i..], ".ZIGVER")) {
7307+
} else if (std.mem.startsWith(u8, contents[i + 1 ..], "ZIGVER")) {
73167308
try templates.buffer.appendSlice(build_options.version);
7317-
i += ".ZIGVER".len;
7309+
i += "_ZIGVER".len;
73187310
continue;
73197311
}
73207312
}
7313+
73217314
try templates.buffer.append(contents[i]);
73227315
i += 1;
73237316
}

0 commit comments

Comments
 (0)