mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-22 14:03:10 -05:00
build: improve coding style in ZiglingStep
- Use an anonymous struct when initializing std.Build.Step. - Rename the builder parameter in the create method to b - Avoid lines too long Additionally: - In the run method, rename output to raw_output in order to make the next variable names shorter. - In the compile method, rename zig_file to path.
This commit is contained in:
parent
771b499cbc
commit
a5d93c0b20
1 changed files with 45 additions and 26 deletions
71
build.zig
71
build.zig
|
@ -228,10 +228,15 @@ const ZiglingStep = struct {
|
||||||
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(builder: *Build, exercise: Exercise, work_path: []const u8) *ZiglingStep {
|
pub fn create(b: *Build, exercise: Exercise, work_path: []const u8) *ZiglingStep {
|
||||||
const self = builder.allocator.create(ZiglingStep) catch @panic("OOM");
|
const self = b.allocator.create(ZiglingStep) catch @panic("OOM");
|
||||||
self.* = .{
|
self.* = .{
|
||||||
.step = Step.init(Step.Options{ .id = .custom, .name = exercise.main_file, .owner = builder, .makeFn = make }),
|
.step = Step.init(.{
|
||||||
|
.id = .custom,
|
||||||
|
.name = exercise.main_file,
|
||||||
|
.owner = b,
|
||||||
|
.makeFn = make,
|
||||||
|
}),
|
||||||
.exercise = exercise,
|
.exercise = exercise,
|
||||||
.work_path = work_path,
|
.work_path = work_path,
|
||||||
};
|
};
|
||||||
|
@ -249,7 +254,9 @@ const ZiglingStep = struct {
|
||||||
|
|
||||||
const exe_path = self.compile(prog_node) catch {
|
const exe_path = self.compile(prog_node) catch {
|
||||||
if (self.exercise.hint.len > 0) {
|
if (self.exercise.hint.len > 0) {
|
||||||
print("\n{s}HINT: {s}{s}", .{ bold_text, self.exercise.hint, reset_text });
|
print("\n{s}HINT: {s}{s}", .{
|
||||||
|
bold_text, self.exercise.hint, reset_text,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
self.help();
|
self.help();
|
||||||
|
@ -258,7 +265,9 @@ const ZiglingStep = struct {
|
||||||
|
|
||||||
self.run(exe_path, prog_node) catch {
|
self.run(exe_path, prog_node) catch {
|
||||||
if (self.exercise.hint.len > 0) {
|
if (self.exercise.hint.len > 0) {
|
||||||
print("\n{s}HINT: {s}{s}", .{ bold_text, self.exercise.hint, reset_text });
|
print("\n{s}HINT: {s}{s}", .{
|
||||||
|
bold_text, self.exercise.hint, reset_text,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
self.help();
|
self.help();
|
||||||
|
@ -288,7 +297,7 @@ const ZiglingStep = struct {
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
|
|
||||||
const output = if (self.exercise.check_stdout)
|
const raw_output = if (self.exercise.check_stdout)
|
||||||
result.stdout
|
result.stdout
|
||||||
else
|
else
|
||||||
result.stderr;
|
result.stderr;
|
||||||
|
@ -297,20 +306,27 @@ const ZiglingStep = struct {
|
||||||
switch (result.term) {
|
switch (result.term) {
|
||||||
.Exited => |code| {
|
.Exited => |code| {
|
||||||
if (code != 0) {
|
if (code != 0) {
|
||||||
print("{s}{s} exited with error code {d} (expected {d}){s}\n", .{ red_text, self.exercise.main_file, code, 0, reset_text });
|
print("{s}{s} exited with error code {d} (expected {d}){s}\n", .{
|
||||||
|
red_text, self.exercise.main_file, code, 0, reset_text,
|
||||||
|
});
|
||||||
return error.BadExitCode;
|
return error.BadExitCode;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
print("{s}{s} terminated unexpectedly{s}\n", .{ red_text, self.exercise.main_file, reset_text });
|
print("{s}{s} terminated unexpectedly{s}\n", .{
|
||||||
|
red_text, self.exercise.main_file, reset_text,
|
||||||
|
});
|
||||||
return error.UnexpectedTermination;
|
return error.UnexpectedTermination;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the output.
|
// Validate the output.
|
||||||
const trimOutput = std.mem.trimRight(u8, output, " \r\n");
|
const output = std.mem.trimRight(u8, raw_output, " \r\n");
|
||||||
const trimExerciseOutput = std.mem.trimRight(u8, self.exercise.output, " \r\n");
|
const exercise_output = std.mem.trimRight(u8, self.exercise.output, " \r\n");
|
||||||
if (!std.mem.eql(u8, trimOutput, trimExerciseOutput)) {
|
if (!std.mem.eql(u8, output, exercise_output)) {
|
||||||
|
const red = red_text;
|
||||||
|
const reset = reset_text;
|
||||||
|
|
||||||
print(
|
print(
|
||||||
\\
|
\\
|
||||||
\\{s}========= expected this output: =========={s}
|
\\{s}========= expected this output: =========={s}
|
||||||
|
@ -319,17 +335,20 @@ const ZiglingStep = struct {
|
||||||
\\{s}
|
\\{s}
|
||||||
\\{s}=========================================={s}
|
\\{s}=========================================={s}
|
||||||
\\
|
\\
|
||||||
, .{ red_text, reset_text, trimExerciseOutput, red_text, reset_text, trimOutput, red_text, reset_text });
|
, .{ red, reset, exercise_output, red, reset, output, red, reset });
|
||||||
return error.InvalidOutput;
|
return error.InvalidOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
print("{s}PASSED:\n{s}{s}\n\n", .{ green_text, trimOutput, reset_text });
|
print("{s}PASSED:\n{s}{s}\n\n", .{ green_text, output, reset_text });
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile(self: *ZiglingStep, prog_node: *std.Progress.Node) ![]const u8 {
|
fn compile(self: *ZiglingStep, prog_node: *std.Progress.Node) ![]const u8 {
|
||||||
print("Compiling {s}...\n", .{self.exercise.main_file});
|
print("Compiling {s}...\n", .{self.exercise.main_file});
|
||||||
|
|
||||||
const b = self.step.owner;
|
const b = self.step.owner;
|
||||||
|
const exercise_path = self.exercise.main_file;
|
||||||
|
const path = join(b.allocator, &.{ self.work_path, exercise_path }) catch
|
||||||
|
@panic("OOM");
|
||||||
|
|
||||||
var zig_args = std.ArrayList([]const u8).init(b.allocator);
|
var zig_args = std.ArrayList([]const u8).init(b.allocator);
|
||||||
defer zig_args.deinit();
|
defer zig_args.deinit();
|
||||||
|
@ -337,13 +356,12 @@ const ZiglingStep = struct {
|
||||||
zig_args.append(b.zig_exe) catch @panic("OOM");
|
zig_args.append(b.zig_exe) catch @panic("OOM");
|
||||||
zig_args.append("build-exe") catch @panic("OOM");
|
zig_args.append("build-exe") catch @panic("OOM");
|
||||||
|
|
||||||
// Enable C support for exercises that use C functions
|
// Enable C support for exercises that use C functions.
|
||||||
if (self.exercise.link_libc) {
|
if (self.exercise.link_libc) {
|
||||||
zig_args.append("-lc") catch @panic("OOM");
|
zig_args.append("-lc") catch @panic("OOM");
|
||||||
}
|
}
|
||||||
|
|
||||||
const zig_file = join(b.allocator, &.{ self.work_path, self.exercise.main_file }) catch @panic("OOM");
|
zig_args.append(b.pathFromRoot(path)) catch @panic("OOM");
|
||||||
zig_args.append(b.pathFromRoot(zig_file)) catch @panic("OOM");
|
|
||||||
|
|
||||||
zig_args.append("--cache-dir") catch @panic("OOM");
|
zig_args.append("--cache-dir") catch @panic("OOM");
|
||||||
zig_args.append(b.pathFromRoot(b.cache_root.path.?)) catch @panic("OOM");
|
zig_args.append(b.pathFromRoot(b.cache_root.path.?)) catch @panic("OOM");
|
||||||
|
@ -357,35 +375,36 @@ const ZiglingStep = struct {
|
||||||
|
|
||||||
switch (err) {
|
switch (err) {
|
||||||
error.FileNotFound => {
|
error.FileNotFound => {
|
||||||
print("{s}{s}: Unable to spawn the following command: file not found{s}\n", .{ red_text, self.exercise.main_file, reset_text });
|
print("{s}{s}: Unable to spawn the following command: file not found{s}\n", .{
|
||||||
|
red_text, self.exercise.main_file, reset_text,
|
||||||
|
});
|
||||||
for (argv) |v| print("{s} ", .{v});
|
for (argv) |v| print("{s} ", .{v});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
},
|
},
|
||||||
error.ExitCodeFailure => {
|
error.ExitCodeFailure => {
|
||||||
print("{s}{s}: The following command exited with error code {}:{s}\n", .{ red_text, self.exercise.main_file, code, reset_text });
|
print("{s}{s}: The following command exited with error code {}:{s}\n", .{
|
||||||
|
red_text, self.exercise.main_file, code, reset_text,
|
||||||
|
});
|
||||||
for (argv) |v| print("{s} ", .{v});
|
for (argv) |v| print("{s} ", .{v});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
},
|
},
|
||||||
error.ProcessTerminated => {
|
error.ProcessTerminated => {
|
||||||
print("{s}{s}: The following command terminated unexpectedly:{s}\n", .{ red_text, self.exercise.main_file, reset_text });
|
print("{s}{s}: The following command terminated unexpectedly:{s}\n", .{
|
||||||
|
red_text, self.exercise.main_file, reset_text,
|
||||||
|
});
|
||||||
for (argv) |v| print("{s} ", .{v});
|
for (argv) |v| print("{s} ", .{v});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
},
|
},
|
||||||
error.ZigIPCError => {
|
error.ZigIPCError => {
|
||||||
print("{s}{s}: The following command failed to communicate the compilation result:{s}\n", .{
|
print("{s}{s}: The following command failed to communicate the compilation result:{s}\n", .{
|
||||||
red_text,
|
red_text, self.exercise.main_file, reset_text,
|
||||||
self.exercise.main_file,
|
|
||||||
reset_text,
|
|
||||||
});
|
});
|
||||||
for (argv) |v| print("{s} ", .{v});
|
for (argv) |v| print("{s} ", .{v});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
print("{s}{s}: Unexpected error: {s}{s}\n", .{
|
print("{s}{s}: Unexpected error: {s}{s}\n", .{
|
||||||
red_text,
|
red_text, self.exercise.main_file, @errorName(err), reset_text,
|
||||||
self.exercise.main_file,
|
|
||||||
@errorName(err),
|
|
||||||
reset_text,
|
|
||||||
});
|
});
|
||||||
for (argv) |v| print("{s} ", .{v});
|
for (argv) |v| print("{s} ", .{v});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
|
|
Loading…
Reference in a new issue