Skip to content

refactor(codegen): extract lifecycle/loop.zig from main_zig.zig (#183 step 8a) - #204

Merged
apotema merged 1 commit into
feat/183-split-main-zigfrom
refactor/183-extract-lifecycle-loop
May 25, 2026
Merged

apotema merged 1 commit into
feat/183-split-main-zigfrom
refactor/183-extract-lifecycle-loop

Conversation

@apotema

@apotema apotema commented May 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Step 8a of the #183 refactor plan. Moves buildSetupCode (raylib/sdl/bgfx/wgpu loop-backend setup) and buildGuiDrawCode out of src/main_zig.zig into a dedicated src/codegen/lifecycle/loop.zig (235 lines). Re-exported from main_zig.zig so the orchestrator's buildSetupCode(...) / buildGuiDrawCode(...) call sites stay unchanged.

HIGH-risk extraction per the plan -- preserves call order across writeImageBackendWiring, writeAudioBackendWiring, writeFontBackendWiring, and emitResourceLoad. Verified bit-identical across all 8 working bundled examples (raylib, sokol, null, plugin-controllers, flows-smoke, asset-streaming-smoke, bgfx, wgpu). sokol_imgui still soft-fails because the labelle-imgui sibling repo is absent locally, same as on origin/main.

  • main_zig.zig: 3817 -> 3628 lines (-189)
  • src/codegen/lifecycle/loop.zig: 235 lines

Helpers reached back via main_zig.zig

On this branch the helpers consumed by buildSetupCode (writeImageBackendWiring, writeAudioBackendWiring, writeFontBackendWiring, emitResourceLoad, LoadStyle) are still inline in main_zig.zig -- they're pubified here so the new module can reach them. When the parallel blocks/asset_wiring.zig (#197) + blocks/resource_loader.zig (#199) cuts land, lifecycle/loop.zig's imports flip to those new modules and the pub markers in main_zig.zig retire alongside the moved bodies.

Test plan

  • zig build -- clean
  • zig build test -- 506/510 passed (4 skipped), unchanged from baseline
  • scripts/gen_all_examples.sh before/after -- every generated main.zig byte-identical (only flow_catalog.json's generated_at timestamp differs, as documented in the plan)
  • No --force push, no logic edits, pure cut + import

Builds on: #202 (umbrella branch feat/183-split-main-zig).
Refs: #183.

Co-authored-by: Claude Opus 4.7 (1M context) noreply@anthropic.com

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request extracts the loop-lifecycle code builders (buildSetupCode and buildGuiDrawCode) from src/main_zig.zig into a new dedicated module src/codegen/lifecycle/loop.zig as part of the #183 refactoring plan. To maintain backward compatibility and preserve existing call sites, these functions are re-exported from src/main_zig.zig. Additionally, several internal helpers in src/main_zig.zig (such as writeImageBackendWiring, LoadStyle, and emitResourceLoad) have been made public so they can be accessed by the new module. I have no feedback to provide as there are no review comments and the refactoring is clean and well-documented.

@apotema
apotema marked this pull request as ready for review May 25, 2026 20:25
@cursor

cursor Bot commented May 25, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches ordering-sensitive generated startup/GUI code for all loop backends, but the change is a verified byte-identical cut with no behavioral edits.

Overview
Step 8a of the #183 main_zig.zig split moves loop-backend lifecycle codegen into src/codegen/lifecycle/loop.zig: buildSetupCode (raylib/sdl/bgfx/wgpu setup: GUI init, asset wiring, runner, embedded resources/prefabs/scenes, plugins) and buildGuiDrawCode (GUI frame draw block). The bodies are a pure move—no logic edits—with the same helper call order as before.

main_zig.zig drops the inline implementations (~189 lines), imports lifecycle_loop, and re-exports both builders so generateMainZigFromTemplate still calls them unchanged. loop.zig reaches write*BackendWiring, emitResourceLoad, and pathToIdent via main_zig / scan (same pattern as callback.zig for sokol/wasm).

Reviewed by Cursor Bugbot for commit 7ac29fe. Bugbot is set up for automated code reviews on this repo. Configure here.

… of #183)

Per the plan in #193. Moved buildSetupCode (raylib/sdl/bgfx/wgpu
loop-backend setup) + buildGuiDrawCode to
src/codegen/lifecycle/loop.zig (235 lines). Re-exported from
main_zig.zig so the orchestrator's references stay unchanged.

HIGH-risk extraction per the plan -- preserves call order across
writeXxxWiring helpers + emitResourceLoad. Verified bit-identical
across all 8 working bundled examples (scripts/gen_all_examples.sh);
sokol_imgui still soft-fails because the labelle-imgui sibling repo
is absent, same as on origin/main.

On this branch the helpers reached by buildSetupCode
(writeImageBackendWiring, writeAudioBackendWiring,
writeFontBackendWiring, emitResourceLoad, LoadStyle) are still inline
in main_zig.zig -- pubified here so the new module can reach them.
When the parallel blocks/asset_wiring + blocks/resource_loader cuts
(PRs #197, #199) land, lifecycle/loop.zig's imports flip to the new
modules and the pub markers in main_zig.zig retire alongside the
moved bodies.

main_zig.zig shrank 3817 -> 3628 lines (-189).
zig build test: 506/510 passed (4 skipped) -- unchanged from baseline.

Refs: #183
Builds on: #202 (umbrella)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@apotema
apotema force-pushed the refactor/183-extract-lifecycle-loop branch from 3226c9d to 7ac29fe Compare May 25, 2026 20:35
@apotema
apotema merged commit 6b4a5c0 into feat/183-split-main-zig May 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant