@@ -86,8 +86,7 @@ const normal_usage =
86
86
\\
87
87
\\ build Build project from build.zig
88
88
\\ fetch Copy a package into global cache and print its hash
89
- \\ init-exe Initialize a `zig build` application in the cwd
90
- \\ init-lib Initialize a `zig build` library in the cwd
89
+ \\ init Initialize a Zig package in the current directory
91
90
\\
92
91
\\ ast-check Look for simple compile errors in any set of files
93
92
\\ build-exe Create executable from source or object files
@@ -320,10 +319,8 @@ pub fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
320
319
return cmdFetch (gpa , arena , cmd_args );
321
320
} else if (mem .eql (u8 , cmd , "libc" )) {
322
321
return cmdLibC (gpa , cmd_args );
323
- } else if (mem .eql (u8 , cmd , "init-exe" )) {
324
- return cmdInit (gpa , arena , cmd_args , .Exe );
325
- } else if (mem .eql (u8 , cmd , "init-lib" )) {
326
- return cmdInit (gpa , arena , cmd_args , .Lib );
322
+ } else if (mem .eql (u8 , cmd , "init" )) {
323
+ return cmdInit (gpa , arena , cmd_args );
327
324
} else if (mem .eql (u8 , cmd , "targets" )) {
328
325
const info = try detectNativeTargetInfo (.{});
329
326
const stdout = io .getStdOut ().writer ();
@@ -4835,8 +4832,7 @@ pub fn cmdLibC(gpa: Allocator, args: []const []const u8) !void {
4835
4832
}
4836
4833
4837
4834
pub const usage_init =
4838
- \\Usage: zig init-exe
4839
- \\ zig init-lib
4835
+ \\Usage: zig init
4840
4836
\\
4841
4837
\\ Initializes a `zig build` project in the current working
4842
4838
\\ directory.
@@ -4847,12 +4843,7 @@ pub const usage_init =
4847
4843
\\
4848
4844
;
4849
4845
4850
- pub fn cmdInit (
4851
- gpa : Allocator ,
4852
- arena : Allocator ,
4853
- args : []const []const u8 ,
4854
- output_mode : std.builtin.OutputMode ,
4855
- ) ! void {
4846
+ pub fn cmdInit (gpa : Allocator , arena : Allocator , args : []const []const u8 ) ! void {
4856
4847
_ = gpa ;
4857
4848
{
4858
4849
var i : usize = 0 ;
@@ -4877,61 +4868,65 @@ pub fn cmdInit(
4877
4868
defer zig_lib_directory .handle .close ();
4878
4869
4879
4870
const s = fs .path .sep_str ;
4880
- const template_sub_path = switch (output_mode ) {
4881
- .Obj = > unreachable ,
4882
- .Lib = > "init-lib" ,
4883
- .Exe = > "init-exe" ,
4884
- };
4871
+ const template_sub_path = "init" ;
4885
4872
var template_dir = zig_lib_directory .handle .openDir (template_sub_path , .{}) catch | err | {
4886
4873
const path = zig_lib_directory .path orelse "." ;
4887
- fatal ("unable to open zig project template directory '{s}{s}{s}': {s}" , .{ path , s , template_sub_path , @errorName (err ) });
4874
+ fatal ("unable to open zig project template directory '{s}{s}{s}': {s}" , .{
4875
+ path , s , template_sub_path , @errorName (err ),
4876
+ });
4888
4877
};
4889
4878
defer template_dir .close ();
4890
4879
4891
4880
const cwd_path = try process .getCwdAlloc (arena );
4892
4881
const cwd_basename = fs .path .basename (cwd_path );
4893
4882
4894
4883
const max_bytes = 10 * 1024 * 1024 ;
4895
- const build_zig_contents = template_dir .readFileAlloc (arena , "build.zig" , max_bytes ) catch | err | {
4896
- fatal ("unable to read template file 'build.zig': {s}" , .{@errorName (err )});
4884
+ const template_paths = [_ ][]const u8 {
4885
+ "build.zig" ,
4886
+ "build.zig.zon" ,
4887
+ "src" ++ s ++ "main.zig" ,
4888
+ "src" ++ s ++ "root.zig" ,
4897
4889
};
4898
- var modified_build_zig_contents = try std .ArrayList (u8 ).initCapacity (arena , build_zig_contents .len );
4899
- for (build_zig_contents ) | c | {
4900
- if (c == '$' ) {
4901
- try modified_build_zig_contents .appendSlice (cwd_basename );
4902
- } else {
4903
- try modified_build_zig_contents .append (c );
4890
+ var ok_count : usize = 0 ;
4891
+
4892
+ for (template_paths ) | template_path | {
4893
+ if (fs .path .dirname (template_path )) | dirname | {
4894
+ fs .cwd ().makePath (dirname ) catch | err | {
4895
+ fatal ("unable to make path '{s}': {s}" , .{ dirname , @errorName (err ) });
4896
+ };
4904
4897
}
4905
- }
4906
- const main_zig_contents = template_dir .readFileAlloc (arena , "src" ++ s ++ "main.zig" , max_bytes ) catch | err | {
4907
- fatal ("unable to read template file 'main.zig': {s}" , .{@errorName (err )});
4908
- };
4909
- if (fs .cwd ().access ("build.zig" , .{})) | _ | {
4910
- fatal ("existing build.zig file would be overwritten" , .{});
4911
- } else | err | switch (err ) {
4912
- error .FileNotFound = > {},
4913
- else = > fatal ("unable to test existence of build.zig: {s}\n " , .{@errorName (err )}),
4914
- }
4915
- if (fs .cwd ().access ("src" ++ s ++ "main.zig" , .{})) | _ | {
4916
- fatal ("existing src" ++ s ++ "main.zig file would be overwritten" , .{});
4917
- } else | err | switch (err ) {
4918
- error .FileNotFound = > {},
4919
- else = > fatal ("unable to test existence of src" ++ s ++ "main.zig: {s}\n " , .{@errorName (err )}),
4920
- }
4921
- var src_dir = try fs .cwd ().makeOpenPath ("src" , .{});
4922
- defer src_dir .close ();
4923
4898
4924
- try src_dir .writeFile ("main.zig" , main_zig_contents );
4925
- try fs .cwd ().writeFile ("build.zig" , modified_build_zig_contents .items );
4899
+ const contents = template_dir .readFileAlloc (arena , template_path , max_bytes ) catch | err | {
4900
+ fatal ("unable to read template file '{s}': {s}" , .{ template_path , @errorName (err ) });
4901
+ };
4902
+ var modified_contents = try std .ArrayList (u8 ).initCapacity (arena , contents .len );
4903
+ for (contents ) | c | {
4904
+ if (c == '$' ) {
4905
+ try modified_contents .appendSlice (cwd_basename );
4906
+ } else {
4907
+ try modified_contents .append (c );
4908
+ }
4909
+ }
4926
4910
4927
- std .log .info ("Created build.zig" , .{});
4928
- std .log .info ("Created src" ++ s ++ "main.zig" , .{});
4911
+ if (fs .cwd ().writeFile2 (.{
4912
+ .sub_path = template_path ,
4913
+ .data = modified_contents .items ,
4914
+ .flags = .{ .exclusive = true },
4915
+ })) | _ | {
4916
+ std .log .info ("created {s}" , .{template_path });
4917
+ ok_count += 1 ;
4918
+ } else | err | switch (err ) {
4919
+ error .PathAlreadyExists = > std .log .info ("preserving already existing file: {s}" , .{
4920
+ template_path ,
4921
+ }),
4922
+ else = > std .log .err ("unable to write {s}: {s}\n " , .{ template_path , @errorName (err ) }),
4923
+ }
4924
+ }
4929
4925
4930
- switch (output_mode ) {
4931
- .Lib = > std .log .info ("Next, try `zig build --help` or `zig build test`" , .{}),
4932
- .Exe = > std .log .info ("Next, try `zig build --help` or `zig build run`" , .{}),
4933
- .Obj = > unreachable ,
4926
+ if (ok_count == template_paths .len ) {
4927
+ std .log .info ("see `zig build --help` for a menu of options" , .{});
4934
4928
}
4929
+ return cleanExit ();
4935
4930
}
4936
4931
4937
4932
pub const usage_build =
0 commit comments