mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
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.
This commit is contained in:
parent
a74119ac49
commit
180e8442af
5 changed files with 22 additions and 28 deletions
|
|
@ -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
|
// 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
|
// multiple modules and consumers will need to be able to specify which
|
||||||
// module they want to access.
|
// 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
|
// The root source file is the "entry point" of this module. Users of
|
||||||
// this module will only be able to access public declarations contained
|
// this module will only be able to access public declarations contained
|
||||||
// in this file, which means that if you have declarations that you
|
// 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
|
// If neither case applies to you, feel free to delete the declaration you
|
||||||
// don't need and to put everything under a single module.
|
// don't need and to put everything under a single module.
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
.name = ".NAME",
|
.name = "_NAME",
|
||||||
.root_module = b.createModule(.{
|
.root_module = b.createModule(.{
|
||||||
// b.createModule defines a new module just like b.addModule but,
|
// b.createModule defines a new module just like b.addModule but,
|
||||||
// unlike b.addModule, it does not expose the module to consumers of
|
// 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
|
// List of modules available for import in source files part of the
|
||||||
// root module.
|
// root module.
|
||||||
.imports = &.{
|
.imports = &.{
|
||||||
// Here ".NAME" is the name you will use in your source code to
|
// Here "_NAME" is the name you will use in your source code to
|
||||||
// import this module (e.g. `@import(".NAME")`). The name is
|
// import this module (e.g. `@import("_NAME")`). The name is
|
||||||
// repeated because you are allowed to rename your imports, which
|
// repeated because you are allowed to rename your imports, which
|
||||||
// can be extremely useful in case of collisions (which can happen
|
// can be extremely useful in case of collisions (which can happen
|
||||||
// importing modules from different packages).
|
// importing modules from different packages).
|
||||||
.{ .name = ".NAME", .module = mod },
|
.{ .name = "_NAME", .module = mod },
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -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 = .LITNAME,
|
.name = ._NAME,
|
||||||
// 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.
|
||||||
.version = "0.0.0",
|
.version = "0.0.0",
|
||||||
|
|
@ -25,7 +25,7 @@
|
||||||
.fingerprint = .FINGERPRINT, // Changing this has security and trust implications.
|
.fingerprint = .FINGERPRINT, // 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 = ".ZIGVER",
|
.minimum_zig_version = "_ZIGVER",
|
||||||
// This field is optional.
|
// This field is optional.
|
||||||
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
// 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.
|
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const _LITNAME = @import(".NAME");
|
const _NAME = @import(".NAME");
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
// Prints to stderr, ignoring potential errors.
|
// Prints to stderr, ignoring potential errors.
|
||||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
||||||
try .NAME.advancedPrint();
|
try _NAME.bufferedPrint();
|
||||||
}
|
}
|
||||||
|
|
||||||
test "simple test" {
|
test "simple test" {
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
//! By convention, root.zig is the root source file when making a library.
|
//! By convention, root.zig is the root source file when making a library.
|
||||||
const std = @import("std");
|
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
|
// 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
|
// are implementing gzip, then only the compressed bytes should be sent to
|
||||||
// stdout, not any debugging messages.
|
// stdout, not any debugging messages.
|
||||||
const stdout_file = std.io.getStdOut().writer();
|
const stdout_file = std.io.getStdOut().writer();
|
||||||
|
// Buffering can improve performance significantly in print-heavy programs.
|
||||||
var bw = std.io.bufferedWriter(stdout_file);
|
var bw = std.io.bufferedWriter(stdout_file);
|
||||||
const stdout = bw.writer();
|
const stdout = bw.writer();
|
||||||
|
|
||||||
|
|
|
||||||
29
src/main.zig
29
src/main.zig
|
|
@ -7290,34 +7290,27 @@ const Templates = struct {
|
||||||
new_line = false;
|
new_line = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (templates.strip and contents[i] == '\n') {
|
if (templates.strip and contents[i] == '\n') {
|
||||||
new_line = true;
|
new_line = true;
|
||||||
} else if (contents[i] == '_') {
|
} else if (contents[i] == '_' or contents[i] == '.') {
|
||||||
if (std.mem.startsWith(u8, contents[i..], "_LITNAME")) {
|
// 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);
|
try templates.buffer.appendSlice(root_name);
|
||||||
i += "_LITNAME".len;
|
i += "_NAME".len;
|
||||||
continue;
|
continue;
|
||||||
}
|
} else if (std.mem.startsWith(u8, contents[i + 1 ..], "FINGERPRINT")) {
|
||||||
} 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")) {
|
|
||||||
try templates.buffer.writer().print("0x{x}", .{fingerprint.int()});
|
try templates.buffer.writer().print("0x{x}", .{fingerprint.int()});
|
||||||
i += ".FINGERPRINT".len;
|
i += "_FINGERPRINT".len;
|
||||||
continue;
|
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);
|
try templates.buffer.appendSlice(build_options.version);
|
||||||
i += ".ZIGVER".len;
|
i += "_ZIGVER".len;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try templates.buffer.append(contents[i]);
|
try templates.buffer.append(contents[i]);
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue