Skip to content

Commit ac331ef

Browse files
committed
refactor(build): extract helpers and improve organization
- Extract createObject and createExecutable helper functions - Refactor function signatures to use option structs - Extract buildExample function from buildExamples loop - Improve variable naming for code clarity - Add organizational section comments
1 parent 508e55e commit ac331ef

File tree

1 file changed

+123
-56
lines changed

1 file changed

+123
-56
lines changed

build.zig

Lines changed: 123 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,96 @@ pub fn build(b: *Build) !void {
7474
std.process.exit(1);
7575
};
7676

77-
generateDocs(b, optimize, target, flags_module);
77+
generateDocs(b, .{
78+
.optimize = optimize,
79+
.target = target,
80+
.flags_module = flags_module,
81+
});
7882
}
7983

84+
// ========== Options Structures ==========
85+
8086
const BuildExamplesOptions = struct {
8187
optimize: OptimizeMode,
8288
target: Build.ResolvedTarget,
8389
webui_module: *Module,
8490
compat_module: *Module,
8591
};
8692

87-
fn generateDocs(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget, flags_module: *Module) void {
88-
const webui_lib = b.addObject(if (builtin.zig_version.minor == 14) .{
89-
.name = "webui_lib",
90-
.root_source_file = b.path(b.pathJoin(&.{ "src", "webui.zig" })),
91-
.target = target,
92-
.optimize = optimize,
93-
} else .{
94-
.name = "webui_lib",
95-
.root_module = b.addModule("webui_lib", .{
96-
.root_source_file = b.path(b.pathJoin(&.{ "src", "webui.zig" })),
93+
const GenerateDocsOptions = struct {
94+
optimize: OptimizeMode,
95+
target: Build.ResolvedTarget,
96+
flags_module: *Module,
97+
};
98+
99+
// ========== Helper Functions ==========
100+
101+
/// Create an object artifact with version compatibility
102+
fn createObject(
103+
b: *Build,
104+
name: []const u8,
105+
root_source: Build.LazyPath,
106+
target: Build.ResolvedTarget,
107+
optimize: OptimizeMode,
108+
) *Compile {
109+
if (builtin.zig_version.minor == 14) {
110+
return b.addObject(.{
111+
.name = name,
112+
.root_source_file = root_source,
97113
.target = target,
98114
.optimize = optimize,
99-
}),
100-
});
115+
});
116+
} else {
117+
return b.addObject(.{
118+
.name = name,
119+
.root_module = b.addModule(name, .{
120+
.root_source_file = root_source,
121+
.target = target,
122+
.optimize = optimize,
123+
}),
124+
});
125+
}
126+
}
101127

102-
webui_lib.root_module.addImport("flags", flags_module);
128+
/// Create an executable artifact with version compatibility
129+
fn createExecutable(
130+
b: *Build,
131+
name: []const u8,
132+
root_source: Build.LazyPath,
133+
target: Build.ResolvedTarget,
134+
optimize: OptimizeMode,
135+
) *Compile {
136+
if (builtin.zig_version.minor == 14) {
137+
return b.addExecutable(.{
138+
.name = name,
139+
.root_source_file = root_source,
140+
.target = target,
141+
.optimize = optimize,
142+
});
143+
} else {
144+
return b.addExecutable(.{
145+
.name = name,
146+
.root_module = b.addModule(name, .{
147+
.root_source_file = root_source,
148+
.target = target,
149+
.optimize = optimize,
150+
}),
151+
});
152+
}
153+
}
154+
155+
// ========== Documentation Generation ==========
156+
157+
fn generateDocs(b: *Build, options: GenerateDocsOptions) void {
158+
const webui_lib = createObject(
159+
b,
160+
"webui_lib",
161+
b.path(b.pathJoin(&.{ "src", "webui.zig" })),
162+
options.target,
163+
options.optimize,
164+
);
165+
166+
webui_lib.root_module.addImport("flags", options.flags_module);
103167

104168
const docs_step = b.step("docs", "Generate docs");
105169
const docs_install = b.addInstallDirectory(.{
@@ -111,59 +175,62 @@ fn generateDocs(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget,
111175
docs_step.dependOn(&docs_install.step);
112176
}
113177

178+
// ========== Examples Building ==========
179+
114180
fn buildExamples(b: *Build, options: BuildExamplesOptions) !void {
115-
var lazy_path = b.path("examples");
181+
const lazy_path = b.path("examples");
116182
const build_all_step = b.step("examples", "build all examples");
117183
const examples_path = lazy_path.getPath(b);
118-
var iter_dir = std.fs.openDirAbsolute(
119-
examples_path,
120-
.{ .iterate = true },
121-
) catch |err| {
184+
185+
var examples_dir = std.fs.openDirAbsolute(examples_path, .{ .iterate = true }) catch |err| {
122186
switch (err) {
123187
error.FileNotFound => return,
124188
else => return err,
125189
}
126190
};
127-
defer iter_dir.close();
191+
defer examples_dir.close();
128192

129-
var itera = iter_dir.iterate();
130-
131-
while (try itera.next()) |val| {
132-
if (val.kind != .directory) {
193+
var iter = examples_dir.iterate();
194+
while (try iter.next()) |entry| {
195+
if (entry.kind != .directory) {
133196
continue;
134197
}
135198

136-
const example_name = val.name;
137-
const path = b.pathJoin(&.{ "examples", example_name, "main.zig" });
138-
const exe = b.addExecutable(if (builtin.zig_version.minor == 14) .{
139-
.name = example_name,
140-
.root_source_file = b.path(path),
141-
.target = options.target,
142-
.optimize = options.optimize,
143-
} else .{
144-
.name = example_name,
145-
.root_module = b.addModule(example_name, .{
146-
.root_source_file = b.path(path),
147-
.target = options.target,
148-
.optimize = options.optimize,
149-
}),
150-
});
151-
152-
exe.root_module.addImport("webui", options.webui_module);
153-
exe.root_module.addImport("compat", options.compat_module);
154-
155-
const exe_install = b.addInstallArtifact(exe, .{});
156-
build_all_step.dependOn(&exe_install.step);
157-
158-
const exe_run = b.addRunArtifact(exe);
159-
exe_run.step.dependOn(&exe_install.step);
160-
161-
const cwd = b.path(b.pathJoin(&.{ "examples", example_name }));
162-
exe_run.setCwd(cwd);
163-
const step_name = try std.fmt.allocPrint(b.allocator, "run_{s}", .{example_name});
164-
const step_desc = try std.fmt.allocPrint(b.allocator, "run_{s}", .{example_name});
165-
166-
const exe_run_step = b.step(step_name, step_desc);
167-
exe_run_step.dependOn(&exe_run.step);
199+
try buildExample(b, entry.name, options, build_all_step);
168200
}
169201
}
202+
203+
fn buildExample(
204+
b: *Build,
205+
example_name: []const u8,
206+
options: BuildExamplesOptions,
207+
build_all_step: *Build.Step,
208+
) !void {
209+
const main_path = b.pathJoin(&.{ "examples", example_name, "main.zig" });
210+
const exe = createExecutable(
211+
b,
212+
example_name,
213+
b.path(main_path),
214+
options.target,
215+
options.optimize,
216+
);
217+
218+
exe.root_module.addImport("webui", options.webui_module);
219+
exe.root_module.addImport("compat", options.compat_module);
220+
221+
// Install step
222+
const exe_install = b.addInstallArtifact(exe, .{});
223+
build_all_step.dependOn(&exe_install.step);
224+
225+
// Run step
226+
const exe_run = b.addRunArtifact(exe);
227+
exe_run.step.dependOn(&exe_install.step);
228+
exe_run.setCwd(b.path(b.pathJoin(&.{ "examples", example_name })));
229+
230+
const step_name = try std.fmt.allocPrint(b.allocator, "run_{s}", .{example_name});
231+
const step_desc = try std.fmt.allocPrint(b.allocator, "run {s} example", .{example_name});
232+
233+
const exe_run_step = b.step(step_name, step_desc);
234+
exe_run_step.dependOn(&exe_run.step);
235+
}
236+

0 commit comments

Comments
 (0)