Skip to content

Commit a8605e7

Browse files
committed
Fix the missing examples dir bug
1 parent 1111802 commit a8605e7

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,8 @@ pub fn build(b: *std.Build) void {
6565
// 1. Get the dependency object from the builder
6666
const chilli_dep = b.dependency("chilli", .{});
6767
68-
// 2. Create a module for the dependency directly from its source file
69-
const chilli_module = b.createModule(.{
70-
.root_source_file = chilli_dep.path("src/lib.zig"),
71-
.target = target,
72-
.optimize = optimize,
73-
});
68+
// 2. Create a module for the dependency
69+
const chilli_module = chilli_dep.module("chilli");
7470
7571
// 3. Create your executable module and add chilli as import
7672
const exe_module = b.createModule(.{

build.zig

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,40 @@ pub fn build(b: *std.Build) void {
6262

6363
// --- Example Setup ---
6464
const examples_path = "examples";
65-
var examples_dir = fs.cwd().openDir(examples_path, .{ .iterate = true }) catch @panic("Can't open 'examples' directory");
66-
defer examples_dir.close();
67-
68-
var dir_iter = examples_dir.iterate();
69-
while (dir_iter.next() catch @panic("Failed to iterate examples")) |entry| {
70-
if (!std.mem.endsWith(u8, entry.name, ".zig")) continue;
71-
72-
const exe_name = fs.path.stem(entry.name);
73-
const exe_path = b.fmt("{s}/{s}", .{ examples_path, entry.name });
74-
75-
const exe_module = b.createModule(.{
76-
.root_source_file = b.path(exe_path),
77-
.target = target,
78-
.optimize = optimize,
79-
});
80-
exe_module.addImport("chilli", lib_module);
81-
82-
const exe = b.addExecutable(.{
83-
.name = exe_name,
84-
.root_module = exe_module,
85-
});
86-
b.installArtifact(exe);
87-
88-
const run_cmd = b.addRunArtifact(exe);
89-
const run_step_name = b.fmt("run-{s}", .{exe_name});
90-
const run_step_desc = b.fmt("Run the {s} example", .{exe_name});
91-
const run_step = b.step(run_step_name, run_step_desc);
92-
run_step.dependOn(&run_cmd.step);
65+
examples_blk: {
66+
// If the examples directory isn't present (common when used as a dependency),
67+
// skip setting up example artifacts instead of panicking.
68+
var examples_dir = fs.cwd().openDir(examples_path, .{ .iterate = true }) catch |err| {
69+
if (err == error.FileNotFound or err == error.NotDir) break :examples_blk;
70+
@panic("Can't open 'examples' directory");
71+
};
72+
defer examples_dir.close();
73+
74+
var dir_iter = examples_dir.iterate();
75+
while (dir_iter.next() catch @panic("Failed to iterate examples")) |entry| {
76+
if (!std.mem.endsWith(u8, entry.name, ".zig")) continue;
77+
78+
const exe_name = fs.path.stem(entry.name);
79+
const exe_path = b.fmt("{s}/{s}", .{ examples_path, entry.name });
80+
81+
const exe_module = b.createModule(.{
82+
.root_source_file = b.path(exe_path),
83+
.target = target,
84+
.optimize = optimize,
85+
});
86+
exe_module.addImport("chilli", lib_module);
87+
88+
const exe = b.addExecutable(.{
89+
.name = exe_name,
90+
.root_module = exe_module,
91+
});
92+
b.installArtifact(exe);
93+
94+
const run_cmd = b.addRunArtifact(exe);
95+
const run_step_name = b.fmt("run-{s}", .{exe_name});
96+
const run_step_desc = b.fmt("Run the {s} example", .{exe_name});
97+
const run_step = b.step(run_step_name, run_step_desc);
98+
run_step.dependOn(&run_cmd.step);
99+
}
93100
}
94101
}

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.{
22
.name = .chilli,
3-
.version = "0.2.0",
3+
.version = "0.2.1",
44
.fingerprint = 0x6c259741ae4f5f73, // Changing this has security and trust implications.
55
.minimum_zig_version = "0.15.1",
66
.paths = .{

0 commit comments

Comments
 (0)