aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dunbar <[email protected]>2024-06-25 17:22:57 +1000
committerAndrew Dunbar <[email protected]>2024-06-25 17:22:57 +1000
commit2259a18ece9b277c7006ec37632d41799d75fd9f (patch)
tree081a8986647e8a007bb386a858120a42f05ff4b7
parent6d3d3651d094d42a80ccdf3ffd7f175422f572ac (diff)
downloadziglings-2259a18ece9b277c7006ec37632d41799d75fd9f.tar.xz
ziglings-2259a18ece9b277c7006ec37632d41799d75fd9f.zip
attempt at implementing #113 "Add a way to do one random exercise"
-rw-r--r--build.zig31
1 files changed, 31 insertions, 0 deletions
diff --git a/build.zig b/build.zig
index 0d83737..1e25681 100644
--- a/build.zig
+++ b/build.zig
@@ -103,6 +103,8 @@ const Mode = enum {
normal,
/// Named build mode: `zig build -Dn=n`
named,
+ /// Random build mode: `zig build -Drandom`
+ random,
};
pub const logo =
@@ -158,6 +160,7 @@ pub fn build(b: *Build) !void {
false;
const override_healed_path = b.option([]const u8, "healed-path", "Override healed path");
const exno: ?usize = b.option(usize, "n", "Select exercise");
+ const rand: ?bool = b.option(bool, "random", "Select random exercise");
const sep = std.fs.path.sep_str;
const healed_path = if (override_healed_path) |path|
@@ -191,6 +194,33 @@ pub fn build(b: *Build) !void {
return;
}
+ if (rand) |_| {
+ // Random build mode: verifies one random exercise.
+ // like for 'exno' but chooses a random exersise number.
+ print("work in progress: check a random exercise\n", .{});
+
+ var prng = std.rand.DefaultPrng.init(blk: {
+ var seed: u64 = undefined;
+ try std.posix.getrandom(std.mem.asBytes(&seed));
+ break :blk seed;
+ });
+ const rnd = prng.random();
+ const ex = exercises[rnd.intRangeLessThan(usize, 0, exercises.len)];
+
+ print("random exercise: {s}\n", .{ex.main_file});
+
+ const zigling_step = b.step(
+ "random",
+ b.fmt("Check the solution of {s}", .{ex.main_file}),
+ );
+ b.default_step = zigling_step;
+ zigling_step.dependOn(&header_step.step);
+ const verify_step = ZiglingStep.create(b, ex, work_path, .random);
+ verify_step.step.dependOn(&header_step.step);
+ zigling_step.dependOn(&verify_step.step);
+ return;
+ }
+
// Normal build mode: verifies all exercises according to the recommended
// order.
const ziglings_step = b.step("ziglings", "Check all ziglings");
@@ -417,6 +447,7 @@ const ZiglingStep = struct {
const cmd = switch (self.mode) {
.normal => "zig build",
.named => b.fmt("zig build -Dn={s}", .{key}),
+ .random => "zig build -Drandom",
};
print("\n{s}Edit exercises/{s} and run '{s}' again.{s}\n", .{