From 266fc3b354c8e63ecf6db6d92c837a89d486daeb Mon Sep 17 00:00:00 2001 From: Christian Brendlin Date: Thu, 6 Feb 2025 12:10:55 +0100 Subject: [PATCH 1/3] Handle every example seperatly --- build.zig | 70 ++++++++++++++++++++++++------------------------------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/build.zig b/build.zig index 7d31566..ab67827 100644 --- a/build.zig +++ b/build.zig @@ -58,51 +58,41 @@ pub fn build(b: *std.Build) void { } // build.zig section for examples - - // Create an examples step - const run_examples = b.step("examples", "Run all examples"); - - // Add examples from examples directory - var examples_dir = std.fs.cwd().openDir("examples", .{ .iterate = true }) catch |err| { - if (err == error.FileNotFound) return; - unreachable; + const examples = [_]struct { + file: []const u8, + name: []const u8, + libc: bool = false, + }{ + .{ .file = "examples/basic_usage.zig", .name = "example_1" }, + .{ .file = "examples/custom_handler.zig", .name = "example_2" }, + .{ .file = "examples/file_rotation.zig", .name = "example_3" }, + .{ .file = "examples/json_logging.zig", .name = "example_4" }, }; - var examples_it = examples_dir.iterate(); - while (examples_it.next() catch unreachable) |entry| { - if (entry.kind == .file) { - const extension = std.fs.path.extension(entry.name); - if (std.mem.eql(u8, extension, ".zig")) { - // Check if file has content - const example_file = examples_dir.openFile(entry.name, .{}) catch continue; - defer example_file.close(); - - const file_size = example_file.getEndPos() catch continue; - if (file_size == 0) continue; // Skip empty files - - const example_path = b.fmt("examples/{s}", .{entry.name}); - const example_name = std.fs.path.stem(entry.name); - - // Create executable for this example - const example_exe = b.addExecutable(.{ - .name = example_name, - .root_source_file = b.path(example_path), - .target = target, - .optimize = optimize, - }); - example_exe.root_module.addImport("nexlog", nexlog_module); + { + for (examples) |example| { + const exe = b.addExecutable(.{ + .name = example.name, + .target = target, + .optimize = optimize, + .root_source_file = b.path(example.file), + }); + exe.root_module.addImport("nexlog", nexlog_module); + if (example.libc) { + exe.linkLibC(); + } + b.installArtifact(exe); - // Install the example binary - b.installArtifact(example_exe); + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } - // Create run step for this example - const run_cmd = b.addRunArtifact(example_exe); - const run_step = b.step(b.fmt("run-{s}", .{example_name}), b.fmt("Run the {s} example", .{example_name})); - run_step.dependOn(&run_cmd.step); + const run_step = b.step(example.name, example.file); + run_step.dependOn(&run_cmd.step); - // Add to main examples step - run_examples.dependOn(&run_cmd.step); - } + test_step.dependOn(&run_cmd.step); } } } From cdd7a8345c779ecd3a49f0ddaaf9824db991846f Mon Sep 17 00:00:00 2001 From: Christian Brendlin Date: Thu, 6 Feb 2025 12:59:21 +0100 Subject: [PATCH 2/3] fix: CI error due to missing run step (added step all-examples to build and run ALL examples (for CI)) --- build.zig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build.zig b/build.zig index ab67827..0479180 100644 --- a/build.zig +++ b/build.zig @@ -69,6 +69,8 @@ pub fn build(b: *std.Build) void { .{ .file = "examples/json_logging.zig", .name = "example_4" }, }; + const all_examples_step = b.step("all-examples", "Run all examples (for CI)"); + { for (examples) |example| { const exe = b.addExecutable(.{ @@ -93,6 +95,7 @@ pub fn build(b: *std.Build) void { run_step.dependOn(&run_cmd.step); test_step.dependOn(&run_cmd.step); + all_examples_step.dependOn(&run_cmd.step); } } } From bf2e69348882d05162dc49ef146b2b77f88b89ef Mon Sep 17 00:00:00 2001 From: chrischtel <92454917+chrischtel@users.noreply.github.com> Date: Thu, 6 Feb 2025 13:00:57 +0100 Subject: [PATCH 3/3] Update blank.yml --- .github/workflows/blank.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 8514e09..49f7be5 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -55,4 +55,4 @@ jobs: - name: Run examples if: success() - run: zig build examples + run: zig build all-examples