aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-11-20 23:01:45 -0700
committerAndrew Kelley <andrew@ziglang.org>2023-11-20 23:01:45 -0700
commitf645022d16361865e24582d28f1e62312fbc73bb (patch)
tree7a7f03aff7bc7b9dc3f4ba1b3be57bd17caae1b7 /src/main.zig
parent8ca4a5240e0258c84e437ed4a37972c8fff7842d (diff)
downloadzig-f645022d16361865e24582d28f1e62312fbc73bb.tar.gz
zig-f645022d16361865e24582d28f1e62312fbc73bb.zip
merge `zig init-exe` and `zig init-lib` into `zig init`
Instead of `zig init-lib` and `zig init-exe`, now there is only `zig init`, which initializes any of the template files that do not already exist, and makes a package that contains both an executable and a static library. The idea is that the user can delete whatever they don't want. In fact, I think even more things should be added to the build.zig template.
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig102
1 files changed, 48 insertions, 54 deletions
diff --git a/src/main.zig b/src/main.zig
index 6bab14df56..954e00e7f1 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -86,8 +86,7 @@ const normal_usage =
\\
\\ build Build project from build.zig
\\ fetch Copy a package into global cache and print its hash
- \\ init-exe Initialize a `zig build` application in the cwd
- \\ init-lib Initialize a `zig build` library in the cwd
+ \\ init Initialize a Zig package in the current directory
\\
\\ ast-check Look for simple compile errors in any set of files
\\ 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
return cmdFetch(gpa, arena, cmd_args);
} else if (mem.eql(u8, cmd, "libc")) {
return cmdLibC(gpa, cmd_args);
- } else if (mem.eql(u8, cmd, "init-exe")) {
- return cmdInit(gpa, arena, cmd_args, .Exe);
- } else if (mem.eql(u8, cmd, "init-lib")) {
- return cmdInit(gpa, arena, cmd_args, .Lib);
+ } else if (mem.eql(u8, cmd, "init")) {
+ return cmdInit(gpa, arena, cmd_args);
} else if (mem.eql(u8, cmd, "targets")) {
const info = try detectNativeTargetInfo(.{});
const stdout = io.getStdOut().writer();
@@ -4835,8 +4832,7 @@ pub fn cmdLibC(gpa: Allocator, args: []const []const u8) !void {
}
pub const usage_init =
- \\Usage: zig init-exe
- \\ zig init-lib
+ \\Usage: zig init
\\
\\ Initializes a `zig build` project in the current working
\\ directory.
@@ -4847,12 +4843,7 @@ pub const usage_init =
\\
;
-pub fn cmdInit(
- gpa: Allocator,
- arena: Allocator,
- args: []const []const u8,
- output_mode: std.builtin.OutputMode,
-) !void {
+pub fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
_ = gpa;
{
var i: usize = 0;
@@ -4877,14 +4868,12 @@ pub fn cmdInit(
defer zig_lib_directory.handle.close();
const s = fs.path.sep_str;
- const template_sub_path = switch (output_mode) {
- .Obj => unreachable,
- .Lib => "init-lib",
- .Exe => "init-exe",
- };
+ const template_sub_path = "init";
var template_dir = zig_lib_directory.handle.openDir(template_sub_path, .{}) catch |err| {
const path = zig_lib_directory.path orelse ".";
- fatal("unable to open zig project template directory '{s}{s}{s}': {s}", .{ path, s, template_sub_path, @errorName(err) });
+ fatal("unable to open zig project template directory '{s}{s}{s}': {s}", .{
+ path, s, template_sub_path, @errorName(err),
+ });
};
defer template_dir.close();
@@ -4892,46 +4881,51 @@ pub fn cmdInit(
const cwd_basename = fs.path.basename(cwd_path);
const max_bytes = 10 * 1024 * 1024;
- const build_zig_contents = template_dir.readFileAlloc(arena, "build.zig", max_bytes) catch |err| {
- fatal("unable to read template file 'build.zig': {s}", .{@errorName(err)});
+ const template_paths = [_][]const u8{
+ "build.zig",
+ "src" ++ s ++ "main.zig",
+ "src" ++ s ++ "root.zig",
};
- var modified_build_zig_contents = try std.ArrayList(u8).initCapacity(arena, build_zig_contents.len);
- for (build_zig_contents) |c| {
- if (c == '$') {
- try modified_build_zig_contents.appendSlice(cwd_basename);
- } else {
- try modified_build_zig_contents.append(c);
+ var ok_count: usize = 0;
+
+ for (template_paths) |template_path| {
+ if (fs.path.dirname(template_path)) |dirname| {
+ fs.cwd().makePath(dirname) catch |err| {
+ fatal("unable to make path '{s}': {s}", .{ dirname, @errorName(err) });
+ };
}
- }
- const main_zig_contents = template_dir.readFileAlloc(arena, "src" ++ s ++ "main.zig", max_bytes) catch |err| {
- fatal("unable to read template file 'main.zig': {s}", .{@errorName(err)});
- };
- if (fs.cwd().access("build.zig", .{})) |_| {
- fatal("existing build.zig file would be overwritten", .{});
- } else |err| switch (err) {
- error.FileNotFound => {},
- else => fatal("unable to test existence of build.zig: {s}\n", .{@errorName(err)}),
- }
- if (fs.cwd().access("src" ++ s ++ "main.zig", .{})) |_| {
- fatal("existing src" ++ s ++ "main.zig file would be overwritten", .{});
- } else |err| switch (err) {
- error.FileNotFound => {},
- else => fatal("unable to test existence of src" ++ s ++ "main.zig: {s}\n", .{@errorName(err)}),
- }
- var src_dir = try fs.cwd().makeOpenPath("src", .{});
- defer src_dir.close();
- try src_dir.writeFile("main.zig", main_zig_contents);
- try fs.cwd().writeFile("build.zig", modified_build_zig_contents.items);
+ const contents = template_dir.readFileAlloc(arena, template_path, max_bytes) catch |err| {
+ fatal("unable to read template file '{s}': {s}", .{ template_path, @errorName(err) });
+ };
+ var modified_contents = try std.ArrayList(u8).initCapacity(arena, contents.len);
+ for (contents) |c| {
+ if (c == '$') {
+ try modified_contents.appendSlice(cwd_basename);
+ } else {
+ try modified_contents.append(c);
+ }
+ }
- std.log.info("Created build.zig", .{});
- std.log.info("Created src" ++ s ++ "main.zig", .{});
+ if (fs.cwd().writeFile2(.{
+ .sub_path = template_path,
+ .data = modified_contents.items,
+ .flags = .{ .exclusive = true },
+ })) |_| {
+ 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) }),
+ }
+ }
- switch (output_mode) {
- .Lib => std.log.info("Next, try `zig build --help` or `zig build test`", .{}),
- .Exe => std.log.info("Next, try `zig build --help` or `zig build run`", .{}),
- .Obj => unreachable,
+ if (ok_count == template_paths.len) {
+ std.log.info("see `zig build --help` for a menu of options", .{});
}
+ return cleanExit();
}
pub const usage_build =