| ... | ... | @@ -1791,7 +1791,7 @@ fn cmdInit(gpa: Allocator, graph: *Graph, args: []const []const u8) !void { |
| 1791 | 1791 | |
| 1792 | 1792 | switch (template) { |
| 1793 | 1793 | .example => { |
| 1794 | | var templates = findTemplates(gpa, arena, io); |
| 1794 | var templates = Templates.find(gpa, io, graph.zig_lib_directory); |
| 1795 | 1795 | defer templates.deinit(io); |
| 1796 | 1796 | |
| 1797 | 1797 | const s = Dir.path.sep_str; |
| ... | ... | @@ -3640,6 +3640,20 @@ const Templates = struct { |
| 3640 | 3640 | .flags = .{ .exclusive = true }, |
| 3641 | 3641 | }); |
| 3642 | 3642 | } |
| 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 | } |
| 3643 | 3657 | }; |
| 3644 | 3658 | fn writeSimpleTemplateFile(io: Io, file_name: []const u8, comptime format: []const u8, args: anytype) !void { |
| 3645 | 3659 | 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 |
| 3649 | 3663 | try fw.interface.print(format, args); |
| 3650 | 3664 | try fw.interface.flush(); |
| 3651 | 3665 | } |
| 3652 | | |
| 3653 | | fn 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 | | } |