zig init template: add compiler version to build.zig.zon by default#22698
zig init template: add compiler version to build.zig.zon by default#22698sweetbbak wants to merge 4 commits intoziglang:masterfrom
Conversation
|
You can now define variables and replacements like so: // default replacements for templates
const replacements = &[_]Replacement{
.{ .variable = "$root", .replacement = cwd_basename },
.{ .variable = "$version", .replacement = build_options.version },
};
for (template_paths) |template_path| {
if (templates.write(arena, fs.cwd(), template_path, replacements)) |_| {
std.log.info("created {s}", .{template_path});
ok_count += 1;
} else |err| switch (err) {
error.PathAlreadyExists => std.log.info("preserving already existing file: {s}", .{
template_path,
}),
else => std.log.err("unable to write {s}: {s}\n", .{ template_path, @errorName(err) }),
}
}one edge case I haven't addressed would be if variables start with the same prefix. I guess a simple solution to that would be to ensure that all identifiers have unique prefixes. The variable |
db12558 to
423a9a3
Compare
|
Needs a |
such a silly mistake lol
26b3fec to
986565b
Compare
andrewrk
left a comment
There was a problem hiding this comment.
sorry, this is adhoc templating language is too complicated for how featureless it is.
it either should be kept simple by using fixed width tokens (i.e. finding another symbol besides '$' to mean different things, or updating to use length-2 tokens such as "$a" and "$b"
or it should go full tokenization and be able to say "unrecognized substitution: $misspelled_thing"
what's in this PR is an awkward middle ground between both of these things.
maybe it can be recovered if you fail with an error message that prints the rest of the line in case no match is found.
please use mem.startsWith rather than crashing when $ is at the end of the file
|
I have this solved as part of #22994 |
referenced here on ziggit which describes the problem in depth, but the gist is that this solves the problem of having to manually try many different compiler versions to attempt to build a project that is unclear what version was used. If someone uses a tagged version, its not such a big deal (still annoying) but if they used a non-tagged version, it becomes nigh impossible to pin down what zig version was used to build a project. This currently just serves as an anchor point of reference so this is easier. It could also allow zig version managers to automatically use the correct version of the zig compiler.
Instead of parsing the zon file, I opted for using a wildcard like what is used for the '.name' field and just appending the version to the buffer that gets written.
It doesn't handle updating the version, I guess its better for the programmer to do so manually, and another simple expansion upon this could be emitting a warning when the current zig version is lower than or not equal to what is specified in the
build.zig.zonfile.