refactor: extract modules from bot.zig
This commit creates a bunch of new modules that contain code and tests for various concepts/implementations that used to exist inside bot.zig. Notable amongst these are: - buffer.zig, which contain the circular buffer containing both backlog and outbox messages. - parser.zig, which contain the parser used to parse commands from IRC messages.
This commit is contained in:
parent
508e084ddf
commit
e1e1938359
7 changed files with 694 additions and 285 deletions
116
build.zig
116
build.zig
|
|
@ -1,12 +1,16 @@
|
|||
const std = @import("std");
|
||||
|
||||
const Import = std.Build.Module.Import;
|
||||
const ResolvedTarget = std.Build.ResolvedTarget;
|
||||
const Run = std.Build.Step.Run;
|
||||
|
||||
// Although this function looks imperative, it does not perform the build
|
||||
// directly and instead it mutates the build graph (`b`) that will be then
|
||||
// executed by an external runner. The functions in `std.Build` implement a DSL
|
||||
// for defining build steps and express dependencies between them, allowing the
|
||||
// build runner to parallelize the build automatically (and the cache system to
|
||||
// know when a step doesn't need to be re-run).
|
||||
pub fn build(b: *std.Build) void {
|
||||
pub fn build(b: *std.Build) !void {
|
||||
// Standard target options allow the person running `zig build` to choose
|
||||
// what target to build for. Here we do not override the defaults, which
|
||||
// means any target is allowed, and the default is native. Other options
|
||||
|
|
@ -106,7 +110,7 @@ pub fn build(b: *std.Build) void {
|
|||
// This will evaluate the `run` step rather than the default step.
|
||||
// For a top level step to actually do something, it must depend on other
|
||||
// steps (e.g. a Run step, as we will see in a moment).
|
||||
const run_step = b.step("run", "Run the app");
|
||||
const runStep = b.step("run", "Run the app");
|
||||
|
||||
// This creates a RunArtifact step in the build graph. A RunArtifact step
|
||||
// invokes an executable compiled by Zig. Steps will only be executed by the
|
||||
|
|
@ -114,61 +118,56 @@ pub fn build(b: *std.Build) void {
|
|||
// or if another step depends on it, so it's up to you to define when and
|
||||
// how this Run step will be executed. In our case we want to run it when
|
||||
// the user runs `zig build run`, so we create a dependency link.
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
const runCmd = b.addRunArtifact(exe);
|
||||
runStep.dependOn(&runCmd.step);
|
||||
|
||||
// By making the run step depend on the default step, it will be run from the
|
||||
// installation directory rather than directly from within the cache directory.
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
runCmd.step.dependOn(b.getInstallStep());
|
||||
|
||||
// This allows the user to pass arguments to the application in the build
|
||||
// command itself, like this: `zig build run -- arg1 arg2 etc`
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
runCmd.addArgs(args);
|
||||
}
|
||||
|
||||
// Creates an executable that will run `test` blocks from the provided module.
|
||||
// Here `mod` needs to define a target, which is why earlier we made sure to
|
||||
// set the releative field.
|
||||
const mod_tests = b.addTest(.{
|
||||
.root_module = mod,
|
||||
});
|
||||
|
||||
// A run step that will run the test executable.
|
||||
const run_mod_tests = b.addRunArtifact(mod_tests);
|
||||
|
||||
const bot_tests = b.addTest(.{
|
||||
.root_module = b.addModule("bot", .{
|
||||
.target = target,
|
||||
.root_source_file = b.path("src/bot.zig"),
|
||||
.imports = &.{
|
||||
.{
|
||||
.name = "zircon",
|
||||
.module = zircon.module("zircon"),
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const run_bot_tests = b.addRunArtifact(bot_tests);
|
||||
|
||||
// Creates an executable that will run `test` blocks from the executable's
|
||||
// root module. Note that test executables only test one module at a time,
|
||||
// hence why we have to create two separate ones.
|
||||
const exe_tests = b.addTest(.{
|
||||
.root_module = exe.root_module,
|
||||
});
|
||||
|
||||
// A run step that will run the second test executable.
|
||||
const run_exe_tests = b.addRunArtifact(exe_tests);
|
||||
|
||||
// A top level step for running all tests. dependOn can be called multiple
|
||||
// times and since the two run steps do not depend on one another, this will
|
||||
// make the two of them run in parallel.
|
||||
const test_step = b.step("test", "Run tests");
|
||||
test_step.dependOn(&run_mod_tests.step);
|
||||
test_step.dependOn(&run_exe_tests.step);
|
||||
test_step.dependOn(&run_bot_tests.step);
|
||||
const testStep = b.step("test", "Run tests");
|
||||
|
||||
const testRunArtifacts: [6]*Run = .{
|
||||
// Creates an executable that will run `test` blocks from the provided module.
|
||||
// Here `mod` needs to define a target, which is why earlier we made sure to
|
||||
// set the releative field.
|
||||
b.addRunArtifact(b.addTest(.{
|
||||
.root_module = mod,
|
||||
})),
|
||||
|
||||
// Creates an executable that will run `test` blocks from the executable's
|
||||
// root module. Note that test executables only test one module at a time,
|
||||
// hence why we have to create two separate ones.
|
||||
b.addRunArtifact(b.addTest(.{
|
||||
.root_module = exe.root_module,
|
||||
})),
|
||||
|
||||
// Our bot tests needs zircon module import,
|
||||
try testRunWithImports(b, target, "bot", &.{
|
||||
.{
|
||||
.name = "zircon",
|
||||
.module = zircon.module("zircon"),
|
||||
},
|
||||
}),
|
||||
// Our module tests for each module we want to add. If
|
||||
// breaking out new functionality to a module, remember to
|
||||
// bump the length of the array above.
|
||||
try testRun(b, target, "buffer"),
|
||||
try testRun(b, target, "parser"),
|
||||
try testRun(b, target, "commands"),
|
||||
};
|
||||
for (testRunArtifacts) |test_run_artifact| {
|
||||
testStep.dependOn(&test_run_artifact.step);
|
||||
}
|
||||
|
||||
// Just like flags, top level steps are also listed in the `--help` menu.
|
||||
//
|
||||
|
|
@ -182,3 +181,30 @@ pub fn build(b: *std.Build) void {
|
|||
// Lastly, the Zig build system is relatively simple and self-contained,
|
||||
// and reading its source code will allow you to master it.
|
||||
}
|
||||
|
||||
// Creates a Run reference that can be depended on when creating a
|
||||
// test step.
|
||||
//
|
||||
// Assumes there exists a file called "src/$name.zig" that contains
|
||||
// tests.
|
||||
fn testRun(b: *std.Build, target: ?ResolvedTarget, name: []const u8) error{OutOfMemory}!*Run {
|
||||
return testRunWithImports(b, target, name, &.{});
|
||||
}
|
||||
|
||||
// Creates a Run reference that can be depended on when creating a
|
||||
// test step, for modules with external imports.
|
||||
//
|
||||
// Assumes there exists a file called "src/$name.zig" that contains
|
||||
// tests.
|
||||
fn testRunWithImports(b: *std.Build, target: ?ResolvedTarget, name: []const u8, imports: []const Import) error{OutOfMemory}!*Run {
|
||||
return b.addRunArtifact(b.addTest(.{
|
||||
.root_module = b.addModule(
|
||||
name,
|
||||
.{
|
||||
.target = target,
|
||||
.root_source_file = b.path(try std.mem.concat(b.allocator, u8, &.{ "src/", name, ".zig" })),
|
||||
.imports = imports,
|
||||
},
|
||||
),
|
||||
}));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue