authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-06-25 13:57:57-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-06-29 23:50:20-07:00
log7f9851c0d8630c6ed10fa591ef520b38c4a16f00
tree092de1d79aa063dd898764e84b4489160ae7aeef
parent2821a58fcda853487531ed054d64b5271869b79b

zig init: use the provided args rather than redundant searching


1 files changed, 15 insertions(+), 28 deletions(-)

lib/compiler/Maker.zig+15-28
......@@ -1791,7 +1791,7 @@ fn cmdInit(gpa: Allocator, graph: *Graph, args: []const []const u8) !void {
17911791
17921792 switch (template) {
17931793 .example => {
1794 var templates = findTemplates(gpa, arena, io);
1794 var templates = Templates.find(gpa, io, graph.zig_lib_directory);
17951795 defer templates.deinit(io);
17961796
17971797 const s = Dir.path.sep_str;
......@@ -3640,6 +3640,20 @@ const Templates = struct {
36403640 .flags = .{ .exclusive = true },
36413641 });
36423642 }
3643
3644 fn find(gpa: Allocator, io: Io, zig_lib_directory: Cache.Directory) Templates {
3645 const template_path: Path = .{
3646 .root_dir = zig_lib_directory,
3647 .sub_path = "init",
3648 };
3649 const template_dir = template_path.root_dir.handle.openDir(io, template_path.sub_path, .{}) catch |err|
3650 fatal("unable to open zig project template directory {f}: {t}", .{ template_path, err });
3651 return .{
3652 .zig_lib_directory = zig_lib_directory,
3653 .dir = template_dir,
3654 .buffer = std.array_list.Managed(u8).init(gpa),
3655 };
3656 }
36433657};
36443658fn writeSimpleTemplateFile(io: Io, file_name: []const u8, comptime format: []const u8, args: anytype) !void {
36453659 const f = try Io.Dir.cwd().createFile(io, file_name, .{ .exclusive = true });
......@@ -3649,30 +3663,3 @@ fn writeSimpleTemplateFile(io: Io, file_name: []const u8, comptime format: []con
36493663 try fw.interface.print(format, args);
36503664 try fw.interface.flush();
36513665}
3652
3653fn findTemplates(gpa: Allocator, arena: Allocator, io: Io) Templates {
3654 const cwd_path = std.zig.getResolvedCwd(io, arena) catch |err| {
3655 fatal("unable to get cwd: {t}", .{err});
3656 };
3657 const self_exe_path = process.executablePathAlloc(io, arena) catch |err| {
3658 fatal("unable to find self exe path: {t}", .{err});
3659 };
3660 var zig_lib_directory = std.zig.findZigLibDirFromSelfExe(arena, io, cwd_path, self_exe_path) catch |err| {
3661 fatal("unable to find zig installation directory {q}: {t}", .{ self_exe_path, err });
3662 };
3663
3664 const s = Dir.path.sep_str;
3665 const template_sub_path = "init";
3666 const template_dir = zig_lib_directory.handle.openDir(io, template_sub_path, .{}) catch |err| {
3667 const path = zig_lib_directory.path orelse ".";
3668 fatal("unable to open zig project template directory '{s}{s}{s}': {t}", .{
3669 path, s, template_sub_path, err,
3670 });
3671 };
3672
3673 return .{
3674 .zig_lib_directory = zig_lib_directory,
3675 .dir = template_dir,
3676 .buffer = std.array_list.Managed(u8).init(gpa),
3677 };
3678}