@native-sdk/cli 0.0.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (367) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +50 -1
  3. package/app.zon +30 -0
  4. package/bin/native.js +156 -0
  5. package/build/app.zig +716 -0
  6. package/build.zig +2593 -0
  7. package/build.zig.zon +24 -0
  8. package/native-sdk.d.ts +615 -0
  9. package/package.json +44 -8
  10. package/skill-data/automation/SKILL.md +248 -0
  11. package/skill-data/core/SKILL.md +249 -0
  12. package/skill-data/core/references/app-model-runtime.md +141 -0
  13. package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
  14. package/skill-data/core/references/frontend-assets.md +124 -0
  15. package/skill-data/core/references/project-anatomy.md +108 -0
  16. package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
  17. package/skill-data/native-ui/SKILL.md +1080 -0
  18. package/skills/native-sdk/SKILL.md +33 -0
  19. package/src/app_runner/root.zig +740 -0
  20. package/src/assets/root.zig +20 -0
  21. package/src/automation/protocol.zig +285 -0
  22. package/src/automation/root.zig +13 -0
  23. package/src/automation/server.zig +506 -0
  24. package/src/automation/snapshot.zig +1202 -0
  25. package/src/automation/watcher.zig +165 -0
  26. package/src/bridge/root.zig +507 -0
  27. package/src/debug/root.zig +246 -0
  28. package/src/embed/app_exports.zig +11 -0
  29. package/src/embed/c_api.zig +793 -0
  30. package/src/embed/c_exports.zig +10 -0
  31. package/src/embed/chrome.zig +168 -0
  32. package/src/embed/conversions.zig +359 -0
  33. package/src/embed/host.zig +960 -0
  34. package/src/embed/root.zig +81 -0
  35. package/src/embed/tests.zig +2552 -0
  36. package/src/embed/types.zig +382 -0
  37. package/src/embed/ui_host.zig +289 -0
  38. package/src/extensions/root.zig +197 -0
  39. package/src/frontend/root.zig +48 -0
  40. package/src/js/root.zig +65 -0
  41. package/src/platform/android/NativeSdkActivity.java +1370 -0
  42. package/src/platform/android/android_host.c +696 -0
  43. package/src/platform/android/files.zig +9 -0
  44. package/src/platform/android/native_sdk_app.h +237 -0
  45. package/src/platform/ios/files.zig +8 -0
  46. package/src/platform/ios/native_sdk_app.h +289 -0
  47. package/src/platform/ios/uikit_host.m +2739 -0
  48. package/src/platform/linux/cef_host.cpp +599 -0
  49. package/src/platform/linux/gtk_host.c +5362 -0
  50. package/src/platform/linux/gtk_host.h +265 -0
  51. package/src/platform/linux/root.zig +1519 -0
  52. package/src/platform/macos/appkit_host.h +604 -0
  53. package/src/platform/macos/appkit_host.m +10392 -0
  54. package/src/platform/macos/automation_host.h +14 -0
  55. package/src/platform/macos/automation_host.m +12 -0
  56. package/src/platform/macos/cef_host.mm +3028 -0
  57. package/src/platform/macos/root.zig +2260 -0
  58. package/src/platform/null_platform.zig +2232 -0
  59. package/src/platform/null_platform_tests.zig +670 -0
  60. package/src/platform/policy_values.zig +14 -0
  61. package/src/platform/root.zig +181 -0
  62. package/src/platform/types.zig +2681 -0
  63. package/src/platform/windows/cef_host.cpp +3 -0
  64. package/src/platform/windows/root.zig +1521 -0
  65. package/src/platform/windows/webview2_host.cpp +6230 -0
  66. package/src/primitives/app_dirs/root.zig +440 -0
  67. package/src/primitives/app_manifest/root.zig +109 -0
  68. package/src/primitives/app_manifest/tests.zig +758 -0
  69. package/src/primitives/app_manifest/types.zig +587 -0
  70. package/src/primitives/app_manifest/validation.zig +828 -0
  71. package/src/primitives/assets/root.zig +520 -0
  72. package/src/primitives/canvas/a11y_audit.zig +390 -0
  73. package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
  74. package/src/primitives/canvas/app_icon.zig +1590 -0
  75. package/src/primitives/canvas/chart.zig +392 -0
  76. package/src/primitives/canvas/chart_tests.zig +751 -0
  77. package/src/primitives/canvas/commands.zig +446 -0
  78. package/src/primitives/canvas/drawing.zig +309 -0
  79. package/src/primitives/canvas/equality.zig +316 -0
  80. package/src/primitives/canvas/events.zig +696 -0
  81. package/src/primitives/canvas/font_coverage.zig +118 -0
  82. package/src/primitives/canvas/font_ttf.zig +510 -0
  83. package/src/primitives/canvas/font_ttf_tests.zig +280 -0
  84. package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
  85. package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
  86. package/src/primitives/canvas/frame.zig +679 -0
  87. package/src/primitives/canvas/frame_metrics.zig +462 -0
  88. package/src/primitives/canvas/gpu.zig +639 -0
  89. package/src/primitives/canvas/hash.zig +112 -0
  90. package/src/primitives/canvas/icons/alert.svg +1 -0
  91. package/src/primitives/canvas/icons/archive.svg +1 -0
  92. package/src/primitives/canvas/icons/arrow-down.svg +1 -0
  93. package/src/primitives/canvas/icons/arrow-right.svg +1 -0
  94. package/src/primitives/canvas/icons/arrow-up.svg +1 -0
  95. package/src/primitives/canvas/icons/check-circle.svg +1 -0
  96. package/src/primitives/canvas/icons/check.svg +1 -0
  97. package/src/primitives/canvas/icons/chevron-down.svg +1 -0
  98. package/src/primitives/canvas/icons/chevron-left.svg +1 -0
  99. package/src/primitives/canvas/icons/chevron-right.svg +1 -0
  100. package/src/primitives/canvas/icons/chevron-up.svg +1 -0
  101. package/src/primitives/canvas/icons/circle-dot.svg +1 -0
  102. package/src/primitives/canvas/icons/clock.svg +1 -0
  103. package/src/primitives/canvas/icons/copy.svg +1 -0
  104. package/src/primitives/canvas/icons/download.svg +1 -0
  105. package/src/primitives/canvas/icons/edit.svg +1 -0
  106. package/src/primitives/canvas/icons/ellipsis.svg +1 -0
  107. package/src/primitives/canvas/icons/external-link.svg +1 -0
  108. package/src/primitives/canvas/icons/eye.svg +1 -0
  109. package/src/primitives/canvas/icons/file-text.svg +1 -0
  110. package/src/primitives/canvas/icons/folder-open.svg +1 -0
  111. package/src/primitives/canvas/icons/folder.svg +1 -0
  112. package/src/primitives/canvas/icons/git-branch.svg +1 -0
  113. package/src/primitives/canvas/icons/git-merge.svg +1 -0
  114. package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
  115. package/src/primitives/canvas/icons/info.svg +1 -0
  116. package/src/primitives/canvas/icons/menu.svg +1 -0
  117. package/src/primitives/canvas/icons/missing.svg +1 -0
  118. package/src/primitives/canvas/icons/moon.svg +1 -0
  119. package/src/primitives/canvas/icons/music.svg +1 -0
  120. package/src/primitives/canvas/icons/panel-left.svg +1 -0
  121. package/src/primitives/canvas/icons/panel-right.svg +1 -0
  122. package/src/primitives/canvas/icons/pause.svg +1 -0
  123. package/src/primitives/canvas/icons/play.svg +1 -0
  124. package/src/primitives/canvas/icons/plus.svg +1 -0
  125. package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
  126. package/src/primitives/canvas/icons/repeat.svg +1 -0
  127. package/src/primitives/canvas/icons/save.svg +1 -0
  128. package/src/primitives/canvas/icons/search.svg +1 -0
  129. package/src/primitives/canvas/icons/send.svg +1 -0
  130. package/src/primitives/canvas/icons/settings.svg +1 -0
  131. package/src/primitives/canvas/icons/shuffle.svg +1 -0
  132. package/src/primitives/canvas/icons/skip-back.svg +1 -0
  133. package/src/primitives/canvas/icons/skip-forward.svg +1 -0
  134. package/src/primitives/canvas/icons/sun.svg +1 -0
  135. package/src/primitives/canvas/icons/terminal.svg +1 -0
  136. package/src/primitives/canvas/icons/trash.svg +1 -0
  137. package/src/primitives/canvas/icons/volume.svg +1 -0
  138. package/src/primitives/canvas/icons/wrench.svg +1 -0
  139. package/src/primitives/canvas/icons/x-circle.svg +1 -0
  140. package/src/primitives/canvas/icons/x.svg +1 -0
  141. package/src/primitives/canvas/icons.zig +249 -0
  142. package/src/primitives/canvas/layout_audit.zig +744 -0
  143. package/src/primitives/canvas/layout_audit_tests.zig +426 -0
  144. package/src/primitives/canvas/markdown.zig +1031 -0
  145. package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
  146. package/src/primitives/canvas/markdown_tests.zig +538 -0
  147. package/src/primitives/canvas/plan_key_index.zig +120 -0
  148. package/src/primitives/canvas/png.zig +355 -0
  149. package/src/primitives/canvas/reference.zig +1457 -0
  150. package/src/primitives/canvas/reference_blur.zig +104 -0
  151. package/src/primitives/canvas/reference_memo.zig +322 -0
  152. package/src/primitives/canvas/reference_paths.zig +184 -0
  153. package/src/primitives/canvas/reference_tests.zig +1650 -0
  154. package/src/primitives/canvas/render.zig +747 -0
  155. package/src/primitives/canvas/render_effects.zig +255 -0
  156. package/src/primitives/canvas/render_fingerprints.zig +256 -0
  157. package/src/primitives/canvas/render_generic_resources.zig +376 -0
  158. package/src/primitives/canvas/render_images.zig +247 -0
  159. package/src/primitives/canvas/render_layers.zig +260 -0
  160. package/src/primitives/canvas/render_paths.zig +347 -0
  161. package/src/primitives/canvas/render_resources.zig +51 -0
  162. package/src/primitives/canvas/render_tests.zig +3186 -0
  163. package/src/primitives/canvas/root.zig +744 -0
  164. package/src/primitives/canvas/serialization.zig +1528 -0
  165. package/src/primitives/canvas/svg_icon.zig +636 -0
  166. package/src/primitives/canvas/svg_icon_tests.zig +312 -0
  167. package/src/primitives/canvas/test_support.zig +422 -0
  168. package/src/primitives/canvas/testdata/markdown_document.md +83 -0
  169. package/src/primitives/canvas/tests.zig +30 -0
  170. package/src/primitives/canvas/text.zig +82 -0
  171. package/src/primitives/canvas/text_atlas.zig +408 -0
  172. package/src/primitives/canvas/text_batch_tests.zig +431 -0
  173. package/src/primitives/canvas/text_interaction.zig +615 -0
  174. package/src/primitives/canvas/text_layout.zig +1069 -0
  175. package/src/primitives/canvas/text_layout_cache.zig +281 -0
  176. package/src/primitives/canvas/text_layout_hash.zig +113 -0
  177. package/src/primitives/canvas/text_layout_types.zig +128 -0
  178. package/src/primitives/canvas/text_measure_cache.zig +279 -0
  179. package/src/primitives/canvas/text_metrics.zig +309 -0
  180. package/src/primitives/canvas/text_metrics_tests.zig +262 -0
  181. package/src/primitives/canvas/text_span_tests.zig +608 -0
  182. package/src/primitives/canvas/text_spans.zig +1010 -0
  183. package/src/primitives/canvas/text_tests.zig +1949 -0
  184. package/src/primitives/canvas/themes/geist.zig +470 -0
  185. package/src/primitives/canvas/tokens.zig +1522 -0
  186. package/src/primitives/canvas/ui.zig +2686 -0
  187. package/src/primitives/canvas/ui_markup.zig +3791 -0
  188. package/src/primitives/canvas/ui_markup_binary.zig +522 -0
  189. package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
  190. package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
  191. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
  192. package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
  193. package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
  194. package/src/primitives/canvas/ui_markup_edit.zig +416 -0
  195. package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
  196. package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
  197. package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
  198. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
  199. package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
  200. package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
  201. package/src/primitives/canvas/ui_markup_view.zig +2352 -0
  202. package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
  203. package/src/primitives/canvas/ui_provenance.zig +96 -0
  204. package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
  205. package/src/primitives/canvas/ui_schema.zig +825 -0
  206. package/src/primitives/canvas/ui_schema_tests.zig +128 -0
  207. package/src/primitives/canvas/ui_tests.zig +1432 -0
  208. package/src/primitives/canvas/vector.zig +919 -0
  209. package/src/primitives/canvas/vector_tests.zig +547 -0
  210. package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
  211. package/src/primitives/canvas/virtual_extents.zig +540 -0
  212. package/src/primitives/canvas/widget_access.zig +208 -0
  213. package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
  214. package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
  215. package/src/primitives/canvas/widget_invalidation.zig +748 -0
  216. package/src/primitives/canvas/widget_layout.zig +2055 -0
  217. package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
  218. package/src/primitives/canvas/widget_metrics.zig +228 -0
  219. package/src/primitives/canvas/widget_render.zig +2534 -0
  220. package/src/primitives/canvas/widget_render_controls.zig +1563 -0
  221. package/src/primitives/canvas/widget_render_scroll.zig +136 -0
  222. package/src/primitives/canvas/widget_render_style.zig +626 -0
  223. package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
  224. package/src/primitives/canvas/widget_routing.zig +475 -0
  225. package/src/primitives/canvas/widget_runtime.zig +307 -0
  226. package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
  227. package/src/primitives/canvas/widget_semantics.zig +480 -0
  228. package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
  229. package/src/primitives/canvas/widget_tests.zig +8 -0
  230. package/src/primitives/canvas/widget_text_input.zig +351 -0
  231. package/src/primitives/canvas/widget_text_select.zig +147 -0
  232. package/src/primitives/canvas/widget_tree.zig +232 -0
  233. package/src/primitives/canvas/widgets.zig +1319 -0
  234. package/src/primitives/diagnostics/root.zig +445 -0
  235. package/src/primitives/geometry/root.zig +889 -0
  236. package/src/primitives/json/root.zig +272 -0
  237. package/src/primitives/platform_info/root.zig +450 -0
  238. package/src/primitives/trace/root.zig +569 -0
  239. package/src/root.zig +165 -0
  240. package/src/runtime/api.zig +431 -0
  241. package/src/runtime/async_bridge.zig +49 -0
  242. package/src/runtime/automation_commands.zig +686 -0
  243. package/src/runtime/automation_liveness_tests.zig +140 -0
  244. package/src/runtime/automation_snapshot.zig +246 -0
  245. package/src/runtime/automation_widget_dispatch.zig +617 -0
  246. package/src/runtime/bridge_dispatch_tests.zig +202 -0
  247. package/src/runtime/bridge_payload.zig +194 -0
  248. package/src/runtime/bridge_permission_tests.zig +551 -0
  249. package/src/runtime/bridge_responses.zig +414 -0
  250. package/src/runtime/builtin_bridge.zig +554 -0
  251. package/src/runtime/canvas_budget_edge_tests.zig +415 -0
  252. package/src/runtime/canvas_font_tests.zig +410 -0
  253. package/src/runtime/canvas_fonts.zig +229 -0
  254. package/src/runtime/canvas_frame.zig +1734 -0
  255. package/src/runtime/canvas_frame_animation_tests.zig +609 -0
  256. package/src/runtime/canvas_frame_cache_tests.zig +333 -0
  257. package/src/runtime/canvas_frame_helpers.zig +414 -0
  258. package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
  259. package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
  260. package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
  261. package/src/runtime/canvas_frame_retained_tests.zig +753 -0
  262. package/src/runtime/canvas_frame_tests.zig +9 -0
  263. package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
  264. package/src/runtime/canvas_image_tests.zig +362 -0
  265. package/src/runtime/canvas_images.zig +224 -0
  266. package/src/runtime/canvas_limits.zig +317 -0
  267. package/src/runtime/canvas_screenshot_tests.zig +289 -0
  268. package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
  269. package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
  270. package/src/runtime/canvas_widget_chart_tests.zig +249 -0
  271. package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
  272. package/src/runtime/canvas_widget_context_menu.zig +318 -0
  273. package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
  274. package/src/runtime/canvas_widget_control_tests.zig +2164 -0
  275. package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
  276. package/src/runtime/canvas_widget_display.zig +677 -0
  277. package/src/runtime/canvas_widget_event_tests.zig +1779 -0
  278. package/src/runtime/canvas_widget_events.zig +1098 -0
  279. package/src/runtime/canvas_widget_floating_tests.zig +695 -0
  280. package/src/runtime/canvas_widget_layout_tests.zig +26 -0
  281. package/src/runtime/canvas_widget_runtime.zig +1335 -0
  282. package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
  283. package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
  284. package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
  285. package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
  286. package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
  287. package/src/runtime/canvas_widget_state.zig +798 -0
  288. package/src/runtime/canvas_widget_text_tests.zig +2104 -0
  289. package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
  290. package/src/runtime/clock.zig +231 -0
  291. package/src/runtime/core.zig +927 -0
  292. package/src/runtime/effects.zig +4197 -0
  293. package/src/runtime/effects_audio_tests.zig +690 -0
  294. package/src/runtime/effects_clipboard_tests.zig +317 -0
  295. package/src/runtime/effects_fetch_tests.zig +1031 -0
  296. package/src/runtime/effects_file_tests.zig +407 -0
  297. package/src/runtime/effects_tests.zig +1251 -0
  298. package/src/runtime/flow.zig +1175 -0
  299. package/src/runtime/frame_profile.zig +221 -0
  300. package/src/runtime/gpu_surface_events.zig +639 -0
  301. package/src/runtime/launch_timing.zig +59 -0
  302. package/src/runtime/markdown_app_tests.zig +224 -0
  303. package/src/runtime/native_command_event_tests.zig +431 -0
  304. package/src/runtime/platform_bridge_tests.zig +8 -0
  305. package/src/runtime/platform_config_tests.zig +392 -0
  306. package/src/runtime/root.zig +150 -0
  307. package/src/runtime/runtime_event_tests.zig +1079 -0
  308. package/src/runtime/session_journal.zig +1522 -0
  309. package/src/runtime/session_record.zig +332 -0
  310. package/src/runtime/session_replay.zig +274 -0
  311. package/src/runtime/session_state.zig +30 -0
  312. package/src/runtime/session_tests.zig +399 -0
  313. package/src/runtime/shell_layout.zig +472 -0
  314. package/src/runtime/shell_layout_tests.zig +1246 -0
  315. package/src/runtime/shell_view_tests.zig +604 -0
  316. package/src/runtime/state.zig +110 -0
  317. package/src/runtime/system_services.zig +392 -0
  318. package/src/runtime/test_support.zig +79 -0
  319. package/src/runtime/tests.zig +30 -0
  320. package/src/runtime/ui_app.zig +3290 -0
  321. package/src/runtime/ui_app_huge_model_tests.zig +351 -0
  322. package/src/runtime/ui_app_provenance.zig +370 -0
  323. package/src/runtime/ui_app_tests.zig +3254 -0
  324. package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
  325. package/src/runtime/ui_app_window_tests.zig +442 -0
  326. package/src/runtime/validation.zig +206 -0
  327. package/src/runtime/view.zig +908 -0
  328. package/src/runtime/view_canvas.zig +850 -0
  329. package/src/runtime/view_widget_control.zig +433 -0
  330. package/src/runtime/view_widget_scroll.zig +360 -0
  331. package/src/runtime/view_widget_text.zig +275 -0
  332. package/src/runtime/view_widget_tree.zig +843 -0
  333. package/src/runtime/webview_view_bridge_tests.zig +664 -0
  334. package/src/runtime/widget_bridge.zig +231 -0
  335. package/src/runtime/window_command_bridge_tests.zig +403 -0
  336. package/src/runtime/window_storage.zig +255 -0
  337. package/src/runtime/window_view_runtime.zig +727 -0
  338. package/src/runtime/window_views.zig +334 -0
  339. package/src/security/root.zig +118 -0
  340. package/src/tooling/android.zig +1172 -0
  341. package/src/tooling/assets.zig +128 -0
  342. package/src/tooling/buildgraph.zig +379 -0
  343. package/src/tooling/cef.zig +671 -0
  344. package/src/tooling/codesign.zig +172 -0
  345. package/src/tooling/components/identity_tests.zig +185 -0
  346. package/src/tooling/components/stepper.zig +113 -0
  347. package/src/tooling/components/timeline.native +27 -0
  348. package/src/tooling/components/timeline_item.zig +131 -0
  349. package/src/tooling/default_icon.icns +0 -0
  350. package/src/tooling/default_icon.png +0 -0
  351. package/src/tooling/dev.zig +172 -0
  352. package/src/tooling/doctor.zig +349 -0
  353. package/src/tooling/eject_components.zig +170 -0
  354. package/src/tooling/embedlib.zig +290 -0
  355. package/src/tooling/ios.zig +528 -0
  356. package/src/tooling/manifest.zig +1939 -0
  357. package/src/tooling/package.zig +2388 -0
  358. package/src/tooling/process_tree.zig +114 -0
  359. package/src/tooling/raw_manifest.zig +193 -0
  360. package/src/tooling/root.zig +22 -0
  361. package/src/tooling/templates.zig +3191 -0
  362. package/src/tooling/toolchain.zig +371 -0
  363. package/src/tooling/verbs.zig +204 -0
  364. package/src/tooling/web_engine.zig +135 -0
  365. package/src/tooling/xcodeproj.zig +462 -0
  366. package/src/window_state/root.zig +507 -0
  367. package/index.js +0 -2
@@ -0,0 +1,3191 @@
1
+ const std = @import("std");
2
+
3
+ /// The SDK's default app icon, rendered from vector source by
4
+ /// `zig build generate-icon` (tools/generate_app_icon.zig). Embedded so
5
+ /// `native init` always scaffolds a real icon regardless of the
6
+ /// directory the CLI runs from. The scaffold ships the one-image PNG
7
+ /// contract (`assets/icon.png` in app.zon `.icons`; packaging generates
8
+ /// every platform's artifacts from it); the `.icns` twin stays embedded
9
+ /// for packaging's no-icon fallback (src/tooling/package.zig).
10
+ const default_icon_png = @embedFile("default_icon.png");
11
+
12
+ pub const Frontend = enum {
13
+ next,
14
+ vite,
15
+ react,
16
+ svelte,
17
+ vue,
18
+ /// Native-rendered markup app (.native + Zig): no WebView, no npm frontend.
19
+ native,
20
+
21
+ pub fn parse(value: []const u8) ?Frontend {
22
+ if (std.mem.eql(u8, value, "next")) return .next;
23
+ if (std.mem.eql(u8, value, "vite")) return .vite;
24
+ if (std.mem.eql(u8, value, "react")) return .react;
25
+ if (std.mem.eql(u8, value, "svelte")) return .svelte;
26
+ if (std.mem.eql(u8, value, "vue")) return .vue;
27
+ if (std.mem.eql(u8, value, "native")) return .native;
28
+ return null;
29
+ }
30
+
31
+ pub fn distDir(self: Frontend) []const u8 {
32
+ return switch (self) {
33
+ .next => "frontend/out",
34
+ .vite, .react, .svelte, .vue => "frontend/dist",
35
+ .native => "assets",
36
+ };
37
+ }
38
+
39
+ pub fn devPort(self: Frontend) []const u8 {
40
+ return switch (self) {
41
+ .next => "3000",
42
+ .vite, .react, .svelte, .vue, .native => "5173",
43
+ };
44
+ }
45
+
46
+ pub fn devUrl(self: Frontend) []const u8 {
47
+ return switch (self) {
48
+ .next => "http://127.0.0.1:3000/",
49
+ .vite, .react, .svelte, .vue, .native => "http://127.0.0.1:5173/",
50
+ };
51
+ }
52
+ };
53
+
54
+ /// Scaffold shape for the native frontend. `slim` is the zero-config
55
+ /// default: app.zon + src/ + assets + README only — the `native` CLI owns
56
+ /// the build graph (`native dev|build|test`) and `native eject` writes an
57
+ /// owned build.zig later. `full` keeps the pre-zero-config shape
58
+ /// (build.zig, build.zig.zon, .vscode, CI workflow) for users who want to
59
+ /// own the build from day one. Web frontends always scaffold full: their
60
+ /// npm build pipeline needs the expanded build.zig.
61
+ pub const Shape = enum {
62
+ slim,
63
+ full,
64
+ };
65
+
66
+ pub const InitOptions = struct {
67
+ app_name: []const u8,
68
+ framework_path: []const u8 = ".",
69
+ frontend: Frontend = .vite,
70
+ shape: Shape = .slim,
71
+ };
72
+
73
+ pub fn writeDefaultApp(allocator: std.mem.Allocator, io: std.Io, destination: []const u8, options: InitOptions) !void {
74
+ const names = try TemplateNames.init(allocator, options.app_name);
75
+ defer names.deinit(allocator);
76
+ const framework_path = try defaultFrameworkPath(allocator, io, destination, options.framework_path);
77
+ defer allocator.free(framework_path);
78
+
79
+ var cwd = std.Io.Dir.cwd();
80
+ try cwd.createDirPath(io, destination);
81
+ var app_dir = try cwd.openDir(io, destination, .{});
82
+ defer app_dir.close(io);
83
+
84
+ try app_dir.createDirPath(io, "src");
85
+ try app_dir.createDirPath(io, "assets");
86
+
87
+ if (options.frontend == .native) {
88
+ if (options.shape == .slim) {
89
+ return writeNativeAppSlim(allocator, io, app_dir, names);
90
+ }
91
+ // build.zig.zon path dependencies must be relative to the app root.
92
+ const dependency_path = try nativeDependencyPath(allocator, io, destination, framework_path);
93
+ defer allocator.free(dependency_path);
94
+ return writeNativeApp(allocator, io, app_dir, names, dependency_path);
95
+ }
96
+
97
+ const build_zig = try buildZig(allocator, names, framework_path, options.frontend);
98
+ defer allocator.free(build_zig);
99
+ const build_zon = try buildZon(allocator, names);
100
+ defer allocator.free(build_zon);
101
+ const main_zig = try mainZig(allocator, names, options.frontend);
102
+ defer allocator.free(main_zig);
103
+ const app_zon = try appZon(allocator, names, options.frontend);
104
+ defer allocator.free(app_zon);
105
+ const readme_md = try readme(allocator, names, framework_path, options.frontend);
106
+ defer allocator.free(readme_md);
107
+ const ci_yaml = try frontendCiYaml(allocator, names);
108
+ defer allocator.free(ci_yaml);
109
+
110
+ try app_dir.createDirPath(io, ".github/workflows");
111
+ try writeFile(app_dir, io, "build.zig", build_zig);
112
+ try writeFile(app_dir, io, "build.zig.zon", build_zon);
113
+ try writeFile(app_dir, io, "src/main.zig", main_zig);
114
+ try writeFile(app_dir, io, "src/runner.zig", runnerZig());
115
+ try writeFile(app_dir, io, "app.zon", app_zon);
116
+ try writeFile(app_dir, io, "assets/icon.png", default_icon_png);
117
+ try writeFile(app_dir, io, ".github/workflows/ci.yml", ci_yaml);
118
+ try writeFile(app_dir, io, "README.md", readme_md);
119
+
120
+ try writeFrontendFiles(allocator, io, app_dir, names, options.frontend);
121
+ }
122
+
123
+ /// The zero-config scaffold: no build files, no editor config, no CI — the
124
+ /// README teaches the `native` verbs and everything else is app source.
125
+ fn writeNativeAppSlim(allocator: std.mem.Allocator, io: std.Io, app_dir: std.Io.Dir, names: TemplateNames) !void {
126
+ const main_zig = try nativeMainZig(allocator, names);
127
+ defer allocator.free(main_zig);
128
+ const tests_zig = try nativeTestsZig(allocator, names);
129
+ defer allocator.free(tests_zig);
130
+ const app_zon = try nativeAppZon(allocator, names);
131
+ defer allocator.free(app_zon);
132
+ const readme_md = try slimNativeReadme(allocator, names);
133
+ defer allocator.free(readme_md);
134
+
135
+ try writeFile(app_dir, io, "src/main.zig", main_zig);
136
+ try writeFile(app_dir, io, "src/app.native", nativeAppMarkup());
137
+ try writeFile(app_dir, io, "src/tests.zig", tests_zig);
138
+ try writeFile(app_dir, io, "app.zon", app_zon);
139
+ try writeFile(app_dir, io, "assets/icon.png", default_icon_png);
140
+ try writeFile(app_dir, io, ".gitignore", slimGitignore());
141
+ try writeFile(app_dir, io, "README.md", readme_md);
142
+ }
143
+
144
+ fn slimGitignore() []const u8 {
145
+ return
146
+ \\.native/
147
+ \\zig-out/
148
+ \\.zig-cache/
149
+ \\
150
+ ;
151
+ }
152
+
153
+ fn slimNativeReadme(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
154
+ var out: std.ArrayList(u8) = .empty;
155
+ errdefer out.deinit(allocator);
156
+ try out.appendSlice(allocator, "# ");
157
+ try out.appendSlice(allocator, names.display_name);
158
+ try out.appendSlice(allocator,
159
+ \\
160
+ \\
161
+ \\A native-rendered Native SDK app: the view lives in `src/app.native`
162
+ \\(declarative markup) and the logic in `src/main.zig` (`Model`, `Msg`,
163
+ \\`update`). No WebView, no npm, no build files — the `native` CLI owns
164
+ \\the build.
165
+ \\
166
+ \\## Commands
167
+ \\
168
+ \\```sh
169
+ \\native dev # build and run the app with hot reload
170
+ \\native test # run the app's test suite
171
+ \\native build # produce a ReleaseFast binary in zig-out/bin/
172
+ \\native check # validate src/*.native markup and app.zon
173
+ \\```
174
+ \\
175
+ \\## Hot reload
176
+ \\
177
+ \\`src/app.native` is watched while `native dev` runs: edit it and the
178
+ \\window updates within ~2s without losing model state. Parse failures
179
+ \\keep the last good view.
180
+ \\
181
+ \\## Owning the build
182
+ \\
183
+ \\Need custom build logic? `native eject` writes a build.zig and
184
+ \\build.zig.zon into the app — from then on the `native` verbs drive
185
+ \\your files through `zig build` and never regenerate them.
186
+ \\
187
+ );
188
+ return out.toOwnedSlice(allocator);
189
+ }
190
+
191
+ fn writeNativeApp(allocator: std.mem.Allocator, io: std.Io, app_dir: std.Io.Dir, names: TemplateNames, framework_path: []const u8) !void {
192
+ try app_dir.createDirPath(io, ".vscode");
193
+ try app_dir.createDirPath(io, ".github/workflows");
194
+
195
+ const build_zig = try nativeBuildZig(allocator, names);
196
+ defer allocator.free(build_zig);
197
+ const build_zon = try nativeBuildZon(allocator, names, framework_path);
198
+ defer allocator.free(build_zon);
199
+ const main_zig = try nativeMainZig(allocator, names);
200
+ defer allocator.free(main_zig);
201
+ const tests_zig = try nativeTestsZig(allocator, names);
202
+ defer allocator.free(tests_zig);
203
+ const app_zon = try nativeAppZon(allocator, names);
204
+ defer allocator.free(app_zon);
205
+ const readme_md = try nativeReadme(allocator, names, framework_path);
206
+ defer allocator.free(readme_md);
207
+ const ci_yaml = try nativeCiYaml(allocator, names, framework_path);
208
+ defer allocator.free(ci_yaml);
209
+
210
+ try writeFile(app_dir, io, "build.zig", build_zig);
211
+ try writeFile(app_dir, io, "build.zig.zon", build_zon);
212
+ try writeFile(app_dir, io, "src/main.zig", main_zig);
213
+ try writeFile(app_dir, io, "src/app.native", nativeAppMarkup());
214
+ try writeFile(app_dir, io, "src/tests.zig", tests_zig);
215
+ try writeFile(app_dir, io, "app.zon", app_zon);
216
+ try writeFile(app_dir, io, "assets/icon.png", default_icon_png);
217
+ try writeFile(app_dir, io, ".vscode/settings.json", nativeVscodeSettings());
218
+ try writeFile(app_dir, io, ".github/workflows/ci.yml", ci_yaml);
219
+ try writeFile(app_dir, io, ".gitignore", nativeGitignore());
220
+ try writeFile(app_dir, io, "README.md", readme_md);
221
+ }
222
+
223
+ fn nativeBuildZig(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
224
+ var out: std.ArrayList(u8) = .empty;
225
+ errdefer out.deinit(allocator);
226
+ try out.appendSlice(allocator,
227
+ \\const std = @import("std");
228
+ \\const native_sdk = @import("native_sdk");
229
+ \\
230
+ \\pub fn build(b: *std.Build) void {
231
+ \\ native_sdk.addApp(b, b.dependency("native_sdk", .{}), .{ .name =
232
+ );
233
+ try out.appendSlice(allocator, " ");
234
+ try appendZigString(&out, allocator, names.package_name);
235
+ try out.appendSlice(allocator,
236
+ \\ });
237
+ \\}
238
+ \\
239
+ );
240
+ return out.toOwnedSlice(allocator);
241
+ }
242
+
243
+ fn nativeBuildZon(allocator: std.mem.Allocator, names: TemplateNames, framework_path: []const u8) ![]const u8 {
244
+ var out: std.ArrayList(u8) = .empty;
245
+ errdefer out.deinit(allocator);
246
+ try out.appendSlice(allocator,
247
+ \\.{
248
+ \\ .name = .
249
+ );
250
+ try out.appendSlice(allocator, names.module_name);
251
+ try out.appendSlice(allocator,
252
+ \\,
253
+ \\ .fingerprint = 0x
254
+ );
255
+ var fingerprint_buffer: [16]u8 = undefined;
256
+ const fingerprint = try std.fmt.bufPrint(&fingerprint_buffer, "{x}", .{fingerprintForName(names.module_name)});
257
+ try out.appendSlice(allocator, fingerprint);
258
+ try out.appendSlice(allocator,
259
+ \\,
260
+ \\ .version = "0.1.0",
261
+ \\ .minimum_zig_version = "0.16.0",
262
+ \\ .dependencies = .{ .native_sdk = .{ .path =
263
+ );
264
+ try out.appendSlice(allocator, " ");
265
+ try appendZigString(&out, allocator, framework_path);
266
+ try out.appendSlice(allocator,
267
+ \\ } },
268
+ \\ .paths = .{ "build.zig", "build.zig.zon", "src", "assets", "app.zon", "README.md" },
269
+ \\}
270
+ \\
271
+ );
272
+ return out.toOwnedSlice(allocator);
273
+ }
274
+
275
+ fn nativeMainZig(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
276
+ var out: std.ArrayList(u8) = .empty;
277
+ errdefer out.deinit(allocator);
278
+ try out.appendSlice(allocator,
279
+ \\//! A minimal native-rendered Native SDK app: the view lives in
280
+ \\//! `app.native` (embedded into the binary, and watched for hot reload in
281
+ \\//! dev); this file is the logic: `Model`, `Msg`, and `update`.
282
+ \\
283
+ \\const std = @import("std");
284
+ \\const runner = @import("runner");
285
+ \\const native_sdk = @import("native_sdk");
286
+ \\
287
+ \\pub const panic = std.debug.FullPanic(native_sdk.debug.capturePanic);
288
+ \\
289
+ \\const canvas = native_sdk.canvas;
290
+ \\const geometry = native_sdk.geometry;
291
+ \\
292
+ \\const canvas_label = "main-canvas";
293
+ \\const window_width: f32 = 480;
294
+ \\const window_height: f32 = 320;
295
+ \\
296
+ \\const app_permissions = [_][]const u8{ native_sdk.security.permission_command, native_sdk.security.permission_view };
297
+ \\const shell_views = [_]native_sdk.ShellView{
298
+ \\ .{ .label = canvas_label, .kind = .gpu_surface, .fill = true, .role = "Counter canvas", .accessibility_label = "Counter", .gpu_backend = .metal, .gpu_pixel_format = .bgra8_unorm, .gpu_present_mode = .timer, .gpu_alpha_mode = .@"opaque", .gpu_color_space = .srgb, .gpu_vsync = true },
299
+ \\};
300
+ \\const shell_windows = [_]native_sdk.ShellWindow{.{
301
+ \\ .label = "main",
302
+ \\ .title =
303
+ );
304
+ try out.appendSlice(allocator, " ");
305
+ try appendZigString(&out, allocator, names.display_name);
306
+ try out.appendSlice(allocator,
307
+ \\,
308
+ \\ .width = window_width,
309
+ \\ .height = window_height,
310
+ \\ .restore_state = false,
311
+ \\ .views = &shell_views,
312
+ \\}};
313
+ \\const shell_scene: native_sdk.ShellConfig = .{ .windows = &shell_windows };
314
+ \\
315
+ \\// ------------------------------------------------------------------ model
316
+ \\
317
+ \\pub const Msg = union(enum) {
318
+ \\ increment,
319
+ \\ decrement,
320
+ \\ reset,
321
+ \\};
322
+ \\
323
+ \\pub const Model = struct {
324
+ \\ count: i64 = 0,
325
+ \\};
326
+ \\
327
+ \\pub fn update(model: *Model, msg: Msg) void {
328
+ \\ switch (msg) {
329
+ \\ .increment => model.count += 1,
330
+ \\ .decrement => model.count -= 1,
331
+ \\ .reset => model.count = 0,
332
+ \\ }
333
+ \\}
334
+ \\
335
+ \\// ------------------------------------------------------------------- view
336
+ \\
337
+ \\pub const AppUi = canvas.Ui(Msg);
338
+ \\pub const app_markup = @embedFile("app.native");
339
+ \\
340
+ \\// -------------------------------------------------------------------- app
341
+ \\
342
+ \\const CounterApp = native_sdk.UiApp(Model, Msg);
343
+ \\
344
+ \\pub fn initialModel() Model {
345
+ \\ return .{};
346
+ \\}
347
+ \\
348
+ \\pub fn main(init: std.process.Init) !void {
349
+ \\ // The app struct (and any real Model) is multi-MB: `create`
350
+ \\ // heap-allocates and constructs everything in place, so neither
351
+ \\ // ever rides the stack. Mutate `app_state.model` through the
352
+ \\ // pointer before running if boot state is not the default.
353
+ \\ const app_state = try CounterApp.create(std.heap.page_allocator, .{
354
+ \\ .name =
355
+ );
356
+ try out.appendSlice(allocator, " ");
357
+ try appendZigString(&out, allocator, names.package_name);
358
+ try out.appendSlice(allocator,
359
+ \\,
360
+ \\ .scene = shell_scene,
361
+ \\ .canvas_label = canvas_label,
362
+ \\ .update = update,
363
+ \\ .markup = .{ .source = app_markup, .watch_path = "src/app.native", .io = init.io },
364
+ \\ });
365
+ \\ defer app_state.destroy();
366
+ \\ app_state.model = initialModel();
367
+ \\
368
+ \\ try runner.runWithOptions(app_state.app(), .{
369
+ \\ .app_name =
370
+ );
371
+ try out.appendSlice(allocator, " ");
372
+ try appendZigString(&out, allocator, names.package_name);
373
+ try out.appendSlice(allocator,
374
+ \\,
375
+ \\ .window_title =
376
+ );
377
+ try out.appendSlice(allocator, " ");
378
+ try appendZigString(&out, allocator, names.display_name);
379
+ try out.appendSlice(allocator,
380
+ \\,
381
+ \\ .bundle_id =
382
+ );
383
+ try out.appendSlice(allocator, " ");
384
+ try appendZigString(&out, allocator, names.app_id);
385
+ try out.appendSlice(allocator,
386
+ \\,
387
+ \\ .icon_path = "assets/icon.png",
388
+ \\ .default_frame = geometry.RectF.init(0, 0, window_width, window_height),
389
+ \\ .restore_state = false,
390
+ \\ .js_window_api = false,
391
+ \\ .security = .{
392
+ \\ .permissions = &app_permissions,
393
+ \\ .navigation = .{ .allowed_origins = &.{ "zero://inline", "zero://app" } },
394
+ \\ },
395
+ \\ }, init);
396
+ \\}
397
+ \\
398
+ \\test {
399
+ \\ _ = @import("tests.zig");
400
+ \\}
401
+ \\
402
+ );
403
+ return out.toOwnedSlice(allocator);
404
+ }
405
+
406
+ fn nativeAppMarkup() []const u8 {
407
+ return
408
+ \\<!-- The whole view. Embedded into the binary and hot-reloaded in dev:
409
+ \\ edit this file while the app runs and the window updates without
410
+ \\ losing the count. Validate with: native markup check src/app.native -->
411
+ \\<column gap="12" padding="16">
412
+ \\ <row gap="8" cross="center">
413
+ \\ <text grow="1">Counter</text>
414
+ \\ <button size="sm" variant="ghost" on-press="reset">Reset</button>
415
+ \\ </row>
416
+ \\ <row gap="8" main="center" cross="center" grow="1">
417
+ \\ <button variant="secondary" on-press="decrement">-</button>
418
+ \\ <text>{count}</text>
419
+ \\ <button variant="primary" on-press="increment">+</button>
420
+ \\ </row>
421
+ \\ <status-bar>count: {count}</status-bar>
422
+ \\</column>
423
+ \\
424
+ ;
425
+ }
426
+
427
+ fn nativeTestsZig(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
428
+ _ = names;
429
+ var out: std.ArrayList(u8) = .empty;
430
+ errdefer out.deinit(allocator);
431
+ try out.appendSlice(allocator,
432
+ \\const std = @import("std");
433
+ \\const native_sdk = @import("native_sdk");
434
+ \\const main = @import("main.zig");
435
+ \\
436
+ \\const canvas = native_sdk.canvas;
437
+ \\const testing = std.testing;
438
+ \\
439
+ \\const AppUi = main.AppUi;
440
+ \\const Model = main.Model;
441
+ \\const Msg = main.Msg;
442
+ \\
443
+ \\const AppMarkup = canvas.MarkupView(Model, Msg);
444
+ \\
445
+ \\fn buildTree(arena: std.mem.Allocator, model: *const Model) !AppUi.Tree {
446
+ \\ var view = try AppMarkup.init(arena, main.app_markup);
447
+ \\ var ui = AppUi.init(arena);
448
+ \\ const node = view.build(&ui, model) catch |err| {
449
+ \\ // Name the app.native position instead of leaving a bare error
450
+ \\ // trace: the usual causes are a binding without a matching
451
+ \\ // Model field or an on-* message without a Msg arm.
452
+ \\ if (err == error.MarkupBuild) {
453
+ \\ std.debug.print("app.native:{d}:{d}: {s}\n", .{ view.diagnostic.line, view.diagnostic.column, view.diagnostic.message });
454
+ \\ }
455
+ \\ return err;
456
+ \\ };
457
+ \\ return ui.finalize(node);
458
+ \\}
459
+ \\
460
+ \\fn findByText(widget: canvas.Widget, kind: canvas.WidgetKind, text: []const u8) ?canvas.Widget {
461
+ \\ if (widget.kind == kind and std.mem.eql(u8, widget.text, text)) return widget;
462
+ \\ for (widget.children) |child| {
463
+ \\ if (findByText(child, kind, text)) |found| return found;
464
+ \\ }
465
+ \\ return null;
466
+ \\}
467
+ \\
468
+ \\/// A miss fails the test with the mismatch spelled out instead of a
469
+ \\/// null-unwrap panic: the usual cause is app.native and this test
470
+ \\/// drifting apart after an edit.
471
+ \\fn expectByText(widget: canvas.Widget, kind: canvas.WidgetKind, text: []const u8) !canvas.Widget {
472
+ \\ return findByText(widget, kind, text) orelse {
473
+ \\ std.debug.print("no {t} with text \"{s}\" in the view - if you changed app.native, update this test to match\n", .{ kind, text });
474
+ \\ return error.WidgetNotFound;
475
+ \\ };
476
+ \\}
477
+ \\
478
+ \\test "clicking the buttons drives the model through typed dispatch" {
479
+ \\ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
480
+ \\ defer arena_state.deinit();
481
+ \\ const arena = arena_state.allocator();
482
+ \\
483
+ \\ var model = main.initialModel();
484
+ \\
485
+ \\ var tree = try buildTree(arena, &model);
486
+ \\ _ = try expectByText(tree.root, .text, "0");
487
+ \\ _ = try expectByText(tree.root, .status_bar, "count: 0");
488
+ \\
489
+ \\ // Click "+": the count increments and the view rebuilds with the
490
+ \\ // new value, keeping widget ids stable.
491
+ \\ const plus = try expectByText(tree.root, .button, "+");
492
+ \\ main.update(&model, tree.msgForPointer(plus.id, .up).?);
493
+ \\ try testing.expectEqual(@as(i64, 1), model.count);
494
+ \\
495
+ \\ tree = try buildTree(arena, &model);
496
+ \\ _ = try expectByText(tree.root, .text, "1");
497
+ \\ _ = try expectByText(tree.root, .status_bar, "count: 1");
498
+ \\ try testing.expectEqual(plus.id, (try expectByText(tree.root, .button, "+")).id);
499
+ \\
500
+ \\ // Click "-" twice: the count goes negative.
501
+ \\ const minus = try expectByText(tree.root, .button, "-");
502
+ \\ main.update(&model, tree.msgForPointer(minus.id, .up).?);
503
+ \\ main.update(&model, tree.msgForPointer(minus.id, .up).?);
504
+ \\ try testing.expectEqual(@as(i64, -1), model.count);
505
+ \\
506
+ \\ // Click "Reset": back to zero.
507
+ \\ tree = try buildTree(arena, &model);
508
+ \\ const reset = try expectByText(tree.root, .button, "Reset");
509
+ \\ main.update(&model, tree.msgForPointer(reset.id, .up).?);
510
+ \\ try testing.expectEqual(@as(i64, 0), model.count);
511
+ \\
512
+ \\ tree = try buildTree(arena, &model);
513
+ \\ _ = try expectByText(tree.root, .status_bar, "count: 0");
514
+ \\}
515
+ \\
516
+ \\test "the view lays out through the canvas engine" {
517
+ \\ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
518
+ \\ defer arena_state.deinit();
519
+ \\
520
+ \\ var model = main.initialModel();
521
+ \\ const tree = try buildTree(arena_state.allocator(), &model);
522
+ \\
523
+ \\ var nodes: [64]canvas.WidgetLayoutNode = undefined;
524
+ \\ const layout = try canvas.layoutWidgetTree(tree.root, native_sdk.geometry.RectF.init(0, 0, 480, 320), &nodes);
525
+ \\ try testing.expect(layout.nodes.len > 0);
526
+ \\
527
+ \\ const plus = try expectByText(tree.root, .button, "+");
528
+ \\ var saw_button = false;
529
+ \\ for (layout.nodes) |node| {
530
+ \\ if (node.widget.id == plus.id) saw_button = true;
531
+ \\ }
532
+ \\ try testing.expect(saw_button);
533
+ \\}
534
+ \\
535
+ );
536
+ return out.toOwnedSlice(allocator);
537
+ }
538
+
539
+ fn nativeAppZon(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
540
+ var out: std.ArrayList(u8) = .empty;
541
+ errdefer out.deinit(allocator);
542
+ try out.appendSlice(allocator,
543
+ \\.{
544
+ \\ .id =
545
+ );
546
+ try out.appendSlice(allocator, " ");
547
+ try appendZigString(&out, allocator, names.app_id);
548
+ try out.appendSlice(allocator,
549
+ \\,
550
+ \\ .name =
551
+ );
552
+ try out.appendSlice(allocator, " ");
553
+ try appendZigString(&out, allocator, names.package_name);
554
+ try out.appendSlice(allocator,
555
+ \\,
556
+ \\ .display_name =
557
+ );
558
+ try out.appendSlice(allocator, " ");
559
+ try appendZigString(&out, allocator, names.display_name);
560
+ try out.appendSlice(allocator,
561
+ \\,
562
+ \\ .description = "A counter that lives in one native window.",
563
+ \\ .version = "0.1.0",
564
+ \\ .icons = .{"assets/icon.png"},
565
+ \\ .platforms = .{"macos"},
566
+ \\ .permissions = .{ "view", "command" },
567
+ \\ .capabilities = .{ "native_views", "gpu_surfaces" },
568
+ \\ .shell = .{
569
+ \\ .windows = .{
570
+ \\ .{
571
+ \\ .label = "main",
572
+ \\ .title =
573
+ );
574
+ try out.appendSlice(allocator, " ");
575
+ try appendZigString(&out, allocator, names.display_name);
576
+ try out.appendSlice(allocator,
577
+ \\,
578
+ \\ .width = 480,
579
+ \\ .height = 320,
580
+ \\ .restore_state = false,
581
+ \\ .restore_policy = "center_on_primary",
582
+ \\ .views = .{
583
+ \\ .{ .label = "main-canvas", .kind = "gpu_surface", .fill = true, .role = "Counter canvas", .accessibility_label = "Counter", .gpu_backend = "metal", .gpu_pixel_format = "bgra8_unorm", .gpu_present_mode = "timer", .gpu_alpha_mode = "opaque", .gpu_color_space = "srgb", .gpu_vsync = true },
584
+ \\ },
585
+ \\ },
586
+ \\ },
587
+ \\ },
588
+ \\ .security = .{
589
+ \\ .navigation = .{
590
+ \\ .allowed_origins = .{ "zero://app", "zero://inline" },
591
+ \\ .external_links = .{ .action = "deny" },
592
+ \\ },
593
+ \\ },
594
+ \\ .web_engine = "system",
595
+ \\ .cef = .{ .dir = "third_party/cef/macos", .auto_install = false },
596
+ \\}
597
+ \\
598
+ );
599
+ return out.toOwnedSlice(allocator);
600
+ }
601
+
602
+ fn nativeVscodeSettings() []const u8 {
603
+ return
604
+ \\{
605
+ \\ "files.associations": { "*.native": "html" }
606
+ \\}
607
+ \\
608
+ ;
609
+ }
610
+
611
+ fn nativeGitignore() []const u8 {
612
+ return
613
+ \\zig-out/
614
+ \\.zig-cache/
615
+ \\
616
+ ;
617
+ }
618
+
619
+ fn nativeReadme(allocator: std.mem.Allocator, names: TemplateNames, framework_path: []const u8) ![]const u8 {
620
+ var out: std.ArrayList(u8) = .empty;
621
+ errdefer out.deinit(allocator);
622
+ try out.appendSlice(allocator, "# ");
623
+ try out.appendSlice(allocator, names.display_name);
624
+ try out.appendSlice(allocator,
625
+ \\
626
+ \\
627
+ \\A native-rendered Native SDK app: the view lives in `src/app.native`
628
+ \\(declarative markup) and the logic in `src/main.zig` (`Model`, `Msg`,
629
+ \\`update`). No WebView, no npm — the UI renders on a GPU surface.
630
+ \\
631
+ \\## Commands
632
+ \\
633
+ \\```sh
634
+ \\zig build run # build and launch the app
635
+ \\zig build test # run the full-loop UI tests
636
+ \\native markup check src/app.native # validate the markup without building
637
+ \\```
638
+ \\
639
+ \\## Hot reload
640
+ \\
641
+ \\`src/app.native` is embedded into the binary and watched during development:
642
+ \\edit it while the app runs and the window updates within ~2s without
643
+ \\losing model state. Parse failures keep the last good view.
644
+ \\
645
+ \\## Framework path
646
+ \\
647
+ \\`build.zig.zon` points the `native_sdk` dependency at:
648
+ \\
649
+ \\```text
650
+ \\
651
+ );
652
+ try out.appendSlice(allocator, framework_path);
653
+ try out.appendSlice(allocator,
654
+ \\
655
+ \\```
656
+ \\
657
+ \\Edit `.dependencies.native_sdk.path` in `build.zig.zon` if you move
658
+ \\this app or the framework checkout.
659
+ \\
660
+ );
661
+ return out.toOwnedSlice(allocator);
662
+ }
663
+
664
+ /// GitHub Actions workflow for a native-rendered app: a null-platform
665
+ /// logic-test job plus a Linux Xvfb automation smoke job that launches the
666
+ /// real binary and asserts on the accessibility snapshot. The generated
667
+ /// file belongs to the user, like everything init writes.
668
+ fn nativeCiYaml(allocator: std.mem.Allocator, names: TemplateNames, framework_path: []const u8) ![]const u8 {
669
+ var out: std.ArrayList(u8) = .empty;
670
+ errdefer out.deinit(allocator);
671
+ try out.appendSlice(allocator,
672
+ \\name: CI
673
+ \\
674
+ \\on:
675
+ \\ pull_request:
676
+ \\ push:
677
+ \\ branches:
678
+ \\ - main
679
+ \\
680
+ \\permissions:
681
+ \\ contents: read
682
+ \\
683
+ \\env:
684
+ \\ # build.zig.zon expects the Native SDK framework checkout at this
685
+ \\ # path, relative to the repository root. Adjust both together if
686
+ \\ # your framework checkout lives elsewhere.
687
+ \\ NATIVE_SDK_PATH:
688
+ );
689
+ try out.appendSlice(allocator, " ");
690
+ try appendEscapedString(&out, allocator, framework_path);
691
+ try out.appendSlice(allocator,
692
+ \\
693
+ \\
694
+ \\jobs:
695
+ \\ test:
696
+ \\ name: Logic Tests
697
+ \\ runs-on: ubuntu-latest
698
+ \\ steps:
699
+ \\ - uses: actions/checkout@v4
700
+ \\ - uses: mlugg/setup-zig@v2
701
+ \\ with:
702
+ \\ version: 0.16.0
703
+ \\ - name: Fetch native-sdk
704
+ \\ run: |
705
+ \\ if [ ! -f "$NATIVE_SDK_PATH/build.zig" ]; then
706
+ \\ git clone --depth 1 https://github.com/vercel-labs/zero-native.git "$NATIVE_SDK_PATH"
707
+ \\ fi
708
+ \\ - run: zig build test -Dplatform=null
709
+ \\
710
+ \\ smoke:
711
+ \\ name: Automation Smoke (Linux)
712
+ \\ runs-on: ubuntu-latest
713
+ \\ steps:
714
+ \\ - uses: actions/checkout@v4
715
+ \\ - uses: mlugg/setup-zig@v2
716
+ \\ with:
717
+ \\ version: 0.16.0
718
+ \\ - name: Install GTK and Xvfb
719
+ \\ run: sudo apt-get update && sudo apt-get install -y libgtk-4-dev libwebkitgtk-6.0-dev xvfb
720
+ \\ - name: Fetch native-sdk
721
+ \\ run: |
722
+ \\ if [ ! -f "$NATIVE_SDK_PATH/build.zig" ]; then
723
+ \\ git clone --depth 1 https://github.com/vercel-labs/zero-native.git "$NATIVE_SDK_PATH"
724
+ \\ fi
725
+ \\ - name: Build the Native SDK CLI
726
+ \\ run: cd "$NATIVE_SDK_PATH" && zig build
727
+ \\ - name: Build and drive the app headless
728
+ \\ run: |
729
+ \\ set -euo pipefail
730
+ \\ cli="$NATIVE_SDK_PATH/zig-out/bin/native"
731
+ \\ zig build -Dplatform=linux -Dweb-engine=system -Dautomation=true
732
+ \\ rm -rf .zig-cache/native-sdk-automation
733
+ \\ xvfb-run -a ./zig-out/bin/
734
+ );
735
+ try out.appendSlice(allocator, names.package_name);
736
+ try out.appendSlice(allocator,
737
+ \\ &
738
+ \\ pid=$!
739
+ \\ trap 'kill "$pid" >/dev/null 2>&1 || true' EXIT
740
+ \\ "$cli" automate wait
741
+ \\ "$cli" automate assert 'gpu_nonblank=true' 'role=button name="Reset"' 'count: 0'
742
+ \\ "$cli" automate screenshot main-canvas
743
+ \\ test -s .zig-cache/native-sdk-automation/screenshot-main-canvas.png
744
+ \\
745
+ );
746
+ return out.toOwnedSlice(allocator);
747
+ }
748
+
749
+ /// GitHub Actions workflow for a web-frontend app: null-platform logic
750
+ /// tests, pointing `-Dnative-sdk-path` at a framework checkout fetched in
751
+ /// CI (web templates default to a machine-local path).
752
+ fn frontendCiYaml(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
753
+ _ = names;
754
+ var out: std.ArrayList(u8) = .empty;
755
+ errdefer out.deinit(allocator);
756
+ try out.appendSlice(allocator,
757
+ \\name: CI
758
+ \\
759
+ \\on:
760
+ \\ pull_request:
761
+ \\ push:
762
+ \\ branches:
763
+ \\ - main
764
+ \\
765
+ \\permissions:
766
+ \\ contents: read
767
+ \\
768
+ \\env:
769
+ \\ # Where CI keeps the Native SDK framework checkout; the build
770
+ \\ # override below points the app at it.
771
+ \\ NATIVE_SDK_PATH: "../native-sdk"
772
+ \\
773
+ \\jobs:
774
+ \\ test:
775
+ \\ name: Logic Tests
776
+ \\ runs-on: ubuntu-latest
777
+ \\ steps:
778
+ \\ - uses: actions/checkout@v4
779
+ \\ - uses: mlugg/setup-zig@v2
780
+ \\ with:
781
+ \\ version: 0.16.0
782
+ \\ - name: Fetch native-sdk
783
+ \\ run: |
784
+ \\ if [ ! -f "$NATIVE_SDK_PATH/build.zig" ]; then
785
+ \\ git clone --depth 1 https://github.com/vercel-labs/zero-native.git "$NATIVE_SDK_PATH"
786
+ \\ fi
787
+ \\ - run: zig build test -Dplatform=null -Dnative-sdk-path="$NATIVE_SDK_PATH"
788
+ \\
789
+ );
790
+ return out.toOwnedSlice(allocator);
791
+ }
792
+
793
+ fn writeFile(dir: std.Io.Dir, io: std.Io, path: []const u8, bytes: []const u8) !void {
794
+ try dir.writeFile(io, .{ .sub_path = path, .data = bytes });
795
+ }
796
+
797
+ const TemplateNames = struct {
798
+ package_name: []const u8,
799
+ module_name: []const u8,
800
+ display_name: []const u8,
801
+ app_id: []const u8,
802
+
803
+ fn init(allocator: std.mem.Allocator, app_name: []const u8) !TemplateNames {
804
+ const package_name = try normalizePackageName(allocator, app_name);
805
+ errdefer allocator.free(package_name);
806
+ const module_name = try normalizeModuleName(allocator, package_name);
807
+ errdefer allocator.free(module_name);
808
+ const display_name = try displayName(allocator, package_name);
809
+ errdefer allocator.free(display_name);
810
+ const app_id = try std.fmt.allocPrint(allocator, "dev.native_sdk.{s}", .{package_name});
811
+ errdefer allocator.free(app_id);
812
+ return .{
813
+ .package_name = package_name,
814
+ .module_name = module_name,
815
+ .display_name = display_name,
816
+ .app_id = app_id,
817
+ };
818
+ }
819
+
820
+ fn deinit(self: TemplateNames, allocator: std.mem.Allocator) void {
821
+ allocator.free(self.package_name);
822
+ allocator.free(self.module_name);
823
+ allocator.free(self.display_name);
824
+ allocator.free(self.app_id);
825
+ }
826
+ };
827
+
828
+ fn buildZig(allocator: std.mem.Allocator, names: TemplateNames, framework_path: []const u8, frontend: Frontend) ![]const u8 {
829
+ var out: std.ArrayList(u8) = .empty;
830
+ errdefer out.deinit(allocator);
831
+
832
+ try out.appendSlice(allocator,
833
+ \\const std = @import("std");
834
+ \\
835
+ \\const PlatformOption = enum {
836
+ \\ auto,
837
+ \\ @"null",
838
+ \\ macos,
839
+ \\ linux,
840
+ \\ windows,
841
+ \\};
842
+ \\
843
+ \\const TraceOption = enum {
844
+ \\ off,
845
+ \\ events,
846
+ \\ runtime,
847
+ \\ all,
848
+ \\};
849
+ \\
850
+ \\const WebEngineOption = enum {
851
+ \\ system,
852
+ \\ chromium,
853
+ \\};
854
+ \\
855
+ \\const PackageTarget = enum {
856
+ \\ macos,
857
+ \\ windows,
858
+ \\ linux,
859
+ \\};
860
+ \\
861
+ \\const default_native_sdk_path =
862
+ );
863
+ try appendZigString(&out, allocator, framework_path);
864
+ try out.appendSlice(allocator, ";\nconst app_exe_name = ");
865
+ try appendZigString(&out, allocator, names.package_name);
866
+ try out.appendSlice(allocator,
867
+ \\;
868
+ \\
869
+ \\pub fn build(b: *std.Build) void {
870
+ \\ const target = nativeSdkTarget(b);
871
+ \\ const optimize = b.standardOptimizeOption(.{});
872
+ \\ const platform_option = b.option(PlatformOption, "platform", "Desktop backend: auto, null, macos, linux, windows") orelse .auto;
873
+ \\ const trace_option = b.option(TraceOption, "trace", "Trace output: off, events, runtime, all") orelse .events;
874
+ \\ const debug_overlay = b.option(bool, "debug-overlay", "Enable debug overlay output") orelse false;
875
+ \\ const automation_enabled = b.option(bool, "automation", "Enable Native SDK automation artifacts") orelse false;
876
+ \\ const js_bridge_enabled = b.option(bool, "js-bridge", "Enable optional JavaScript bridge stubs") orelse false;
877
+ \\ const web_engine_override = b.option(WebEngineOption, "web-engine", "Override app.zon web engine: system, chromium");
878
+ \\ const cef_dir_override = b.option([]const u8, "cef-dir", "Override CEF root directory for Chromium builds");
879
+ \\ const cef_auto_install_override = b.option(bool, "cef-auto-install", "Override app.zon CEF auto-install setting");
880
+ \\ const package_target = b.option(PackageTarget, "package-target", "Package target: macos, windows, linux") orelse .macos;
881
+ \\ const native_sdk_path = b.option([]const u8, "native-sdk-path", "Path to the Native SDK framework checkout") orelse default_native_sdk_path;
882
+ \\ const optimize_name = @tagName(optimize);
883
+ \\ const selected_platform: PlatformOption = switch (platform_option) {
884
+ \\ .auto => if (target.result.os.tag == .macos) .macos else if (target.result.os.tag == .linux) .linux else if (target.result.os.tag == .windows) .windows else .@"null",
885
+ \\ else => platform_option,
886
+ \\ };
887
+ \\ if (selected_platform == .macos and target.result.os.tag != .macos) {
888
+ \\ @panic("-Dplatform=macos requires a macOS target");
889
+ \\ }
890
+ \\ if (selected_platform == .linux and target.result.os.tag != .linux) {
891
+ \\ @panic("-Dplatform=linux requires a Linux target");
892
+ \\ }
893
+ \\ if (selected_platform == .windows and target.result.os.tag != .windows) {
894
+ \\ @panic("-Dplatform=windows requires a Windows target");
895
+ \\ }
896
+ \\ const app_web_engine = appWebEngineConfig();
897
+ \\ const web_engine = web_engine_override orelse app_web_engine.web_engine;
898
+ \\ const cef_dir = cef_dir_override orelse defaultCefDir(selected_platform, app_web_engine.cef_dir);
899
+ \\ const cef_auto_install = cef_auto_install_override orelse app_web_engine.cef_auto_install;
900
+ \\ if (web_engine == .chromium and selected_platform != .macos) {
901
+ \\ @panic("-Dweb-engine=chromium currently requires -Dplatform=macos");
902
+ \\ }
903
+ \\
904
+ \\ const native_sdk_mod = nativeSdkModule(b, target, optimize, native_sdk_path);
905
+ \\ const options = b.addOptions();
906
+ \\ options.addOption([]const u8, "platform", switch (selected_platform) {
907
+ \\ .auto => unreachable,
908
+ \\ .@"null" => "null",
909
+ \\ .macos => "macos",
910
+ \\ .linux => "linux",
911
+ \\ .windows => "windows",
912
+ \\ });
913
+ \\ options.addOption([]const u8, "trace", @tagName(trace_option));
914
+ \\ options.addOption([]const u8, "web_engine", @tagName(web_engine));
915
+ \\ options.addOption(bool, "debug_overlay", debug_overlay);
916
+ \\ options.addOption(bool, "automation", automation_enabled);
917
+ \\ options.addOption(bool, "js_bridge", js_bridge_enabled);
918
+ \\ const options_mod = options.createModule();
919
+ \\
920
+ \\ const runner_mod = localModule(b, target, optimize, "src/runner.zig");
921
+ \\ runner_mod.addImport("native_sdk", native_sdk_mod);
922
+ \\ runner_mod.addImport("build_options", options_mod);
923
+ \\ runner_mod.addImport("app_manifest_zon", b.createModule(.{ .root_source_file = b.path("app.zon") }));
924
+ \\
925
+ \\ const app_mod = localModule(b, target, optimize, "src/main.zig");
926
+ \\ app_mod.addImport("native_sdk", native_sdk_mod);
927
+ \\ app_mod.addImport("runner", runner_mod);
928
+ \\ const exe = b.addExecutable(.{
929
+ \\ .name = app_exe_name,
930
+ \\ .root_module = app_mod,
931
+ \\ });
932
+ \\ linkPlatform(b, target, app_mod, exe, selected_platform, web_engine, native_sdk_path, cef_dir, cef_auto_install);
933
+ \\ b.installArtifact(exe);
934
+ \\
935
+ \\ const frontend_install = b.addSystemCommand(&.{ "npm", "install", "--prefix", "frontend" });
936
+ \\ const frontend_install_step = b.step("frontend-install", "Install frontend dependencies");
937
+ \\ frontend_install_step.dependOn(&frontend_install.step);
938
+ \\
939
+ \\ const frontend_build = b.addSystemCommand(&.{ "npm", "--prefix", "frontend", "run", "build" });
940
+ \\ frontend_build.step.dependOn(&frontend_install.step);
941
+ \\ const frontend_step = b.step("frontend-build", "Build the frontend");
942
+ \\ frontend_step.dependOn(&frontend_build.step);
943
+ \\
944
+ \\ const run = b.addRunArtifact(exe);
945
+ \\ run.step.dependOn(&frontend_build.step);
946
+ \\ addCefRuntimeRunFiles(b, target, run, exe, web_engine, cef_dir);
947
+ \\ const run_step = b.step("run", "Run the app");
948
+ \\ run_step.dependOn(&run.step);
949
+ \\
950
+ \\ const dev = b.addSystemCommand(&.{ "native", "dev", "--manifest", "app.zon", "--binary" });
951
+ \\ dev.addFileArg(exe.getEmittedBin());
952
+ \\ dev.step.dependOn(&exe.step);
953
+ \\ dev.step.dependOn(&frontend_install.step);
954
+ \\ const dev_step = b.step("dev", "Run the frontend dev server and native shell");
955
+ \\ dev_step.dependOn(&dev.step);
956
+ \\
957
+ \\ const package = b.addSystemCommand(&.{
958
+ \\ "native",
959
+ \\ "package",
960
+ \\ "--target",
961
+ \\ @tagName(package_target),
962
+ \\ "--manifest",
963
+ \\ "app.zon",
964
+ \\ "--assets",
965
+ );
966
+ try appendZigString(&out, allocator, frontend.distDir());
967
+ try out.appendSlice(allocator,
968
+ \\,
969
+ \\ "--optimize",
970
+ \\ optimize_name,
971
+ \\ "--output",
972
+ \\ b.fmt("zig-out/package/{s}-0.1.0-{s}-{s}{s}", .{ app_exe_name, @tagName(package_target), optimize_name, packageSuffix(package_target) }),
973
+ \\ "--binary",
974
+ \\ });
975
+ \\ package.addFileArg(exe.getEmittedBin());
976
+ \\ package.addArgs(&.{ "--web-engine", @tagName(web_engine), "--cef-dir", cef_dir });
977
+ \\ if (cef_auto_install) package.addArg("--cef-auto-install");
978
+ \\ package.step.dependOn(&exe.step);
979
+ \\ package.step.dependOn(&frontend_build.step);
980
+ \\ const package_step = b.step("package", "Create a local package artifact");
981
+ \\ package_step.dependOn(&package.step);
982
+ \\
983
+ \\ const tests = b.addTest(.{ .root_module = app_mod });
984
+ \\ const test_step = b.step("test", "Run tests");
985
+ \\ test_step.dependOn(&b.addRunArtifact(tests).step);
986
+ \\}
987
+ \\
988
+ \\fn nativeSdkTarget(b: *std.Build) std.Build.ResolvedTarget {
989
+ \\ const target = b.standardTargetOptions(.{});
990
+ \\ if (target.result.os.tag != .macos) return target;
991
+ \\
992
+ \\ if (b.sysroot == null) {
993
+ \\ b.sysroot = macosSdkPath(b) orelse b.sysroot;
994
+ \\ }
995
+ \\
996
+ \\ var query = target.query;
997
+ \\ query.os_tag = .macos;
998
+ \\ query.os_version_min = .{ .semver = .{ .major = 11, .minor = 0, .patch = 0 } };
999
+ \\ return b.resolveTargetQuery(query);
1000
+ \\}
1001
+ \\
1002
+ \\fn macosSdkPath(b: *std.Build) ?[]const u8 {
1003
+ \\ if (b.graph.environ_map.get("SDKROOT")) |sdkroot| {
1004
+ \\ if (sdkroot.len > 0) return sdkroot;
1005
+ \\ }
1006
+ \\
1007
+ \\ const result = std.process.run(b.allocator, b.graph.io, .{
1008
+ \\ .argv = &.{ "xcrun", "--sdk", "macosx", "--show-sdk-path" },
1009
+ \\ .stdout_limit = .limited(4096),
1010
+ \\ .stderr_limit = .limited(4096),
1011
+ \\ }) catch return null;
1012
+ \\ defer b.allocator.free(result.stderr);
1013
+ \\ if (result.term != .exited or result.term.exited != 0) {
1014
+ \\ b.allocator.free(result.stdout);
1015
+ \\ return null;
1016
+ \\ }
1017
+ \\ return std.mem.trimEnd(u8, result.stdout, "\r\n");
1018
+ \\}
1019
+ \\
1020
+ \\fn localModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, path: []const u8) *std.Build.Module {
1021
+ \\ return b.createModule(.{
1022
+ \\ .root_source_file = b.path(path),
1023
+ \\ .target = target,
1024
+ \\ .optimize = optimize,
1025
+ \\ });
1026
+ \\}
1027
+ \\
1028
+ \\fn nativeSdkPath(b: *std.Build, native_sdk_path: []const u8, sub_path: []const u8) std.Build.LazyPath {
1029
+ \\ return .{ .cwd_relative = b.pathJoin(&.{ native_sdk_path, sub_path }) };
1030
+ \\}
1031
+ \\
1032
+ \\fn nativeSdkModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, native_sdk_path: []const u8) *std.Build.Module {
1033
+ \\ const geometry_mod = externalModule(b, target, optimize, native_sdk_path, "src/primitives/geometry/root.zig");
1034
+ \\ const assets_mod = externalModule(b, target, optimize, native_sdk_path, "src/primitives/assets/root.zig");
1035
+ \\ const app_dirs_mod = externalModule(b, target, optimize, native_sdk_path, "src/primitives/app_dirs/root.zig");
1036
+ \\ const trace_mod = externalModule(b, target, optimize, native_sdk_path, "src/primitives/trace/root.zig");
1037
+ \\ const app_manifest_mod = externalModule(b, target, optimize, native_sdk_path, "src/primitives/app_manifest/root.zig");
1038
+ \\ const diagnostics_mod = externalModule(b, target, optimize, native_sdk_path, "src/primitives/diagnostics/root.zig");
1039
+ \\ const platform_info_mod = externalModule(b, target, optimize, native_sdk_path, "src/primitives/platform_info/root.zig");
1040
+ \\ const json_mod = externalModule(b, target, optimize, native_sdk_path, "src/primitives/json/root.zig");
1041
+ \\ const canvas_mod = externalModule(b, target, optimize, native_sdk_path, "src/primitives/canvas/root.zig");
1042
+ \\ canvas_mod.addImport("geometry", geometry_mod);
1043
+ \\ canvas_mod.addImport("json", json_mod);
1044
+ \\ const debug_mod = externalModule(b, target, optimize, native_sdk_path, "src/debug/root.zig");
1045
+ \\ debug_mod.addImport("app_dirs", app_dirs_mod);
1046
+ \\ debug_mod.addImport("trace", trace_mod);
1047
+ \\
1048
+ \\ const native_sdk_mod = externalModule(b, target, optimize, native_sdk_path, "src/root.zig");
1049
+ \\ native_sdk_mod.addImport("geometry", geometry_mod);
1050
+ \\ native_sdk_mod.addImport("assets", assets_mod);
1051
+ \\ native_sdk_mod.addImport("app_dirs", app_dirs_mod);
1052
+ \\ native_sdk_mod.addImport("trace", trace_mod);
1053
+ \\ native_sdk_mod.addImport("app_manifest", app_manifest_mod);
1054
+ \\ native_sdk_mod.addImport("diagnostics", diagnostics_mod);
1055
+ \\ native_sdk_mod.addImport("platform_info", platform_info_mod);
1056
+ \\ native_sdk_mod.addImport("json", json_mod);
1057
+ \\ native_sdk_mod.addImport("canvas", canvas_mod);
1058
+ \\ return native_sdk_mod;
1059
+ \\}
1060
+ \\
1061
+ \\fn externalModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, native_sdk_path: []const u8, path: []const u8) *std.Build.Module {
1062
+ \\ return b.createModule(.{
1063
+ \\ .root_source_file = nativeSdkPath(b, native_sdk_path, path),
1064
+ \\ .target = target,
1065
+ \\ .optimize = optimize,
1066
+ \\ });
1067
+ \\}
1068
+ \\
1069
+ \\fn linkPlatform(b: *std.Build, target: std.Build.ResolvedTarget, app_mod: *std.Build.Module, exe: *std.Build.Step.Compile, platform: PlatformOption, web_engine: WebEngineOption, native_sdk_path: []const u8, cef_dir: []const u8, cef_auto_install: bool) void {
1070
+ \\ if (platform == .macos) {
1071
+ \\ switch (web_engine) {
1072
+ \\ .system => {
1073
+ \\ const sdk_include = if (b.sysroot) |sysroot| b.fmt("-I{s}/usr/include", .{sysroot}) else "";
1074
+ \\ const flags: []const []const u8 = if (b.sysroot) |sysroot| &.{ "-fobjc-arc", "-fno-sanitize=builtin", "-ObjC", "-mmacosx-version-min=11.0", "-isysroot", sysroot, sdk_include } else &.{ "-fobjc-arc", "-fno-sanitize=builtin", "-ObjC", "-mmacosx-version-min=11.0" };
1075
+ \\ app_mod.addCSourceFile(.{ .file = nativeSdkPath(b, native_sdk_path, "src/platform/macos/appkit_host.m"), .flags = flags });
1076
+ \\ app_mod.linkFramework("WebKit", .{});
1077
+ \\ },
1078
+ \\ .chromium => {
1079
+ \\ const cef_check = addCefCheck(b, target, cef_dir);
1080
+ \\ if (cef_auto_install) {
1081
+ \\ const cef_auto = b.addSystemCommand(&.{ "native", "cef", "install", "--dir", cef_dir });
1082
+ \\ cef_check.step.dependOn(&cef_auto.step);
1083
+ \\ }
1084
+ \\ exe.step.dependOn(&cef_check.step);
1085
+ \\ const include_arg = b.fmt("-I{s}", .{cef_dir});
1086
+ \\ const define_arg = b.fmt("-DNATIVE_SDK_CEF_DIR=\"{s}\"", .{cef_dir});
1087
+ \\ // The SDK's usr/include must stay a system include dir (searched after zig's
1088
+ \\ // bundled libc++/libc headers). A plain -I shadows libc++'s <string.h>/<math.h>
1089
+ \\ // wrappers in ObjC++ and surfaces SDK nullability gaps as a diagnostic flood.
1090
+ \\ const sdk_include = if (b.sysroot) |sysroot| b.fmt("-isystem{s}/usr/include", .{sysroot}) else "";
1091
+ \\ const flags: []const []const u8 = if (b.sysroot) |sysroot| &.{ "-fobjc-arc", "-fno-sanitize=builtin", "-ObjC++", "-std=c++17", "-stdlib=libc++", "-mmacosx-version-min=11.0", "-isysroot", sysroot, sdk_include, include_arg, define_arg } else &.{ "-fobjc-arc", "-fno-sanitize=builtin", "-ObjC++", "-std=c++17", "-stdlib=libc++", "-mmacosx-version-min=11.0", include_arg, define_arg };
1092
+ \\ app_mod.addCSourceFile(.{ .file = nativeSdkPath(b, native_sdk_path, "src/platform/macos/cef_host.mm"), .flags = flags });
1093
+ \\ app_mod.addObjectFile(b.path(b.fmt("{s}/libcef_dll_wrapper/libcef_dll_wrapper.a", .{cef_dir})));
1094
+ \\ app_mod.addFrameworkPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
1095
+ \\ app_mod.linkFramework("Chromium Embedded Framework", .{});
1096
+ \\ app_mod.addRPath(.{ .cwd_relative = "@executable_path/Frameworks" });
1097
+ \\ },
1098
+ \\ }
1099
+ \\ if (b.sysroot) |sysroot| {
1100
+ \\ app_mod.addFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sysroot, "System/Library/Frameworks" }) });
1101
+ \\ }
1102
+ \\ app_mod.linkFramework("AppKit", .{});
1103
+ \\ app_mod.linkFramework("AVFoundation", .{});
1104
+ \\ app_mod.linkFramework("MediaToolbox", .{});
1105
+ \\ app_mod.linkFramework("Accelerate", .{});
1106
+ \\ app_mod.linkFramework("Foundation", .{});
1107
+ \\ app_mod.linkFramework("CoreText", .{});
1108
+ \\ app_mod.linkFramework("UniformTypeIdentifiers", .{});
1109
+ \\ app_mod.linkFramework("Security", .{});
1110
+ \\ app_mod.linkFramework("Metal", .{});
1111
+ \\ app_mod.linkFramework("QuartzCore", .{});
1112
+ \\ app_mod.linkSystemLibrary("c", .{});
1113
+ \\ if (web_engine == .chromium) app_mod.linkSystemLibrary("c++", .{});
1114
+ \\ } else if (platform == .linux) {
1115
+ \\ switch (web_engine) {
1116
+ \\ .system => {
1117
+ \\ app_mod.addCSourceFile(.{ .file = nativeSdkPath(b, native_sdk_path, "src/platform/linux/gtk_host.c"), .flags = &.{} });
1118
+ \\ app_mod.linkSystemLibrary("gtk4", .{});
1119
+ \\ app_mod.linkSystemLibrary("webkitgtk-6.0", .{});
1120
+ \\ app_mod.linkSystemLibrary("dl", .{});
1121
+ \\ },
1122
+ \\ .chromium => {
1123
+ \\ const cef_check = addCefCheck(b, target, cef_dir);
1124
+ \\ if (cef_auto_install) {
1125
+ \\ const cef_auto = b.addSystemCommand(&.{ "native", "cef", "install", "--dir", cef_dir });
1126
+ \\ cef_check.step.dependOn(&cef_auto.step);
1127
+ \\ }
1128
+ \\ exe.step.dependOn(&cef_check.step);
1129
+ \\ const include_arg = b.fmt("-I{s}", .{cef_dir});
1130
+ \\ const define_arg = b.fmt("-DNATIVE_SDK_CEF_DIR=\"{s}\"", .{cef_dir});
1131
+ \\ app_mod.addCSourceFile(.{ .file = nativeSdkPath(b, native_sdk_path, "src/platform/linux/cef_host.cpp"), .flags = &.{ "-std=c++17", include_arg, define_arg } });
1132
+ \\ app_mod.addObjectFile(b.path(b.fmt("{s}/libcef_dll_wrapper/libcef_dll_wrapper.a", .{cef_dir})));
1133
+ \\ app_mod.addLibraryPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
1134
+ \\ app_mod.linkSystemLibrary("cef", .{});
1135
+ \\ app_mod.addRPath(.{ .cwd_relative = "$ORIGIN" });
1136
+ \\ },
1137
+ \\ }
1138
+ \\ app_mod.linkSystemLibrary("c", .{});
1139
+ \\ if (web_engine == .chromium) app_mod.linkSystemLibrary("stdc++", .{});
1140
+ \\ } else if (platform == .windows) {
1141
+ \\ switch (web_engine) {
1142
+ \\ .system => app_mod.addCSourceFile(.{ .file = nativeSdkPath(b, native_sdk_path, "src/platform/windows/webview2_host.cpp"), .flags = &.{ "-std=c++17" } }),
1143
+ \\ .chromium => {
1144
+ \\ const cef_check = addCefCheck(b, target, cef_dir);
1145
+ \\ if (cef_auto_install) {
1146
+ \\ const cef_auto = b.addSystemCommand(&.{ "native", "cef", "install", "--dir", cef_dir });
1147
+ \\ cef_check.step.dependOn(&cef_auto.step);
1148
+ \\ }
1149
+ \\ exe.step.dependOn(&cef_check.step);
1150
+ \\ const include_arg = b.fmt("-I{s}", .{cef_dir});
1151
+ \\ const define_arg = b.fmt("-DNATIVE_SDK_CEF_DIR=\"{s}\"", .{cef_dir});
1152
+ \\ app_mod.addCSourceFile(.{ .file = nativeSdkPath(b, native_sdk_path, "src/platform/windows/cef_host.cpp"), .flags = &.{ "-std=c++17", include_arg, define_arg } });
1153
+ \\ app_mod.addObjectFile(b.path(b.fmt("{s}/libcef_dll_wrapper/libcef_dll_wrapper.lib", .{cef_dir})));
1154
+ \\ app_mod.addLibraryPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
1155
+ \\ },
1156
+ \\ }
1157
+ \\ app_mod.linkSystemLibrary("c", .{});
1158
+ \\ app_mod.linkSystemLibrary("c++", .{});
1159
+ \\ app_mod.linkSystemLibrary("user32", .{});
1160
+ \\ app_mod.linkSystemLibrary("gdi32", .{});
1161
+ \\ app_mod.linkSystemLibrary("imm32", .{});
1162
+ \\ app_mod.linkSystemLibrary("comctl32", .{});
1163
+ \\ app_mod.linkSystemLibrary("ole32", .{});
1164
+ \\ app_mod.linkSystemLibrary("oleacc", .{});
1165
+ \\ app_mod.linkSystemLibrary("shell32", .{});
1166
+ \\ // The audio backend: Media Foundation (session + source resolver
1167
+ \\ // + streaming audio renderer) and WinHTTP (the cache fill).
1168
+ \\ app_mod.linkSystemLibrary("mf", .{});
1169
+ \\ app_mod.linkSystemLibrary("mfplat", .{});
1170
+ \\ app_mod.linkSystemLibrary("winhttp", .{});
1171
+ \\ if (web_engine == .chromium) app_mod.linkSystemLibrary("libcef", .{});
1172
+ \\ }
1173
+ \\}
1174
+ \\
1175
+ \\fn addCefRuntimeRunFiles(b: *std.Build, target: std.Build.ResolvedTarget, run: *std.Build.Step.Run, exe: *std.Build.Step.Compile, web_engine: WebEngineOption, cef_dir: []const u8) void {
1176
+ \\ if (web_engine != .chromium) return;
1177
+ \\ if (target.result.os.tag != .macos) return;
1178
+ \\ const copy = b.addSystemCommand(&.{ "sh", "-c", b.fmt(
1179
+ \\ \\set -e
1180
+ \\ \\exe="$0"
1181
+ \\ \\exe_dir="$(dirname "$exe")"
1182
+ \\ \\rm -rf "zig-out/Frameworks/Chromium Embedded Framework.framework" "zig-out/bin/Frameworks/Chromium Embedded Framework.framework" ".zig-cache/o/Frameworks/Chromium Embedded Framework.framework" &&
1183
+ \\ \\mkdir -p "zig-out/Frameworks" "zig-out/bin/Frameworks" ".zig-cache/o/Frameworks" "$exe_dir" &&
1184
+ \\ \\cp -R "{s}/Release/Chromium Embedded Framework.framework" "zig-out/Frameworks/" &&
1185
+ \\ \\cp -R "{s}/Release/Chromium Embedded Framework.framework" "zig-out/bin/Frameworks/" &&
1186
+ \\ \\cp -R "{s}/Release/Chromium Embedded Framework.framework" ".zig-cache/o/Frameworks/" &&
1187
+ \\ \\cp "{s}/Release/Chromium Embedded Framework.framework/Libraries/libEGL.dylib" "$exe_dir/" &&
1188
+ \\ \\cp "{s}/Release/Chromium Embedded Framework.framework/Libraries/libGLESv2.dylib" "$exe_dir/" &&
1189
+ \\ \\cp "{s}/Release/Chromium Embedded Framework.framework/Libraries/libvk_swiftshader.dylib" "$exe_dir/" &&
1190
+ \\ \\cp "{s}/Release/Chromium Embedded Framework.framework/Libraries/vk_swiftshader_icd.json" "$exe_dir/"
1191
+ \\ , .{ cef_dir, cef_dir, cef_dir, cef_dir, cef_dir, cef_dir, cef_dir }) });
1192
+ \\ copy.addFileArg(exe.getEmittedBin());
1193
+ \\ run.step.dependOn(&copy.step);
1194
+ \\}
1195
+ \\
1196
+ \\fn addCefCheck(b: *std.Build, target: std.Build.ResolvedTarget, cef_dir: []const u8) *std.Build.Step.Run {
1197
+ \\ const script = switch (target.result.os.tag) {
1198
+ \\ .macos => b.fmt(
1199
+ \\ \\test -f "{s}/include/cef_app.h" &&
1200
+ \\ \\test -d "{s}/Release/Chromium Embedded Framework.framework" &&
1201
+ \\ \\test -f "{s}/libcef_dll_wrapper/libcef_dll_wrapper.a" || {{
1202
+ \\ \\ echo "missing CEF dependency for -Dweb-engine=chromium" >&2
1203
+ \\ \\ echo "Expected:" >&2
1204
+ \\ \\ echo " {s}/include/cef_app.h" >&2
1205
+ \\ \\ echo " {s}/Release/Chromium Embedded Framework.framework" >&2
1206
+ \\ \\ echo " {s}/libcef_dll_wrapper/libcef_dll_wrapper.a" >&2
1207
+ \\ \\ echo "Fix with: native cef install --dir {s}" >&2
1208
+ \\ \\ echo "Or rerun with: -Dcef-auto-install=true" >&2
1209
+ \\ \\ echo "Pass -Dcef-dir=/path/to/cef if your bundle lives elsewhere." >&2
1210
+ \\ \\ exit 1
1211
+ \\ \\}}
1212
+ \\ , .{ cef_dir, cef_dir, cef_dir, cef_dir, cef_dir, cef_dir, cef_dir }),
1213
+ \\ .linux => b.fmt(
1214
+ \\ \\test -f "{s}/include/cef_app.h" &&
1215
+ \\ \\test -f "{s}/Release/libcef.so" &&
1216
+ \\ \\test -f "{s}/libcef_dll_wrapper/libcef_dll_wrapper.a" || {{
1217
+ \\ \\ echo "missing CEF dependency for -Dweb-engine=chromium" >&2
1218
+ \\ \\ echo "Fix with: native cef install --dir {s}" >&2
1219
+ \\ \\ exit 1
1220
+ \\ \\}}
1221
+ \\ , .{ cef_dir, cef_dir, cef_dir, cef_dir }),
1222
+ \\ .windows => b.fmt(
1223
+ \\ \\test -f "{s}/include/cef_app.h" &&
1224
+ \\ \\test -f "{s}/Release/libcef.dll" &&
1225
+ \\ \\test -f "{s}/libcef_dll_wrapper/libcef_dll_wrapper.lib" || {{
1226
+ \\ \\ echo "missing CEF dependency for -Dweb-engine=chromium" >&2
1227
+ \\ \\ echo "Fix with: native cef install --dir {s}" >&2
1228
+ \\ \\ exit 1
1229
+ \\ \\}}
1230
+ \\ , .{ cef_dir, cef_dir, cef_dir, cef_dir }),
1231
+ \\ else => "echo unsupported CEF target >&2; exit 1",
1232
+ \\ };
1233
+ \\ return b.addSystemCommand(&.{ "sh", "-c", script });
1234
+ \\}
1235
+ \\
1236
+ \\fn packageSuffix(target: PackageTarget) []const u8 {
1237
+ \\ return switch (target) {
1238
+ \\ .macos => ".app",
1239
+ \\ .windows, .linux => "",
1240
+ \\ };
1241
+ \\}
1242
+ \\
1243
+ \\const AppWebEngineConfig = struct {
1244
+ \\ web_engine: WebEngineOption = .system,
1245
+ \\ cef_dir: []const u8 = "third_party/cef/macos",
1246
+ \\ cef_auto_install: bool = false,
1247
+ \\};
1248
+ \\
1249
+ \\fn defaultCefDir(platform: PlatformOption, configured: []const u8) []const u8 {
1250
+ \\ if (!std.mem.eql(u8, configured, "third_party/cef/macos")) return configured;
1251
+ \\ return switch (platform) {
1252
+ \\ .linux => "third_party/cef/linux",
1253
+ \\ .windows => "third_party/cef/windows",
1254
+ \\ else => configured,
1255
+ \\ };
1256
+ \\}
1257
+ \\
1258
+ \\fn appWebEngineConfig() AppWebEngineConfig {
1259
+ \\ const source = @embedFile("app.zon");
1260
+ \\ var config: AppWebEngineConfig = .{};
1261
+ \\ if (stringField(source, ".web_engine")) |value| {
1262
+ \\ config.web_engine = parseWebEngine(value) orelse .system;
1263
+ \\ }
1264
+ \\ if (objectSection(source, ".cef")) |cef| {
1265
+ \\ if (stringField(cef, ".dir")) |value| config.cef_dir = value;
1266
+ \\ if (boolField(cef, ".auto_install")) |value| config.cef_auto_install = value;
1267
+ \\ }
1268
+ \\ return config;
1269
+ \\}
1270
+ \\
1271
+ \\fn parseWebEngine(value: []const u8) ?WebEngineOption {
1272
+ \\ if (std.mem.eql(u8, value, "system")) return .system;
1273
+ \\ if (std.mem.eql(u8, value, "chromium")) return .chromium;
1274
+ \\ return null;
1275
+ \\}
1276
+ \\
1277
+ \\fn stringField(source: []const u8, field: []const u8) ?[]const u8 {
1278
+ \\ const field_index = std.mem.indexOf(u8, source, field) orelse return null;
1279
+ \\ const equals = std.mem.indexOfScalarPos(u8, source, field_index, '=') orelse return null;
1280
+ \\ const start_quote = std.mem.indexOfScalarPos(u8, source, equals, '"') orelse return null;
1281
+ \\ const end_quote = std.mem.indexOfScalarPos(u8, source, start_quote + 1, '"') orelse return null;
1282
+ \\ return source[start_quote + 1 .. end_quote];
1283
+ \\}
1284
+ \\
1285
+ \\fn objectSection(source: []const u8, field: []const u8) ?[]const u8 {
1286
+ \\ const field_index = std.mem.indexOf(u8, source, field) orelse return null;
1287
+ \\ const open = std.mem.indexOfScalarPos(u8, source, field_index, '{') orelse return null;
1288
+ \\ var depth: usize = 0;
1289
+ \\ var index = open;
1290
+ \\ while (index < source.len) : (index += 1) {
1291
+ \\ switch (source[index]) {
1292
+ \\ '{' => depth += 1,
1293
+ \\ '}' => {
1294
+ \\ depth -= 1;
1295
+ \\ if (depth == 0) return source[open + 1 .. index];
1296
+ \\ },
1297
+ \\ else => {},
1298
+ \\ }
1299
+ \\ }
1300
+ \\ return null;
1301
+ \\}
1302
+ \\
1303
+ \\fn boolField(source: []const u8, field: []const u8) ?bool {
1304
+ \\ const field_index = std.mem.indexOf(u8, source, field) orelse return null;
1305
+ \\ const equals = std.mem.indexOfScalarPos(u8, source, field_index, '=') orelse return null;
1306
+ \\ var index = equals + 1;
1307
+ \\ while (index < source.len and std.ascii.isWhitespace(source[index])) : (index += 1) {}
1308
+ \\ if (std.mem.startsWith(u8, source[index..], "true")) return true;
1309
+ \\ if (std.mem.startsWith(u8, source[index..], "false")) return false;
1310
+ \\ return null;
1311
+ \\}
1312
+ \\
1313
+ );
1314
+ return out.toOwnedSlice(allocator);
1315
+ }
1316
+
1317
+ fn buildZon(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
1318
+ var out: std.ArrayList(u8) = .empty;
1319
+ errdefer out.deinit(allocator);
1320
+ try out.appendSlice(allocator,
1321
+ \\.{
1322
+ \\ .name = .
1323
+ );
1324
+ try out.appendSlice(allocator, names.module_name);
1325
+ try out.appendSlice(allocator,
1326
+ \\,
1327
+ \\ .fingerprint = 0x
1328
+ );
1329
+ var fingerprint_buffer: [16]u8 = undefined;
1330
+ const fingerprint = try std.fmt.bufPrint(&fingerprint_buffer, "{x}", .{fingerprintForName(names.module_name)});
1331
+ try out.appendSlice(allocator, fingerprint);
1332
+ try out.appendSlice(allocator,
1333
+ \\,
1334
+ \\ .version = "0.1.0",
1335
+ \\ .minimum_zig_version = "0.16.0",
1336
+ \\ .dependencies = .{},
1337
+ \\ .paths = .{ "build.zig", "build.zig.zon", "src", "assets", "frontend", "app.zon", "README.md" },
1338
+ \\}
1339
+ \\
1340
+ );
1341
+ return out.toOwnedSlice(allocator);
1342
+ }
1343
+
1344
+ fn mainZig(allocator: std.mem.Allocator, names: TemplateNames, frontend: Frontend) ![]const u8 {
1345
+ var out: std.ArrayList(u8) = .empty;
1346
+ errdefer out.deinit(allocator);
1347
+ try out.appendSlice(allocator,
1348
+ \\const std = @import("std");
1349
+ \\const runner = @import("runner");
1350
+ \\const native_sdk = @import("native_sdk");
1351
+ \\
1352
+ \\pub const panic = std.debug.FullPanic(native_sdk.debug.capturePanic);
1353
+ \\
1354
+ \\const App = struct {
1355
+ \\ env_map: *std.process.Environ.Map,
1356
+ \\
1357
+ \\ fn app(self: *@This()) native_sdk.App {
1358
+ \\ return .{
1359
+ \\ .context = self,
1360
+ \\ .name =
1361
+ );
1362
+ try appendZigString(&out, allocator, names.package_name);
1363
+ try out.appendSlice(allocator,
1364
+ \\,
1365
+ \\ .source = native_sdk.frontend.productionSource(.{ .dist =
1366
+ );
1367
+ try appendZigString(&out, allocator, frontend.distDir());
1368
+ try out.appendSlice(allocator,
1369
+ \\ }),
1370
+ \\ .source_fn = source,
1371
+ \\ };
1372
+ \\ }
1373
+ \\
1374
+ \\ fn source(context: *anyopaque) anyerror!native_sdk.WebViewSource {
1375
+ \\ const self: *@This() = @ptrCast(@alignCast(context));
1376
+ \\ return native_sdk.frontend.sourceFromEnv(self.env_map, .{
1377
+ \\ .dist =
1378
+ );
1379
+ try appendZigString(&out, allocator, frontend.distDir());
1380
+ try out.appendSlice(allocator,
1381
+ \\,
1382
+ \\ .entry = "index.html",
1383
+ \\ });
1384
+ \\ }
1385
+ \\};
1386
+ \\
1387
+ \\const dev_origins = [_][]const u8{ "zero://app", "zero://inline",
1388
+ );
1389
+ const dev_origin = try std.fmt.allocPrint(allocator, "http://127.0.0.1:{s}", .{frontend.devPort()});
1390
+ defer allocator.free(dev_origin);
1391
+ try out.appendSlice(allocator, " ");
1392
+ try appendZigString(&out, allocator, dev_origin);
1393
+ try out.appendSlice(allocator,
1394
+ \\ };
1395
+ \\
1396
+ \\pub fn main(init: std.process.Init) !void {
1397
+ \\ var app = App{ .env_map = init.environ_map };
1398
+ \\ try runner.runWithOptions(app.app(), .{
1399
+ \\ .app_name =
1400
+ );
1401
+ try appendZigString(&out, allocator, names.display_name);
1402
+ try out.appendSlice(allocator,
1403
+ \\,
1404
+ \\ .window_title =
1405
+ );
1406
+ try appendZigString(&out, allocator, names.display_name);
1407
+ try out.appendSlice(allocator,
1408
+ \\,
1409
+ \\ .bundle_id =
1410
+ );
1411
+ try appendZigString(&out, allocator, names.app_id);
1412
+ try out.appendSlice(allocator,
1413
+ \\,
1414
+ \\ .icon_path = "assets/icon.png",
1415
+ \\ .security = .{
1416
+ \\ .navigation = .{ .allowed_origins = &dev_origins },
1417
+ \\ },
1418
+ \\ }, init);
1419
+ \\}
1420
+ \\
1421
+ \\test "app name is configured" {
1422
+ \\ try std.testing.expectEqualStrings(
1423
+ );
1424
+ try appendZigString(&out, allocator, names.package_name);
1425
+ try out.appendSlice(allocator,
1426
+ \\,
1427
+ );
1428
+ try appendZigString(&out, allocator, names.package_name);
1429
+ try out.appendSlice(allocator,
1430
+ \\);
1431
+ \\}
1432
+ \\
1433
+ );
1434
+ return out.toOwnedSlice(allocator);
1435
+ }
1436
+
1437
+ fn runnerZig() []const u8 {
1438
+ return
1439
+ \\const std = @import("std");
1440
+ \\const build_options = @import("build_options");
1441
+ \\const native_sdk = @import("native_sdk");
1442
+ \\const app_manifest = @import("app_manifest_zon");
1443
+ \\const manifest_commands = if (@hasField(@TypeOf(app_manifest), "commands")) app_manifest.commands else .{};
1444
+ \\const manifest_shortcuts = if (@hasField(@TypeOf(app_manifest), "shortcuts")) app_manifest.shortcuts else .{};
1445
+ \\const manifest_menus = if (@hasField(@TypeOf(app_manifest), "menus")) app_manifest.menus else .{};
1446
+ \\const manifest_windows = if (@hasField(@TypeOf(app_manifest), "windows")) app_manifest.windows else .{};
1447
+ \\
1448
+ \\pub const StdoutTraceSink = struct {
1449
+ \\ pub fn sink(self: *StdoutTraceSink) native_sdk.trace.Sink {
1450
+ \\ return .{ .context = self, .write_fn = write };
1451
+ \\ }
1452
+ \\
1453
+ \\ fn write(context: *anyopaque, record: native_sdk.trace.Record) native_sdk.trace.WriteError!void {
1454
+ \\ _ = context;
1455
+ \\ if (!shouldTrace(record)) return;
1456
+ \\ // Never fail on an oversized record: logging failures must
1457
+ \\ // degrade (truncated output), not fail dispatch upstream.
1458
+ \\ var buffer: [4096]u8 = undefined;
1459
+ \\ std.debug.print("{s}\n", .{native_sdk.trace.formatTextBounded(record, &buffer)});
1460
+ \\ }
1461
+ \\};
1462
+ \\
1463
+ \\pub const RunOptions = struct {
1464
+ \\ app_name: []const u8,
1465
+ \\ window_title: []const u8 = "",
1466
+ \\ bundle_id: []const u8,
1467
+ \\ icon_path: []const u8 = "assets/icon.png",
1468
+ \\ bridge: ?native_sdk.BridgeDispatcher = null,
1469
+ \\ builtin_bridge: native_sdk.BridgePolicy = .{},
1470
+ \\ security: native_sdk.SecurityPolicy = .{},
1471
+ \\ js_window_api: bool = false,
1472
+ \\ commands: ?[]const native_sdk.Command = null,
1473
+ \\ menus: ?[]const native_sdk.Menu = null,
1474
+ \\ shortcuts: ?[]const native_sdk.Shortcut = null,
1475
+ \\
1476
+ \\ fn appInfo(self: RunOptions, buffers: *StateBuffers) native_sdk.AppInfo {
1477
+ \\ var info: native_sdk.AppInfo = .{
1478
+ \\ .app_name = self.app_name,
1479
+ \\ .window_title = self.window_title,
1480
+ \\ .bundle_id = self.bundle_id,
1481
+ \\ .icon_path = self.icon_path,
1482
+ \\ };
1483
+ \\ const windows = manifestWindowOptions(buffers);
1484
+ \\ if (windows.len > 0) {
1485
+ \\ info.main_window = windows[0];
1486
+ \\ info.windows = windows;
1487
+ \\ }
1488
+ \\ return info;
1489
+ \\ }
1490
+ \\
1491
+ \\ fn resolvedShortcuts(self: RunOptions, storage: *ShortcutStorage) []const native_sdk.Shortcut {
1492
+ \\ return self.shortcuts orelse storage.fromManifest();
1493
+ \\ }
1494
+ \\
1495
+ \\ fn resolvedCommands(self: RunOptions, storage: *CommandStorage) []const native_sdk.Command {
1496
+ \\ return self.commands orelse storage.fromManifest();
1497
+ \\ }
1498
+ \\
1499
+ \\ fn resolvedMenus(self: RunOptions, storage: *MenuStorage) []const native_sdk.Menu {
1500
+ \\ return self.menus orelse storage.fromManifest();
1501
+ \\ }
1502
+ \\};
1503
+ \\
1504
+ \\const CommandStorage = struct {
1505
+ \\ commands: [native_sdk.app_manifest.max_commands]native_sdk.Command = undefined,
1506
+ \\
1507
+ \\ fn fromManifest(self: *CommandStorage) []const native_sdk.Command {
1508
+ \\ comptime {
1509
+ \\ if (manifest_commands.len > native_sdk.app_manifest.max_commands) {
1510
+ \\ @compileError("app.zon defines too many commands");
1511
+ \\ }
1512
+ \\ }
1513
+ \\
1514
+ \\ inline for (manifest_commands, 0..) |command, index| {
1515
+ \\ self.commands[index] = .{
1516
+ \\ .id = command.id,
1517
+ \\ .title = if (@hasField(@TypeOf(command), "title")) command.title else "",
1518
+ \\ .enabled = if (@hasField(@TypeOf(command), "enabled")) command.enabled else true,
1519
+ \\ .checked = if (@hasField(@TypeOf(command), "checked")) command.checked else false,
1520
+ \\ };
1521
+ \\ }
1522
+ \\ return self.commands[0..manifest_commands.len];
1523
+ \\ }
1524
+ \\};
1525
+ \\
1526
+ \\const MenuStorage = struct {
1527
+ \\ menus: [native_sdk.platform.max_menus]native_sdk.Menu = undefined,
1528
+ \\ items: [native_sdk.platform.max_menu_items]native_sdk.MenuItem = undefined,
1529
+ \\
1530
+ \\ fn fromManifest(self: *MenuStorage) []const native_sdk.Menu {
1531
+ \\ comptime {
1532
+ \\ if (manifest_menus.len > native_sdk.platform.max_menus) {
1533
+ \\ @compileError("app.zon defines too many menus");
1534
+ \\ }
1535
+ \\ var item_count: usize = 0;
1536
+ \\ for (manifest_menus) |menu| {
1537
+ \\ const items = if (@hasField(@TypeOf(menu), "items")) menu.items else .{};
1538
+ \\ item_count += items.len;
1539
+ \\ }
1540
+ \\ if (item_count > native_sdk.platform.max_menu_items) {
1541
+ \\ @compileError("app.zon defines too many menu items");
1542
+ \\ }
1543
+ \\ }
1544
+ \\
1545
+ \\ var item_index: usize = 0;
1546
+ \\ inline for (manifest_menus, 0..) |menu, menu_index| {
1547
+ \\ const items = if (@hasField(@TypeOf(menu), "items")) menu.items else .{};
1548
+ \\ const first_item = item_index;
1549
+ \\ inline for (items) |item| {
1550
+ \\ self.items[item_index] = menuItem(item);
1551
+ \\ item_index += 1;
1552
+ \\ }
1553
+ \\ self.menus[menu_index] = .{
1554
+ \\ .title = menu.title,
1555
+ \\ .items = self.items[first_item..item_index],
1556
+ \\ };
1557
+ \\ }
1558
+ \\ return self.menus[0..manifest_menus.len];
1559
+ \\ }
1560
+ \\};
1561
+ \\
1562
+ \\const ShortcutStorage = struct {
1563
+ \\ shortcuts: [native_sdk.platform.max_shortcuts]native_sdk.Shortcut = undefined,
1564
+ \\
1565
+ \\ fn fromManifest(self: *ShortcutStorage) []const native_sdk.Shortcut {
1566
+ \\ comptime {
1567
+ \\ if (manifest_shortcuts.len > native_sdk.platform.max_shortcuts) {
1568
+ \\ @compileError("app.zon defines too many shortcuts");
1569
+ \\ }
1570
+ \\ }
1571
+ \\
1572
+ \\ inline for (manifest_shortcuts, 0..) |shortcut, index| {
1573
+ \\ self.shortcuts[index] = .{
1574
+ \\ .id = shortcut.id,
1575
+ \\ .key = shortcut.key,
1576
+ \\ .modifiers = shortcutModifiers(shortcut),
1577
+ \\ };
1578
+ \\ }
1579
+ \\ return self.shortcuts[0..manifest_shortcuts.len];
1580
+ \\ }
1581
+ \\};
1582
+ \\
1583
+ \\fn manifestWindowOptions(buffers: *StateBuffers) []const native_sdk.WindowOptions {
1584
+ \\ comptime {
1585
+ \\ if (manifest_windows.len > native_sdk.platform.max_windows) {
1586
+ \\ @compileError("app.zon defines too many windows");
1587
+ \\ }
1588
+ \\ }
1589
+ \\
1590
+ \\ inline for (manifest_windows, 0..) |window, index| {
1591
+ \\ buffers.restored_windows[index] = manifestWindow(window, index);
1592
+ \\ }
1593
+ \\ return buffers.restored_windows[0..manifest_windows.len];
1594
+ \\}
1595
+ \\
1596
+ \\fn manifestWindow(comptime window: anytype, comptime index: usize) native_sdk.WindowOptions {
1597
+ \\ return .{
1598
+ \\ .id = index + 1,
1599
+ \\ .label = windowLabel(window, index),
1600
+ \\ .title = windowTitle(window),
1601
+ \\ .default_frame = native_sdk.geometry.RectF.init(
1602
+ \\ windowFloat(window, "x", 0),
1603
+ \\ windowFloat(window, "y", 0),
1604
+ \\ windowFloat(window, "width", 720),
1605
+ \\ windowFloat(window, "height", 480),
1606
+ \\ ),
1607
+ \\ .resizable = windowBool(window, "resizable", true),
1608
+ \\ .restore_state = windowBool(window, "restore_state", true),
1609
+ \\ .restore_policy = windowRestorePolicy(window),
1610
+ \\ };
1611
+ \\}
1612
+ \\
1613
+ \\fn windowLabel(comptime window: anytype, comptime index: usize) []const u8 {
1614
+ \\ if (comptime @hasField(@TypeOf(window), "label")) return window.label;
1615
+ \\ return if (index == 0) "main" else "window";
1616
+ \\}
1617
+ \\
1618
+ \\fn windowTitle(comptime window: anytype) []const u8 {
1619
+ \\ if (comptime !@hasField(@TypeOf(window), "title")) return "";
1620
+ \\ const title = window.title;
1621
+ \\ if (comptime @TypeOf(title) == @TypeOf(null)) return "";
1622
+ \\ return title;
1623
+ \\}
1624
+ \\
1625
+ \\fn windowFloat(comptime window: anytype, comptime field: []const u8, comptime default_value: f32) f32 {
1626
+ \\ if (comptime @hasField(@TypeOf(window), field)) return @field(window, field);
1627
+ \\ return default_value;
1628
+ \\}
1629
+ \\
1630
+ \\fn windowBool(comptime window: anytype, comptime field: []const u8, comptime default_value: bool) bool {
1631
+ \\ if (comptime @hasField(@TypeOf(window), field)) return @field(window, field);
1632
+ \\ return default_value;
1633
+ \\}
1634
+ \\
1635
+ \\fn windowRestorePolicy(comptime window: anytype) native_sdk.WindowRestorePolicy {
1636
+ \\ if (comptime !@hasField(@TypeOf(window), "restore_policy")) return .clamp_to_visible_screen;
1637
+ \\ const value = window.restore_policy;
1638
+ \\ if (comptime std.mem.eql(u8, value, "clamp_to_visible_screen")) return .clamp_to_visible_screen;
1639
+ \\ if (comptime std.mem.eql(u8, value, "center_on_primary")) return .center_on_primary;
1640
+ \\ @compileError("unknown app.zon window restore_policy");
1641
+ \\}
1642
+ \\
1643
+ \\fn menuItem(comptime item: anytype) native_sdk.MenuItem {
1644
+ \\ return .{
1645
+ \\ .label = if (@hasField(@TypeOf(item), "label")) item.label else "",
1646
+ \\ .command = if (@hasField(@TypeOf(item), "command")) item.command else "",
1647
+ \\ .key = if (@hasField(@TypeOf(item), "key")) item.key else "",
1648
+ \\ .modifiers = shortcutModifiers(item),
1649
+ \\ .separator = if (@hasField(@TypeOf(item), "separator")) item.separator else false,
1650
+ \\ .enabled = if (@hasField(@TypeOf(item), "enabled")) item.enabled else true,
1651
+ \\ .checked = if (@hasField(@TypeOf(item), "checked")) item.checked else false,
1652
+ \\ };
1653
+ \\}
1654
+ \\
1655
+ \\fn shortcutModifiers(comptime shortcut: anytype) native_sdk.ShortcutModifiers {
1656
+ \\ const values = if (@hasField(@TypeOf(shortcut), "modifiers")) shortcut.modifiers else .{};
1657
+ \\ var modifiers: native_sdk.ShortcutModifiers = .{};
1658
+ \\ inline for (values) |value| {
1659
+ \\ const modifier: []const u8 = value;
1660
+ \\ if (comptime std.mem.eql(u8, modifier, "primary")) {
1661
+ \\ modifiers.primary = true;
1662
+ \\ } else if (comptime std.mem.eql(u8, modifier, "command")) {
1663
+ \\ modifiers.command = true;
1664
+ \\ } else if (comptime std.mem.eql(u8, modifier, "control")) {
1665
+ \\ modifiers.control = true;
1666
+ \\ } else if (comptime std.mem.eql(u8, modifier, "option") or std.mem.eql(u8, modifier, "alt")) {
1667
+ \\ modifiers.option = true;
1668
+ \\ } else if (comptime std.mem.eql(u8, modifier, "shift")) {
1669
+ \\ modifiers.shift = true;
1670
+ \\ } else {
1671
+ \\ @compileError("unknown app.zon shortcut modifier");
1672
+ \\ }
1673
+ \\ }
1674
+ \\ return modifiers;
1675
+ \\}
1676
+ \\
1677
+ \\pub fn runWithOptions(app: native_sdk.App, options: RunOptions, init: std.process.Init) !void {
1678
+ \\ if (build_options.debug_overlay) {
1679
+ \\ std.debug.print("debug-overlay=true backend={s} web-engine={s} trace={s}\n", .{ build_options.platform, build_options.web_engine, build_options.trace });
1680
+ \\ }
1681
+ \\ if (comptime std.mem.eql(u8, build_options.platform, "macos")) {
1682
+ \\ try runMacos(app, options, init);
1683
+ \\ } else if (comptime std.mem.eql(u8, build_options.platform, "linux")) {
1684
+ \\ try runLinux(app, options, init);
1685
+ \\ } else if (comptime std.mem.eql(u8, build_options.platform, "windows")) {
1686
+ \\ try runWindows(app, options, init);
1687
+ \\ } else {
1688
+ \\ try runNull(app, options, init);
1689
+ \\ }
1690
+ \\}
1691
+ \\
1692
+ \\fn runNull(app: native_sdk.App, options: RunOptions, init: std.process.Init) !void {
1693
+ \\ var buffers: StateBuffers = undefined;
1694
+ \\ var app_info = options.appInfo(&buffers);
1695
+ \\ const store = prepareStateStore(init.io, init.environ_map, &app_info, &buffers);
1696
+ \\ var null_platform = native_sdk.NullPlatform.initWithOptions(.{}, webEngine(), app_info);
1697
+ \\ var trace_sink = StdoutTraceSink{};
1698
+ \\ var log_buffers: native_sdk.debug.LogPathBuffers = .{};
1699
+ \\ const log_setup = native_sdk.debug.setupLogging(init.io, init.environ_map, app_info.bundle_id, &log_buffers) catch null;
1700
+ \\ if (log_setup) |setup| native_sdk.debug.installPanicCapture(init.io, setup.paths);
1701
+ \\ var file_trace_sink: native_sdk.debug.FileTraceSink = undefined;
1702
+ \\ var fanout_sinks: [2]native_sdk.trace.Sink = undefined;
1703
+ \\ var fanout_sink: native_sdk.debug.FanoutTraceSink = undefined;
1704
+ \\ var runtime_trace_sink = trace_sink.sink();
1705
+ \\ if (log_setup) |setup| {
1706
+ \\ file_trace_sink = native_sdk.debug.FileTraceSink.init(init.io, setup.paths.log_dir, setup.paths.log_file, setup.format);
1707
+ \\ fanout_sinks = .{ trace_sink.sink(), file_trace_sink.sink() };
1708
+ \\ fanout_sink = .{ .sinks = &fanout_sinks };
1709
+ \\ runtime_trace_sink = fanout_sink.sink();
1710
+ \\ }
1711
+ \\ var shortcut_storage: ShortcutStorage = .{};
1712
+ \\ const shortcuts = options.resolvedShortcuts(&shortcut_storage);
1713
+ \\ var menu_storage: MenuStorage = .{};
1714
+ \\ const menus = options.resolvedMenus(&menu_storage);
1715
+ \\ var command_storage: CommandStorage = .{};
1716
+ \\ const commands = options.resolvedCommands(&command_storage);
1717
+ \\ // The Runtime is multi-megabyte; default thread stacks overflow on a
1718
+ \\ // stack instance, so construct it on the heap.
1719
+ \\ const runtime = try std.heap.page_allocator.create(native_sdk.Runtime);
1720
+ \\ defer std.heap.page_allocator.destroy(runtime);
1721
+ \\ native_sdk.Runtime.initAt(runtime, .{
1722
+ \\ .platform = null_platform.platform(),
1723
+ \\ .trace_sink = runtime_trace_sink,
1724
+ \\ .log_path = if (log_setup) |setup| setup.paths.log_file else null,
1725
+ \\ .bridge = options.bridge,
1726
+ \\ .builtin_bridge = options.builtin_bridge,
1727
+ \\ .security = options.security,
1728
+ \\ .js_window_api = options.js_window_api,
1729
+ \\ .commands = commands,
1730
+ \\ .menus = menus,
1731
+ \\ .shortcuts = shortcuts,
1732
+ \\ .automation = if (build_options.automation) native_sdk.automation.Server.init(init.io, ".zig-cache/native-sdk-automation", app_info.resolvedWindowTitle()) else null,
1733
+ \\ .window_state_store = store,
1734
+ \\ .environ = init.minimal.environ,
1735
+ \\ });
1736
+ \\
1737
+ \\ try runtime.run(app);
1738
+ \\}
1739
+ \\
1740
+ \\fn runMacos(app: native_sdk.App, options: RunOptions, init: std.process.Init) !void {
1741
+ \\ var buffers: StateBuffers = undefined;
1742
+ \\ var app_info = options.appInfo(&buffers);
1743
+ \\ const store = prepareStateStore(init.io, init.environ_map, &app_info, &buffers);
1744
+ \\ var mac_platform = try native_sdk.platform.macos.MacPlatform.initWithOptions(native_sdk.geometry.SizeF.init(720, 480), webEngine(), app_info);
1745
+ \\ defer mac_platform.deinit();
1746
+ \\ var trace_sink = StdoutTraceSink{};
1747
+ \\ var log_buffers: native_sdk.debug.LogPathBuffers = .{};
1748
+ \\ const log_setup = native_sdk.debug.setupLogging(init.io, init.environ_map, app_info.bundle_id, &log_buffers) catch null;
1749
+ \\ if (log_setup) |setup| native_sdk.debug.installPanicCapture(init.io, setup.paths);
1750
+ \\ var file_trace_sink: native_sdk.debug.FileTraceSink = undefined;
1751
+ \\ var fanout_sinks: [2]native_sdk.trace.Sink = undefined;
1752
+ \\ var fanout_sink: native_sdk.debug.FanoutTraceSink = undefined;
1753
+ \\ var runtime_trace_sink = trace_sink.sink();
1754
+ \\ if (log_setup) |setup| {
1755
+ \\ file_trace_sink = native_sdk.debug.FileTraceSink.init(init.io, setup.paths.log_dir, setup.paths.log_file, setup.format);
1756
+ \\ fanout_sinks = .{ trace_sink.sink(), file_trace_sink.sink() };
1757
+ \\ fanout_sink = .{ .sinks = &fanout_sinks };
1758
+ \\ runtime_trace_sink = fanout_sink.sink();
1759
+ \\ }
1760
+ \\ var shortcut_storage: ShortcutStorage = .{};
1761
+ \\ const shortcuts = options.resolvedShortcuts(&shortcut_storage);
1762
+ \\ var menu_storage: MenuStorage = .{};
1763
+ \\ const menus = options.resolvedMenus(&menu_storage);
1764
+ \\ var command_storage: CommandStorage = .{};
1765
+ \\ const commands = options.resolvedCommands(&command_storage);
1766
+ \\ // The Runtime is multi-megabyte; default thread stacks overflow on a
1767
+ \\ // stack instance, so construct it on the heap.
1768
+ \\ const runtime = try std.heap.page_allocator.create(native_sdk.Runtime);
1769
+ \\ defer std.heap.page_allocator.destroy(runtime);
1770
+ \\ native_sdk.Runtime.initAt(runtime, .{
1771
+ \\ .platform = mac_platform.platform(),
1772
+ \\ .trace_sink = runtime_trace_sink,
1773
+ \\ .log_path = if (log_setup) |setup| setup.paths.log_file else null,
1774
+ \\ .bridge = options.bridge,
1775
+ \\ .builtin_bridge = options.builtin_bridge,
1776
+ \\ .security = options.security,
1777
+ \\ .js_window_api = options.js_window_api,
1778
+ \\ .commands = commands,
1779
+ \\ .menus = menus,
1780
+ \\ .shortcuts = shortcuts,
1781
+ \\ .automation = if (build_options.automation) native_sdk.automation.Server.init(init.io, ".zig-cache/native-sdk-automation", app_info.resolvedWindowTitle()) else null,
1782
+ \\ .window_state_store = store,
1783
+ \\ .environ = init.minimal.environ,
1784
+ \\ });
1785
+ \\
1786
+ \\ try runtime.run(app);
1787
+ \\}
1788
+ \\
1789
+ \\fn runLinux(app: native_sdk.App, options: RunOptions, init: std.process.Init) !void {
1790
+ \\ var buffers: StateBuffers = undefined;
1791
+ \\ var app_info = options.appInfo(&buffers);
1792
+ \\ const store = prepareStateStore(init.io, init.environ_map, &app_info, &buffers);
1793
+ \\ var linux_platform = try native_sdk.platform.linux.LinuxPlatform.initWithOptions(native_sdk.geometry.SizeF.init(720, 480), webEngine(), app_info);
1794
+ \\ defer linux_platform.deinit();
1795
+ \\ var trace_sink = StdoutTraceSink{};
1796
+ \\ var log_buffers: native_sdk.debug.LogPathBuffers = .{};
1797
+ \\ const log_setup = native_sdk.debug.setupLogging(init.io, init.environ_map, app_info.bundle_id, &log_buffers) catch null;
1798
+ \\ if (log_setup) |setup| native_sdk.debug.installPanicCapture(init.io, setup.paths);
1799
+ \\ var file_trace_sink: native_sdk.debug.FileTraceSink = undefined;
1800
+ \\ var fanout_sinks: [2]native_sdk.trace.Sink = undefined;
1801
+ \\ var fanout_sink: native_sdk.debug.FanoutTraceSink = undefined;
1802
+ \\ var runtime_trace_sink = trace_sink.sink();
1803
+ \\ if (log_setup) |setup| {
1804
+ \\ file_trace_sink = native_sdk.debug.FileTraceSink.init(init.io, setup.paths.log_dir, setup.paths.log_file, setup.format);
1805
+ \\ fanout_sinks = .{ trace_sink.sink(), file_trace_sink.sink() };
1806
+ \\ fanout_sink = .{ .sinks = &fanout_sinks };
1807
+ \\ runtime_trace_sink = fanout_sink.sink();
1808
+ \\ }
1809
+ \\ var shortcut_storage: ShortcutStorage = .{};
1810
+ \\ const shortcuts = options.resolvedShortcuts(&shortcut_storage);
1811
+ \\ var menu_storage: MenuStorage = .{};
1812
+ \\ const menus = options.resolvedMenus(&menu_storage);
1813
+ \\ var command_storage: CommandStorage = .{};
1814
+ \\ const commands = options.resolvedCommands(&command_storage);
1815
+ \\ // The Runtime is multi-megabyte; default thread stacks overflow on a
1816
+ \\ // stack instance, so construct it on the heap.
1817
+ \\ const runtime = try std.heap.page_allocator.create(native_sdk.Runtime);
1818
+ \\ defer std.heap.page_allocator.destroy(runtime);
1819
+ \\ native_sdk.Runtime.initAt(runtime, .{
1820
+ \\ .platform = linux_platform.platform(),
1821
+ \\ .trace_sink = runtime_trace_sink,
1822
+ \\ .log_path = if (log_setup) |setup| setup.paths.log_file else null,
1823
+ \\ .bridge = options.bridge,
1824
+ \\ .builtin_bridge = options.builtin_bridge,
1825
+ \\ .security = options.security,
1826
+ \\ .js_window_api = options.js_window_api,
1827
+ \\ .commands = commands,
1828
+ \\ .menus = menus,
1829
+ \\ .shortcuts = shortcuts,
1830
+ \\ .automation = if (build_options.automation) native_sdk.automation.Server.init(init.io, ".zig-cache/native-sdk-automation", app_info.resolvedWindowTitle()) else null,
1831
+ \\ .window_state_store = store,
1832
+ \\ .environ = init.minimal.environ,
1833
+ \\ });
1834
+ \\
1835
+ \\ try runtime.run(app);
1836
+ \\}
1837
+ \\
1838
+ \\fn runWindows(app: native_sdk.App, options: RunOptions, init: std.process.Init) !void {
1839
+ \\ var buffers: StateBuffers = undefined;
1840
+ \\ var app_info = options.appInfo(&buffers);
1841
+ \\ const store = prepareStateStore(init.io, init.environ_map, &app_info, &buffers);
1842
+ \\ var windows_platform = try native_sdk.platform.windows.WindowsPlatform.initWithOptions(native_sdk.geometry.SizeF.init(720, 480), webEngine(), app_info);
1843
+ \\ defer windows_platform.deinit();
1844
+ \\ var trace_sink = StdoutTraceSink{};
1845
+ \\ var log_buffers: native_sdk.debug.LogPathBuffers = .{};
1846
+ \\ const log_setup = native_sdk.debug.setupLogging(init.io, init.environ_map, app_info.bundle_id, &log_buffers) catch null;
1847
+ \\ if (log_setup) |setup| native_sdk.debug.installPanicCapture(init.io, setup.paths);
1848
+ \\ var file_trace_sink: native_sdk.debug.FileTraceSink = undefined;
1849
+ \\ var fanout_sinks: [2]native_sdk.trace.Sink = undefined;
1850
+ \\ var fanout_sink: native_sdk.debug.FanoutTraceSink = undefined;
1851
+ \\ var runtime_trace_sink = trace_sink.sink();
1852
+ \\ if (log_setup) |setup| {
1853
+ \\ file_trace_sink = native_sdk.debug.FileTraceSink.init(init.io, setup.paths.log_dir, setup.paths.log_file, setup.format);
1854
+ \\ fanout_sinks = .{ trace_sink.sink(), file_trace_sink.sink() };
1855
+ \\ fanout_sink = .{ .sinks = &fanout_sinks };
1856
+ \\ runtime_trace_sink = fanout_sink.sink();
1857
+ \\ }
1858
+ \\ var shortcut_storage: ShortcutStorage = .{};
1859
+ \\ const shortcuts = options.resolvedShortcuts(&shortcut_storage);
1860
+ \\ var menu_storage: MenuStorage = .{};
1861
+ \\ const menus = options.resolvedMenus(&menu_storage);
1862
+ \\ var command_storage: CommandStorage = .{};
1863
+ \\ const commands = options.resolvedCommands(&command_storage);
1864
+ \\ // The Runtime is multi-megabyte; default thread stacks overflow on a
1865
+ \\ // stack instance, so construct it on the heap.
1866
+ \\ const runtime = try std.heap.page_allocator.create(native_sdk.Runtime);
1867
+ \\ defer std.heap.page_allocator.destroy(runtime);
1868
+ \\ native_sdk.Runtime.initAt(runtime, .{
1869
+ \\ .platform = windows_platform.platform(),
1870
+ \\ .trace_sink = runtime_trace_sink,
1871
+ \\ .log_path = if (log_setup) |setup| setup.paths.log_file else null,
1872
+ \\ .bridge = options.bridge,
1873
+ \\ .builtin_bridge = options.builtin_bridge,
1874
+ \\ .security = options.security,
1875
+ \\ .js_window_api = options.js_window_api,
1876
+ \\ .commands = commands,
1877
+ \\ .menus = menus,
1878
+ \\ .shortcuts = shortcuts,
1879
+ \\ .automation = if (build_options.automation) native_sdk.automation.Server.init(init.io, ".zig-cache/native-sdk-automation", app_info.resolvedWindowTitle()) else null,
1880
+ \\ .window_state_store = store,
1881
+ \\ .environ = init.minimal.environ,
1882
+ \\ });
1883
+ \\
1884
+ \\ try runtime.run(app);
1885
+ \\}
1886
+ \\
1887
+ \\fn shouldTrace(record: native_sdk.trace.Record) bool {
1888
+ \\ if (comptime std.mem.eql(u8, build_options.trace, "off")) return false;
1889
+ \\ if (comptime std.mem.eql(u8, build_options.trace, "all")) return true;
1890
+ \\ if (comptime std.mem.eql(u8, build_options.trace, "events")) return true;
1891
+ \\ return std.mem.indexOf(u8, record.name, build_options.trace) != null;
1892
+ \\}
1893
+ \\
1894
+ \\fn webEngine() native_sdk.WebEngine {
1895
+ \\ if (comptime std.mem.eql(u8, build_options.web_engine, "chromium")) return .chromium;
1896
+ \\ return .system;
1897
+ \\}
1898
+ \\
1899
+ \\const StateBuffers = struct {
1900
+ \\ state_dir: [1024]u8 = undefined,
1901
+ \\ file_path: [1200]u8 = undefined,
1902
+ \\ read: [8192]u8 = undefined,
1903
+ \\ restored_windows: [native_sdk.platform.max_windows]native_sdk.WindowOptions = undefined,
1904
+ \\};
1905
+ \\
1906
+ \\fn prepareStateStore(io: std.Io, env_map: *std.process.Environ.Map, app_info: *native_sdk.AppInfo, buffers: *StateBuffers) ?native_sdk.window_state.Store {
1907
+ \\ const paths = native_sdk.window_state.defaultPaths(&buffers.state_dir, &buffers.file_path, app_info.bundle_id, native_sdk.debug.envFromMap(env_map)) catch return null;
1908
+ \\ const store = native_sdk.window_state.Store.init(io, paths.state_dir, paths.file_path);
1909
+ \\ if (app_info.windows.len > 0) {
1910
+ \\ const restored_windows = buffers.restored_windows[0..app_info.windows.len];
1911
+ \\ for (restored_windows, 0..) |*window, index| {
1912
+ \\ if (!window.restore_state) continue;
1913
+ \\ if (store.loadWindow(window.label, &buffers.read) catch null) |saved| {
1914
+ \\ window.default_frame = saved.frame;
1915
+ \\ if (index == 0) app_info.main_window.default_frame = saved.frame;
1916
+ \\ }
1917
+ \\ }
1918
+ \\ } else if (app_info.main_window.restore_state) {
1919
+ \\ if (store.loadWindow(app_info.main_window.label, &buffers.read) catch null) |saved| {
1920
+ \\ app_info.main_window.default_frame = saved.frame;
1921
+ \\ }
1922
+ \\ }
1923
+ \\ return store;
1924
+ \\}
1925
+ \\
1926
+ ;
1927
+ }
1928
+
1929
+ fn appZon(allocator: std.mem.Allocator, names: TemplateNames, frontend: Frontend) ![]const u8 {
1930
+ var out: std.ArrayList(u8) = .empty;
1931
+ errdefer out.deinit(allocator);
1932
+ try out.appendSlice(allocator,
1933
+ \\.{
1934
+ \\ .id =
1935
+ );
1936
+ try appendZigString(&out, allocator, names.app_id);
1937
+ try out.appendSlice(allocator,
1938
+ \\,
1939
+ \\ .name =
1940
+ );
1941
+ try appendZigString(&out, allocator, names.package_name);
1942
+ try out.appendSlice(allocator,
1943
+ \\,
1944
+ \\ .display_name =
1945
+ );
1946
+ try appendZigString(&out, allocator, names.display_name);
1947
+ try out.appendSlice(allocator,
1948
+ \\,
1949
+ \\ .version = "0.1.0",
1950
+ \\ .icons = .{ "assets/icon.png" },
1951
+ \\ .platforms = .{ "macos", "linux" },
1952
+ \\ .permissions = .{},
1953
+ \\ .capabilities = .{ "webview" },
1954
+ \\ .frontend = .{
1955
+ \\ .dist =
1956
+ );
1957
+ try appendZigString(&out, allocator, frontend.distDir());
1958
+ try out.appendSlice(allocator,
1959
+ \\,
1960
+ \\ .entry = "index.html",
1961
+ \\ .spa_fallback = true,
1962
+ \\ .dev = .{
1963
+ \\ .url =
1964
+ );
1965
+ try appendZigString(&out, allocator, frontend.devUrl());
1966
+ try out.appendSlice(allocator,
1967
+ \\,
1968
+ \\ .command = .{ "npm", "--prefix", "frontend", "run", "dev"
1969
+ );
1970
+ if (frontend != .next) {
1971
+ try out.appendSlice(allocator,
1972
+ \\, "--", "--host", "127.0.0.1"
1973
+ );
1974
+ }
1975
+ try out.appendSlice(allocator,
1976
+ \\ },
1977
+ \\ .ready_path = "/",
1978
+ \\ .timeout_ms = 30000,
1979
+ \\ },
1980
+ \\ },
1981
+ \\ .security = .{
1982
+ \\ .navigation = .{
1983
+ \\ .allowed_origins = .{ "zero://app", "zero://inline",
1984
+ );
1985
+ try out.appendSlice(allocator, " ");
1986
+ const dev_origin = try std.fmt.allocPrint(allocator, "http://127.0.0.1:{s}", .{frontend.devPort()});
1987
+ defer allocator.free(dev_origin);
1988
+ try appendZigString(&out, allocator, dev_origin);
1989
+ try out.appendSlice(allocator,
1990
+ \\ },
1991
+ \\ .external_links = .{ .action = "deny" },
1992
+ \\ },
1993
+ \\ },
1994
+ \\ .web_engine = "system",
1995
+ \\ .cef = .{ .dir = "third_party/cef/macos", .auto_install = false },
1996
+ \\ .windows = .{
1997
+ \\ .{ .label = "main", .title =
1998
+ );
1999
+ try appendZigString(&out, allocator, names.display_name);
2000
+ try out.appendSlice(allocator,
2001
+ \\, .width = 720, .height = 480, .restore_state = true },
2002
+ \\ },
2003
+ \\}
2004
+ \\
2005
+ );
2006
+ return out.toOwnedSlice(allocator);
2007
+ }
2008
+
2009
+ fn writeFrontendFiles(allocator: std.mem.Allocator, io: std.Io, app_dir: std.Io.Dir, names: TemplateNames, frontend: Frontend) !void {
2010
+ switch (frontend) {
2011
+ .next => try writeNextFrontend(allocator, io, app_dir, names),
2012
+ .vite => try writeViteFrontend(allocator, io, app_dir, names),
2013
+ .react => try writeReactFrontend(allocator, io, app_dir, names),
2014
+ .svelte => try writeSvelteFrontend(allocator, io, app_dir, names),
2015
+ .vue => try writeVueFrontend(allocator, io, app_dir, names),
2016
+ // Native apps never reach here: writeDefaultApp dispatches to
2017
+ // writeNativeApp before any frontend files are written.
2018
+ .native => unreachable,
2019
+ }
2020
+ }
2021
+
2022
+ fn writeNextFrontend(allocator: std.mem.Allocator, io: std.Io, app_dir: std.Io.Dir, names: TemplateNames) !void {
2023
+ try app_dir.createDirPath(io, "frontend/app");
2024
+ const package_json = try nextPackageJson(allocator, names);
2025
+ defer allocator.free(package_json);
2026
+ try writeFile(app_dir, io, "frontend/package.json", package_json);
2027
+ try writeFile(app_dir, io, "frontend/next.config.js", nextConfig());
2028
+ try writeFile(app_dir, io, "frontend/tsconfig.json", nextTsconfig());
2029
+ const layout = try nextLayout(allocator, names);
2030
+ defer allocator.free(layout);
2031
+ try writeFile(app_dir, io, "frontend/app/layout.tsx", layout);
2032
+ const page = try nextPage(allocator, names);
2033
+ defer allocator.free(page);
2034
+ try writeFile(app_dir, io, "frontend/app/page.tsx", page);
2035
+ try writeFile(app_dir, io, "frontend/app/globals.css", frontendStylesCss());
2036
+ }
2037
+
2038
+ fn writeViteFrontend(allocator: std.mem.Allocator, io: std.Io, app_dir: std.Io.Dir, names: TemplateNames) !void {
2039
+ try app_dir.createDirPath(io, "frontend/src");
2040
+ const package_json = try vitePackageJson(allocator, names);
2041
+ defer allocator.free(package_json);
2042
+ const index_html = try viteIndexHtml(allocator, names);
2043
+ defer allocator.free(index_html);
2044
+ try writeFile(app_dir, io, "frontend/package.json", package_json);
2045
+ try writeFile(app_dir, io, "frontend/index.html", index_html);
2046
+ try writeFile(app_dir, io, "frontend/src/main.js", viteMainJs());
2047
+ try writeFile(app_dir, io, "frontend/src/styles.css", frontendStylesCss());
2048
+ }
2049
+
2050
+ fn writeReactFrontend(allocator: std.mem.Allocator, io: std.Io, app_dir: std.Io.Dir, names: TemplateNames) !void {
2051
+ try app_dir.createDirPath(io, "frontend/src");
2052
+ const package_json = try reactPackageJson(allocator, names);
2053
+ defer allocator.free(package_json);
2054
+ const index_html = try reactIndexHtml(allocator, names);
2055
+ defer allocator.free(index_html);
2056
+ const app_tsx = try reactAppTsx(allocator, names);
2057
+ defer allocator.free(app_tsx);
2058
+ try writeFile(app_dir, io, "frontend/package.json", package_json);
2059
+ try writeFile(app_dir, io, "frontend/vite.config.js", reactViteConfig());
2060
+ try writeFile(app_dir, io, "frontend/index.html", index_html);
2061
+ try writeFile(app_dir, io, "frontend/src/main.tsx", reactMainTsx());
2062
+ try writeFile(app_dir, io, "frontend/src/App.tsx", app_tsx);
2063
+ try writeFile(app_dir, io, "frontend/src/index.css", frontendStylesCss());
2064
+ }
2065
+
2066
+ fn writeSvelteFrontend(allocator: std.mem.Allocator, io: std.Io, app_dir: std.Io.Dir, names: TemplateNames) !void {
2067
+ try app_dir.createDirPath(io, "frontend/src");
2068
+ const package_json = try sveltePackageJson(allocator, names);
2069
+ defer allocator.free(package_json);
2070
+ const index_html = try svelteIndexHtml(allocator, names);
2071
+ defer allocator.free(index_html);
2072
+ try writeFile(app_dir, io, "frontend/package.json", package_json);
2073
+ try writeFile(app_dir, io, "frontend/svelte.config.js", svelteConfig());
2074
+ try writeFile(app_dir, io, "frontend/vite.config.js", svelteViteConfig());
2075
+ try writeFile(app_dir, io, "frontend/index.html", index_html);
2076
+ try writeFile(app_dir, io, "frontend/src/main.js", svelteMainJs());
2077
+ try writeFile(app_dir, io, "frontend/src/App.svelte", svelteAppComponent(names));
2078
+ try writeFile(app_dir, io, "frontend/src/app.css", frontendStylesCss());
2079
+ }
2080
+
2081
+ fn writeVueFrontend(allocator: std.mem.Allocator, io: std.Io, app_dir: std.Io.Dir, names: TemplateNames) !void {
2082
+ try app_dir.createDirPath(io, "frontend/src");
2083
+ const package_json = try vuePackageJson(allocator, names);
2084
+ defer allocator.free(package_json);
2085
+ const index_html = try vueIndexHtml(allocator, names);
2086
+ defer allocator.free(index_html);
2087
+ try writeFile(app_dir, io, "frontend/package.json", package_json);
2088
+ try writeFile(app_dir, io, "frontend/vite.config.js", vueViteConfig());
2089
+ try writeFile(app_dir, io, "frontend/index.html", index_html);
2090
+ try writeFile(app_dir, io, "frontend/src/main.js", vueMainJs());
2091
+ try writeFile(app_dir, io, "frontend/src/App.vue", vueAppComponent(names));
2092
+ try writeFile(app_dir, io, "frontend/src/style.css", frontendStylesCss());
2093
+ }
2094
+
2095
+ fn nextPackageJson(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
2096
+ var out: std.ArrayList(u8) = .empty;
2097
+ errdefer out.deinit(allocator);
2098
+ try out.appendSlice(allocator, "{\n \"name\": ");
2099
+ try appendJsonString(&out, allocator, names.package_name);
2100
+ try out.appendSlice(allocator,
2101
+ \\,
2102
+ \\ "private": true,
2103
+ \\ "version": "0.1.0",
2104
+ \\ "scripts": {
2105
+ \\ "dev": "next dev",
2106
+ \\ "build": "next build",
2107
+ \\ "start": "next start"
2108
+ \\ },
2109
+ \\ "dependencies": {
2110
+ \\ "next": "^16.2.6",
2111
+ \\ "react": "^19.2.6",
2112
+ \\ "react-dom": "^19.2.6"
2113
+ \\ },
2114
+ \\ "devDependencies": {
2115
+ \\ "@types/node": "^25.6.2",
2116
+ \\ "@types/react": "^19.2.14",
2117
+ \\ "@types/react-dom": "^19.2.3",
2118
+ \\ "typescript": "^6.0.3"
2119
+ \\ }
2120
+ \\}
2121
+ \\
2122
+ );
2123
+ return out.toOwnedSlice(allocator);
2124
+ }
2125
+
2126
+ fn nextConfig() []const u8 {
2127
+ return
2128
+ \\/** @type {import('next').NextConfig} */
2129
+ \\const nextConfig = {
2130
+ \\ output: "export",
2131
+ \\};
2132
+ \\
2133
+ \\module.exports = nextConfig;
2134
+ \\
2135
+ ;
2136
+ }
2137
+
2138
+ fn nextTsconfig() []const u8 {
2139
+ return
2140
+ \\{
2141
+ \\ "compilerOptions": {
2142
+ \\ "target": "ES2017",
2143
+ \\ "lib": ["dom", "dom.iterable", "esnext"],
2144
+ \\ "allowJs": true,
2145
+ \\ "skipLibCheck": true,
2146
+ \\ "strict": true,
2147
+ \\ "noEmit": true,
2148
+ \\ "esModuleInterop": true,
2149
+ \\ "module": "esnext",
2150
+ \\ "moduleResolution": "bundler",
2151
+ \\ "resolveJsonModule": true,
2152
+ \\ "isolatedModules": true,
2153
+ \\ "jsx": "react-jsx",
2154
+ \\ "incremental": true,
2155
+ \\ "plugins": [{ "name": "next" }],
2156
+ \\ "paths": { "@/*": ["./app/*"] }
2157
+ \\ },
2158
+ \\ "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", ".next/dev/types/**/*.ts"],
2159
+ \\ "exclude": ["node_modules"]
2160
+ \\}
2161
+ \\
2162
+ ;
2163
+ }
2164
+
2165
+ fn nextLayout(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
2166
+ var out: std.ArrayList(u8) = .empty;
2167
+ errdefer out.deinit(allocator);
2168
+ try out.appendSlice(allocator,
2169
+ \\import "./globals.css";
2170
+ \\
2171
+ \\export const metadata = {
2172
+ \\ title: "
2173
+ );
2174
+ try out.appendSlice(allocator, names.display_name);
2175
+ try out.appendSlice(allocator,
2176
+ \\",
2177
+ \\};
2178
+ \\
2179
+ \\export default function RootLayout({ children }: { children: React.ReactNode }) {
2180
+ \\ return (
2181
+ \\ <html lang="en">
2182
+ \\ <body>{children}</body>
2183
+ \\ </html>
2184
+ \\ );
2185
+ \\}
2186
+ \\
2187
+ );
2188
+ return out.toOwnedSlice(allocator);
2189
+ }
2190
+
2191
+ fn nextPage(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
2192
+ var out: std.ArrayList(u8) = .empty;
2193
+ errdefer out.deinit(allocator);
2194
+ try out.appendSlice(allocator,
2195
+ \\"use client";
2196
+ \\
2197
+ \\import { useEffect, useState } from "react";
2198
+ \\
2199
+ \\export default function Home() {
2200
+ \\ const [bridge, setBridge] = useState("checking...");
2201
+ \\
2202
+ \\ useEffect(() => {
2203
+ \\ setBridge((window as any).zero ? "available" : "not enabled");
2204
+ \\ }, []);
2205
+ \\
2206
+ \\ return (
2207
+ \\ <main>
2208
+ \\ <p className="eyebrow">Native SDK + Next.js</p>
2209
+ \\ <h1>
2210
+ );
2211
+ try out.appendSlice(allocator, names.display_name);
2212
+ try out.appendSlice(allocator,
2213
+ \\</h1>
2214
+ \\ <p className="lede">A Next.js frontend running inside the system WebView.</p>
2215
+ \\ <div className="card">
2216
+ \\ <span>Native bridge</span>
2217
+ \\ <strong>{bridge}</strong>
2218
+ \\ </div>
2219
+ \\ </main>
2220
+ \\ );
2221
+ \\}
2222
+ \\
2223
+ );
2224
+ return out.toOwnedSlice(allocator);
2225
+ }
2226
+
2227
+ fn vitePackageJson(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
2228
+ var out: std.ArrayList(u8) = .empty;
2229
+ errdefer out.deinit(allocator);
2230
+ try out.appendSlice(allocator, "{\n \"name\": ");
2231
+ try appendJsonString(&out, allocator, names.package_name);
2232
+ try out.appendSlice(allocator,
2233
+ \\,
2234
+ \\ "private": true,
2235
+ \\ "version": "0.1.0",
2236
+ \\ "type": "module",
2237
+ \\ "scripts": {
2238
+ \\ "dev": "vite",
2239
+ \\ "build": "vite build",
2240
+ \\ "preview": "vite preview"
2241
+ \\ },
2242
+ \\ "devDependencies": {
2243
+ \\ "vite": "^8.0.11"
2244
+ \\ }
2245
+ \\}
2246
+ \\
2247
+ );
2248
+ return out.toOwnedSlice(allocator);
2249
+ }
2250
+
2251
+ fn viteIndexHtml(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
2252
+ var out: std.ArrayList(u8) = .empty;
2253
+ errdefer out.deinit(allocator);
2254
+ try out.appendSlice(allocator,
2255
+ \\<!doctype html>
2256
+ \\<html lang="en">
2257
+ \\ <head>
2258
+ \\ <meta charset="UTF-8" />
2259
+ \\ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
2260
+ \\ <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self' http://127.0.0.1:5173 ws://127.0.0.1:5173" />
2261
+ \\ <title>
2262
+ );
2263
+ try out.appendSlice(allocator, names.display_name);
2264
+ try out.appendSlice(allocator,
2265
+ \\</title>
2266
+ \\ </head>
2267
+ \\ <body>
2268
+ \\ <main id="app">
2269
+ \\ <p class="eyebrow">Native SDK + Vite</p>
2270
+ \\ <h1>
2271
+ );
2272
+ try out.appendSlice(allocator, names.display_name);
2273
+ try out.appendSlice(allocator,
2274
+ \\</h1>
2275
+ \\ <p class="lede">A minimal web frontend running inside the system WebView.</p>
2276
+ \\ <div class="card">
2277
+ \\ <span>Native bridge</span>
2278
+ \\ <strong id="bridge-status">checking...</strong>
2279
+ \\ </div>
2280
+ \\ </main>
2281
+ \\ <script type="module" src="/src/main.js"></script>
2282
+ \\ </body>
2283
+ \\</html>
2284
+ \\
2285
+ );
2286
+ return out.toOwnedSlice(allocator);
2287
+ }
2288
+
2289
+ fn viteMainJs() []const u8 {
2290
+ return
2291
+ \\import "./styles.css";
2292
+ \\
2293
+ \\const bridgeStatus = document.querySelector("#bridge-status");
2294
+ \\const hasBridge = typeof window !== "undefined" && Boolean(window.zero);
2295
+ \\
2296
+ \\bridgeStatus.textContent = hasBridge ? "available" : "not enabled";
2297
+ \\bridgeStatus.dataset.ready = "true";
2298
+ \\
2299
+ ;
2300
+ }
2301
+
2302
+ fn frontendStylesCss() []const u8 {
2303
+ return
2304
+ \\:root {
2305
+ \\ color: #0f172a;
2306
+ \\ background: #f8fafc;
2307
+ \\ font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
2308
+ \\}
2309
+ \\
2310
+ \\body {
2311
+ \\ min-width: 320px;
2312
+ \\ min-height: 100vh;
2313
+ \\ margin: 0;
2314
+ \\ display: grid;
2315
+ \\ place-items: center;
2316
+ \\}
2317
+ \\
2318
+ \\main {
2319
+ \\ width: min(560px, calc(100vw - 48px));
2320
+ \\ padding: 32px;
2321
+ \\ border-radius: 24px;
2322
+ \\ background: white;
2323
+ \\ box-shadow: 0 24px 60px rgba(15, 23, 42, 0.14);
2324
+ \\}
2325
+ \\
2326
+ \\h1 {
2327
+ \\ margin: 0 0 12px;
2328
+ \\ font-size: clamp(2rem, 8vw, 4rem);
2329
+ \\ line-height: 1;
2330
+ \\}
2331
+ \\
2332
+ \\.eyebrow {
2333
+ \\ margin: 0 0 12px;
2334
+ \\ color: #2563eb;
2335
+ \\ font-weight: 700;
2336
+ \\ letter-spacing: 0.08em;
2337
+ \\ text-transform: uppercase;
2338
+ \\}
2339
+ \\
2340
+ \\.lede {
2341
+ \\ margin: 0 0 24px;
2342
+ \\ color: #475569;
2343
+ \\ line-height: 1.6;
2344
+ \\}
2345
+ \\
2346
+ \\.card {
2347
+ \\ display: flex;
2348
+ \\ align-items: center;
2349
+ \\ justify-content: space-between;
2350
+ \\ gap: 16px;
2351
+ \\ padding: 16px;
2352
+ \\ border: 1px solid #e2e8f0;
2353
+ \\ border-radius: 16px;
2354
+ \\ background: #f8fafc;
2355
+ \\}
2356
+ \\
2357
+ ;
2358
+ }
2359
+
2360
+ fn reactPackageJson(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
2361
+ var out: std.ArrayList(u8) = .empty;
2362
+ errdefer out.deinit(allocator);
2363
+ try out.appendSlice(allocator, "{\n \"name\": ");
2364
+ try appendJsonString(&out, allocator, names.package_name);
2365
+ try out.appendSlice(allocator,
2366
+ \\,
2367
+ \\ "private": true,
2368
+ \\ "version": "0.1.0",
2369
+ \\ "type": "module",
2370
+ \\ "scripts": {
2371
+ \\ "dev": "vite",
2372
+ \\ "build": "vite build",
2373
+ \\ "preview": "vite preview"
2374
+ \\ },
2375
+ \\ "dependencies": {
2376
+ \\ "react": "^19.2.6",
2377
+ \\ "react-dom": "^19.2.6"
2378
+ \\ },
2379
+ \\ "devDependencies": {
2380
+ \\ "@types/react": "^19.2.14",
2381
+ \\ "@types/react-dom": "^19.2.3",
2382
+ \\ "@vitejs/plugin-react": "^6.0.1",
2383
+ \\ "vite": "^8.0.11"
2384
+ \\ }
2385
+ \\}
2386
+ \\
2387
+ );
2388
+ return out.toOwnedSlice(allocator);
2389
+ }
2390
+
2391
+ fn reactViteConfig() []const u8 {
2392
+ return
2393
+ \\import { defineConfig } from "vite";
2394
+ \\import react from "@vitejs/plugin-react";
2395
+ \\
2396
+ \\export default defineConfig({
2397
+ \\ plugins: [react()],
2398
+ \\});
2399
+ \\
2400
+ ;
2401
+ }
2402
+
2403
+ fn reactIndexHtml(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
2404
+ var out: std.ArrayList(u8) = .empty;
2405
+ errdefer out.deinit(allocator);
2406
+ try out.appendSlice(allocator,
2407
+ \\<!doctype html>
2408
+ \\<html lang="en">
2409
+ \\ <head>
2410
+ \\ <meta charset="UTF-8" />
2411
+ \\ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
2412
+ \\ <title>
2413
+ );
2414
+ try out.appendSlice(allocator, names.display_name);
2415
+ try out.appendSlice(allocator,
2416
+ \\</title>
2417
+ \\ </head>
2418
+ \\ <body>
2419
+ \\ <div id="root"></div>
2420
+ \\ <script type="module" src="/src/main.tsx"></script>
2421
+ \\ </body>
2422
+ \\</html>
2423
+ \\
2424
+ );
2425
+ return out.toOwnedSlice(allocator);
2426
+ }
2427
+
2428
+ fn reactMainTsx() []const u8 {
2429
+ return
2430
+ \\import { StrictMode } from "react";
2431
+ \\import { createRoot } from "react-dom/client";
2432
+ \\import App from "./App";
2433
+ \\import "./index.css";
2434
+ \\
2435
+ \\createRoot(document.getElementById("root")!).render(
2436
+ \\ <StrictMode>
2437
+ \\ <App />
2438
+ \\ </StrictMode>
2439
+ \\);
2440
+ \\
2441
+ ;
2442
+ }
2443
+
2444
+ fn reactAppTsx(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
2445
+ var out: std.ArrayList(u8) = .empty;
2446
+ errdefer out.deinit(allocator);
2447
+ try out.appendSlice(allocator,
2448
+ \\import { useEffect, useState } from "react";
2449
+ \\
2450
+ \\export default function App() {
2451
+ \\ const [bridge, setBridge] = useState("checking...");
2452
+ \\
2453
+ \\ useEffect(() => {
2454
+ \\ setBridge((window as any).zero ? "available" : "not enabled");
2455
+ \\ }, []);
2456
+ \\
2457
+ \\ return (
2458
+ \\ <main>
2459
+ \\ <p className="eyebrow">Native SDK + React</p>
2460
+ \\ <h1>
2461
+ );
2462
+ try out.appendSlice(allocator, names.display_name);
2463
+ try out.appendSlice(allocator,
2464
+ \\</h1>
2465
+ \\ <p className="lede">A React frontend running inside the system WebView.</p>
2466
+ \\ <div className="card">
2467
+ \\ <span>Native bridge</span>
2468
+ \\ <strong>{bridge}</strong>
2469
+ \\ </div>
2470
+ \\ </main>
2471
+ \\ );
2472
+ \\}
2473
+ \\
2474
+ );
2475
+ return out.toOwnedSlice(allocator);
2476
+ }
2477
+
2478
+ fn sveltePackageJson(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
2479
+ var out: std.ArrayList(u8) = .empty;
2480
+ errdefer out.deinit(allocator);
2481
+ try out.appendSlice(allocator, "{\n \"name\": ");
2482
+ try appendJsonString(&out, allocator, names.package_name);
2483
+ try out.appendSlice(allocator,
2484
+ \\,
2485
+ \\ "private": true,
2486
+ \\ "version": "0.1.0",
2487
+ \\ "type": "module",
2488
+ \\ "scripts": {
2489
+ \\ "dev": "vite",
2490
+ \\ "build": "vite build",
2491
+ \\ "preview": "vite preview"
2492
+ \\ },
2493
+ \\ "dependencies": {
2494
+ \\ "svelte": "^5.55.5"
2495
+ \\ },
2496
+ \\ "devDependencies": {
2497
+ \\ "@sveltejs/vite-plugin-svelte": "^7.1.2",
2498
+ \\ "vite": "^8.0.11"
2499
+ \\ }
2500
+ \\}
2501
+ \\
2502
+ );
2503
+ return out.toOwnedSlice(allocator);
2504
+ }
2505
+
2506
+ fn svelteViteConfig() []const u8 {
2507
+ return
2508
+ \\import { defineConfig } from "vite";
2509
+ \\import { svelte } from "@sveltejs/vite-plugin-svelte";
2510
+ \\
2511
+ \\export default defineConfig({
2512
+ \\ plugins: [svelte()],
2513
+ \\});
2514
+ \\
2515
+ ;
2516
+ }
2517
+
2518
+ fn svelteConfig() []const u8 {
2519
+ return
2520
+ \\export default {};
2521
+ \\
2522
+ ;
2523
+ }
2524
+
2525
+ fn svelteIndexHtml(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
2526
+ var out: std.ArrayList(u8) = .empty;
2527
+ errdefer out.deinit(allocator);
2528
+ try out.appendSlice(allocator,
2529
+ \\<!doctype html>
2530
+ \\<html lang="en">
2531
+ \\ <head>
2532
+ \\ <meta charset="UTF-8" />
2533
+ \\ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
2534
+ \\ <title>
2535
+ );
2536
+ try out.appendSlice(allocator, names.display_name);
2537
+ try out.appendSlice(allocator,
2538
+ \\</title>
2539
+ \\ </head>
2540
+ \\ <body>
2541
+ \\ <div id="app"></div>
2542
+ \\ <script type="module" src="/src/main.js"></script>
2543
+ \\ </body>
2544
+ \\</html>
2545
+ \\
2546
+ );
2547
+ return out.toOwnedSlice(allocator);
2548
+ }
2549
+
2550
+ fn svelteMainJs() []const u8 {
2551
+ return
2552
+ \\import App from "./App.svelte";
2553
+ \\import "./app.css";
2554
+ \\
2555
+ \\const app = new App({ target: document.getElementById("app") });
2556
+ \\
2557
+ \\export default app;
2558
+ \\
2559
+ ;
2560
+ }
2561
+
2562
+ fn svelteAppComponent(names: TemplateNames) []const u8 {
2563
+ _ = names;
2564
+ return
2565
+ \\<script>
2566
+ \\ import { onMount } from "svelte";
2567
+ \\
2568
+ \\ let bridge = $state("checking...");
2569
+ \\
2570
+ \\ onMount(() => {
2571
+ \\ bridge = window.zero ? "available" : "not enabled";
2572
+ \\ });
2573
+ \\</script>
2574
+ \\
2575
+ \\<main>
2576
+ \\ <p class="eyebrow">Native SDK + Svelte</p>
2577
+ \\ <h1>App</h1>
2578
+ \\ <p class="lede">A Svelte frontend running inside the system WebView.</p>
2579
+ \\ <div class="card">
2580
+ \\ <span>Native bridge</span>
2581
+ \\ <strong>{bridge}</strong>
2582
+ \\ </div>
2583
+ \\</main>
2584
+ \\
2585
+ ;
2586
+ }
2587
+
2588
+ fn vuePackageJson(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
2589
+ var out: std.ArrayList(u8) = .empty;
2590
+ errdefer out.deinit(allocator);
2591
+ try out.appendSlice(allocator, "{\n \"name\": ");
2592
+ try appendJsonString(&out, allocator, names.package_name);
2593
+ try out.appendSlice(allocator,
2594
+ \\,
2595
+ \\ "private": true,
2596
+ \\ "version": "0.1.0",
2597
+ \\ "type": "module",
2598
+ \\ "scripts": {
2599
+ \\ "dev": "vite",
2600
+ \\ "build": "vite build",
2601
+ \\ "preview": "vite preview"
2602
+ \\ },
2603
+ \\ "dependencies": {
2604
+ \\ "vue": "^3.5.34"
2605
+ \\ },
2606
+ \\ "devDependencies": {
2607
+ \\ "@vitejs/plugin-vue": "^6.0.6",
2608
+ \\ "vite": "^8.0.11"
2609
+ \\ }
2610
+ \\}
2611
+ \\
2612
+ );
2613
+ return out.toOwnedSlice(allocator);
2614
+ }
2615
+
2616
+ fn vueViteConfig() []const u8 {
2617
+ return
2618
+ \\import { defineConfig } from "vite";
2619
+ \\import vue from "@vitejs/plugin-vue";
2620
+ \\
2621
+ \\export default defineConfig({
2622
+ \\ plugins: [vue()],
2623
+ \\});
2624
+ \\
2625
+ ;
2626
+ }
2627
+
2628
+ fn vueIndexHtml(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8 {
2629
+ var out: std.ArrayList(u8) = .empty;
2630
+ errdefer out.deinit(allocator);
2631
+ try out.appendSlice(allocator,
2632
+ \\<!doctype html>
2633
+ \\<html lang="en">
2634
+ \\ <head>
2635
+ \\ <meta charset="UTF-8" />
2636
+ \\ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
2637
+ \\ <title>
2638
+ );
2639
+ try out.appendSlice(allocator, names.display_name);
2640
+ try out.appendSlice(allocator,
2641
+ \\</title>
2642
+ \\ </head>
2643
+ \\ <body>
2644
+ \\ <div id="app"></div>
2645
+ \\ <script type="module" src="/src/main.js"></script>
2646
+ \\ </body>
2647
+ \\</html>
2648
+ \\
2649
+ );
2650
+ return out.toOwnedSlice(allocator);
2651
+ }
2652
+
2653
+ fn vueMainJs() []const u8 {
2654
+ return
2655
+ \\import { createApp } from "vue";
2656
+ \\import App from "./App.vue";
2657
+ \\import "./style.css";
2658
+ \\
2659
+ \\createApp(App).mount("#app");
2660
+ \\
2661
+ ;
2662
+ }
2663
+
2664
+ fn vueAppComponent(names: TemplateNames) []const u8 {
2665
+ _ = names;
2666
+ return
2667
+ \\<script setup>
2668
+ \\import { ref, onMounted } from "vue";
2669
+ \\
2670
+ \\const bridge = ref("checking...");
2671
+ \\
2672
+ \\onMounted(() => {
2673
+ \\ bridge.value = window.zero ? "available" : "not enabled";
2674
+ \\});
2675
+ \\</script>
2676
+ \\
2677
+ \\<template>
2678
+ \\ <main>
2679
+ \\ <p class="eyebrow">Native SDK + Vue</p>
2680
+ \\ <h1>App</h1>
2681
+ \\ <p class="lede">A Vue frontend running inside the system WebView.</p>
2682
+ \\ <div class="card">
2683
+ \\ <span>Native bridge</span>
2684
+ \\ <strong>{{ bridge }}</strong>
2685
+ \\ </div>
2686
+ \\ </main>
2687
+ \\</template>
2688
+ \\
2689
+ ;
2690
+ }
2691
+
2692
+ fn readme(allocator: std.mem.Allocator, names: TemplateNames, framework_path: []const u8, frontend: Frontend) ![]const u8 {
2693
+ var out: std.ArrayList(u8) = .empty;
2694
+ errdefer out.deinit(allocator);
2695
+ try out.appendSlice(allocator, "# ");
2696
+ try out.appendSlice(allocator, names.display_name);
2697
+ try out.appendSlice(allocator,
2698
+ \\
2699
+ \\
2700
+ \\A minimal native-sdk desktop app with a web frontend.
2701
+ \\
2702
+ \\## Setup
2703
+ \\
2704
+ \\`zig build dev`, `zig build run`, and `zig build package` install frontend dependencies automatically. To install them explicitly, run:
2705
+ \\
2706
+ \\```sh
2707
+ \\npm install --prefix frontend
2708
+ \\```
2709
+ \\
2710
+ \\The generated build defaults to this Native SDK framework path:
2711
+ \\
2712
+ \\```text
2713
+ );
2714
+ try out.append(allocator, '\n');
2715
+ try out.appendSlice(allocator, framework_path);
2716
+ try out.append(allocator, '\n');
2717
+ try out.appendSlice(allocator,
2718
+ \\
2719
+ \\```
2720
+ \\
2721
+ \\Override it with `-Dnative-sdk-path=/path/to/native-sdk` if you move this app.
2722
+ \\
2723
+ \\## Commands
2724
+ \\
2725
+ \\```sh
2726
+ \\zig build dev
2727
+ \\zig build run
2728
+ \\zig build test
2729
+ \\zig build package
2730
+ \\native doctor --manifest app.zon
2731
+ \\```
2732
+ \\
2733
+ \\`zig build dev` starts the frontend dev server from `app.zon`, waits for it, and launches the native shell with `NATIVE_SDK_FRONTEND_URL`.
2734
+ \\
2735
+ \\Frontend:
2736
+ \\
2737
+ \\- Type:
2738
+ );
2739
+ try out.appendSlice(allocator, @tagName(frontend));
2740
+ try out.appendSlice(allocator,
2741
+ \\
2742
+ \\- Production assets: `
2743
+ );
2744
+ try out.appendSlice(allocator, frontend.distDir());
2745
+ try out.appendSlice(allocator,
2746
+ \\`
2747
+ \\- Dev URL: `
2748
+ );
2749
+ try out.appendSlice(allocator, frontend.devUrl());
2750
+ try out.appendSlice(allocator,
2751
+ \\`
2752
+ \\
2753
+ \\## Web Engines
2754
+ \\
2755
+ \\The generated app defaults to the system WebView. On macOS you can switch to Chromium/CEF with:
2756
+ \\
2757
+ \\```sh
2758
+ \\native cef install
2759
+ \\zig build run -Dplatform=macos -Dweb-engine=chromium
2760
+ \\```
2761
+ \\
2762
+ \\`native cef install` downloads Native SDK's prepared CEF runtime, including the native wrapper library.
2763
+ \\
2764
+ \\For one-command local setup, opt into build-time install:
2765
+ \\
2766
+ \\```sh
2767
+ \\zig build run -Dplatform=macos -Dweb-engine=chromium -Dcef-auto-install=true
2768
+ \\```
2769
+ \\
2770
+ \\Use `-Dcef-dir=/path/to/cef` when you keep CEF outside the platform default under `third_party/cef`.
2771
+ \\
2772
+ \\```sh
2773
+ \\native doctor --web-engine chromium
2774
+ \\```
2775
+ \\
2776
+ \\Diagnostics:
2777
+ \\
2778
+ \\- Set `NATIVE_SDK_LOG_DIR` to override the platform log directory during development.
2779
+ \\- Set `NATIVE_SDK_LOG_FORMAT=text|jsonl` to choose persistent log format.
2780
+ \\
2781
+ );
2782
+ return out.toOwnedSlice(allocator);
2783
+ }
2784
+
2785
+ test "template strings are non-empty" {
2786
+ const names = try TemplateNames.init(std.testing.allocator, "app");
2787
+ defer names.deinit(std.testing.allocator);
2788
+ const build_zig = try buildZig(std.testing.allocator, names, "..", .vite);
2789
+ defer std.testing.allocator.free(build_zig);
2790
+ const main_zig = try mainZig(std.testing.allocator, names, .vite);
2791
+ defer std.testing.allocator.free(main_zig);
2792
+ try std.testing.expect(build_zig.len > 0);
2793
+ try std.testing.expect(main_zig.len > 0);
2794
+ try std.testing.expect(runnerZig().len > 0);
2795
+ }
2796
+
2797
+ test "template names are sanitized for generated metadata" {
2798
+ const names = try TemplateNames.init(std.testing.allocator, "My Cool_App!");
2799
+ defer names.deinit(std.testing.allocator);
2800
+
2801
+ try std.testing.expectEqualStrings("my-cool-app", names.package_name);
2802
+ try std.testing.expectEqualStrings("my_cool_app", names.module_name);
2803
+ try std.testing.expectEqualStrings("My Cool App", names.display_name);
2804
+ try std.testing.expectEqualStrings("dev.native_sdk.my-cool-app", names.app_id);
2805
+ }
2806
+
2807
+ test "template fingerprint includes package name checksum" {
2808
+ try std.testing.expectEqual(@as(u64, 0x92a6f71c5a707070), fingerprintForName("test_vite_init_smoke"));
2809
+ }
2810
+
2811
+ test "writeDefaultApp emits Vite project files" {
2812
+ const destination = ".zig-cache/test-vite-init-template";
2813
+ try writeDefaultApp(std.testing.allocator, std.testing.io, destination, .{ .app_name = "My App", .framework_path = ".", .frontend = .vite });
2814
+
2815
+ const app_zon_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "app.zon");
2816
+ defer std.testing.allocator.free(app_zon_text);
2817
+ const build_zig_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "build.zig");
2818
+ defer std.testing.allocator.free(build_zig_text);
2819
+ const main_zig_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "src/main.zig");
2820
+ defer std.testing.allocator.free(main_zig_text);
2821
+ const runner_zig_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "src/runner.zig");
2822
+ defer std.testing.allocator.free(runner_zig_text);
2823
+ const package_json_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "frontend/package.json");
2824
+ defer std.testing.allocator.free(package_json_text);
2825
+ const main_js_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "frontend/src/main.js");
2826
+ defer std.testing.allocator.free(main_js_text);
2827
+
2828
+ try std.testing.expect(std.mem.indexOf(u8, app_zon_text, ".frontend") != null);
2829
+ try std.testing.expect(std.mem.indexOf(u8, app_zon_text, "frontend/dist") != null);
2830
+ try std.testing.expect(std.mem.indexOf(u8, app_zon_text, "npm") != null);
2831
+ try std.testing.expect(std.mem.indexOf(u8, app_zon_text, ".windows") != null);
2832
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "frontend-install") != null);
2833
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "\"npm\", \"install\", \"--prefix\", \"frontend\"") != null);
2834
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "frontend-build") != null);
2835
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "frontend_build.step.dependOn(&frontend_install.step)") != null);
2836
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "\"native\", \"dev\"") != null);
2837
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "dev.step.dependOn(&frontend_install.step)") != null);
2838
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "chromium") != null);
2839
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "cef-dir") != null);
2840
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "src/platform/macos/cef_host.mm") != null);
2841
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "src/platform/linux/gtk_host.c") != null);
2842
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "app_manifest_zon") != null);
2843
+ try std.testing.expect(std.mem.indexOf(u8, main_zig_text, "frontend/dist") != null);
2844
+ try std.testing.expect(std.mem.indexOf(u8, main_zig_text, "127.0.0.1:5173") != null);
2845
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "@import(\"app_manifest_zon\")") != null);
2846
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "commands: ?[]const native_sdk.Command = null") != null);
2847
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "resolvedCommands") != null);
2848
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "menus: ?[]const native_sdk.Menu = null") != null);
2849
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "resolvedMenus") != null);
2850
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "shortcuts: ?[]const native_sdk.Shortcut = null") != null);
2851
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "resolvedShortcuts") != null);
2852
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "const manifest_windows") != null);
2853
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "fn appInfo(self: RunOptions, buffers: *StateBuffers)") != null);
2854
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "fn manifestWindowOptions") != null);
2855
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "info.windows = windows") != null);
2856
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "for (restored_windows, 0..)") != null);
2857
+ // The Runtime is multi-megabyte; the generated runner must heap-allocate
2858
+ // it and construct in place, never build it by value on the stack.
2859
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "std.heap.page_allocator.create(native_sdk.Runtime)") != null);
2860
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "defer std.heap.page_allocator.destroy(runtime)") != null);
2861
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "native_sdk.Runtime.initAt(runtime, .{") != null);
2862
+ try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "Runtime.init(.{") == null);
2863
+ try std.testing.expect(std.mem.indexOf(u8, package_json_text, "\"vite\"") != null);
2864
+ try std.testing.expect(std.mem.indexOf(u8, main_js_text, "window.zero") != null);
2865
+
2866
+ const ci_yaml_text = try readTestFile(std.testing.allocator, std.testing.io, destination, ".github/workflows/ci.yml");
2867
+ defer std.testing.allocator.free(ci_yaml_text);
2868
+ try expectBasicYaml(ci_yaml_text);
2869
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "zig build test -Dplatform=null -Dnative-sdk-path=\"$NATIVE_SDK_PATH\"") != null);
2870
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "mlugg/setup-zig@v2") != null);
2871
+ // Web-frontend workflows stop at logic tests: the automation smoke
2872
+ // recipe is native-app specific.
2873
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "smoke:") == null);
2874
+ }
2875
+
2876
+ test "writeDefaultApp emits frontend-specific Next paths" {
2877
+ const destination = ".zig-cache/test-next-init-template";
2878
+ try writeDefaultApp(std.testing.allocator, std.testing.io, destination, .{ .app_name = "Next App", .framework_path = ".", .frontend = .next });
2879
+
2880
+ const app_zon_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "app.zon");
2881
+ defer std.testing.allocator.free(app_zon_text);
2882
+ const build_zig_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "build.zig");
2883
+ defer std.testing.allocator.free(build_zig_text);
2884
+ const main_zig_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "src/main.zig");
2885
+ defer std.testing.allocator.free(main_zig_text);
2886
+ const tsconfig_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "frontend/tsconfig.json");
2887
+ defer std.testing.allocator.free(tsconfig_text);
2888
+
2889
+ try std.testing.expect(std.mem.indexOf(u8, app_zon_text, "frontend/out") != null);
2890
+ try std.testing.expect(std.mem.indexOf(u8, app_zon_text, "127.0.0.1:3000") != null);
2891
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "frontend/out") != null);
2892
+ try std.testing.expect(std.mem.indexOf(u8, main_zig_text, "frontend/out") != null);
2893
+ try std.testing.expect(std.mem.indexOf(u8, main_zig_text, "127.0.0.1:3000") != null);
2894
+ try std.testing.expect(std.mem.indexOf(u8, tsconfig_text, "\"@/*\": [\"./app/*\"]") != null);
2895
+ }
2896
+
2897
+ test "writeDefaultApp emits a slim native scaffold by default" {
2898
+ const destination = ".zig-cache/test-native-slim-template";
2899
+ try writeDefaultApp(std.testing.allocator, std.testing.io, destination, .{ .app_name = "My App", .framework_path = ".", .frontend = .native });
2900
+
2901
+ const app_zon_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "app.zon");
2902
+ defer std.testing.allocator.free(app_zon_text);
2903
+ const main_zig_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "src/main.zig");
2904
+ defer std.testing.allocator.free(main_zig_text);
2905
+ const gitignore_text = try readTestFile(std.testing.allocator, std.testing.io, destination, ".gitignore");
2906
+ defer std.testing.allocator.free(gitignore_text);
2907
+ const readme_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "README.md");
2908
+ defer std.testing.allocator.free(readme_text);
2909
+ const icon = try readTestFile(std.testing.allocator, std.testing.io, destination, "assets/icon.png");
2910
+ defer std.testing.allocator.free(icon);
2911
+
2912
+ // Zero-config: no build files, no editor config, no CI workflow.
2913
+ try std.testing.expectError(error.FileNotFound, readTestFile(std.testing.allocator, std.testing.io, destination, "build.zig"));
2914
+ try std.testing.expectError(error.FileNotFound, readTestFile(std.testing.allocator, std.testing.io, destination, "build.zig.zon"));
2915
+ try std.testing.expectError(error.FileNotFound, readTestFile(std.testing.allocator, std.testing.io, destination, ".vscode/settings.json"));
2916
+ try std.testing.expectError(error.FileNotFound, readTestFile(std.testing.allocator, std.testing.io, destination, ".github/workflows/ci.yml"));
2917
+
2918
+ try std.testing.expect(std.mem.indexOf(u8, app_zon_text, "gpu_surface") != null);
2919
+ try std.testing.expect(std.mem.indexOf(u8, main_zig_text, "native_sdk.UiApp(Model, Msg)") != null);
2920
+ // The generated + derived state is ignored wholesale.
2921
+ try std.testing.expect(std.mem.indexOf(u8, gitignore_text, ".native/") != null);
2922
+ try std.testing.expect(std.mem.indexOf(u8, gitignore_text, "zig-out/") != null);
2923
+ // The README teaches the native verbs, not zig build.
2924
+ try std.testing.expect(std.mem.indexOf(u8, readme_text, "native dev") != null);
2925
+ try std.testing.expect(std.mem.indexOf(u8, readme_text, "native test") != null);
2926
+ try std.testing.expect(std.mem.indexOf(u8, readme_text, "native build") != null);
2927
+ try std.testing.expect(std.mem.indexOf(u8, readme_text, "native check") != null);
2928
+ try std.testing.expect(std.mem.indexOf(u8, readme_text, "native eject") != null);
2929
+ try std.testing.expect(std.mem.indexOf(u8, readme_text, "zig build run") == null);
2930
+ }
2931
+
2932
+ test "writeDefaultApp emits native project files" {
2933
+ const destination = ".zig-cache/test-native-init-template";
2934
+ try writeDefaultApp(std.testing.allocator, std.testing.io, destination, .{ .app_name = "My App", .framework_path = ".", .frontend = .native, .shape = .full });
2935
+
2936
+ const app_zon_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "app.zon");
2937
+ defer std.testing.allocator.free(app_zon_text);
2938
+ const build_zig_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "build.zig");
2939
+ defer std.testing.allocator.free(build_zig_text);
2940
+ const build_zon_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "build.zig.zon");
2941
+ defer std.testing.allocator.free(build_zon_text);
2942
+ const main_zig_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "src/main.zig");
2943
+ defer std.testing.allocator.free(main_zig_text);
2944
+ const app_markup_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "src/app.native");
2945
+ defer std.testing.allocator.free(app_markup_text);
2946
+ const tests_zig_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "src/tests.zig");
2947
+ defer std.testing.allocator.free(tests_zig_text);
2948
+ const vscode_text = try readTestFile(std.testing.allocator, std.testing.io, destination, ".vscode/settings.json");
2949
+ defer std.testing.allocator.free(vscode_text);
2950
+ const gitignore_text = try readTestFile(std.testing.allocator, std.testing.io, destination, ".gitignore");
2951
+ defer std.testing.allocator.free(gitignore_text);
2952
+ const readme_text = try readTestFile(std.testing.allocator, std.testing.io, destination, "README.md");
2953
+ defer std.testing.allocator.free(readme_text);
2954
+
2955
+ // No WebView frontend files.
2956
+ try std.testing.expectError(error.FileNotFound, readTestFile(std.testing.allocator, std.testing.io, destination, "frontend/package.json"));
2957
+ try std.testing.expectError(error.FileNotFound, readTestFile(std.testing.allocator, std.testing.io, destination, "src/runner.zig"));
2958
+
2959
+ try std.testing.expect(std.mem.indexOf(u8, app_zon_text, "gpu_surface") != null);
2960
+ try std.testing.expect(std.mem.indexOf(u8, app_zon_text, "\"native_views\", \"gpu_surfaces\"") != null);
2961
+ try std.testing.expect(std.mem.indexOf(u8, app_zon_text, "dev.native_sdk.my-app") != null);
2962
+ try std.testing.expect(std.mem.indexOf(u8, app_zon_text, ".frontend") == null);
2963
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "native_sdk.addApp(b, b.dependency(\"native_sdk\", .{}), .{ .name = \"my-app\" })") != null);
2964
+ try std.testing.expect(std.mem.indexOf(u8, build_zon_text, ".native_sdk = .{ .path = ") != null);
2965
+ try std.testing.expect(std.mem.indexOf(u8, build_zon_text, ".name = .my_app") != null);
2966
+ try std.testing.expect(std.mem.indexOf(u8, main_zig_text, "native_sdk.UiApp(Model, Msg)") != null);
2967
+ try std.testing.expect(std.mem.indexOf(u8, main_zig_text, "@embedFile(\"app.native\")") != null);
2968
+ try std.testing.expect(std.mem.indexOf(u8, main_zig_text, ".watch_path = \"src/app.native\"") != null);
2969
+ try std.testing.expect(std.mem.indexOf(u8, app_markup_text, "on-press=\"increment\"") != null);
2970
+ try std.testing.expect(std.mem.indexOf(u8, tests_zig_text, "msgForPointer") != null);
2971
+ try std.testing.expect(std.mem.indexOf(u8, tests_zig_text, "canvas.MarkupView(Model, Msg)") != null);
2972
+ try std.testing.expect(std.mem.indexOf(u8, vscode_text, "\"*.native\": \"html\"") != null);
2973
+ try std.testing.expect(std.mem.indexOf(u8, gitignore_text, "zig-out/") != null);
2974
+ try std.testing.expect(std.mem.indexOf(u8, readme_text, "native markup check src/app.native") != null);
2975
+ try std.testing.expect(std.mem.indexOf(u8, readme_text, "hot") != null or std.mem.indexOf(u8, readme_text, "Hot") != null);
2976
+ }
2977
+
2978
+ test "writeDefaultApp emits a CI workflow for native apps" {
2979
+ const destination = ".zig-cache/test-native-ci-template";
2980
+ try writeDefaultApp(std.testing.allocator, std.testing.io, destination, .{ .app_name = "My App", .framework_path = ".", .frontend = .native, .shape = .full });
2981
+
2982
+ const ci_yaml_text = try readTestFile(std.testing.allocator, std.testing.io, destination, ".github/workflows/ci.yml");
2983
+ defer std.testing.allocator.free(ci_yaml_text);
2984
+
2985
+ try expectBasicYaml(ci_yaml_text);
2986
+ // Both jobs are present, sized for a user app.
2987
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "jobs:") != null);
2988
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, " test:") != null);
2989
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, " smoke:") != null);
2990
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "mlugg/setup-zig@v2") != null);
2991
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "version: 0.16.0") != null);
2992
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "zig build test -Dplatform=null") != null);
2993
+ // The smoke job builds with automation, launches under Xvfb, and drives
2994
+ // the snapshot: the binary name comes from the template context.
2995
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "libgtk-4-dev libwebkitgtk-6.0-dev xvfb") != null);
2996
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "zig build -Dplatform=linux -Dweb-engine=system -Dautomation=true") != null);
2997
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "xvfb-run -a ./zig-out/bin/my-app &") != null);
2998
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "automate wait") != null);
2999
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "automate assert 'gpu_nonblank=true' 'role=button name=\"Reset\"' 'count: 0'") != null);
3000
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "automate screenshot main-canvas") != null);
3001
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "test -s .zig-cache/native-sdk-automation/screenshot-main-canvas.png") != null);
3002
+ // The framework fetch step reuses the build.zig.zon dependency path.
3003
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "NATIVE_SDK_PATH:") != null);
3004
+ try std.testing.expect(std.mem.indexOf(u8, ci_yaml_text, "git clone --depth 1 https://github.com/vercel-labs/zero-native.git \"$NATIVE_SDK_PATH\"") != null);
3005
+ }
3006
+
3007
+ /// Structural sanity for generated workflows (the repo scaffold CI job runs
3008
+ /// a real YAML parse over the generated file): top-level keys present, no
3009
+ /// tabs, space indentation in even steps, no trailing whitespace.
3010
+ fn expectBasicYaml(text: []const u8) !void {
3011
+ try std.testing.expect(std.mem.startsWith(u8, text, "name: CI\n"));
3012
+ try std.testing.expect(std.mem.indexOf(u8, text, "\non:\n") != null);
3013
+ try std.testing.expect(std.mem.indexOf(u8, text, "\njobs:\n") != null);
3014
+ try std.testing.expect(std.mem.indexOfScalar(u8, text, '\t') == null);
3015
+ var lines = std.mem.splitScalar(u8, text, '\n');
3016
+ while (lines.next()) |line| {
3017
+ if (line.len == 0) continue;
3018
+ try std.testing.expect(!std.ascii.isWhitespace(line[line.len - 1]));
3019
+ var indent: usize = 0;
3020
+ while (indent < line.len and line[indent] == ' ') indent += 1;
3021
+ try std.testing.expect(indent % 2 == 0);
3022
+ }
3023
+ }
3024
+
3025
+ pub fn normalizePackageName(allocator: std.mem.Allocator, value: []const u8) ![]const u8 {
3026
+ var out: std.ArrayList(u8) = .empty;
3027
+ errdefer out.deinit(allocator);
3028
+ var last_separator = false;
3029
+ for (value) |ch| {
3030
+ if (isAsciiAlpha(ch) or isAsciiDigit(ch)) {
3031
+ try out.append(allocator, std.ascii.toLower(ch));
3032
+ last_separator = false;
3033
+ } else if (!last_separator and out.items.len > 0) {
3034
+ try out.append(allocator, '-');
3035
+ last_separator = true;
3036
+ }
3037
+ }
3038
+ if (out.items.len > 0 and out.items[out.items.len - 1] == '-') _ = out.pop();
3039
+ if (out.items.len == 0) try out.appendSlice(allocator, "native-sdk-app");
3040
+ return out.toOwnedSlice(allocator);
3041
+ }
3042
+
3043
+ pub fn normalizeModuleName(allocator: std.mem.Allocator, value: []const u8) ![]const u8 {
3044
+ const max_zig_package_name_len = 32;
3045
+ var out: std.ArrayList(u8) = .empty;
3046
+ errdefer out.deinit(allocator);
3047
+ if (value.len == 0 or isAsciiDigit(value[0])) try out.appendSlice(allocator, "app_");
3048
+ for (value) |ch| {
3049
+ if (out.items.len >= max_zig_package_name_len) break;
3050
+ if (isAsciiAlpha(ch) or isAsciiDigit(ch)) {
3051
+ try out.append(allocator, std.ascii.toLower(ch));
3052
+ } else {
3053
+ try out.append(allocator, '_');
3054
+ }
3055
+ }
3056
+ return out.toOwnedSlice(allocator);
3057
+ }
3058
+
3059
+ test "normalizeModuleName caps Zig package names" {
3060
+ const module_name = try normalizeModuleName(std.testing.allocator, "scaffold-package-smoke-1778284313");
3061
+ defer std.testing.allocator.free(module_name);
3062
+
3063
+ try std.testing.expect(module_name.len <= 32);
3064
+ try std.testing.expectEqualStrings("scaffold_package_smoke_177828431", module_name);
3065
+ }
3066
+
3067
+ fn displayName(allocator: std.mem.Allocator, value: []const u8) ![]const u8 {
3068
+ var out: std.ArrayList(u8) = .empty;
3069
+ errdefer out.deinit(allocator);
3070
+ var start_word = true;
3071
+ for (value) |ch| {
3072
+ if (ch == '-') {
3073
+ if (out.items.len > 0 and out.items[out.items.len - 1] != ' ') try out.append(allocator, ' ');
3074
+ start_word = true;
3075
+ continue;
3076
+ }
3077
+ if (start_word and isAsciiAlpha(ch)) {
3078
+ try out.append(allocator, std.ascii.toUpper(ch));
3079
+ } else {
3080
+ try out.append(allocator, ch);
3081
+ }
3082
+ start_word = false;
3083
+ }
3084
+ if (out.items.len == 0) try out.appendSlice(allocator, "Native SDK app");
3085
+ return out.toOwnedSlice(allocator);
3086
+ }
3087
+
3088
+ pub fn fingerprintForName(name: []const u8) u64 {
3089
+ const checksum: u64 = std.hash.Crc32.hash(name);
3090
+ return (checksum << 32) | 0x5a707070;
3091
+ }
3092
+
3093
+ fn defaultFrameworkPath(allocator: std.mem.Allocator, io: std.Io, destination: []const u8, framework_path: []const u8) ![]const u8 {
3094
+ if (std.fs.path.isAbsolute(framework_path)) {
3095
+ return allocator.dupe(u8, framework_path);
3096
+ }
3097
+ if (std.fs.path.isAbsolute(destination)) {
3098
+ const cwd = try std.process.currentPathAlloc(io, allocator);
3099
+ defer allocator.free(cwd);
3100
+ return std.fs.path.join(allocator, &.{ cwd, framework_path });
3101
+ }
3102
+
3103
+ var out: std.ArrayList(u8) = .empty;
3104
+ errdefer out.deinit(allocator);
3105
+
3106
+ var destination_parts = std.mem.tokenizeAny(u8, destination, "/\\");
3107
+ while (destination_parts.next()) |part| {
3108
+ if (std.mem.eql(u8, part, ".")) continue;
3109
+ if (std.mem.eql(u8, part, "..")) continue;
3110
+ if (out.items.len > 0) try out.append(allocator, '/');
3111
+ try out.appendSlice(allocator, "..");
3112
+ }
3113
+
3114
+ var framework_parts = std.mem.tokenizeAny(u8, framework_path, "/\\");
3115
+ while (framework_parts.next()) |part| {
3116
+ if (std.mem.eql(u8, part, ".")) continue;
3117
+ if (part.len == 0) continue;
3118
+ if (out.items.len > 0) try out.append(allocator, '/');
3119
+ try out.appendSlice(allocator, part);
3120
+ }
3121
+
3122
+ if (out.items.len == 0) try out.append(allocator, '.');
3123
+ return out.toOwnedSlice(allocator);
3124
+ }
3125
+
3126
+ /// The native_sdk dependency path for build.zig.zon: always relative to the
3127
+ /// generated app root, since Zig rejects absolute paths in path dependencies.
3128
+ /// `framework_path` comes from defaultFrameworkPath, so it is either already
3129
+ /// destination-relative or absolute.
3130
+ fn nativeDependencyPath(allocator: std.mem.Allocator, io: std.Io, destination: []const u8, framework_path: []const u8) ![]const u8 {
3131
+ if (!std.fs.path.isAbsolute(framework_path)) {
3132
+ return allocator.dupe(u8, framework_path);
3133
+ }
3134
+
3135
+ // Resolve symlinks (e.g. /tmp -> /private/tmp on macOS) before computing
3136
+ // the relative path, so `..` segments traverse the real directory tree.
3137
+ const cwd = try std.process.currentPathAlloc(io, allocator);
3138
+ defer allocator.free(cwd);
3139
+ const destination_real = try std.Io.Dir.cwd().realPathFileAlloc(io, destination, allocator);
3140
+ defer allocator.free(destination_real);
3141
+ const framework_real = try std.Io.Dir.realPathFileAbsoluteAlloc(io, framework_path, allocator);
3142
+ defer allocator.free(framework_real);
3143
+
3144
+ const relative = try std.fs.path.relative(allocator, cwd, null, destination_real, framework_real);
3145
+ if (relative.len == 0) {
3146
+ allocator.free(relative);
3147
+ return allocator.dupe(u8, ".");
3148
+ }
3149
+ return relative;
3150
+ }
3151
+
3152
+ fn appendZigString(out: *std.ArrayList(u8), allocator: std.mem.Allocator, value: []const u8) !void {
3153
+ try appendEscapedString(out, allocator, value);
3154
+ }
3155
+
3156
+ fn appendJsonString(out: *std.ArrayList(u8), allocator: std.mem.Allocator, value: []const u8) !void {
3157
+ try appendEscapedString(out, allocator, value);
3158
+ }
3159
+
3160
+ fn appendEscapedString(out: *std.ArrayList(u8), allocator: std.mem.Allocator, value: []const u8) !void {
3161
+ try out.append(allocator, '"');
3162
+ for (value) |ch| {
3163
+ switch (ch) {
3164
+ '\\' => try out.appendSlice(allocator, "\\\\"),
3165
+ '"' => try out.appendSlice(allocator, "\\\""),
3166
+ '\n' => try out.appendSlice(allocator, "\\n"),
3167
+ '\r' => try out.appendSlice(allocator, "\\r"),
3168
+ '\t' => try out.appendSlice(allocator, "\\t"),
3169
+ else => try out.append(allocator, ch),
3170
+ }
3171
+ }
3172
+ try out.append(allocator, '"');
3173
+ }
3174
+
3175
+ fn isAsciiAlpha(ch: u8) bool {
3176
+ return (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z');
3177
+ }
3178
+
3179
+ fn isAsciiDigit(ch: u8) bool {
3180
+ return ch >= '0' and ch <= '9';
3181
+ }
3182
+
3183
+ fn readTestFile(allocator: std.mem.Allocator, io: std.Io, root: []const u8, path: []const u8) ![]u8 {
3184
+ var root_dir = try std.Io.Dir.cwd().openDir(io, root, .{});
3185
+ defer root_dir.close(io);
3186
+ var file = try root_dir.openFile(io, path, .{});
3187
+ defer file.close(io);
3188
+ var read_buffer: [4096]u8 = undefined;
3189
+ var reader = file.reader(io, &read_buffer);
3190
+ return reader.interface.allocRemaining(allocator, .limited(1024 * 1024));
3191
+ }