build: improve the help message in ZiglingStep

Add the Mode enum, so that ZiglingStep can detect if it was called in
normal mode or in named mode.

Update the help method to print the correct message based on the current
build mode.
This commit is contained in:
Manlio Perillo 2023-05-13 21:52:57 +02:00
parent 9b7ef7d8cf
commit c393debe44

View file

@ -66,6 +66,14 @@ pub const Exercise = struct {
} }
}; };
/// Build mode.
const Mode = enum {
/// Normal build mode: `zig build`
normal,
/// Named build mode: `zig build -Dn=n`
named,
};
pub const logo = pub const logo =
\\ _ _ _ \\ _ _ _
\\ ___(_) __ _| (_)_ __ __ _ ___ \\ ___(_) __ _| (_)_ __ __ _ ___
@ -141,7 +149,7 @@ pub fn build(b: *Build) !void {
b.default_step = zigling_step; b.default_step = zigling_step;
zigling_step.dependOn(&header_step.step); zigling_step.dependOn(&header_step.step);
const verify_step = ZiglingStep.create(b, ex, work_path); const verify_step = ZiglingStep.create(b, ex, work_path, .named);
verify_step.step.dependOn(&header_step.step); verify_step.step.dependOn(&header_step.step);
zigling_step.dependOn(&verify_step.step); zigling_step.dependOn(&verify_step.step);
@ -156,7 +164,7 @@ pub fn build(b: *Build) !void {
var prev_step = &header_step.step; var prev_step = &header_step.step;
for (exercises) |ex| { for (exercises) |ex| {
const verify_stepn = ZiglingStep.create(b, ex, work_path); const verify_stepn = ZiglingStep.create(b, ex, work_path, .normal);
verify_stepn.step.dependOn(prev_step); verify_stepn.step.dependOn(prev_step);
prev_step = &verify_stepn.step; prev_step = &verify_stepn.step;
@ -177,12 +185,18 @@ const ZiglingStep = struct {
step: Step, step: Step,
exercise: Exercise, exercise: Exercise,
work_path: []const u8, work_path: []const u8,
mode: Mode,
is_testing: bool = false, is_testing: bool = false,
result_messages: []const u8 = "", result_messages: []const u8 = "",
result_error_bundle: std.zig.ErrorBundle = std.zig.ErrorBundle.empty, result_error_bundle: std.zig.ErrorBundle = std.zig.ErrorBundle.empty,
pub fn create(b: *Build, exercise: Exercise, work_path: []const u8) *ZiglingStep { pub fn create(
b: *Build,
exercise: Exercise,
work_path: []const u8,
mode: Mode,
) *ZiglingStep {
const self = b.allocator.create(ZiglingStep) catch @panic("OOM"); const self = b.allocator.create(ZiglingStep) catch @panic("OOM");
self.* = .{ self.* = .{
.step = Step.init(.{ .step = Step.init(.{
@ -193,6 +207,7 @@ const ZiglingStep = struct {
}), }),
.exercise = exercise, .exercise = exercise,
.work_path = work_path, .work_path = work_path,
.mode = mode,
}; };
return self; return self;
} }
@ -533,23 +548,18 @@ const ZiglingStep = struct {
} }
fn help(self: *ZiglingStep) void { fn help(self: *ZiglingStep) void {
const b = self.step.owner;
const key = self.exercise.key();
const path = self.exercise.main_file; const path = self.exercise.main_file;
print("\n{s}Edit exercises/{s} and run 'zig build' again.{s}\n", .{ const cmd = switch (self.mode) {
red_text, path, reset_text, .normal => "zig build",
}); .named => b.fmt("zig build -Dn={s}", .{key}),
};
// NOTE: The README explains this "advanced feature" if anyone wishes to use print("\n{s}Edit exercises/{s} and run '{s}' again.{s}\n", .{
// it. Otherwise, beginners are thinking they *have* to do this. red_text, path, cmd, reset_text,
//const key = self.exercise.key(); });
//const format =
// \\
// \\{s}To compile only this exercise, you can also use this command:{s}
// \\{s}zig build -Dn={s}{s}
// \\
// \\
//;
//print(format, .{ red_text, reset_text, bold_text, key, reset_text });
} }
fn printErrors(self: *ZiglingStep) void { fn printErrors(self: *ZiglingStep) void {