@native-sdk/cli 0.0.0 → 0.4.1

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 (373) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +50 -1
  3. package/app.zon +30 -0
  4. package/assets/icon.icns +0 -0
  5. package/assets/icon.ico +0 -0
  6. package/assets/icon.png +0 -0
  7. package/assets/icon.svg +15 -0
  8. package/assets/native-sdk.entitlements +10 -0
  9. package/assets/native-sdk.manifest +12 -0
  10. package/bin/native.js +156 -0
  11. package/build/app.zig +716 -0
  12. package/build.zig +2593 -0
  13. package/build.zig.zon +24 -0
  14. package/native-sdk.d.ts +615 -0
  15. package/package.json +45 -8
  16. package/skill-data/automation/SKILL.md +248 -0
  17. package/skill-data/core/SKILL.md +249 -0
  18. package/skill-data/core/references/app-model-runtime.md +141 -0
  19. package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
  20. package/skill-data/core/references/frontend-assets.md +124 -0
  21. package/skill-data/core/references/project-anatomy.md +108 -0
  22. package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
  23. package/skill-data/native-ui/SKILL.md +1080 -0
  24. package/skills/native-sdk/SKILL.md +33 -0
  25. package/src/app_runner/root.zig +740 -0
  26. package/src/assets/root.zig +20 -0
  27. package/src/automation/protocol.zig +285 -0
  28. package/src/automation/root.zig +13 -0
  29. package/src/automation/server.zig +506 -0
  30. package/src/automation/snapshot.zig +1202 -0
  31. package/src/automation/watcher.zig +165 -0
  32. package/src/bridge/root.zig +507 -0
  33. package/src/debug/root.zig +246 -0
  34. package/src/embed/app_exports.zig +11 -0
  35. package/src/embed/c_api.zig +793 -0
  36. package/src/embed/c_exports.zig +10 -0
  37. package/src/embed/chrome.zig +168 -0
  38. package/src/embed/conversions.zig +359 -0
  39. package/src/embed/host.zig +960 -0
  40. package/src/embed/root.zig +81 -0
  41. package/src/embed/tests.zig +2552 -0
  42. package/src/embed/types.zig +382 -0
  43. package/src/embed/ui_host.zig +289 -0
  44. package/src/extensions/root.zig +197 -0
  45. package/src/frontend/root.zig +48 -0
  46. package/src/js/root.zig +65 -0
  47. package/src/platform/android/NativeSdkActivity.java +1370 -0
  48. package/src/platform/android/android_host.c +696 -0
  49. package/src/platform/android/files.zig +9 -0
  50. package/src/platform/android/native_sdk_app.h +237 -0
  51. package/src/platform/ios/files.zig +8 -0
  52. package/src/platform/ios/native_sdk_app.h +289 -0
  53. package/src/platform/ios/uikit_host.m +2739 -0
  54. package/src/platform/linux/cef_host.cpp +599 -0
  55. package/src/platform/linux/gtk_host.c +5362 -0
  56. package/src/platform/linux/gtk_host.h +265 -0
  57. package/src/platform/linux/root.zig +1519 -0
  58. package/src/platform/macos/appkit_host.h +604 -0
  59. package/src/platform/macos/appkit_host.m +10392 -0
  60. package/src/platform/macos/automation_host.h +14 -0
  61. package/src/platform/macos/automation_host.m +12 -0
  62. package/src/platform/macos/cef_host.mm +3028 -0
  63. package/src/platform/macos/root.zig +2260 -0
  64. package/src/platform/null_platform.zig +2232 -0
  65. package/src/platform/null_platform_tests.zig +670 -0
  66. package/src/platform/policy_values.zig +14 -0
  67. package/src/platform/root.zig +181 -0
  68. package/src/platform/types.zig +2681 -0
  69. package/src/platform/windows/cef_host.cpp +3 -0
  70. package/src/platform/windows/root.zig +1521 -0
  71. package/src/platform/windows/webview2_host.cpp +6230 -0
  72. package/src/primitives/app_dirs/root.zig +440 -0
  73. package/src/primitives/app_manifest/root.zig +109 -0
  74. package/src/primitives/app_manifest/tests.zig +758 -0
  75. package/src/primitives/app_manifest/types.zig +587 -0
  76. package/src/primitives/app_manifest/validation.zig +828 -0
  77. package/src/primitives/assets/root.zig +520 -0
  78. package/src/primitives/canvas/a11y_audit.zig +390 -0
  79. package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
  80. package/src/primitives/canvas/app_icon.zig +1590 -0
  81. package/src/primitives/canvas/chart.zig +392 -0
  82. package/src/primitives/canvas/chart_tests.zig +751 -0
  83. package/src/primitives/canvas/commands.zig +446 -0
  84. package/src/primitives/canvas/drawing.zig +309 -0
  85. package/src/primitives/canvas/equality.zig +316 -0
  86. package/src/primitives/canvas/events.zig +696 -0
  87. package/src/primitives/canvas/font_coverage.zig +118 -0
  88. package/src/primitives/canvas/font_ttf.zig +510 -0
  89. package/src/primitives/canvas/font_ttf_tests.zig +280 -0
  90. package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
  91. package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
  92. package/src/primitives/canvas/frame.zig +679 -0
  93. package/src/primitives/canvas/frame_metrics.zig +462 -0
  94. package/src/primitives/canvas/gpu.zig +639 -0
  95. package/src/primitives/canvas/hash.zig +112 -0
  96. package/src/primitives/canvas/icons/alert.svg +1 -0
  97. package/src/primitives/canvas/icons/archive.svg +1 -0
  98. package/src/primitives/canvas/icons/arrow-down.svg +1 -0
  99. package/src/primitives/canvas/icons/arrow-right.svg +1 -0
  100. package/src/primitives/canvas/icons/arrow-up.svg +1 -0
  101. package/src/primitives/canvas/icons/check-circle.svg +1 -0
  102. package/src/primitives/canvas/icons/check.svg +1 -0
  103. package/src/primitives/canvas/icons/chevron-down.svg +1 -0
  104. package/src/primitives/canvas/icons/chevron-left.svg +1 -0
  105. package/src/primitives/canvas/icons/chevron-right.svg +1 -0
  106. package/src/primitives/canvas/icons/chevron-up.svg +1 -0
  107. package/src/primitives/canvas/icons/circle-dot.svg +1 -0
  108. package/src/primitives/canvas/icons/clock.svg +1 -0
  109. package/src/primitives/canvas/icons/copy.svg +1 -0
  110. package/src/primitives/canvas/icons/download.svg +1 -0
  111. package/src/primitives/canvas/icons/edit.svg +1 -0
  112. package/src/primitives/canvas/icons/ellipsis.svg +1 -0
  113. package/src/primitives/canvas/icons/external-link.svg +1 -0
  114. package/src/primitives/canvas/icons/eye.svg +1 -0
  115. package/src/primitives/canvas/icons/file-text.svg +1 -0
  116. package/src/primitives/canvas/icons/folder-open.svg +1 -0
  117. package/src/primitives/canvas/icons/folder.svg +1 -0
  118. package/src/primitives/canvas/icons/git-branch.svg +1 -0
  119. package/src/primitives/canvas/icons/git-merge.svg +1 -0
  120. package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
  121. package/src/primitives/canvas/icons/info.svg +1 -0
  122. package/src/primitives/canvas/icons/menu.svg +1 -0
  123. package/src/primitives/canvas/icons/missing.svg +1 -0
  124. package/src/primitives/canvas/icons/moon.svg +1 -0
  125. package/src/primitives/canvas/icons/music.svg +1 -0
  126. package/src/primitives/canvas/icons/panel-left.svg +1 -0
  127. package/src/primitives/canvas/icons/panel-right.svg +1 -0
  128. package/src/primitives/canvas/icons/pause.svg +1 -0
  129. package/src/primitives/canvas/icons/play.svg +1 -0
  130. package/src/primitives/canvas/icons/plus.svg +1 -0
  131. package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
  132. package/src/primitives/canvas/icons/repeat.svg +1 -0
  133. package/src/primitives/canvas/icons/save.svg +1 -0
  134. package/src/primitives/canvas/icons/search.svg +1 -0
  135. package/src/primitives/canvas/icons/send.svg +1 -0
  136. package/src/primitives/canvas/icons/settings.svg +1 -0
  137. package/src/primitives/canvas/icons/shuffle.svg +1 -0
  138. package/src/primitives/canvas/icons/skip-back.svg +1 -0
  139. package/src/primitives/canvas/icons/skip-forward.svg +1 -0
  140. package/src/primitives/canvas/icons/sun.svg +1 -0
  141. package/src/primitives/canvas/icons/terminal.svg +1 -0
  142. package/src/primitives/canvas/icons/trash.svg +1 -0
  143. package/src/primitives/canvas/icons/volume.svg +1 -0
  144. package/src/primitives/canvas/icons/wrench.svg +1 -0
  145. package/src/primitives/canvas/icons/x-circle.svg +1 -0
  146. package/src/primitives/canvas/icons/x.svg +1 -0
  147. package/src/primitives/canvas/icons.zig +249 -0
  148. package/src/primitives/canvas/layout_audit.zig +744 -0
  149. package/src/primitives/canvas/layout_audit_tests.zig +426 -0
  150. package/src/primitives/canvas/markdown.zig +1031 -0
  151. package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
  152. package/src/primitives/canvas/markdown_tests.zig +538 -0
  153. package/src/primitives/canvas/plan_key_index.zig +120 -0
  154. package/src/primitives/canvas/png.zig +355 -0
  155. package/src/primitives/canvas/reference.zig +1457 -0
  156. package/src/primitives/canvas/reference_blur.zig +104 -0
  157. package/src/primitives/canvas/reference_memo.zig +322 -0
  158. package/src/primitives/canvas/reference_paths.zig +184 -0
  159. package/src/primitives/canvas/reference_tests.zig +1650 -0
  160. package/src/primitives/canvas/render.zig +747 -0
  161. package/src/primitives/canvas/render_effects.zig +255 -0
  162. package/src/primitives/canvas/render_fingerprints.zig +256 -0
  163. package/src/primitives/canvas/render_generic_resources.zig +376 -0
  164. package/src/primitives/canvas/render_images.zig +247 -0
  165. package/src/primitives/canvas/render_layers.zig +260 -0
  166. package/src/primitives/canvas/render_paths.zig +347 -0
  167. package/src/primitives/canvas/render_resources.zig +51 -0
  168. package/src/primitives/canvas/render_tests.zig +3186 -0
  169. package/src/primitives/canvas/root.zig +744 -0
  170. package/src/primitives/canvas/serialization.zig +1528 -0
  171. package/src/primitives/canvas/svg_icon.zig +636 -0
  172. package/src/primitives/canvas/svg_icon_tests.zig +312 -0
  173. package/src/primitives/canvas/test_support.zig +422 -0
  174. package/src/primitives/canvas/testdata/markdown_document.md +83 -0
  175. package/src/primitives/canvas/tests.zig +30 -0
  176. package/src/primitives/canvas/text.zig +82 -0
  177. package/src/primitives/canvas/text_atlas.zig +408 -0
  178. package/src/primitives/canvas/text_batch_tests.zig +431 -0
  179. package/src/primitives/canvas/text_interaction.zig +615 -0
  180. package/src/primitives/canvas/text_layout.zig +1069 -0
  181. package/src/primitives/canvas/text_layout_cache.zig +281 -0
  182. package/src/primitives/canvas/text_layout_hash.zig +113 -0
  183. package/src/primitives/canvas/text_layout_types.zig +128 -0
  184. package/src/primitives/canvas/text_measure_cache.zig +279 -0
  185. package/src/primitives/canvas/text_metrics.zig +309 -0
  186. package/src/primitives/canvas/text_metrics_tests.zig +262 -0
  187. package/src/primitives/canvas/text_span_tests.zig +608 -0
  188. package/src/primitives/canvas/text_spans.zig +1010 -0
  189. package/src/primitives/canvas/text_tests.zig +1949 -0
  190. package/src/primitives/canvas/themes/geist.zig +470 -0
  191. package/src/primitives/canvas/tokens.zig +1522 -0
  192. package/src/primitives/canvas/ui.zig +2686 -0
  193. package/src/primitives/canvas/ui_markup.zig +3791 -0
  194. package/src/primitives/canvas/ui_markup_binary.zig +522 -0
  195. package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
  196. package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
  197. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
  198. package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
  199. package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
  200. package/src/primitives/canvas/ui_markup_edit.zig +416 -0
  201. package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
  202. package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
  203. package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
  204. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
  205. package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
  206. package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
  207. package/src/primitives/canvas/ui_markup_view.zig +2352 -0
  208. package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
  209. package/src/primitives/canvas/ui_provenance.zig +96 -0
  210. package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
  211. package/src/primitives/canvas/ui_schema.zig +825 -0
  212. package/src/primitives/canvas/ui_schema_tests.zig +128 -0
  213. package/src/primitives/canvas/ui_tests.zig +1432 -0
  214. package/src/primitives/canvas/vector.zig +919 -0
  215. package/src/primitives/canvas/vector_tests.zig +547 -0
  216. package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
  217. package/src/primitives/canvas/virtual_extents.zig +540 -0
  218. package/src/primitives/canvas/widget_access.zig +208 -0
  219. package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
  220. package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
  221. package/src/primitives/canvas/widget_invalidation.zig +748 -0
  222. package/src/primitives/canvas/widget_layout.zig +2055 -0
  223. package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
  224. package/src/primitives/canvas/widget_metrics.zig +228 -0
  225. package/src/primitives/canvas/widget_render.zig +2534 -0
  226. package/src/primitives/canvas/widget_render_controls.zig +1563 -0
  227. package/src/primitives/canvas/widget_render_scroll.zig +136 -0
  228. package/src/primitives/canvas/widget_render_style.zig +626 -0
  229. package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
  230. package/src/primitives/canvas/widget_routing.zig +475 -0
  231. package/src/primitives/canvas/widget_runtime.zig +307 -0
  232. package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
  233. package/src/primitives/canvas/widget_semantics.zig +480 -0
  234. package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
  235. package/src/primitives/canvas/widget_tests.zig +8 -0
  236. package/src/primitives/canvas/widget_text_input.zig +351 -0
  237. package/src/primitives/canvas/widget_text_select.zig +147 -0
  238. package/src/primitives/canvas/widget_tree.zig +232 -0
  239. package/src/primitives/canvas/widgets.zig +1319 -0
  240. package/src/primitives/diagnostics/root.zig +445 -0
  241. package/src/primitives/geometry/root.zig +889 -0
  242. package/src/primitives/json/root.zig +272 -0
  243. package/src/primitives/platform_info/root.zig +450 -0
  244. package/src/primitives/trace/root.zig +569 -0
  245. package/src/root.zig +165 -0
  246. package/src/runtime/api.zig +431 -0
  247. package/src/runtime/async_bridge.zig +49 -0
  248. package/src/runtime/automation_commands.zig +686 -0
  249. package/src/runtime/automation_liveness_tests.zig +140 -0
  250. package/src/runtime/automation_snapshot.zig +246 -0
  251. package/src/runtime/automation_widget_dispatch.zig +617 -0
  252. package/src/runtime/bridge_dispatch_tests.zig +202 -0
  253. package/src/runtime/bridge_payload.zig +194 -0
  254. package/src/runtime/bridge_permission_tests.zig +551 -0
  255. package/src/runtime/bridge_responses.zig +414 -0
  256. package/src/runtime/builtin_bridge.zig +554 -0
  257. package/src/runtime/canvas_budget_edge_tests.zig +415 -0
  258. package/src/runtime/canvas_font_tests.zig +410 -0
  259. package/src/runtime/canvas_fonts.zig +229 -0
  260. package/src/runtime/canvas_frame.zig +1734 -0
  261. package/src/runtime/canvas_frame_animation_tests.zig +609 -0
  262. package/src/runtime/canvas_frame_cache_tests.zig +333 -0
  263. package/src/runtime/canvas_frame_helpers.zig +414 -0
  264. package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
  265. package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
  266. package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
  267. package/src/runtime/canvas_frame_retained_tests.zig +753 -0
  268. package/src/runtime/canvas_frame_tests.zig +9 -0
  269. package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
  270. package/src/runtime/canvas_image_tests.zig +362 -0
  271. package/src/runtime/canvas_images.zig +224 -0
  272. package/src/runtime/canvas_limits.zig +317 -0
  273. package/src/runtime/canvas_screenshot_tests.zig +289 -0
  274. package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
  275. package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
  276. package/src/runtime/canvas_widget_chart_tests.zig +249 -0
  277. package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
  278. package/src/runtime/canvas_widget_context_menu.zig +318 -0
  279. package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
  280. package/src/runtime/canvas_widget_control_tests.zig +2164 -0
  281. package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
  282. package/src/runtime/canvas_widget_display.zig +677 -0
  283. package/src/runtime/canvas_widget_event_tests.zig +1779 -0
  284. package/src/runtime/canvas_widget_events.zig +1098 -0
  285. package/src/runtime/canvas_widget_floating_tests.zig +695 -0
  286. package/src/runtime/canvas_widget_layout_tests.zig +26 -0
  287. package/src/runtime/canvas_widget_runtime.zig +1335 -0
  288. package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
  289. package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
  290. package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
  291. package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
  292. package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
  293. package/src/runtime/canvas_widget_state.zig +798 -0
  294. package/src/runtime/canvas_widget_text_tests.zig +2104 -0
  295. package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
  296. package/src/runtime/clock.zig +231 -0
  297. package/src/runtime/core.zig +927 -0
  298. package/src/runtime/effects.zig +4197 -0
  299. package/src/runtime/effects_audio_tests.zig +690 -0
  300. package/src/runtime/effects_clipboard_tests.zig +317 -0
  301. package/src/runtime/effects_fetch_tests.zig +1031 -0
  302. package/src/runtime/effects_file_tests.zig +407 -0
  303. package/src/runtime/effects_tests.zig +1251 -0
  304. package/src/runtime/flow.zig +1175 -0
  305. package/src/runtime/frame_profile.zig +221 -0
  306. package/src/runtime/gpu_surface_events.zig +639 -0
  307. package/src/runtime/launch_timing.zig +59 -0
  308. package/src/runtime/markdown_app_tests.zig +224 -0
  309. package/src/runtime/native_command_event_tests.zig +431 -0
  310. package/src/runtime/platform_bridge_tests.zig +8 -0
  311. package/src/runtime/platform_config_tests.zig +392 -0
  312. package/src/runtime/root.zig +150 -0
  313. package/src/runtime/runtime_event_tests.zig +1079 -0
  314. package/src/runtime/session_journal.zig +1522 -0
  315. package/src/runtime/session_record.zig +332 -0
  316. package/src/runtime/session_replay.zig +274 -0
  317. package/src/runtime/session_state.zig +30 -0
  318. package/src/runtime/session_tests.zig +399 -0
  319. package/src/runtime/shell_layout.zig +472 -0
  320. package/src/runtime/shell_layout_tests.zig +1246 -0
  321. package/src/runtime/shell_view_tests.zig +604 -0
  322. package/src/runtime/state.zig +110 -0
  323. package/src/runtime/system_services.zig +392 -0
  324. package/src/runtime/test_support.zig +79 -0
  325. package/src/runtime/tests.zig +30 -0
  326. package/src/runtime/ui_app.zig +3290 -0
  327. package/src/runtime/ui_app_huge_model_tests.zig +351 -0
  328. package/src/runtime/ui_app_provenance.zig +370 -0
  329. package/src/runtime/ui_app_tests.zig +3254 -0
  330. package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
  331. package/src/runtime/ui_app_window_tests.zig +442 -0
  332. package/src/runtime/validation.zig +206 -0
  333. package/src/runtime/view.zig +908 -0
  334. package/src/runtime/view_canvas.zig +850 -0
  335. package/src/runtime/view_widget_control.zig +433 -0
  336. package/src/runtime/view_widget_scroll.zig +360 -0
  337. package/src/runtime/view_widget_text.zig +275 -0
  338. package/src/runtime/view_widget_tree.zig +843 -0
  339. package/src/runtime/webview_view_bridge_tests.zig +664 -0
  340. package/src/runtime/widget_bridge.zig +231 -0
  341. package/src/runtime/window_command_bridge_tests.zig +403 -0
  342. package/src/runtime/window_storage.zig +255 -0
  343. package/src/runtime/window_view_runtime.zig +727 -0
  344. package/src/runtime/window_views.zig +334 -0
  345. package/src/security/root.zig +118 -0
  346. package/src/tooling/android.zig +1172 -0
  347. package/src/tooling/assets.zig +128 -0
  348. package/src/tooling/buildgraph.zig +379 -0
  349. package/src/tooling/cef.zig +671 -0
  350. package/src/tooling/codesign.zig +172 -0
  351. package/src/tooling/components/identity_tests.zig +185 -0
  352. package/src/tooling/components/stepper.zig +113 -0
  353. package/src/tooling/components/timeline.native +27 -0
  354. package/src/tooling/components/timeline_item.zig +131 -0
  355. package/src/tooling/default_icon.icns +0 -0
  356. package/src/tooling/default_icon.png +0 -0
  357. package/src/tooling/dev.zig +172 -0
  358. package/src/tooling/doctor.zig +349 -0
  359. package/src/tooling/eject_components.zig +170 -0
  360. package/src/tooling/embedlib.zig +290 -0
  361. package/src/tooling/ios.zig +528 -0
  362. package/src/tooling/manifest.zig +1939 -0
  363. package/src/tooling/package.zig +2388 -0
  364. package/src/tooling/process_tree.zig +114 -0
  365. package/src/tooling/raw_manifest.zig +193 -0
  366. package/src/tooling/root.zig +22 -0
  367. package/src/tooling/templates.zig +3191 -0
  368. package/src/tooling/toolchain.zig +371 -0
  369. package/src/tooling/verbs.zig +204 -0
  370. package/src/tooling/web_engine.zig +135 -0
  371. package/src/tooling/xcodeproj.zig +462 -0
  372. package/src/window_state/root.zig +507 -0
  373. package/index.js +0 -2
@@ -0,0 +1,2388 @@
1
+ const std = @import("std");
2
+ const android_tool = @import("android.zig");
3
+ const app_icon_tool = @import("app_icon");
4
+ const assets_tool = @import("assets.zig");
5
+ const cef = @import("cef.zig");
6
+ const codesign = @import("codesign.zig");
7
+ const diagnostics = @import("diagnostics");
8
+ const ios_tool = @import("ios.zig");
9
+ const manifest_tool = @import("manifest.zig");
10
+ const web_engine_tool = @import("web_engine.zig");
11
+ const xcodeproj_tool = @import("xcodeproj.zig");
12
+
13
+ /// The SDK's default app icon (kept in sync by `zig build generate-icon`):
14
+ /// what a bundle ships when app.zon configures no usable icon at all, so
15
+ /// a fresh package is never a text placeholder pretending to be an icon.
16
+ const default_icon_icns = @embedFile("default_icon.icns");
17
+ /// The same default as a PNG source, for targets whose icons re-render
18
+ /// from a square source (the iOS asset catalog).
19
+ const default_icon_png = @embedFile("default_icon.png");
20
+
21
+ pub const PackageTarget = enum {
22
+ macos,
23
+ windows,
24
+ linux,
25
+ ios,
26
+ android,
27
+
28
+ pub fn parse(value: []const u8) ?PackageTarget {
29
+ inline for (@typeInfo(PackageTarget).@"enum".fields) |field| {
30
+ if (std.mem.eql(u8, value, field.name)) return @enumFromInt(field.value);
31
+ }
32
+ return null;
33
+ }
34
+ };
35
+
36
+ pub const SigningMode = enum {
37
+ none,
38
+ adhoc,
39
+ identity,
40
+
41
+ pub fn parse(value: []const u8) ?SigningMode {
42
+ if (std.mem.eql(u8, value, "none")) return .none;
43
+ if (std.mem.eql(u8, value, "adhoc") or std.mem.eql(u8, value, "ad-hoc")) return .adhoc;
44
+ if (std.mem.eql(u8, value, "identity")) return .identity;
45
+ return null;
46
+ }
47
+ };
48
+
49
+ pub const WebEngine = web_engine_tool.Engine;
50
+
51
+ pub const SigningConfig = struct {
52
+ mode: SigningMode = .none,
53
+ identity: ?[]const u8 = null,
54
+ entitlements: ?[]const u8 = null,
55
+ profile: ?[]const u8 = null,
56
+ team_id: ?[]const u8 = null,
57
+ };
58
+
59
+ pub const PackageOptions = struct {
60
+ metadata: manifest_tool.Metadata,
61
+ target: PackageTarget = .macos,
62
+ optimize: []const u8 = "Debug",
63
+ output_path: []const u8,
64
+ binary_path: ?[]const u8 = null,
65
+ assets_dir: []const u8 = "assets",
66
+ frontend: ?manifest_tool.FrontendMetadata = null,
67
+ web_engine: WebEngine = .system,
68
+ cef_dir: []const u8 = web_engine_tool.default_cef_dir,
69
+ signing: SigningConfig = .{},
70
+ archive: bool = false,
71
+ /// The process environment, when the caller has one (the CLI). The
72
+ /// Android artifact probes it for the SDK/NDK toolchain to assemble
73
+ /// the debug APK; without it the generated project is still complete
74
+ /// and the assembly is skipped with a notice — which also keeps
75
+ /// package unit tests hermetic.
76
+ env_map: ?*std.process.Environ.Map = null,
77
+ };
78
+
79
+ pub const PackageStats = struct {
80
+ path: []const u8,
81
+ artifact_name: []const u8 = "",
82
+ target: PackageTarget = .macos,
83
+ signing_mode: SigningMode = .none,
84
+ asset_count: usize = 0,
85
+ web_engine: WebEngine = .system,
86
+ archive_path: ?[]const u8 = null,
87
+ };
88
+
89
+ pub fn artifactName(buffer: []u8, metadata: manifest_tool.Metadata, target: PackageTarget, optimize: []const u8) ![]const u8 {
90
+ return std.fmt.bufPrint(buffer, "{s}-{s}-{s}-{s}{s}", .{
91
+ metadata.name,
92
+ metadata.version,
93
+ @tagName(target),
94
+ optimize,
95
+ artifactSuffix(target),
96
+ });
97
+ }
98
+
99
+ pub fn createPackage(allocator: std.mem.Allocator, io: std.Io, options: PackageOptions) !PackageStats {
100
+ try validateWebEngineTarget(options.target, options.web_engine);
101
+ var stats = switch (options.target) {
102
+ .macos => try createMacosApp(allocator, io, options),
103
+ .windows, .linux => try createDesktopArtifact(allocator, io, options),
104
+ .ios => try createIosArtifact(allocator, io, options),
105
+ .android => try createAndroidArtifact(allocator, io, options),
106
+ };
107
+ if (options.archive) {
108
+ const archive_path = try createArchive(allocator, io, options);
109
+ if (archive_path) |path| {
110
+ stats.archive_path = path;
111
+ }
112
+ }
113
+ return stats;
114
+ }
115
+
116
+ fn validateWebEngineTarget(target: PackageTarget, web_engine: WebEngine) !void {
117
+ if (web_engine != .chromium) return;
118
+ switch (target) {
119
+ .macos, .ios, .android => {},
120
+ .windows, .linux => return error.UnsupportedWebEngine,
121
+ }
122
+ }
123
+
124
+ pub fn printDiagnostic(stats: PackageStats) void {
125
+ var buffer: [256]u8 = undefined;
126
+ var message_buffer: [192]u8 = undefined;
127
+ var writer = std.Io.Writer.fixed(&buffer);
128
+ diagnostics.formatShort(.{
129
+ .severity = .info,
130
+ .code = diagnostics.code("package", "created"),
131
+ .message = std.fmt.bufPrint(&message_buffer, "created {s} artifact at {s}", .{ @tagName(stats.target), stats.path }) catch "created package",
132
+ }, &writer) catch return;
133
+ std.debug.print("{s}\n", .{writer.buffered()});
134
+ if (stats.archive_path) |archive| {
135
+ std.debug.print(" archive: {s}\n", .{archive});
136
+ }
137
+ }
138
+
139
+ pub fn createLocalPackage(io: std.Io, output_path: []const u8) !PackageStats {
140
+ const metadata: manifest_tool.Metadata = .{
141
+ .id = "dev.native_sdk.local",
142
+ .name = "native-sdk-local",
143
+ .version = "0.1.0",
144
+ };
145
+ return createMacosApp(std.heap.page_allocator, io, .{
146
+ .metadata = metadata,
147
+ .output_path = output_path,
148
+ .binary_path = null,
149
+ });
150
+ }
151
+
152
+ pub fn createMacosApp(allocator: std.mem.Allocator, io: std.Io, options: PackageOptions) !PackageStats {
153
+ var cwd = std.Io.Dir.cwd();
154
+ try cwd.createDirPath(io, options.output_path);
155
+ var package_dir = try cwd.openDir(io, options.output_path, .{});
156
+ defer package_dir.close(io);
157
+ try package_dir.createDirPath(io, "Contents/MacOS");
158
+ try package_dir.createDirPath(io, "Contents/Resources");
159
+
160
+ const executable_name = std.fs.path.basename(options.metadata.name);
161
+ if (options.binary_path) |binary_path| {
162
+ const executable_subpath = try std.fmt.allocPrint(allocator, "Contents/MacOS/{s}", .{executable_name});
163
+ defer allocator.free(executable_subpath);
164
+ try copyFileToDir(allocator, io, package_dir, binary_path, executable_subpath);
165
+ try makeExecutable(package_dir, io, executable_subpath);
166
+ } else {
167
+ try writeFile(package_dir, io, "Contents/MacOS/README.txt", "No app binary was supplied for this local package.\n");
168
+ }
169
+
170
+ const info_plist = try macosInfoPlist(allocator, options.metadata, executable_name);
171
+ defer allocator.free(info_plist);
172
+ try writeFile(package_dir, io, "Contents/Info.plist", info_plist);
173
+ try writeFile(package_dir, io, "Contents/PkgInfo", "APPL????");
174
+ try writeFile(package_dir, io, "Contents/Resources/README.txt", "Unsigned local Native SDK macOS app bundle.\n");
175
+ const assets_output = try macosAssetOutputPath(allocator, options);
176
+ defer allocator.free(assets_output);
177
+ const bundle_stats = try assets_tool.bundle(allocator, io, options.assets_dir, assets_output);
178
+ try copyMacosIcon(allocator, io, package_dir, options);
179
+ try copyMacosDocumentIcons(allocator, io, package_dir, options.metadata);
180
+ try writeReport(allocator, package_dir, io, "Contents/Resources/package-manifest.zon", options, executable_name, bundle_stats.asset_count);
181
+ if (options.web_engine == .chromium) {
182
+ try cef.ensureLayout(io, options.cef_dir);
183
+ try copyMacosCefRuntime(allocator, io, package_dir, options.cef_dir);
184
+ }
185
+ try runSigning(allocator, io, package_dir, options);
186
+
187
+ return .{
188
+ .path = options.output_path,
189
+ .artifact_name = std.fs.path.basename(options.output_path),
190
+ .target = .macos,
191
+ .signing_mode = options.signing.mode,
192
+ .asset_count = bundle_stats.asset_count,
193
+ .web_engine = options.web_engine,
194
+ };
195
+ }
196
+
197
+ fn createDesktopArtifact(allocator: std.mem.Allocator, io: std.Io, options: PackageOptions) !PackageStats {
198
+ var cwd = std.Io.Dir.cwd();
199
+ try cwd.createDirPath(io, options.output_path);
200
+ var dir = try cwd.openDir(io, options.output_path, .{});
201
+ defer dir.close(io);
202
+ try dir.createDirPath(io, "bin");
203
+ try dir.createDirPath(io, "resources");
204
+
205
+ const executable_name = if (options.target == .windows)
206
+ try std.fmt.allocPrint(allocator, "{s}.exe", .{options.metadata.name})
207
+ else
208
+ try allocator.dupe(u8, options.metadata.name);
209
+ defer allocator.free(executable_name);
210
+
211
+ if (options.binary_path) |binary_path| {
212
+ const binary_subpath = try std.fmt.allocPrint(allocator, "bin/{s}", .{executable_name});
213
+ defer allocator.free(binary_subpath);
214
+ try copyFileToDir(allocator, io, dir, binary_path, binary_subpath);
215
+ } else {
216
+ try writeFile(dir, io, "bin/README.txt", "Build the app binary separately and place it here for this target.\n");
217
+ }
218
+
219
+ const assets_output = try assetOutputPath(allocator, options.output_path, "resources", options);
220
+ defer allocator.free(assets_output);
221
+ const bundle_stats = try assets_tool.bundle(allocator, io, options.assets_dir, assets_output);
222
+ try writeFile(dir, io, "README.txt", artifactReadme(options.target));
223
+ if (options.target == .linux) {
224
+ try dir.createDirPath(io, "share/applications");
225
+ try dir.createDirPath(io, "share/icons");
226
+ const desktop_entry = try linuxDesktopEntry(allocator, options.metadata);
227
+ defer allocator.free(desktop_entry);
228
+ const desktop_path = try std.fmt.allocPrint(allocator, "share/applications/{s}.desktop", .{options.metadata.name});
229
+ defer allocator.free(desktop_path);
230
+ try writeFile(dir, io, desktop_path, desktop_entry);
231
+ if (options.metadata.file_associations.len > 0) {
232
+ try dir.createDirPath(io, "share/mime/packages");
233
+ const mime_info = try linuxMimeInfo(allocator, options.metadata);
234
+ defer allocator.free(mime_info);
235
+ const mime_path = try std.fmt.allocPrint(allocator, "share/mime/packages/{s}.xml", .{options.metadata.name});
236
+ defer allocator.free(mime_path);
237
+ try writeFile(dir, io, mime_path, mime_info);
238
+ }
239
+ try writeLinuxIcons(allocator, io, dir, options.metadata);
240
+ } else if (options.target == .windows) {
241
+ try writeWindowsIcon(allocator, io, dir, options.metadata);
242
+ if (hasRegistrationMetadata(options.metadata)) {
243
+ try dir.createDirPath(io, "install");
244
+ const registry_script = try windowsRegistrationScript(allocator, options.metadata, executable_name);
245
+ defer allocator.free(registry_script);
246
+ try writeFile(dir, io, "install/register-file-types.ps1", registry_script);
247
+ }
248
+ }
249
+ if (options.web_engine == .chromium) {
250
+ const cef_platform = cefPlatformForTarget(options.target) orelse return error.UnsupportedWebEngine;
251
+ try cef.ensureLayoutFor(io, cef_platform, options.cef_dir);
252
+ try copyDesktopCefRuntime(allocator, io, dir, options.target, options.cef_dir);
253
+ }
254
+ try writeReport(allocator, dir, io, "package-manifest.zon", options, executable_name, bundle_stats.asset_count);
255
+ return .{ .path = options.output_path, .artifact_name = std.fs.path.basename(options.output_path), .target = options.target, .asset_count = bundle_stats.asset_count, .web_engine = options.web_engine };
256
+ }
257
+
258
+ /// The iOS host tier: a COMPLETE Xcode project the user never edits —
259
+ /// the toolkit-owned UIKit host sources, the generated Info.plist and
260
+ /// asset catalog, the bundled app assets, and the embed static library,
261
+ /// tied together by a deterministic project file (xcodeproj.zig) so
262
+ /// `xcodebuild archive` works with zero edits. Everything app-specific
263
+ /// comes from app.zon. Code signing stays manual, like macOS
264
+ /// notarization.
265
+ fn createIosArtifact(allocator: std.mem.Allocator, io: std.Io, options: PackageOptions) !PackageStats {
266
+ var cwd = std.Io.Dir.cwd();
267
+ try cwd.createDirPath(io, options.output_path);
268
+ var dir = try cwd.openDir(io, options.output_path, .{});
269
+ defer dir.close(io);
270
+
271
+ // iOS bundle identifiers allow only alphanumerics, hyphens, and
272
+ // periods; underscores in app.zon ids normalize to hyphens (the
273
+ // mirror of the Android hyphen-to-underscore mapping).
274
+ const bundle_id = try ios_tool.bundleIdAlloc(allocator, options.metadata.id);
275
+ defer allocator.free(bundle_id);
276
+ const project = xcodeproj_tool.ProjectModel{
277
+ .name = options.metadata.name,
278
+ .bundle_id = bundle_id,
279
+ .version = options.metadata.version,
280
+ };
281
+
282
+ // The project file and its shared scheme (headless xcodebuild needs
283
+ // an on-disk scheme; Xcode only auto-creates them interactively).
284
+ const project_dir = try std.fmt.allocPrint(allocator, "{s}.xcodeproj", .{options.metadata.name});
285
+ defer allocator.free(project_dir);
286
+ const schemes_dir = try std.fmt.allocPrint(allocator, "{s}/xcshareddata/xcschemes", .{project_dir});
287
+ defer allocator.free(schemes_dir);
288
+ try dir.createDirPath(io, schemes_dir);
289
+ const pbxproj = try xcodeproj_tool.pbxprojAlloc(allocator, project);
290
+ defer allocator.free(pbxproj);
291
+ const pbxproj_path = try std.fmt.allocPrint(allocator, "{s}/project.pbxproj", .{project_dir});
292
+ defer allocator.free(pbxproj_path);
293
+ try writeFile(dir, io, pbxproj_path, pbxproj);
294
+ const scheme = try xcodeproj_tool.schemeAlloc(allocator, project);
295
+ defer allocator.free(scheme);
296
+ const scheme_path = try std.fmt.allocPrint(allocator, "{s}/{s}.xcscheme", .{ schemes_dir, options.metadata.name });
297
+ defer allocator.free(scheme_path);
298
+ try writeFile(dir, io, scheme_path, scheme);
299
+
300
+ // The toolkit host sources and the app.zon-derived Info.plist.
301
+ try dir.createDirPath(io, "Host");
302
+ try writeFile(dir, io, "Host/" ++ ios_tool.host_source_name, ios_tool.host_source);
303
+ try writeFile(dir, io, "Host/" ++ ios_tool.host_header_name, ios_tool.host_header);
304
+ const info_plist = try ios_tool.infoPlistAlloc(allocator, options.metadata);
305
+ defer allocator.free(info_plist);
306
+ try writeFile(dir, io, "Host/Info.plist", info_plist);
307
+
308
+ // App icon (single-source pipeline) and bundled assets. The bundled
309
+ // folder is named "Assets", NOT "Resources": a bundle-root directory
310
+ // named Resources makes CFBundle read the .app as a deep
311
+ // (macOS-layout) bundle, and xcodebuild's archive stamping then fails
312
+ // to find the Info.plist ("Archive Missing Bundle Identifier").
313
+ try writeIosIcon(allocator, io, dir, options.metadata);
314
+ const assets_output = try assetOutputPath(allocator, options.output_path, "Assets", options);
315
+ defer allocator.free(assets_output);
316
+ const bundle_stats = try assets_tool.bundle(allocator, io, options.assets_dir, assets_output);
317
+
318
+ // The app's embed static library (device arm64 slice — built by the
319
+ // CLI before packaging, or passed via --binary).
320
+ try dir.createDirPath(io, "Libraries");
321
+ if (options.binary_path) |binary_path| try copyFileToDir(allocator, io, dir, binary_path, "Libraries/libnative-sdk.a");
322
+
323
+ const readme = try iosProjectReadme(allocator, options.metadata);
324
+ defer allocator.free(readme);
325
+ try writeFile(dir, io, "README.md", readme);
326
+ try writeReport(allocator, dir, io, "package-manifest.zon", options, "libnative-sdk.a", bundle_stats.asset_count);
327
+ return .{ .path = options.output_path, .artifact_name = std.fs.path.basename(options.output_path), .target = .ios, .asset_count = bundle_stats.asset_count, .web_engine = options.web_engine };
328
+ }
329
+
330
+ fn iosProjectReadme(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata) ![]const u8 {
331
+ return std.fmt.allocPrint(allocator,
332
+ \\# {s} — iOS host project
333
+ \\
334
+ \\Generated by `native package --target ios`. Everything here is toolkit-owned output derived from app.zon — regenerate instead of editing.
335
+ \\
336
+ \\- `{s}.xcodeproj` — deterministic project with a shared scheme; `xcodebuild -scheme {s} archive` works with zero edits (for an unsigned verification pass add `CODE_SIGNING_ALLOWED=NO`).
337
+ \\- `Host/` — the toolkit UIKit host (canvas presentation, touch/keyboard/IME, safe areas) and the generated Info.plist.
338
+ \\- `Libraries/libnative-sdk.a` — the app compiled as the embed static library (device arm64 slice). The simulator loop is `native dev --target ios`, which rebuilds the library for the simulator.
339
+ \\- `Assets.xcassets`, `Assets/` — the app icon rendered from the single-source icon pipeline, and the bundled app assets.
340
+ \\
341
+ \\Code signing stays manual, like macOS notarization: open the project once in Xcode to pick a team, or pass `DEVELOPMENT_TEAM=<id> CODE_SIGN_IDENTITY="Apple Development"` to xcodebuild.
342
+ \\
343
+ , .{ metadata.displayName(), metadata.name, metadata.name });
344
+ }
345
+
346
+ /// The Android host tier: a COMPLETE generated host project the user
347
+ /// never edits — the toolkit-owned host sources, the app.zon-derived
348
+ /// AndroidManifest.xml, launcher icons, the bundled app assets, and the
349
+ /// embed static library — plus the assembled debug APK when the Android
350
+ /// SDK/NDK toolchain is present. The APK assembles directly with the
351
+ /// SDK's build tools (aapt2/javac/d8/zipalign/apksigner; see
352
+ /// android.zig for the rationale). Store signing keys stay manual, like
353
+ /// macOS notarization.
354
+ fn createAndroidArtifact(allocator: std.mem.Allocator, io: std.Io, options: PackageOptions) !PackageStats {
355
+ var cwd = std.Io.Dir.cwd();
356
+ try cwd.createDirPath(io, options.output_path);
357
+ var dir = try cwd.openDir(io, options.output_path, .{});
358
+ defer dir.close(io);
359
+
360
+ // The toolkit host sources and the app.zon-derived manifest.
361
+ try dir.createDirPath(io, "Host");
362
+ try writeFile(dir, io, "Host/" ++ android_tool.host_activity_name, android_tool.host_activity_source);
363
+ try writeFile(dir, io, "Host/" ++ android_tool.host_bridge_name, android_tool.host_bridge_source);
364
+ try writeFile(dir, io, "Host/" ++ android_tool.host_header_name, android_tool.host_header);
365
+ const manifest = try android_tool.manifestAlloc(allocator, options.metadata, true);
366
+ defer allocator.free(manifest);
367
+ try writeFile(dir, io, "AndroidManifest.xml", manifest);
368
+
369
+ // Launcher icons (single-source pipeline, default fallback so the
370
+ // manifest's @mipmap reference always resolves) and bundled assets
371
+ // in the layout the host mirrors onto the device at first launch.
372
+ try writeAndroidIcons(allocator, io, dir, options.metadata);
373
+ const res_path = try std.fs.path.join(allocator, &.{ options.output_path, "res" });
374
+ defer allocator.free(res_path);
375
+ try android_tool.writeHostResources(io, res_path);
376
+ const assets_output = try assetOutputPath(allocator, options.output_path, "assets/native-sdk", options);
377
+ defer allocator.free(assets_output);
378
+ const bundle_stats = try assets_tool.bundle(allocator, io, options.assets_dir, assets_output);
379
+
380
+ // The app's embed static library (aarch64-linux-android — built by
381
+ // the CLI before packaging, or passed via --binary).
382
+ try dir.createDirPath(io, "Libraries");
383
+ if (options.binary_path) |binary_path| try copyFileToDir(allocator, io, dir, binary_path, "Libraries/libnative-sdk.a");
384
+
385
+ const readme = try androidProjectReadme(allocator, options.metadata);
386
+ defer allocator.free(readme);
387
+ try writeFile(dir, io, "README.md", readme);
388
+ try writeReport(allocator, dir, io, "package-manifest.zon", options, "libnative-sdk.a", bundle_stats.asset_count);
389
+
390
+ var artifact_name: []const u8 = std.fs.path.basename(options.output_path);
391
+ if (try assembleAndroidApk(allocator, io, options)) |apk_name| {
392
+ artifact_name = apk_name;
393
+ }
394
+ return .{ .path = options.output_path, .artifact_name = artifact_name, .target = .android, .asset_count = bundle_stats.asset_count, .web_engine = options.web_engine };
395
+ }
396
+
397
+ /// Assemble the debug APK inside the generated project when the caller
398
+ /// supplied an environment to probe and the Android toolchain + a JDK
399
+ /// are installed. Returns the APK file name, or null when assembly was
400
+ /// skipped (with the reason printed — never silently).
401
+ fn assembleAndroidApk(allocator: std.mem.Allocator, io: std.Io, options: PackageOptions) !?[]const u8 {
402
+ const env_map = options.env_map orelse {
403
+ std.debug.print("native (android): project emitted without the debug APK (no environment to locate the Android toolchain in this context)\n", .{});
404
+ return null;
405
+ };
406
+ const binary_path = options.binary_path orelse {
407
+ std.debug.print("native (android): project emitted without the debug APK (no embed library was built or passed via --binary)\n", .{});
408
+ return null;
409
+ };
410
+ const tc = (try android_tool.findToolchain(allocator, io, env_map)) orelse return null;
411
+ defer tc.deinit(allocator);
412
+ const java = (try android_tool.resolveJavaAlloc(allocator, io, env_map)) orelse {
413
+ std.debug.print("native (android): project emitted without the debug APK (no JDK found - set JAVA_HOME)\n", .{});
414
+ return null;
415
+ };
416
+ defer allocator.free(java);
417
+
418
+ const host_dir = try std.fs.path.join(allocator, &.{ options.output_path, "Host" });
419
+ defer allocator.free(host_dir);
420
+ const work_dir = try std.fs.path.join(allocator, &.{ options.output_path, "build" });
421
+ defer allocator.free(work_dir);
422
+ const so_path = try std.fs.path.join(allocator, &.{ options.output_path, "build-libnative_sdk_host.so" });
423
+ defer allocator.free(so_path);
424
+ const manifest_path = try std.fs.path.join(allocator, &.{ options.output_path, "AndroidManifest.xml" });
425
+ defer allocator.free(manifest_path);
426
+ const res_dir = try std.fs.path.join(allocator, &.{ options.output_path, "res" });
427
+ defer allocator.free(res_dir);
428
+ const assets_dir = try std.fs.path.join(allocator, &.{ options.output_path, "assets", "native-sdk" });
429
+ defer allocator.free(assets_dir);
430
+ const apk_name = try std.fmt.allocPrint(allocator, "{s}-debug.apk", .{options.metadata.name});
431
+ errdefer allocator.free(apk_name);
432
+ const out_apk = try std.fs.path.join(allocator, &.{ options.output_path, apk_name });
433
+ defer allocator.free(out_apk);
434
+ const keystore_path = try android_tool.debugKeystorePathAlloc(allocator, env_map);
435
+ defer allocator.free(keystore_path);
436
+
437
+ std.debug.print("native (android): compiling the toolkit host library ({s})\n", .{android_tool.clang_target});
438
+ try android_tool.compileHostLibrary(allocator, io, &tc, host_dir, binary_path, so_path);
439
+ std.debug.print("native (android): assembling {s}\n", .{out_apk});
440
+ try android_tool.assembleApk(allocator, io, .{
441
+ .toolchain = &tc,
442
+ .java = java,
443
+ .work_dir = work_dir,
444
+ .manifest_path = manifest_path,
445
+ .host_dir = host_dir,
446
+ .so_path = so_path,
447
+ .res_dir = res_dir,
448
+ .assets_dir = assets_dir,
449
+ .keystore_path = keystore_path,
450
+ .out_apk = out_apk,
451
+ });
452
+ // The intermediates served their purpose; the .so is preserved in
453
+ // the APK itself.
454
+ std.Io.Dir.cwd().deleteTree(io, work_dir) catch {};
455
+ std.Io.Dir.cwd().deleteFile(io, so_path) catch {};
456
+ return apk_name;
457
+ }
458
+
459
+ fn androidProjectReadme(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata) ![]const u8 {
460
+ return std.fmt.allocPrint(allocator,
461
+ \\# {s} — Android host project
462
+ \\
463
+ \\Generated by `native package --target android`. Everything here is toolkit-owned output derived from app.zon — regenerate instead of editing.
464
+ \\
465
+ \\- `{s}-debug.apk` — the debug-signed APK, assembled directly with the Android SDK build tools (aapt2, javac, d8, zipalign, apksigner) and the NDK compiler when they are installed; rerun `native package --target android` after installing them if it is missing.
466
+ \\- `Host/` — the toolkit Android host: the activity (canvas presentation, touch/keyboard/IME, safe areas, Paint text measurement) and the JNI bridge over the embed C ABI.
467
+ \\- `AndroidManifest.xml`, `res/` — the app.zon-derived manifest and the launcher icons rendered from the single-source icon pipeline.
468
+ \\- `assets/native-sdk/` — the bundled app assets, mirrored into the app's files directory at first launch.
469
+ \\- `Libraries/libnative-sdk.a` — the app compiled as the embed static library (aarch64-linux-android). The device loop is `native dev --target android`, which rebuilds the library in Debug.
470
+ \\
471
+ \\The APK is debug-signed with the per-user toolkit keystore (~/.native/android/debug.keystore) for installs via `adb install`. Store distribution (Play upload keys, app bundles) stays a manual step, like macOS notarization.
472
+ \\
473
+ , .{ metadata.displayName(), metadata.name });
474
+ }
475
+
476
+ fn writeFile(dir: std.Io.Dir, io: std.Io, path: []const u8, bytes: []const u8) !void {
477
+ try dir.writeFile(io, .{ .sub_path = path, .data = bytes });
478
+ }
479
+
480
+ fn assetOutputPath(allocator: std.mem.Allocator, output_path: []const u8, resources_subpath: []const u8, options: PackageOptions) ![]const u8 {
481
+ if (options.frontend) |frontend| {
482
+ return std.fs.path.join(allocator, &.{ output_path, resources_subpath, frontend.dist });
483
+ }
484
+ return std.fs.path.join(allocator, &.{ output_path, resources_subpath });
485
+ }
486
+
487
+ /// Where the macOS bundle carries the app's assets. Frontend apps keep
488
+ /// the established Resources/<dist> layout their webview asset root
489
+ /// resolves against. Everything else mirrors the asset directory at its
490
+ /// app-relative path — Resources/assets by default — so a relative asset
491
+ /// path the app uses at runtime ("assets/music/track.mp3") names the
492
+ /// same file inside the bundle that it names in a dev run: the packaged
493
+ /// macOS host resolves relative asset paths against Resources. An
494
+ /// absolute or parent-escaping --assets directory has no app-relative
495
+ /// meaning a packaged process could resolve, so it keeps the flat
496
+ /// Resources layout.
497
+ fn macosAssetOutputPath(allocator: std.mem.Allocator, options: PackageOptions) ![]const u8 {
498
+ if (options.frontend != null) return assetOutputPath(allocator, options.output_path, "Contents/Resources", options);
499
+ if (appRelativeAssetSubpath(options.assets_dir)) |subpath| {
500
+ return std.fs.path.join(allocator, &.{ options.output_path, "Contents/Resources", subpath });
501
+ }
502
+ return std.fs.path.join(allocator, &.{ options.output_path, "Contents/Resources" });
503
+ }
504
+
505
+ /// The asset directory as an app-relative bundle subpath, or null when
506
+ /// it cannot honestly be one (empty, ".", absolute, or escaping the app
507
+ /// root through a ".." segment).
508
+ fn appRelativeAssetSubpath(assets_dir: []const u8) ?[]const u8 {
509
+ if (assets_dir.len == 0 or std.fs.path.isAbsolute(assets_dir)) return null;
510
+ var segments = std.mem.tokenizeAny(u8, assets_dir, "/\\");
511
+ var has_component = false;
512
+ while (segments.next()) |segment| {
513
+ if (std.mem.eql(u8, segment, "..")) return null;
514
+ if (!std.mem.eql(u8, segment, ".")) has_component = true;
515
+ }
516
+ if (!has_component) return null;
517
+ return assets_dir;
518
+ }
519
+
520
+ fn macosInfoPlist(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata, executable_name: []const u8) ![]const u8 {
521
+ const icon_name = macosIconFile(metadata);
522
+ const bundle_id = try xmlEscapeAlloc(allocator, metadata.id);
523
+ defer allocator.free(bundle_id);
524
+ const display_name = try xmlEscapeAlloc(allocator, metadata.displayName());
525
+ defer allocator.free(display_name);
526
+ const executable = try xmlEscapeAlloc(allocator, executable_name);
527
+ defer allocator.free(executable);
528
+ const icon = try xmlEscapeAlloc(allocator, icon_name);
529
+ defer allocator.free(icon);
530
+ const version = try xmlEscapeAlloc(allocator, metadata.version);
531
+ defer allocator.free(version);
532
+ const document_types = try macosDocumentTypes(allocator, metadata);
533
+ defer allocator.free(document_types);
534
+ const url_types = try macosUrlTypes(allocator, metadata);
535
+ defer allocator.free(url_types);
536
+ // The About panel's bottom line in packaged bundles: the manifest
537
+ // description rides NSHumanReadableCopyright, the plist key the
538
+ // standard About panel renders as its footer text — the same line
539
+ // dev runs pass to the panel directly.
540
+ const about_line = try macosAboutLine(allocator, metadata);
541
+ defer allocator.free(about_line);
542
+ // CFBundleName is the SHORT user-visible name — the application
543
+ // menu's title next to the Apple menu reads it — while
544
+ // CFBundleDisplayName serves the Finder and longer surfaces. Both
545
+ // carry the manifest display name so every user-facing surface
546
+ // (menu bar, Dock, Gatekeeper prompts) agrees; the manifest `.name`
547
+ // stays the executable's name, exactly like dev runs, whose host
548
+ // titles the application menu with the display name too.
549
+ return std.fmt.allocPrint(allocator,
550
+ \\<?xml version="1.0" encoding="UTF-8"?>
551
+ \\<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
552
+ \\<plist version="1.0">
553
+ \\<dict>
554
+ \\ <key>CFBundleIdentifier</key>
555
+ \\ <string>{s}</string>
556
+ \\ <key>CFBundleName</key>
557
+ \\ <string>{s}</string>
558
+ \\ <key>CFBundleDisplayName</key>
559
+ \\ <string>{s}</string>
560
+ \\ <key>CFBundleExecutable</key>
561
+ \\ <string>{s}</string>
562
+ \\ <key>CFBundleIconFile</key>
563
+ \\ <string>{s}</string>
564
+ \\ <key>CFBundlePackageType</key>
565
+ \\ <string>APPL</string>
566
+ \\ <key>LSMinimumSystemVersion</key>
567
+ \\ <string>11.0</string>
568
+ \\ <key>CFBundleShortVersionString</key>
569
+ \\ <string>{s}</string>
570
+ \\ <key>CFBundleVersion</key>
571
+ \\ <string>{s}</string>
572
+ \\{s}{s}{s}
573
+ \\</dict>
574
+ \\</plist>
575
+ \\
576
+ , .{ bundle_id, display_name, display_name, executable, icon, version, version, about_line, document_types, url_types });
577
+ }
578
+
579
+ /// The optional NSHumanReadableCopyright entry (with trailing newline)
580
+ /// for the manifest description, or "" when the manifest has none.
581
+ fn macosAboutLine(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata) ![]const u8 {
582
+ const description = metadata.description orelse return try allocator.dupe(u8, "");
583
+ const escaped = try xmlEscapeAlloc(allocator, description);
584
+ defer allocator.free(escaped);
585
+ return std.fmt.allocPrint(allocator, " <key>NSHumanReadableCopyright</key>\n <string>{s}</string>\n", .{escaped});
586
+ }
587
+
588
+ fn artifactSuffix(target: PackageTarget) []const u8 {
589
+ return switch (target) {
590
+ .macos => ".app",
591
+ .windows, .linux, .ios, .android => "",
592
+ };
593
+ }
594
+
595
+ fn artifactReadme(target: PackageTarget) []const u8 {
596
+ return switch (target) {
597
+ .windows => "Windows native-sdk artifact directory. Installer generation is future work.\n",
598
+ .linux => "Linux native-sdk artifact directory. AppImage, Flatpak, and tarball generation are future work.\n",
599
+ else => "native-sdk artifact directory.\n",
600
+ };
601
+ }
602
+
603
+ // ---------------------------------------------------------------------------
604
+ // App icons: one square source image (assets/icon.png or assets/icon.svg
605
+ // in app.zon `.icons`) generates every platform's artifacts through the
606
+ // built-in pipeline (`app_icon`). A prebuilt container in `.icons` always
607
+ // wins untouched for its platform: `.icns` on macOS, `.ico` on Windows.
608
+ // Precedence on macOS: explicit .icns > generated-from-image > the SDK
609
+ // default icon.
610
+ // ---------------------------------------------------------------------------
611
+
612
+ /// How `.icons` resolves for packaging: at most one prebuilt container
613
+ /// per platform plus at most one generatable source (first of each wins).
614
+ const IconPlan = struct {
615
+ prebuilt_icns: ?[]const u8 = null,
616
+ prebuilt_ico: ?[]const u8 = null,
617
+ source_path: ?[]const u8 = null,
618
+ source_kind: app_icon_tool.SourceKind = .png,
619
+ };
620
+
621
+ fn resolveIconPlan(metadata: manifest_tool.Metadata) IconPlan {
622
+ var plan: IconPlan = .{};
623
+ for (metadata.icons) |path| {
624
+ if (app_icon_tool.pathHasExtension(path, ".icns")) {
625
+ if (plan.prebuilt_icns == null) plan.prebuilt_icns = path;
626
+ } else if (app_icon_tool.pathHasExtension(path, ".ico")) {
627
+ if (plan.prebuilt_ico == null) plan.prebuilt_ico = path;
628
+ } else if (app_icon_tool.sourceKindForPath(path)) |kind| {
629
+ if (plan.source_path == null) {
630
+ plan.source_path = path;
631
+ plan.source_kind = kind;
632
+ }
633
+ }
634
+ }
635
+ return plan;
636
+ }
637
+
638
+ /// Read and validate the icon source, printing the same teaching
639
+ /// diagnostics `native validate` produces. A missing file warns and
640
+ /// returns null (packaging falls back per platform); a file that exists
641
+ /// but is not a square PNG/supported SVG is an error.
642
+ fn loadIconSource(allocator: std.mem.Allocator, io: std.Io, path: []const u8, kind: app_icon_tool.SourceKind) !?app_icon_tool.Source {
643
+ const bytes = readPath(allocator, io, path) catch |err| switch (err) {
644
+ error.FileNotFound => {
645
+ std.debug.print("warning: app icon source {s} was not found; the artifact falls back to the default icon where one exists\n", .{path});
646
+ return null;
647
+ },
648
+ else => return err,
649
+ };
650
+ defer allocator.free(bytes);
651
+ switch (try app_icon_tool.loadSource(allocator, bytes, kind)) {
652
+ .ok => |loaded| {
653
+ if (kind == .png and loaded.width < app_icon_tool.min_recommended_source_size) {
654
+ var buffer: [512]u8 = undefined;
655
+ std.debug.print("{s}\n", .{app_icon_tool.formatSmallSourceMessage(&buffer, path, loaded.width, loaded.height)});
656
+ }
657
+ return loaded;
658
+ },
659
+ .issue => |issue| {
660
+ var buffer: [512]u8 = undefined;
661
+ const message = switch (issue) {
662
+ .not_square => |dims| app_icon_tool.formatNotSquareMessage(&buffer, path, dims.width, dims.height),
663
+ .unsupported => app_icon_tool.formatUnsupportedMessage(&buffer, path),
664
+ };
665
+ std.debug.print("error: {s}\n", .{message});
666
+ return error.InvalidIconSource;
667
+ },
668
+ }
669
+ }
670
+
671
+ fn macosIconFile(metadata: manifest_tool.Metadata) []const u8 {
672
+ // Only a prebuilt .icns keeps its own name; generated and default
673
+ // icons always ship as AppIcon.icns.
674
+ const plan = resolveIconPlan(metadata);
675
+ if (plan.prebuilt_icns) |path| return std.fs.path.basename(path);
676
+ return "AppIcon.icns";
677
+ }
678
+
679
+ fn copyMacosIcon(allocator: std.mem.Allocator, io: std.Io, package_dir: std.Io.Dir, options: PackageOptions) !void {
680
+ const plan = resolveIconPlan(options.metadata);
681
+ if (plan.prebuilt_icns) |path| {
682
+ // Art-directed prebuilt .icns wins untouched.
683
+ try copyMacosResourceIcon(allocator, io, package_dir, path, "configured app icon");
684
+ return;
685
+ }
686
+ if (plan.source_path) |path| {
687
+ if (try loadIconSource(allocator, io, path, plan.source_kind)) |loaded| {
688
+ var source = loaded;
689
+ defer source.deinit(allocator);
690
+ const icns = app_icon_tool.buildIcns(allocator, &source) catch |err| switch (err) {
691
+ error.OutOfMemory => return error.OutOfMemory,
692
+ else => {
693
+ var buffer: [512]u8 = undefined;
694
+ std.debug.print("error: {s}\n", .{app_icon_tool.formatUnsupportedMessage(&buffer, path)});
695
+ return error.InvalidIconSource;
696
+ },
697
+ };
698
+ defer allocator.free(icns);
699
+ try writeFile(package_dir, io, "Contents/Resources/AppIcon.icns", icns);
700
+ return;
701
+ }
702
+ }
703
+ try writeFile(package_dir, io, "Contents/Resources/AppIcon.icns", default_icon_icns);
704
+ }
705
+
706
+ /// Linux: the hicolor-theme size set the desktop entry's `Icon=app-icon`
707
+ /// name resolves against, generated from the one source image.
708
+ fn writeLinuxIcons(allocator: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, metadata: manifest_tool.Metadata) !void {
709
+ const plan = resolveIconPlan(metadata);
710
+ const path = plan.source_path orelse {
711
+ if (metadata.icons.len > 0) {
712
+ std.debug.print("note: Linux icons generate from a square .png or .svg in app.zon .icons; a prebuilt .icns/.ico only serves macOS/Windows, so this artifact ships without one\n", .{});
713
+ }
714
+ return;
715
+ };
716
+ var source = (try loadIconSource(allocator, io, path, plan.source_kind)) orelse return;
717
+ defer source.deinit(allocator);
718
+ for (app_icon_tool.linux_sizes) |size| {
719
+ const encoded = app_icon_tool.buildSquarePng(allocator, &source, size) catch |err| switch (err) {
720
+ error.OutOfMemory => return error.OutOfMemory,
721
+ else => return error.InvalidIconSource,
722
+ };
723
+ defer allocator.free(encoded);
724
+ const icon_dir = try std.fmt.allocPrint(allocator, "share/icons/hicolor/{d}x{d}/apps", .{ size, size });
725
+ defer allocator.free(icon_dir);
726
+ try dir.createDirPath(io, icon_dir);
727
+ const icon_path = try std.fmt.allocPrint(allocator, "{s}/app-icon.png", .{icon_dir});
728
+ defer allocator.free(icon_path);
729
+ try writeFile(dir, io, icon_path, encoded);
730
+ }
731
+ }
732
+
733
+ /// Windows: a multi-size `.ico` at the artifact root (square, unmasked).
734
+ /// A prebuilt `.ico` in `.icons` ships untouched.
735
+ fn writeWindowsIcon(allocator: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, metadata: manifest_tool.Metadata) !void {
736
+ const plan = resolveIconPlan(metadata);
737
+ if (plan.prebuilt_ico) |path| {
738
+ copyFileToDir(allocator, io, dir, path, "app-icon.ico") catch {
739
+ std.debug.print("warning: configured .ico {s} was not found; the Windows artifact ships without an icon\n", .{path});
740
+ };
741
+ return;
742
+ }
743
+ const path = plan.source_path orelse return;
744
+ var source = (try loadIconSource(allocator, io, path, plan.source_kind)) orelse return;
745
+ defer source.deinit(allocator);
746
+ const ico = app_icon_tool.buildIco(allocator, &source) catch |err| switch (err) {
747
+ error.OutOfMemory => return error.OutOfMemory,
748
+ else => return error.InvalidIconSource,
749
+ };
750
+ defer allocator.free(ico);
751
+ try writeFile(dir, io, "app-icon.ico", ico);
752
+ }
753
+
754
+ /// iOS: an asset-catalog icon set with the single 1024 universal image
755
+ /// modern toolchains take, dropped next to the host skeleton sources.
756
+ fn writeIosIcon(allocator: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, metadata: manifest_tool.Metadata) !void {
757
+ const plan = resolveIconPlan(metadata);
758
+ var source: app_icon_tool.Source = source: {
759
+ if (plan.source_path) |path| {
760
+ if (try loadIconSource(allocator, io, path, plan.source_kind)) |loaded| break :source loaded;
761
+ }
762
+ // No usable PNG/SVG source (an .icns-only manifest, or a missing
763
+ // file): render the default icon — the generated Xcode project
764
+ // references Assets.xcassets unconditionally, so the catalog must
765
+ // always exist.
766
+ switch (try app_icon_tool.loadSource(allocator, default_icon_png, .png)) {
767
+ .ok => |loaded| break :source loaded,
768
+ // The embedded default is a valid square PNG by construction
769
+ // (`zig build generate-icon` regenerates it).
770
+ .issue => unreachable,
771
+ }
772
+ };
773
+ defer source.deinit(allocator);
774
+ const encoded = app_icon_tool.buildSquarePng(allocator, &source, app_icon_tool.ios_icon_size) catch |err| switch (err) {
775
+ error.OutOfMemory => return error.OutOfMemory,
776
+ else => return error.InvalidIconSource,
777
+ };
778
+ defer allocator.free(encoded);
779
+ try dir.createDirPath(io, "Assets.xcassets/AppIcon.appiconset");
780
+ try writeFile(dir, io, "Assets.xcassets/AppIcon.appiconset/AppIcon.png", encoded);
781
+ try writeFile(dir, io, "Assets.xcassets/AppIcon.appiconset/Contents.json",
782
+ \\{
783
+ \\ "images" : [
784
+ \\ {
785
+ \\ "filename" : "AppIcon.png",
786
+ \\ "idiom" : "universal",
787
+ \\ "platform" : "ios",
788
+ \\ "size" : "1024x1024"
789
+ \\ }
790
+ \\ ],
791
+ \\ "info" : {
792
+ \\ "author" : "native",
793
+ \\ "version" : 1
794
+ \\ }
795
+ \\}
796
+ \\
797
+ );
798
+ }
799
+
800
+ /// Android: launcher mipmaps at the standard densities, falling back to
801
+ /// the SDK default icon so the generated manifest's @mipmap reference
802
+ /// always resolves — the Android mirror of writeIosIcon. (Adaptive icons
803
+ /// need two art-directed layers a single flat source cannot honestly
804
+ /// provide, so only the legacy launcher set is generated.)
805
+ fn writeAndroidIcons(allocator: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, metadata: manifest_tool.Metadata) !void {
806
+ const plan = resolveIconPlan(metadata);
807
+ var source: app_icon_tool.Source = source: {
808
+ if (plan.source_path) |path| {
809
+ if (try loadIconSource(allocator, io, path, plan.source_kind)) |loaded| break :source loaded;
810
+ }
811
+ switch (try app_icon_tool.loadSource(allocator, default_icon_png, .png)) {
812
+ .ok => |loaded| break :source loaded,
813
+ // The embedded default is a valid square PNG by construction
814
+ // (`zig build generate-icon` regenerates it).
815
+ .issue => unreachable,
816
+ }
817
+ };
818
+ defer source.deinit(allocator);
819
+ for (app_icon_tool.android_densities) |density| {
820
+ const encoded = app_icon_tool.buildSquarePng(allocator, &source, density.size) catch |err| switch (err) {
821
+ error.OutOfMemory => return error.OutOfMemory,
822
+ else => return error.InvalidIconSource,
823
+ };
824
+ defer allocator.free(encoded);
825
+ const mipmap_dir = try std.fmt.allocPrint(allocator, "res/mipmap-{s}", .{density.name});
826
+ defer allocator.free(mipmap_dir);
827
+ try dir.createDirPath(io, mipmap_dir);
828
+ const icon_path = try std.fmt.allocPrint(allocator, "{s}/ic_launcher.png", .{mipmap_dir});
829
+ defer allocator.free(icon_path);
830
+ try writeFile(dir, io, icon_path, encoded);
831
+ }
832
+ }
833
+
834
+ fn copyMacosDocumentIcons(allocator: std.mem.Allocator, io: std.Io, package_dir: std.Io.Dir, metadata: manifest_tool.Metadata) !void {
835
+ for (metadata.file_associations) |association| {
836
+ const icon_path = association.icon orelse continue;
837
+ try copyMacosResourceIcon(allocator, io, package_dir, icon_path, "configured document icon");
838
+ }
839
+ }
840
+
841
+ fn copyMacosResourceIcon(allocator: std.mem.Allocator, io: std.Io, package_dir: std.Io.Dir, icon_path: []const u8, missing_label: []const u8) !void {
842
+ const dest = try std.fmt.allocPrint(allocator, "Contents/Resources/{s}", .{std.fs.path.basename(icon_path)});
843
+ defer allocator.free(dest);
844
+ const icon_bytes = readPath(allocator, io, icon_path) catch |err| switch (err) {
845
+ error.FileNotFound => {
846
+ const placeholder = try std.fmt.allocPrint(allocator, "placeholder: {s} was not found; replace with a real macOS .icns before distributing\n", .{missing_label});
847
+ defer allocator.free(placeholder);
848
+ try writeFile(package_dir, io, dest, placeholder);
849
+ return;
850
+ },
851
+ else => return err,
852
+ };
853
+ defer allocator.free(icon_bytes);
854
+ if (!isValidIcns(icon_bytes)) {
855
+ std.debug.print("warning: {s} does not appear to be a valid .icns file; replace before distributing\n", .{icon_path});
856
+ }
857
+ try writeFile(package_dir, io, dest, icon_bytes);
858
+ }
859
+
860
+ fn isValidIcns(bytes: []const u8) bool {
861
+ if (bytes.len < 8) return false;
862
+ return std.mem.eql(u8, bytes[0..4], "icns");
863
+ }
864
+
865
+ fn xmlEscapeAlloc(allocator: std.mem.Allocator, value: []const u8) ![]const u8 {
866
+ var out: std.ArrayList(u8) = .empty;
867
+ errdefer out.deinit(allocator);
868
+ for (value) |ch| {
869
+ switch (ch) {
870
+ '&' => try out.appendSlice(allocator, "&amp;"),
871
+ '<' => try out.appendSlice(allocator, "&lt;"),
872
+ '>' => try out.appendSlice(allocator, "&gt;"),
873
+ '"' => try out.appendSlice(allocator, "&quot;"),
874
+ '\'' => try out.appendSlice(allocator, "&apos;"),
875
+ 0...8, 11...12, 14...0x1f => return error.InvalidName,
876
+ else => try out.append(allocator, ch),
877
+ }
878
+ }
879
+ return out.toOwnedSlice(allocator);
880
+ }
881
+
882
+ fn desktopEntryEscapeAlloc(allocator: std.mem.Allocator, value: []const u8) ![]const u8 {
883
+ var out: std.ArrayList(u8) = .empty;
884
+ errdefer out.deinit(allocator);
885
+ for (value) |ch| {
886
+ switch (ch) {
887
+ 0...8, 11...12, 14...0x1f => return error.InvalidName,
888
+ '\n', '\r', '\t' => try out.append(allocator, ' '),
889
+ else => try out.append(allocator, ch),
890
+ }
891
+ }
892
+ return out.toOwnedSlice(allocator);
893
+ }
894
+
895
+ fn desktopExecArgumentAlloc(allocator: std.mem.Allocator, value: []const u8) ![]const u8 {
896
+ var out: std.ArrayList(u8) = .empty;
897
+ errdefer out.deinit(allocator);
898
+ try out.append(allocator, '"');
899
+ for (value) |ch| {
900
+ switch (ch) {
901
+ 0...0x1f => return error.InvalidName,
902
+ '"', '\\', '`', '$' => {
903
+ try out.append(allocator, '\\');
904
+ try out.append(allocator, ch);
905
+ },
906
+ '%' => try out.appendSlice(allocator, "%%"),
907
+ else => try out.append(allocator, ch),
908
+ }
909
+ }
910
+ try out.append(allocator, '"');
911
+ return out.toOwnedSlice(allocator);
912
+ }
913
+
914
+ fn zonStringAlloc(allocator: std.mem.Allocator, value: []const u8) ![]const u8 {
915
+ var out: std.ArrayList(u8) = .empty;
916
+ errdefer out.deinit(allocator);
917
+ try out.append(allocator, '"');
918
+ for (value) |ch| {
919
+ switch (ch) {
920
+ '"' => try out.appendSlice(allocator, "\\\""),
921
+ '\\' => try out.appendSlice(allocator, "\\\\"),
922
+ '\n' => try out.appendSlice(allocator, "\\n"),
923
+ '\r' => try out.appendSlice(allocator, "\\r"),
924
+ '\t' => try out.appendSlice(allocator, "\\t"),
925
+ 0...8, 11...12, 14...0x1f => {
926
+ const escaped = try std.fmt.allocPrint(allocator, "\\x{x:0>2}", .{ch});
927
+ defer allocator.free(escaped);
928
+ try out.appendSlice(allocator, escaped);
929
+ },
930
+ else => try out.append(allocator, ch),
931
+ }
932
+ }
933
+ try out.append(allocator, '"');
934
+ return out.toOwnedSlice(allocator);
935
+ }
936
+
937
+ fn copyFileToDir(allocator: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, source_path: []const u8, dest_subpath: []const u8) !void {
938
+ _ = allocator;
939
+ try std.Io.Dir.copyFile(std.Io.Dir.cwd(), source_path, dir, dest_subpath, io, .{ .make_path = true, .replace = true });
940
+ }
941
+
942
+ fn makeExecutable(dir: std.Io.Dir, io: std.Io, subpath: []const u8) !void {
943
+ if (!std.Io.File.Permissions.has_executable_bit) return;
944
+
945
+ var file = try dir.openFile(io, subpath, .{});
946
+ defer file.close(io);
947
+ const current_mode = (try file.stat(io)).permissions.toMode();
948
+ const execute_if_readable = (current_mode & 0o444) >> 2;
949
+ try file.setPermissions(io, .fromMode(current_mode | execute_if_readable));
950
+ }
951
+
952
+ fn readPath(allocator: std.mem.Allocator, io: std.Io, path: []const u8) ![]u8 {
953
+ var file = try std.Io.Dir.cwd().openFile(io, path, .{});
954
+ defer file.close(io);
955
+ var read_buffer: [4096]u8 = undefined;
956
+ var reader = file.reader(io, &read_buffer);
957
+ return reader.interface.allocRemaining(allocator, .limited(128 * 1024 * 1024));
958
+ }
959
+
960
+ fn writeReport(allocator: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, subpath: []const u8, options: PackageOptions, executable_name: []const u8, asset_count: usize) !void {
961
+ const capabilities = try capabilityLines(allocator, options.metadata.capabilities);
962
+ defer allocator.free(capabilities);
963
+ const frontend = try frontendLines(allocator, options.frontend);
964
+ defer allocator.free(frontend);
965
+ const artifact = try zonStringAlloc(allocator, std.fs.path.basename(options.output_path));
966
+ defer allocator.free(artifact);
967
+ const target = try zonStringAlloc(allocator, @tagName(options.target));
968
+ defer allocator.free(target);
969
+ const version = try zonStringAlloc(allocator, options.metadata.version);
970
+ defer allocator.free(version);
971
+ const app_id = try zonStringAlloc(allocator, options.metadata.id);
972
+ defer allocator.free(app_id);
973
+ const executable = try zonStringAlloc(allocator, executable_name);
974
+ defer allocator.free(executable);
975
+ const optimize = try zonStringAlloc(allocator, options.optimize);
976
+ defer allocator.free(optimize);
977
+ const web_engine = try zonStringAlloc(allocator, @tagName(options.web_engine));
978
+ defer allocator.free(web_engine);
979
+ const signing = try zonStringAlloc(allocator, @tagName(options.signing.mode));
980
+ defer allocator.free(signing);
981
+ const report = try std.fmt.allocPrint(allocator,
982
+ \\.{{
983
+ \\ .artifact = {s},
984
+ \\ .target = {s},
985
+ \\ .version = {s},
986
+ \\ .app_id = {s},
987
+ \\ .executable = {s},
988
+ \\ .optimize = {s},
989
+ \\ .web_engine = {s},
990
+ \\ .signing = {s},
991
+ \\ .asset_count = {d},
992
+ \\{s}
993
+ \\ .capabilities = .{{
994
+ \\{s}
995
+ \\ }},
996
+ \\}}
997
+ \\
998
+ , .{
999
+ artifact,
1000
+ target,
1001
+ version,
1002
+ app_id,
1003
+ executable,
1004
+ optimize,
1005
+ web_engine,
1006
+ signing,
1007
+ asset_count,
1008
+ frontend,
1009
+ capabilities,
1010
+ });
1011
+ defer allocator.free(report);
1012
+ try writeFile(dir, io, subpath, report);
1013
+ }
1014
+
1015
+ fn capabilityLines(allocator: std.mem.Allocator, capabilities: []const []const u8) ![]const u8 {
1016
+ var out: std.ArrayList(u8) = .empty;
1017
+ errdefer out.deinit(allocator);
1018
+ for (capabilities) |capability| {
1019
+ const escaped = try zonStringAlloc(allocator, capability);
1020
+ defer allocator.free(escaped);
1021
+ try out.appendSlice(allocator, " ");
1022
+ try out.appendSlice(allocator, escaped);
1023
+ try out.appendSlice(allocator, ",\n");
1024
+ }
1025
+ return out.toOwnedSlice(allocator);
1026
+ }
1027
+
1028
+ fn frontendLines(allocator: std.mem.Allocator, frontend: ?manifest_tool.FrontendMetadata) ![]const u8 {
1029
+ if (frontend) |config| {
1030
+ const dist = try zonStringAlloc(allocator, config.dist);
1031
+ defer allocator.free(dist);
1032
+ const entry = try zonStringAlloc(allocator, config.entry);
1033
+ defer allocator.free(entry);
1034
+ return std.fmt.allocPrint(allocator,
1035
+ \\ .frontend = .{{ .dist = {s}, .entry = {s}, .spa_fallback = {} }},
1036
+ \\
1037
+ , .{ dist, entry, config.spa_fallback });
1038
+ }
1039
+ return allocator.dupe(u8, "");
1040
+ }
1041
+
1042
+ fn copyMacosCefRuntime(allocator: std.mem.Allocator, io: std.Io, app_dir: std.Io.Dir, cef_dir: []const u8) !void {
1043
+ try app_dir.createDirPath(io, "Contents/Frameworks");
1044
+ try app_dir.createDirPath(io, "Contents/Resources/cef");
1045
+
1046
+ const framework_src = try std.fs.path.join(allocator, &.{ cef_dir, "Release", "Chromium Embedded Framework.framework" });
1047
+ defer allocator.free(framework_src);
1048
+ try copyTree(allocator, io, framework_src, app_dir, "Contents/Frameworks/Chromium Embedded Framework.framework");
1049
+
1050
+ const resources_src = try std.fs.path.join(allocator, &.{ cef_dir, "Resources" });
1051
+ defer allocator.free(resources_src);
1052
+ copyTree(allocator, io, resources_src, app_dir, "Contents/Resources/cef") catch |err| switch (err) {
1053
+ error.FileNotFound => {},
1054
+ else => return err,
1055
+ };
1056
+ }
1057
+
1058
+ fn copyDesktopCefRuntime(allocator: std.mem.Allocator, io: std.Io, package_dir: std.Io.Dir, target: PackageTarget, cef_dir: []const u8) !void {
1059
+ switch (target) {
1060
+ .linux, .windows => {},
1061
+ else => return error.UnsupportedWebEngine,
1062
+ }
1063
+ try package_dir.createDirPath(io, "bin");
1064
+ try package_dir.createDirPath(io, "resources/cef");
1065
+
1066
+ const release_src = try std.fs.path.join(allocator, &.{ cef_dir, "Release" });
1067
+ defer allocator.free(release_src);
1068
+ try copyTree(allocator, io, release_src, package_dir, "bin");
1069
+
1070
+ const resources_src = try std.fs.path.join(allocator, &.{ cef_dir, "Resources" });
1071
+ defer allocator.free(resources_src);
1072
+ copyTree(allocator, io, resources_src, package_dir, "resources/cef") catch |err| switch (err) {
1073
+ error.FileNotFound => {},
1074
+ else => return err,
1075
+ };
1076
+
1077
+ const locales_src = try std.fs.path.join(allocator, &.{ cef_dir, "locales" });
1078
+ defer allocator.free(locales_src);
1079
+ copyTree(allocator, io, locales_src, package_dir, "bin/locales") catch |err| switch (err) {
1080
+ error.FileNotFound => {},
1081
+ else => return err,
1082
+ };
1083
+ }
1084
+
1085
+ fn cefPlatformForTarget(target: PackageTarget) ?cef.Platform {
1086
+ const current = cef.Platform.current() catch null;
1087
+ return switch (target) {
1088
+ .macos => if (current) |platform| switch (platform) {
1089
+ .macosx64, .macosarm64 => platform,
1090
+ else => .macosarm64,
1091
+ } else .macosarm64,
1092
+ .linux => if (current) |platform| switch (platform) {
1093
+ .linux64, .linuxarm64 => platform,
1094
+ else => .linux64,
1095
+ } else .linux64,
1096
+ .windows => if (current) |platform| switch (platform) {
1097
+ .windows64, .windowsarm64 => platform,
1098
+ else => .windows64,
1099
+ } else .windows64,
1100
+ .ios, .android => null,
1101
+ };
1102
+ }
1103
+
1104
+ fn copyTree(allocator: std.mem.Allocator, io: std.Io, source_path: []const u8, dest_dir: std.Io.Dir, dest_subpath: []const u8) !void {
1105
+ var source_dir = try std.Io.Dir.cwd().openDir(io, source_path, .{ .iterate = true });
1106
+ defer source_dir.close(io);
1107
+ try dest_dir.createDirPath(io, dest_subpath);
1108
+
1109
+ var walker = try source_dir.walk(allocator);
1110
+ defer walker.deinit();
1111
+ while (try walker.next(io)) |entry| {
1112
+ const dest = try std.fs.path.join(allocator, &.{ dest_subpath, entry.path });
1113
+ defer allocator.free(dest);
1114
+ switch (entry.kind) {
1115
+ .directory => try dest_dir.createDirPath(io, dest),
1116
+ .file => try std.Io.Dir.copyFile(source_dir, entry.path, dest_dir, dest, io, .{ .make_path = true, .replace = true }),
1117
+ else => {},
1118
+ }
1119
+ }
1120
+ }
1121
+
1122
+ /// Sign the finished bundle and record the signing plan inside it. The
1123
+ /// plan file lives in Contents/Resources, which codesign seals: writing
1124
+ /// it AFTER a successful signature would invalidate the resource seal
1125
+ /// (`codesign --verify --strict` reports "file added" and a quarantined
1126
+ /// install shows Gatekeeper's "damaged" dialog). So the plan is written
1127
+ /// BEFORE codesign runs — the sealed file states what was requested and
1128
+ /// the signature on the bundle is the proof it happened — and only a
1129
+ /// FAILED signing rewrites it, which is safe because a failed codesign
1130
+ /// leaves the bundle without a seal to break.
1131
+ fn runSigning(allocator: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, options: PackageOptions) !void {
1132
+ const plan_path = "Contents/Resources/signing-plan.txt";
1133
+ switch (options.signing.mode) {
1134
+ .none => try writeFile(dir, io, plan_path, "signing=none\nunsigned local package\n"),
1135
+ .adhoc => {
1136
+ try writeFile(dir, io, plan_path, "signing=adhoc\nad-hoc signed\n");
1137
+ const result = codesign.signAdHoc(io, options.output_path) catch {
1138
+ try writeFile(dir, io, plan_path, "signing=adhoc\ncodesign --sign - failed; bundle is unsigned\n");
1139
+ return;
1140
+ };
1141
+ if (!result.ok) try writeFile(dir, io, plan_path, "signing=adhoc\ncodesign --sign - failed; bundle is unsigned\n");
1142
+ },
1143
+ .identity => {
1144
+ const identity = options.signing.identity orelse {
1145
+ try writeFile(dir, io, plan_path, "signing=identity\nno identity provided; bundle is unsigned\n");
1146
+ return;
1147
+ };
1148
+ const plan_text = try std.fmt.allocPrint(allocator, "signing=identity\nsigned with {s}\n", .{identity});
1149
+ defer allocator.free(plan_text);
1150
+ try writeFile(dir, io, plan_path, plan_text);
1151
+ const result = codesign.signIdentity(io, options.output_path, identity, options.signing.entitlements) catch {
1152
+ try writeFile(dir, io, plan_path, "signing=identity\ncodesign failed; bundle is unsigned\n");
1153
+ return;
1154
+ };
1155
+ if (!result.ok) try writeFile(dir, io, plan_path, "signing=identity\ncodesign failed; bundle is unsigned\n");
1156
+ },
1157
+ }
1158
+ }
1159
+
1160
+ fn hasRegistrationMetadata(metadata: manifest_tool.Metadata) bool {
1161
+ return metadata.file_associations.len > 0 or metadata.url_schemes.len > 0;
1162
+ }
1163
+
1164
+ fn appendFmt(allocator: std.mem.Allocator, out: *std.ArrayList(u8), comptime format: []const u8, args: anytype) !void {
1165
+ const text = try std.fmt.allocPrint(allocator, format, args);
1166
+ defer allocator.free(text);
1167
+ try out.appendSlice(allocator, text);
1168
+ }
1169
+
1170
+ fn trimExtensionDot(extension: []const u8) []const u8 {
1171
+ if (extension.len > 0 and extension[0] == '.') return extension[1..];
1172
+ return extension;
1173
+ }
1174
+
1175
+ fn macosDocumentTypes(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata) ![]const u8 {
1176
+ if (metadata.file_associations.len == 0) return allocator.dupe(u8, "");
1177
+
1178
+ var out: std.ArrayList(u8) = .empty;
1179
+ errdefer out.deinit(allocator);
1180
+ try out.appendSlice(allocator,
1181
+ \\ <key>CFBundleDocumentTypes</key>
1182
+ \\ <array>
1183
+ \\
1184
+ );
1185
+ for (metadata.file_associations) |association| {
1186
+ const name = try xmlEscapeAlloc(allocator, association.name);
1187
+ defer allocator.free(name);
1188
+ try appendFmt(allocator, &out,
1189
+ \\ <dict>
1190
+ \\ <key>CFBundleTypeName</key>
1191
+ \\ <string>{s}</string>
1192
+ \\ <key>CFBundleTypeRole</key>
1193
+ \\ <string>{s}</string>
1194
+ \\
1195
+ , .{ name, macosAssociationRole(association.role) });
1196
+ if (association.icon) |icon_path| {
1197
+ const icon = try xmlEscapeAlloc(allocator, std.fs.path.basename(icon_path));
1198
+ defer allocator.free(icon);
1199
+ try appendFmt(allocator, &out,
1200
+ \\ <key>CFBundleTypeIconFile</key>
1201
+ \\ <string>{s}</string>
1202
+ \\
1203
+ , .{icon});
1204
+ }
1205
+ if (association.extensions.len > 0) {
1206
+ try out.appendSlice(allocator,
1207
+ \\ <key>CFBundleTypeExtensions</key>
1208
+ \\ <array>
1209
+ \\
1210
+ );
1211
+ for (association.extensions) |extension| {
1212
+ const escaped = try xmlEscapeAlloc(allocator, trimExtensionDot(extension));
1213
+ defer allocator.free(escaped);
1214
+ try appendFmt(allocator, &out,
1215
+ \\ <string>{s}</string>
1216
+ \\
1217
+ , .{escaped});
1218
+ }
1219
+ try out.appendSlice(allocator,
1220
+ \\ </array>
1221
+ \\
1222
+ );
1223
+ }
1224
+ if (association.mime_types.len > 0) {
1225
+ try out.appendSlice(allocator,
1226
+ \\ <key>CFBundleTypeMIMETypes</key>
1227
+ \\ <array>
1228
+ \\
1229
+ );
1230
+ for (association.mime_types) |mime_type| {
1231
+ const escaped = try xmlEscapeAlloc(allocator, mime_type);
1232
+ defer allocator.free(escaped);
1233
+ try appendFmt(allocator, &out,
1234
+ \\ <string>{s}</string>
1235
+ \\
1236
+ , .{escaped});
1237
+ }
1238
+ try out.appendSlice(allocator,
1239
+ \\ </array>
1240
+ \\
1241
+ );
1242
+ }
1243
+ try out.appendSlice(allocator,
1244
+ \\ </dict>
1245
+ \\
1246
+ );
1247
+ }
1248
+ try out.appendSlice(allocator,
1249
+ \\ </array>
1250
+ \\
1251
+ );
1252
+ return out.toOwnedSlice(allocator);
1253
+ }
1254
+
1255
+ fn macosUrlTypes(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata) ![]const u8 {
1256
+ if (metadata.url_schemes.len == 0) return allocator.dupe(u8, "");
1257
+
1258
+ var out: std.ArrayList(u8) = .empty;
1259
+ errdefer out.deinit(allocator);
1260
+ try out.appendSlice(allocator,
1261
+ \\ <key>CFBundleURLTypes</key>
1262
+ \\ <array>
1263
+ \\
1264
+ );
1265
+ for (metadata.url_schemes) |url_scheme| {
1266
+ const name_value = try std.fmt.allocPrint(allocator, "{s}.{s}", .{ metadata.id, url_scheme.scheme });
1267
+ defer allocator.free(name_value);
1268
+ const name = try xmlEscapeAlloc(allocator, name_value);
1269
+ defer allocator.free(name);
1270
+ const scheme = try xmlEscapeAlloc(allocator, url_scheme.scheme);
1271
+ defer allocator.free(scheme);
1272
+ try appendFmt(allocator, &out,
1273
+ \\ <dict>
1274
+ \\ <key>CFBundleTypeRole</key>
1275
+ \\ <string>{s}</string>
1276
+ \\ <key>CFBundleURLName</key>
1277
+ \\ <string>{s}</string>
1278
+ \\ <key>CFBundleURLSchemes</key>
1279
+ \\ <array>
1280
+ \\ <string>{s}</string>
1281
+ \\ </array>
1282
+ \\ </dict>
1283
+ \\
1284
+ , .{ macosAssociationRole(url_scheme.role), name, scheme });
1285
+ }
1286
+ try out.appendSlice(allocator,
1287
+ \\ </array>
1288
+ \\
1289
+ );
1290
+ return out.toOwnedSlice(allocator);
1291
+ }
1292
+
1293
+ fn macosAssociationRole(role: []const u8) []const u8 {
1294
+ if (std.mem.eql(u8, role, "editor")) return "Editor";
1295
+ if (std.mem.eql(u8, role, "shell")) return "Shell";
1296
+ if (std.mem.eql(u8, role, "none")) return "None";
1297
+ return "Viewer";
1298
+ }
1299
+
1300
+ fn linuxDesktopEntry(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata) ![]const u8 {
1301
+ const display_name = try desktopEntryEscapeAlloc(allocator, metadata.displayName());
1302
+ defer allocator.free(display_name);
1303
+ const executable = try desktopExecArgumentAlloc(allocator, metadata.name);
1304
+ defer allocator.free(executable);
1305
+ const field_code: []const u8 = if (metadata.url_schemes.len > 0) " %U" else if (metadata.file_associations.len > 0) " %F" else "";
1306
+ const mime_line = try linuxDesktopMimeLine(allocator, metadata);
1307
+ defer allocator.free(mime_line);
1308
+ return std.fmt.allocPrint(allocator,
1309
+ \\[Desktop Entry]
1310
+ \\Type=Application
1311
+ \\Name={s}
1312
+ \\Exec={s}{s}
1313
+ \\Icon=app-icon
1314
+ \\Categories=Utility;
1315
+ \\Comment={s} desktop application
1316
+ \\{s}
1317
+ \\
1318
+ , .{ display_name, executable, field_code, display_name, mime_line });
1319
+ }
1320
+
1321
+ fn linuxDesktopMimeLine(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata) ![]const u8 {
1322
+ if (!hasRegistrationMetadata(metadata)) return allocator.dupe(u8, "");
1323
+
1324
+ var out: std.ArrayList(u8) = .empty;
1325
+ errdefer out.deinit(allocator);
1326
+ try out.appendSlice(allocator, "MimeType=");
1327
+ for (metadata.file_associations) |association| {
1328
+ if (association.mime_types.len > 0) {
1329
+ for (association.mime_types) |mime_type| {
1330
+ try out.appendSlice(allocator, mime_type);
1331
+ try out.append(allocator, ';');
1332
+ }
1333
+ } else {
1334
+ const generated = try linuxGeneratedMimeType(allocator, metadata, association);
1335
+ defer allocator.free(generated);
1336
+ try out.appendSlice(allocator, generated);
1337
+ try out.append(allocator, ';');
1338
+ }
1339
+ }
1340
+ for (metadata.url_schemes) |url_scheme| {
1341
+ try appendFmt(allocator, &out, "x-scheme-handler/{s};", .{url_scheme.scheme});
1342
+ }
1343
+ try out.append(allocator, '\n');
1344
+ return out.toOwnedSlice(allocator);
1345
+ }
1346
+
1347
+ fn linuxMimeInfo(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata) ![]const u8 {
1348
+ var out: std.ArrayList(u8) = .empty;
1349
+ errdefer out.deinit(allocator);
1350
+ try out.appendSlice(allocator,
1351
+ \\<?xml version="1.0" encoding="UTF-8"?>
1352
+ \\<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
1353
+ \\
1354
+ );
1355
+ for (metadata.file_associations) |association| {
1356
+ if (association.mime_types.len > 0) {
1357
+ for (association.mime_types) |mime_type| {
1358
+ try appendLinuxMimeType(allocator, &out, association, mime_type);
1359
+ }
1360
+ } else {
1361
+ const generated = try linuxGeneratedMimeType(allocator, metadata, association);
1362
+ defer allocator.free(generated);
1363
+ try appendLinuxMimeType(allocator, &out, association, generated);
1364
+ }
1365
+ }
1366
+ try out.appendSlice(allocator,
1367
+ \\</mime-info>
1368
+ \\
1369
+ );
1370
+ return out.toOwnedSlice(allocator);
1371
+ }
1372
+
1373
+ fn appendLinuxMimeType(allocator: std.mem.Allocator, out: *std.ArrayList(u8), association: manifest_tool.FileAssociationMetadata, mime_type: []const u8) !void {
1374
+ const escaped_type = try xmlEscapeAlloc(allocator, mime_type);
1375
+ defer allocator.free(escaped_type);
1376
+ const comment = try xmlEscapeAlloc(allocator, association.name);
1377
+ defer allocator.free(comment);
1378
+ try appendFmt(allocator, out,
1379
+ \\ <mime-type type="{s}">
1380
+ \\ <comment>{s}</comment>
1381
+ \\
1382
+ , .{ escaped_type, comment });
1383
+ for (association.extensions) |extension| {
1384
+ const pattern = try std.fmt.allocPrint(allocator, "*.{s}", .{trimExtensionDot(extension)});
1385
+ defer allocator.free(pattern);
1386
+ const escaped_pattern = try xmlEscapeAlloc(allocator, pattern);
1387
+ defer allocator.free(escaped_pattern);
1388
+ try appendFmt(allocator, out,
1389
+ \\ <glob pattern="{s}"/>
1390
+ \\
1391
+ , .{escaped_pattern});
1392
+ }
1393
+ try out.appendSlice(allocator,
1394
+ \\ </mime-type>
1395
+ \\
1396
+ );
1397
+ }
1398
+
1399
+ fn linuxGeneratedMimeType(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata, association: manifest_tool.FileAssociationMetadata) ![]const u8 {
1400
+ const app = try slugComponentAlloc(allocator, metadata.name);
1401
+ defer allocator.free(app);
1402
+ const name = try slugComponentAlloc(allocator, association.name);
1403
+ defer allocator.free(name);
1404
+ return std.fmt.allocPrint(allocator, "application/x-{s}-{s}", .{ app, name });
1405
+ }
1406
+
1407
+ fn windowsRegistrationScript(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata, executable_name: []const u8) ![]const u8 {
1408
+ var out: std.ArrayList(u8) = .empty;
1409
+ errdefer out.deinit(allocator);
1410
+ const executable_subpath = try std.fmt.allocPrint(allocator, "bin\\{s}", .{executable_name});
1411
+ defer allocator.free(executable_subpath);
1412
+ const executable_literal = try powerShellStringAlloc(allocator, executable_subpath);
1413
+ defer allocator.free(executable_literal);
1414
+
1415
+ try appendFmt(allocator, &out,
1416
+ \\$ErrorActionPreference = "Stop"
1417
+ \\$AppRoot = Split-Path -Parent $PSScriptRoot
1418
+ \\$Exe = Join-Path $AppRoot {s}
1419
+ \\$OpenCommand = '"' + $Exe + '" "%1"'
1420
+ \\
1421
+ \\function Set-DefaultValue([string]$Key, [string]$Value) {{
1422
+ \\ & reg.exe add $Key /ve /d $Value /f | Out-Null
1423
+ \\}}
1424
+ \\
1425
+ \\function Set-NamedValue([string]$Key, [string]$Name, [string]$Value) {{
1426
+ \\ & reg.exe add $Key /v $Name /d $Value /f | Out-Null
1427
+ \\}}
1428
+ \\
1429
+ , .{executable_literal});
1430
+
1431
+ for (metadata.file_associations) |association| {
1432
+ const prog_id = try windowsProgId(allocator, metadata, association);
1433
+ defer allocator.free(prog_id);
1434
+ const prog_key = try std.fmt.allocPrint(allocator, "HKCU\\Software\\Classes\\{s}", .{prog_id});
1435
+ defer allocator.free(prog_key);
1436
+ const prog_key_literal = try powerShellStringAlloc(allocator, prog_key);
1437
+ defer allocator.free(prog_key_literal);
1438
+ const prog_id_literal = try powerShellStringAlloc(allocator, prog_id);
1439
+ defer allocator.free(prog_id_literal);
1440
+ const name_literal = try powerShellStringAlloc(allocator, association.name);
1441
+ defer allocator.free(name_literal);
1442
+
1443
+ for (association.extensions) |extension| {
1444
+ const extension_key = try std.fmt.allocPrint(allocator, "HKCU\\Software\\Classes\\.{s}", .{trimExtensionDot(extension)});
1445
+ defer allocator.free(extension_key);
1446
+ const extension_key_literal = try powerShellStringAlloc(allocator, extension_key);
1447
+ defer allocator.free(extension_key_literal);
1448
+ try appendFmt(allocator, &out, "Set-DefaultValue {s} {s}\n", .{ extension_key_literal, prog_id_literal });
1449
+ }
1450
+
1451
+ try appendFmt(allocator, &out,
1452
+ \\Set-DefaultValue {s} {s}
1453
+ \\Set-NamedValue {s} 'FriendlyTypeName' {s}
1454
+ \\Set-DefaultValue '{s}\DefaultIcon' $Exe
1455
+ \\Set-DefaultValue '{s}\shell\open\command' $OpenCommand
1456
+ \\
1457
+ , .{ prog_key_literal, name_literal, prog_key_literal, name_literal, prog_key, prog_key });
1458
+ }
1459
+
1460
+ for (metadata.url_schemes) |url_scheme| {
1461
+ const scheme_key = try std.fmt.allocPrint(allocator, "HKCU\\Software\\Classes\\{s}", .{url_scheme.scheme});
1462
+ defer allocator.free(scheme_key);
1463
+ const scheme_key_literal = try powerShellStringAlloc(allocator, scheme_key);
1464
+ defer allocator.free(scheme_key_literal);
1465
+ const description = try std.fmt.allocPrint(allocator, "URL:{s}", .{url_scheme.scheme});
1466
+ defer allocator.free(description);
1467
+ const description_literal = try powerShellStringAlloc(allocator, description);
1468
+ defer allocator.free(description_literal);
1469
+ try appendFmt(allocator, &out,
1470
+ \\Set-DefaultValue {s} {s}
1471
+ \\Set-NamedValue {s} 'URL Protocol' ''
1472
+ \\Set-DefaultValue '{s}\shell\open\command' $OpenCommand
1473
+ \\
1474
+ , .{ scheme_key_literal, description_literal, scheme_key_literal, scheme_key });
1475
+ }
1476
+
1477
+ try out.appendSlice(allocator, "Write-Host \"Registered file associations and URL schemes for this user.\"\n");
1478
+ return out.toOwnedSlice(allocator);
1479
+ }
1480
+
1481
+ fn windowsProgId(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata, association: manifest_tool.FileAssociationMetadata) ![]const u8 {
1482
+ const app = try windowsIdentifierComponentAlloc(allocator, metadata.id);
1483
+ defer allocator.free(app);
1484
+ const name = try windowsIdentifierComponentAlloc(allocator, association.name);
1485
+ defer allocator.free(name);
1486
+ return std.fmt.allocPrint(allocator, "{s}.{s}", .{ app, name });
1487
+ }
1488
+
1489
+ fn windowsIdentifierComponentAlloc(allocator: std.mem.Allocator, value: []const u8) ![]const u8 {
1490
+ var out: std.ArrayList(u8) = .empty;
1491
+ errdefer out.deinit(allocator);
1492
+ for (value) |ch| {
1493
+ if (isAsciiAlphanumeric(ch) or ch == '.') {
1494
+ try out.append(allocator, ch);
1495
+ }
1496
+ }
1497
+ if (out.items.len == 0) try out.appendSlice(allocator, "App");
1498
+ return out.toOwnedSlice(allocator);
1499
+ }
1500
+
1501
+ fn powerShellStringAlloc(allocator: std.mem.Allocator, value: []const u8) ![]const u8 {
1502
+ var out: std.ArrayList(u8) = .empty;
1503
+ errdefer out.deinit(allocator);
1504
+ try out.append(allocator, '\'');
1505
+ for (value) |ch| {
1506
+ switch (ch) {
1507
+ '\'' => try out.appendSlice(allocator, "''"),
1508
+ 0...8, 11...12, 14...0x1f => return error.InvalidName,
1509
+ else => try out.append(allocator, ch),
1510
+ }
1511
+ }
1512
+ try out.append(allocator, '\'');
1513
+ return out.toOwnedSlice(allocator);
1514
+ }
1515
+
1516
+ fn slugComponentAlloc(allocator: std.mem.Allocator, value: []const u8) ![]const u8 {
1517
+ var out: std.ArrayList(u8) = .empty;
1518
+ errdefer out.deinit(allocator);
1519
+ var last_dash = false;
1520
+ for (value) |ch| {
1521
+ if (isAsciiAlphanumeric(ch)) {
1522
+ try out.append(allocator, toLowerAscii(ch));
1523
+ last_dash = false;
1524
+ } else if (!last_dash and out.items.len > 0) {
1525
+ try out.append(allocator, '-');
1526
+ last_dash = true;
1527
+ }
1528
+ }
1529
+ if (last_dash) out.items.len -= 1;
1530
+ if (out.items.len == 0) try out.appendSlice(allocator, "item");
1531
+ return out.toOwnedSlice(allocator);
1532
+ }
1533
+
1534
+ fn isAsciiAlphanumeric(ch: u8) bool {
1535
+ return (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z') or (ch >= '0' and ch <= '9');
1536
+ }
1537
+
1538
+ fn toLowerAscii(ch: u8) u8 {
1539
+ if (ch >= 'A' and ch <= 'Z') return ch + ('a' - 'A');
1540
+ return ch;
1541
+ }
1542
+
1543
+ fn createArchive(allocator: std.mem.Allocator, io: std.Io, options: PackageOptions) !?[]const u8 {
1544
+ const archive_path = try archivePath(allocator, options);
1545
+ errdefer allocator.free(archive_path);
1546
+ switch (options.target) {
1547
+ .ios, .android => {
1548
+ allocator.free(archive_path);
1549
+ return null;
1550
+ },
1551
+ .macos, .windows, .linux => {},
1552
+ }
1553
+ const archive_command_path = try absolutePathAlloc(allocator, io, archive_path);
1554
+ defer allocator.free(archive_command_path);
1555
+
1556
+ const ok = switch (options.target) {
1557
+ .macos => runArchiveCommand(io, &.{ "hdiutil", "create", "-volname", options.metadata.displayName(), "-srcfolder", options.output_path, "-ov", "-format", "UDZO", archive_command_path }, null),
1558
+ .windows => runArchiveCommand(io, &.{ "zip", "-r", archive_command_path, "." }, options.output_path),
1559
+ .linux => runArchiveCommand(io, &.{ "tar", "czf", archive_command_path, "-C", options.output_path, "." }, null),
1560
+ .ios, .android => unreachable,
1561
+ };
1562
+
1563
+ if (!ok) {
1564
+ std.debug.print("warning: archive creation failed for {s}\n", .{archive_path});
1565
+ allocator.free(archive_path);
1566
+ return null;
1567
+ }
1568
+ return archive_path;
1569
+ }
1570
+
1571
+ fn absolutePathAlloc(allocator: std.mem.Allocator, io: std.Io, path: []const u8) ![]const u8 {
1572
+ if (std.fs.path.isAbsolute(path)) return allocator.dupe(u8, path);
1573
+ const cwd = try std.process.currentPathAlloc(io, allocator);
1574
+ defer allocator.free(cwd);
1575
+ return std.fs.path.join(allocator, &.{ cwd, path });
1576
+ }
1577
+
1578
+ fn runArchiveCommand(io: std.Io, argv: []const []const u8, cwd: ?[]const u8) bool {
1579
+ const child_cwd: std.process.Child.Cwd = if (cwd) |path| .{ .path = path } else .inherit;
1580
+ var child = std.process.spawn(io, .{
1581
+ .argv = argv,
1582
+ .cwd = child_cwd,
1583
+ .stdin = .ignore,
1584
+ .stdout = .inherit,
1585
+ .stderr = .inherit,
1586
+ }) catch return false;
1587
+ const term = child.wait(io) catch return false;
1588
+ return switch (term) {
1589
+ .exited => |code| code == 0,
1590
+ else => false,
1591
+ };
1592
+ }
1593
+
1594
+ pub fn archivePath(allocator: std.mem.Allocator, options: PackageOptions) ![]const u8 {
1595
+ const dir = std.fs.path.dirname(options.output_path) orelse ".";
1596
+ return std.fmt.allocPrint(allocator, "{s}/{s}-{s}-{s}-{s}{s}", .{
1597
+ dir,
1598
+ options.metadata.name,
1599
+ options.metadata.version,
1600
+ @tagName(options.target),
1601
+ options.optimize,
1602
+ archiveSuffix(options.target),
1603
+ });
1604
+ }
1605
+
1606
+ fn archiveSuffix(target: PackageTarget) []const u8 {
1607
+ return switch (target) {
1608
+ .macos => ".dmg",
1609
+ .windows => ".zip",
1610
+ .linux => ".tar.gz",
1611
+ .ios, .android => "",
1612
+ };
1613
+ }
1614
+
1615
+ test "archive path includes correct suffix per platform" {
1616
+ const metadata: manifest_tool.Metadata = .{ .id = "dev.example.app", .name = "demo", .version = "1.2.3" };
1617
+ const macos_path = try archivePath(std.testing.allocator, .{ .metadata = metadata, .target = .macos, .output_path = "zig-out/package/demo.app" });
1618
+ defer std.testing.allocator.free(macos_path);
1619
+ try std.testing.expect(std.mem.endsWith(u8, macos_path, ".dmg"));
1620
+ const linux_path = try archivePath(std.testing.allocator, .{ .metadata = metadata, .target = .linux, .output_path = "zig-out/package/demo" });
1621
+ defer std.testing.allocator.free(linux_path);
1622
+ try std.testing.expect(std.mem.endsWith(u8, linux_path, ".tar.gz"));
1623
+ const win_path = try archivePath(std.testing.allocator, .{ .metadata = metadata, .target = .windows, .output_path = "zig-out/package/demo" });
1624
+ defer std.testing.allocator.free(win_path);
1625
+ try std.testing.expect(std.mem.endsWith(u8, win_path, ".zip"));
1626
+ }
1627
+
1628
+ test "archive command reports nonzero exit" {
1629
+ if (@import("builtin").os.tag == .windows) return error.SkipZigTest;
1630
+ try std.testing.expect(!runArchiveCommand(std.testing.io, &.{ "sh", "-c", "exit 7" }, null));
1631
+ }
1632
+
1633
+ test "mobile package templates ship the toolkit hosts" {
1634
+ // The iOS host tier ships the toolkit-owned UIKit host over the
1635
+ // embed ABI (canvas presentation, input, IME, safe areas, CoreText
1636
+ // measurement, and the panic-path dyld shim the iOS SDK hides).
1637
+ const ios_host = ios_tool.host_source;
1638
+ try std.testing.expect(std.mem.indexOf(u8, ios_host, "native_sdk_app_viewport") != null);
1639
+ try std.testing.expect(std.mem.indexOf(u8, ios_host, "native_sdk_app_render_pixels") != null);
1640
+ try std.testing.expect(std.mem.indexOf(u8, ios_host, "native_sdk_app_scroll") != null);
1641
+ try std.testing.expect(std.mem.indexOf(u8, ios_host, "native_sdk_app_text_input_state") != null);
1642
+ try std.testing.expect(std.mem.indexOf(u8, ios_host, "native_sdk_app_set_text_measure") != null);
1643
+ try std.testing.expect(std.mem.indexOf(u8, ios_host, "native_sdk_app_set_asset_root") != null);
1644
+ try std.testing.expect(std.mem.indexOf(u8, ios_host, "native_sdk_app_widget_semantics_by_id") != null);
1645
+ try std.testing.expect(std.mem.indexOf(u8, ios_host, "view.safeAreaInsets") != null);
1646
+ try std.testing.expect(std.mem.indexOf(u8, ios_host, "_dyld_get_image_header_containing_address") != null);
1647
+
1648
+ // The Android host tier ships the same architecture over JNI: the
1649
+ // activity presents pixels, forwards touch/keyboard/IME, reports
1650
+ // safe-area and keyboard insets, and registers Paint measurement.
1651
+ const android_activity = android_tool.host_activity_source;
1652
+ try std.testing.expect(std.mem.indexOf(u8, android_activity, "System.loadLibrary(\"native_sdk_host\")") != null);
1653
+ try std.testing.expect(std.mem.indexOf(u8, android_activity, "nativeTextInputState") != null);
1654
+ try std.testing.expect(std.mem.indexOf(u8, android_activity, "setComposingText") != null);
1655
+ try std.testing.expect(std.mem.indexOf(u8, android_activity, "finishComposingText") != null);
1656
+ try std.testing.expect(std.mem.indexOf(u8, android_activity, "InputMethodManager") != null);
1657
+ try std.testing.expect(std.mem.indexOf(u8, android_activity, "WindowInsets.Type.ime()") != null);
1658
+ try std.testing.expect(std.mem.indexOf(u8, android_activity, "WindowInsets.Type.displayCutout()") != null);
1659
+ try std.testing.expect(std.mem.indexOf(u8, android_activity, "nativeScrollableWidgetAt") != null);
1660
+ try std.testing.expect(std.mem.indexOf(u8, android_activity, "measureText") != null);
1661
+ try std.testing.expect(std.mem.indexOf(u8, android_activity, "native-sdk-automation") != null);
1662
+ const android_bridge = android_tool.host_bridge_source;
1663
+ try std.testing.expect(std.mem.indexOf(u8, android_bridge, "native_sdk_app_viewport") != null);
1664
+ try std.testing.expect(std.mem.indexOf(u8, android_bridge, "native_sdk_app_render_pixels") != null);
1665
+ try std.testing.expect(std.mem.indexOf(u8, android_bridge, "native_sdk_app_ime") != null);
1666
+ try std.testing.expect(std.mem.indexOf(u8, android_bridge, "native_sdk_app_text_input_state") != null);
1667
+ try std.testing.expect(std.mem.indexOf(u8, android_bridge, "native_sdk_app_set_text_measure") != null);
1668
+ try std.testing.expect(std.mem.indexOf(u8, android_bridge, "native_sdk_app_set_asset_root") != null);
1669
+ try std.testing.expect(std.mem.indexOf(u8, android_bridge, "ANativeWindow_fromSurface") != null);
1670
+ try std.testing.expect(std.mem.indexOf(u8, android_bridge, "WINDOW_FORMAT_RGBA_8888") != null);
1671
+ }
1672
+
1673
+
1674
+ test "mobile package artifacts use manifest identity metadata" {
1675
+ var cwd = std.Io.Dir.cwd();
1676
+ try cwd.deleteTree(std.testing.io, ".zig-cache/test-package-mobile-identity");
1677
+ defer cwd.deleteTree(std.testing.io, ".zig-cache/test-package-mobile-identity") catch {};
1678
+ try cwd.createDirPath(std.testing.io, ".zig-cache/test-package-mobile-identity/assets");
1679
+ try cwd.writeFile(std.testing.io, .{ .sub_path = ".zig-cache/test-package-mobile-identity/assets/main.html", .data = "<h1>Mobile</h1>" });
1680
+
1681
+ const shell_views = [_]manifest_tool.ShellViewMetadata{
1682
+ .{ .label = "mobile-header", .kind = "toolbar", .edge = "top", .height = 104 },
1683
+ .{ .label = "mobile-title", .kind = "label", .parent = "mobile-header", .text = "Field Console" },
1684
+ .{ .label = "mobile-status", .kind = "statusbar", .edge = "bottom", .height = 28, .text = "Shell ready" },
1685
+ .{ .label = "mobile-back", .kind = "button", .parent = "mobile-header", .text = "Go Back", .command = "mobile.go_back" },
1686
+ .{ .label = "mobile-refresh", .kind = "button", .parent = "mobile-header", .text = "Sync Now", .command = "mobile.sync" },
1687
+ .{ .label = "workspace", .kind = "webview", .url = "zero://app/index.html", .fill = true },
1688
+ };
1689
+ const shell_windows = [_]manifest_tool.ShellWindowMetadata{.{
1690
+ .label = "main",
1691
+ .title = "Field Console",
1692
+ .views = &shell_views,
1693
+ }};
1694
+ const metadata: manifest_tool.Metadata = .{
1695
+ .id = "dev.native-sdk.mobile-app",
1696
+ .name = "mobile-demo",
1697
+ .display_name = "Mobile Demo",
1698
+ .version = "2.3.4",
1699
+ .frontend = .{ .dist = "dist", .entry = "main.html" },
1700
+ .shell = .{ .windows = &shell_windows },
1701
+ };
1702
+
1703
+ const ios_stats = try createIosArtifact(std.testing.allocator, std.testing.io, .{
1704
+ .metadata = metadata,
1705
+ .output_path = ".zig-cache/test-package-mobile-identity/ios",
1706
+ .assets_dir = ".zig-cache/test-package-mobile-identity/assets",
1707
+ .frontend = metadata.frontend,
1708
+ });
1709
+ const android_stats = try createAndroidArtifact(std.testing.allocator, std.testing.io, .{
1710
+ .metadata = metadata,
1711
+ .output_path = ".zig-cache/test-package-mobile-identity/android",
1712
+ .assets_dir = ".zig-cache/test-package-mobile-identity/assets",
1713
+ .frontend = metadata.frontend,
1714
+ });
1715
+ try std.testing.expectEqual(@as(usize, 1), ios_stats.asset_count);
1716
+ try std.testing.expectEqual(@as(usize, 1), android_stats.asset_count);
1717
+
1718
+ const plist = try readPath(std.testing.allocator, std.testing.io, ".zig-cache/test-package-mobile-identity/ios/Host/Info.plist");
1719
+ defer std.testing.allocator.free(plist);
1720
+ try std.testing.expect(std.mem.indexOf(u8, plist, "dev.native-sdk.mobile-app") != null);
1721
+ try std.testing.expect(std.mem.indexOf(u8, plist, "Mobile Demo") != null);
1722
+ try std.testing.expect(std.mem.indexOf(u8, plist, "2.3.4") != null);
1723
+ try std.testing.expect(std.mem.indexOf(u8, plist, "UILaunchScreen") != null);
1724
+
1725
+ // The generated Xcode project ties the host, library, and resources
1726
+ // together with the app.zon identity — archive-ready with zero edits.
1727
+ const pbxproj = try readPath(std.testing.allocator, std.testing.io, ".zig-cache/test-package-mobile-identity/ios/mobile-demo.xcodeproj/project.pbxproj");
1728
+ defer std.testing.allocator.free(pbxproj);
1729
+ try std.testing.expect(std.mem.indexOf(u8, pbxproj, "PRODUCT_BUNDLE_IDENTIFIER = \"dev.native-sdk.mobile-app\";") != null);
1730
+ try std.testing.expect(std.mem.indexOf(u8, pbxproj, "PRODUCT_NAME = \"mobile-demo\";") != null);
1731
+ try std.testing.expect(std.mem.indexOf(u8, pbxproj, "MARKETING_VERSION = \"2.3.4\";") != null);
1732
+ try std.testing.expect(std.mem.indexOf(u8, pbxproj, "INFOPLIST_FILE = \"Host/Info.plist\";") != null);
1733
+ const scheme = try readPath(std.testing.allocator, std.testing.io, ".zig-cache/test-package-mobile-identity/ios/mobile-demo.xcodeproj/xcshareddata/xcschemes/mobile-demo.xcscheme");
1734
+ defer std.testing.allocator.free(scheme);
1735
+ try std.testing.expect(std.mem.indexOf(u8, scheme, "BuildableName = \"mobile-demo.app\"") != null);
1736
+ const packaged_host = try readPath(std.testing.allocator, std.testing.io, ".zig-cache/test-package-mobile-identity/ios/Host/uikit_host.m");
1737
+ defer std.testing.allocator.free(packaged_host);
1738
+ try std.testing.expectEqualStrings(ios_tool.host_source, packaged_host);
1739
+ var ios_libraries = try cwd.openDir(std.testing.io, ".zig-cache/test-package-mobile-identity/ios/Libraries", .{});
1740
+ ios_libraries.close(std.testing.io);
1741
+
1742
+ // The generated Android host project ties the manifest, host
1743
+ // sources, and resources together with the app.zon identity — the
1744
+ // debug APK assembles with zero edits when the toolchain is present
1745
+ // (skipped here: unit tests pass no environment to probe).
1746
+ const manifest = try readPath(std.testing.allocator, std.testing.io, ".zig-cache/test-package-mobile-identity/android/AndroidManifest.xml");
1747
+ defer std.testing.allocator.free(manifest);
1748
+ try std.testing.expect(std.mem.indexOf(u8, manifest, "package=\"dev.native_sdk.mobile_app\"") != null);
1749
+ try std.testing.expect(std.mem.indexOf(u8, manifest, "android:versionName=\"2.3.4\"") != null);
1750
+ try std.testing.expect(std.mem.indexOf(u8, manifest, "android:label=\"Mobile Demo\"") != null);
1751
+ try std.testing.expect(std.mem.indexOf(u8, manifest, "android:name=\"dev.native_sdk.host.NativeSdkActivity\"") != null);
1752
+ const packaged_activity = try readPath(std.testing.allocator, std.testing.io, ".zig-cache/test-package-mobile-identity/android/Host/NativeSdkActivity.java");
1753
+ defer std.testing.allocator.free(packaged_activity);
1754
+ try std.testing.expectEqualStrings(android_tool.host_activity_source, packaged_activity);
1755
+ const packaged_bridge = try readPath(std.testing.allocator, std.testing.io, ".zig-cache/test-package-mobile-identity/android/Host/android_host.c");
1756
+ defer std.testing.allocator.free(packaged_bridge);
1757
+ try std.testing.expectEqualStrings(android_tool.host_bridge_source, packaged_bridge);
1758
+ const launcher_icon = try readPath(std.testing.allocator, std.testing.io, ".zig-cache/test-package-mobile-identity/android/res/mipmap-xxxhdpi/ic_launcher.png");
1759
+ defer std.testing.allocator.free(launcher_icon);
1760
+ try std.testing.expect(launcher_icon.len > 8);
1761
+ var android_libraries = try cwd.openDir(std.testing.io, ".zig-cache/test-package-mobile-identity/android/Libraries", .{});
1762
+ android_libraries.close(std.testing.io);
1763
+
1764
+ const ios_asset = try readPath(std.testing.allocator, std.testing.io, ".zig-cache/test-package-mobile-identity/ios/Assets/dist/main.html");
1765
+ defer std.testing.allocator.free(ios_asset);
1766
+ try std.testing.expectEqualStrings("<h1>Mobile</h1>", ios_asset);
1767
+
1768
+ const android_asset = try readPath(std.testing.allocator, std.testing.io, ".zig-cache/test-package-mobile-identity/android/assets/native-sdk/dist/main.html");
1769
+ defer std.testing.allocator.free(android_asset);
1770
+ try std.testing.expectEqualStrings("<h1>Mobile</h1>", android_asset);
1771
+ }
1772
+
1773
+ test "mobile packages allow chromium desktop engine metadata" {
1774
+ var cwd = std.Io.Dir.cwd();
1775
+ try cwd.deleteTree(std.testing.io, ".zig-cache/test-package-mobile-chromium");
1776
+ defer cwd.deleteTree(std.testing.io, ".zig-cache/test-package-mobile-chromium") catch {};
1777
+ try cwd.createDirPath(std.testing.io, ".zig-cache/test-package-mobile-chromium/assets");
1778
+ try cwd.writeFile(std.testing.io, .{ .sub_path = ".zig-cache/test-package-mobile-chromium/assets/index.html", .data = "<h1>Mobile</h1>" });
1779
+
1780
+ const metadata: manifest_tool.Metadata = .{
1781
+ .id = "dev.native-sdk.mobile-chromium",
1782
+ .name = "mobile-chromium",
1783
+ .display_name = "Mobile Chromium",
1784
+ .version = "1.0.0",
1785
+ .frontend = .{ .dist = "dist", .entry = "index.html" },
1786
+ };
1787
+
1788
+ const ios_stats = try createPackage(std.testing.allocator, std.testing.io, .{
1789
+ .metadata = metadata,
1790
+ .target = .ios,
1791
+ .output_path = ".zig-cache/test-package-mobile-chromium/ios",
1792
+ .assets_dir = ".zig-cache/test-package-mobile-chromium/assets",
1793
+ .frontend = metadata.frontend,
1794
+ .web_engine = .chromium,
1795
+ });
1796
+ const android_stats = try createPackage(std.testing.allocator, std.testing.io, .{
1797
+ .metadata = metadata,
1798
+ .target = .android,
1799
+ .output_path = ".zig-cache/test-package-mobile-chromium/android",
1800
+ .assets_dir = ".zig-cache/test-package-mobile-chromium/assets",
1801
+ .frontend = metadata.frontend,
1802
+ .web_engine = .chromium,
1803
+ });
1804
+
1805
+ try std.testing.expectEqual(PackageTarget.ios, ios_stats.target);
1806
+ try std.testing.expectEqual(PackageTarget.android, android_stats.target);
1807
+ try std.testing.expectEqual(@as(usize, 1), ios_stats.asset_count);
1808
+ try std.testing.expectEqual(@as(usize, 1), android_stats.asset_count);
1809
+ }
1810
+
1811
+ test "linux desktop entry contains app name" {
1812
+ const metadata: manifest_tool.Metadata = .{ .id = "dev.example.app", .name = "demo", .display_name = "Demo App", .version = "1.2.3" };
1813
+ const entry = try linuxDesktopEntry(std.testing.allocator, metadata);
1814
+ defer std.testing.allocator.free(entry);
1815
+ try std.testing.expect(std.mem.indexOf(u8, entry, "Name=Demo App") != null);
1816
+ try std.testing.expect(std.mem.indexOf(u8, entry, "Exec=\"demo\"") != null);
1817
+ }
1818
+
1819
+ test "linux desktop metadata includes file associations and URL schemes" {
1820
+ const extensions = [_][]const u8{"md"};
1821
+ const associations = [_]manifest_tool.FileAssociationMetadata{.{
1822
+ .name = "Markdown Document",
1823
+ .extensions = &extensions,
1824
+ }};
1825
+ const schemes = [_]manifest_tool.UrlSchemeMetadata{.{ .scheme = "acme-notes" }};
1826
+ const metadata: manifest_tool.Metadata = .{
1827
+ .id = "dev.example.app",
1828
+ .name = "demo",
1829
+ .display_name = "Demo App",
1830
+ .version = "1.2.3",
1831
+ .file_associations = &associations,
1832
+ .url_schemes = &schemes,
1833
+ };
1834
+ const entry = try linuxDesktopEntry(std.testing.allocator, metadata);
1835
+ defer std.testing.allocator.free(entry);
1836
+ try std.testing.expect(std.mem.indexOf(u8, entry, "Exec=\"demo\" %U") != null);
1837
+ try std.testing.expect(std.mem.indexOf(u8, entry, "MimeType=application/x-demo-markdown-document;x-scheme-handler/acme-notes;") != null);
1838
+
1839
+ const mime_info = try linuxMimeInfo(std.testing.allocator, metadata);
1840
+ defer std.testing.allocator.free(mime_info);
1841
+ try std.testing.expect(std.mem.indexOf(u8, mime_info, "<mime-type type=\"application/x-demo-markdown-document\">") != null);
1842
+ try std.testing.expect(std.mem.indexOf(u8, mime_info, "<glob pattern=\"*.md\"/>") != null);
1843
+ }
1844
+
1845
+ test "linux desktop entry quotes executable names with spaces" {
1846
+ const extensions = [_][]const u8{"txt"};
1847
+ const associations = [_]manifest_tool.FileAssociationMetadata{.{
1848
+ .name = "Text Document",
1849
+ .extensions = &extensions,
1850
+ }};
1851
+ const metadata: manifest_tool.Metadata = .{
1852
+ .id = "dev.example.spaced",
1853
+ .name = "Example App",
1854
+ .version = "1.2.3",
1855
+ .file_associations = &associations,
1856
+ };
1857
+ const entry = try linuxDesktopEntry(std.testing.allocator, metadata);
1858
+ defer std.testing.allocator.free(entry);
1859
+ try std.testing.expect(std.mem.indexOf(u8, entry, "Exec=\"Example App\" %F") != null);
1860
+ }
1861
+
1862
+ test "artifact names include metadata target and optimize mode" {
1863
+ var buffer: [128]u8 = undefined;
1864
+ const metadata: manifest_tool.Metadata = .{ .id = "dev.example.app", .name = "demo", .version = "1.2.3" };
1865
+ try std.testing.expectEqualStrings("demo-1.2.3-macos-Debug.app", try artifactName(&buffer, metadata, .macos, "Debug"));
1866
+ }
1867
+
1868
+ test "plist template includes identity executable and version" {
1869
+ const metadata: manifest_tool.Metadata = .{ .id = "dev.example.app", .name = "demo", .display_name = "Demo App", .description = "A demo of the packaging pipeline.", .version = "1.2.3", .icons = &.{"assets/icon.icns"} };
1870
+ const plist = try macosInfoPlist(std.testing.allocator, metadata, "demo");
1871
+ defer std.testing.allocator.free(plist);
1872
+ try std.testing.expect(std.mem.indexOf(u8, plist, "CFBundleIdentifier") != null);
1873
+ try std.testing.expect(std.mem.indexOf(u8, plist, "CFBundleDisplayName") != null);
1874
+ try std.testing.expect(std.mem.indexOf(u8, plist, "dev.example.app") != null);
1875
+ try std.testing.expect(std.mem.indexOf(u8, plist, "Demo App") != null);
1876
+ // CFBundleName is what the application menu shows: it must carry the
1877
+ // display name, never the lowercase manifest/executable name.
1878
+ try std.testing.expect(std.mem.indexOf(u8, plist, "<key>CFBundleName</key>\n <string>Demo App</string>") != null);
1879
+ try std.testing.expect(std.mem.indexOf(u8, plist, "<string>demo</string>\n <key>CFBundleDisplayName</key>") == null);
1880
+ try std.testing.expect(std.mem.indexOf(u8, plist, "<key>CFBundleExecutable</key>\n <string>demo</string>") != null);
1881
+ try std.testing.expect(std.mem.indexOf(u8, plist, "icon.icns") != null);
1882
+ try std.testing.expect(std.mem.indexOf(u8, plist, "LSMinimumSystemVersion") != null);
1883
+ try std.testing.expect(std.mem.indexOf(u8, plist, "11.0") != null);
1884
+ // The manifest description reaches the About panel's footer key.
1885
+ try std.testing.expect(std.mem.indexOf(u8, plist, "NSHumanReadableCopyright") != null);
1886
+ try std.testing.expect(std.mem.indexOf(u8, plist, "A demo of the packaging pipeline.") != null);
1887
+
1888
+ // Without a description the key is absent, not emitted empty.
1889
+ const bare: manifest_tool.Metadata = .{ .id = "dev.example.app", .name = "demo", .version = "1.2.3" };
1890
+ const bare_plist = try macosInfoPlist(std.testing.allocator, bare, "demo");
1891
+ defer std.testing.allocator.free(bare_plist);
1892
+ try std.testing.expect(std.mem.indexOf(u8, bare_plist, "NSHumanReadableCopyright") == null);
1893
+ }
1894
+
1895
+ test "plist template includes document and URL registrations" {
1896
+ const extensions = [_][]const u8{ "md", ".markdown" };
1897
+ const mime_types = [_][]const u8{"text/markdown"};
1898
+ const associations = [_]manifest_tool.FileAssociationMetadata{.{
1899
+ .name = "Markdown Document",
1900
+ .role = "editor",
1901
+ .extensions = &extensions,
1902
+ .mime_types = &mime_types,
1903
+ .icon = "assets/markdown.icns",
1904
+ }};
1905
+ const schemes = [_]manifest_tool.UrlSchemeMetadata{.{ .scheme = "acme-notes" }};
1906
+ const metadata: manifest_tool.Metadata = .{
1907
+ .id = "dev.example.app",
1908
+ .name = "demo",
1909
+ .display_name = "Demo App",
1910
+ .version = "1.2.3",
1911
+ .file_associations = &associations,
1912
+ .url_schemes = &schemes,
1913
+ };
1914
+ const plist = try macosInfoPlist(std.testing.allocator, metadata, "demo");
1915
+ defer std.testing.allocator.free(plist);
1916
+ try std.testing.expect(std.mem.indexOf(u8, plist, "CFBundleDocumentTypes") != null);
1917
+ try std.testing.expect(std.mem.indexOf(u8, plist, "CFBundleTypeRole") != null);
1918
+ try std.testing.expect(std.mem.indexOf(u8, plist, "Editor") != null);
1919
+ try std.testing.expect(std.mem.indexOf(u8, plist, "markdown.icns") != null);
1920
+ try std.testing.expect(std.mem.indexOf(u8, plist, "<string>markdown</string>") != null);
1921
+ try std.testing.expect(std.mem.indexOf(u8, plist, "text/markdown") != null);
1922
+ try std.testing.expect(std.mem.indexOf(u8, plist, "CFBundleURLTypes") != null);
1923
+ try std.testing.expect(std.mem.indexOf(u8, plist, "acme-notes") != null);
1924
+ }
1925
+
1926
+ test "macOS package copies document type icons into resources" {
1927
+ var cwd = std.Io.Dir.cwd();
1928
+ try cwd.deleteTree(std.testing.io, ".zig-cache/test-package-doc-icons");
1929
+ defer cwd.deleteTree(std.testing.io, ".zig-cache/test-package-doc-icons") catch {};
1930
+ try cwd.createDirPath(std.testing.io, ".zig-cache/test-package-doc-icons/assets");
1931
+ try cwd.createDirPath(std.testing.io, ".zig-cache/test-package-doc-icons/doc-icons");
1932
+ try cwd.writeFile(std.testing.io, .{ .sub_path = ".zig-cache/test-package-doc-icons/doc-icons/markdown.icns", .data = "icnsdoc-icon" });
1933
+
1934
+ const extensions = [_][]const u8{"md"};
1935
+ const associations = [_]manifest_tool.FileAssociationMetadata{.{
1936
+ .name = "Markdown Document",
1937
+ .extensions = &extensions,
1938
+ .icon = ".zig-cache/test-package-doc-icons/doc-icons/markdown.icns",
1939
+ }};
1940
+ const metadata: manifest_tool.Metadata = .{
1941
+ .id = "dev.example.app",
1942
+ .name = "demo",
1943
+ .version = "1.2.3",
1944
+ .file_associations = &associations,
1945
+ };
1946
+
1947
+ _ = try createMacosApp(std.testing.allocator, std.testing.io, .{
1948
+ .metadata = metadata,
1949
+ .output_path = ".zig-cache/test-package-doc-icons/Demo.app",
1950
+ .assets_dir = ".zig-cache/test-package-doc-icons/assets",
1951
+ });
1952
+
1953
+ const copied = try readPath(std.testing.allocator, std.testing.io, ".zig-cache/test-package-doc-icons/Demo.app/Contents/Resources/markdown.icns");
1954
+ defer std.testing.allocator.free(copied);
1955
+ try std.testing.expectEqualStrings("icnsdoc-icon", copied);
1956
+ }
1957
+
1958
+ test "macOS package mirrors app assets at their app-relative path" {
1959
+ // The packaged host resolves relative asset paths against
1960
+ // Contents/Resources, so the bundle must carry the asset tree at the
1961
+ // same relative paths a dev run reads ("assets/music/track.mp3" →
1962
+ // Resources/assets/music/track.mp3) — never flattened into the
1963
+ // Resources root where no runtime path ever finds it.
1964
+ var cwd = std.Io.Dir.cwd();
1965
+ try cwd.deleteTree(std.testing.io, ".zig-cache/test-package-asset-layout");
1966
+ defer cwd.deleteTree(std.testing.io, ".zig-cache/test-package-asset-layout") catch {};
1967
+ try cwd.createDirPath(std.testing.io, ".zig-cache/test-package-asset-layout/assets/music");
1968
+ try cwd.writeFile(std.testing.io, .{ .sub_path = ".zig-cache/test-package-asset-layout/assets/music/track.mp3", .data = "mp3-bytes" });
1969
+
1970
+ const metadata: manifest_tool.Metadata = .{ .id = "dev.example.app", .name = "demo", .version = "1.2.3" };
1971
+ const stats = try createMacosApp(std.testing.allocator, std.testing.io, .{
1972
+ .metadata = metadata,
1973
+ .output_path = ".zig-cache/test-package-asset-layout/Demo.app",
1974
+ .assets_dir = ".zig-cache/test-package-asset-layout/assets",
1975
+ });
1976
+ try std.testing.expectEqual(@as(usize, 1), stats.asset_count);
1977
+
1978
+ const bundled = try readPath(std.testing.allocator, std.testing.io, ".zig-cache/test-package-asset-layout/Demo.app/Contents/Resources/.zig-cache/test-package-asset-layout/assets/music/track.mp3");
1979
+ defer std.testing.allocator.free(bundled);
1980
+ try std.testing.expectEqualStrings("mp3-bytes", bundled);
1981
+ }
1982
+
1983
+ test "app-relative asset subpaths accept plain trees and reject escapes" {
1984
+ try std.testing.expectEqualStrings("assets", appRelativeAssetSubpath("assets").?);
1985
+ try std.testing.expectEqualStrings("data/sounds", appRelativeAssetSubpath("data/sounds").?);
1986
+ try std.testing.expect(appRelativeAssetSubpath("") == null);
1987
+ try std.testing.expect(appRelativeAssetSubpath(".") == null);
1988
+ try std.testing.expect(appRelativeAssetSubpath("./.") == null);
1989
+ try std.testing.expect(appRelativeAssetSubpath("../shared") == null);
1990
+ try std.testing.expect(appRelativeAssetSubpath("assets/../..") == null);
1991
+ try std.testing.expect(appRelativeAssetSubpath("/tmp/assets") == null);
1992
+ }
1993
+
1994
+ test "windows registration script contains extension and protocol keys" {
1995
+ const extensions = [_][]const u8{"md"};
1996
+ const associations = [_]manifest_tool.FileAssociationMetadata{.{
1997
+ .name = "Markdown Document",
1998
+ .extensions = &extensions,
1999
+ }};
2000
+ const schemes = [_]manifest_tool.UrlSchemeMetadata{.{ .scheme = "acme-notes" }};
2001
+ const metadata: manifest_tool.Metadata = .{
2002
+ .id = "dev.example.app",
2003
+ .name = "demo",
2004
+ .version = "1.2.3",
2005
+ .file_associations = &associations,
2006
+ .url_schemes = &schemes,
2007
+ };
2008
+ const script = try windowsRegistrationScript(std.testing.allocator, metadata, "demo.exe");
2009
+ defer std.testing.allocator.free(script);
2010
+ try std.testing.expect(std.mem.indexOf(u8, script, "bin\\demo.exe") != null);
2011
+ try std.testing.expect(std.mem.indexOf(u8, script, "HKCU\\Software\\Classes\\.md") != null);
2012
+ try std.testing.expect(std.mem.indexOf(u8, script, "dev.example.app.MarkdownDocument") != null);
2013
+ try std.testing.expect(std.mem.indexOf(u8, script, "HKCU\\Software\\Classes\\acme-notes") != null);
2014
+ try std.testing.expect(std.mem.indexOf(u8, script, "URL:acme-notes") != null);
2015
+ }
2016
+
2017
+ test "copying files preserves executable permissions" {
2018
+ if (!std.Io.File.Permissions.has_executable_bit) return error.SkipZigTest;
2019
+
2020
+ var cwd = std.Io.Dir.cwd();
2021
+ try cwd.deleteTree(std.testing.io, ".zig-cache/test-package-copy-mode");
2022
+ try cwd.createDirPath(std.testing.io, ".zig-cache/test-package-copy-mode/dest");
2023
+ defer cwd.deleteTree(std.testing.io, ".zig-cache/test-package-copy-mode") catch {};
2024
+
2025
+ const source_path = ".zig-cache/test-package-copy-mode/source-bin";
2026
+ var source = try cwd.createFile(std.testing.io, source_path, .{ .permissions = .executable_file });
2027
+ try source.writeStreamingAll(std.testing.io, "test binary");
2028
+ source.close(std.testing.io);
2029
+
2030
+ var dest_dir = try cwd.openDir(std.testing.io, ".zig-cache/test-package-copy-mode/dest", .{});
2031
+ defer dest_dir.close(std.testing.io);
2032
+ try copyFileToDir(std.testing.allocator, std.testing.io, dest_dir, source_path, "Contents/MacOS/app");
2033
+
2034
+ var dest = try dest_dir.openFile(std.testing.io, "Contents/MacOS/app", .{});
2035
+ defer dest.close(std.testing.io);
2036
+ const dest_permissions = (try dest.stat(std.testing.io)).permissions;
2037
+ try std.testing.expect((dest_permissions.toMode() & 0o111) != 0);
2038
+ }
2039
+
2040
+ test "macOS app executable is marked executable" {
2041
+ if (!std.Io.File.Permissions.has_executable_bit) return error.SkipZigTest;
2042
+
2043
+ var cwd = std.Io.Dir.cwd();
2044
+ try cwd.deleteTree(std.testing.io, ".zig-cache/test-package-macos-mode");
2045
+ try cwd.createDirPath(std.testing.io, ".zig-cache/test-package-macos-mode/assets");
2046
+ defer cwd.deleteTree(std.testing.io, ".zig-cache/test-package-macos-mode") catch {};
2047
+
2048
+ const source_path = ".zig-cache/test-package-macos-mode/source-bin";
2049
+ try cwd.writeFile(std.testing.io, .{ .sub_path = source_path, .data = "test binary" });
2050
+
2051
+ const metadata: manifest_tool.Metadata = .{ .id = "dev.example.app", .name = "mode-test", .version = "1.2.3" };
2052
+ _ = try createMacosApp(std.testing.allocator, std.testing.io, .{
2053
+ .metadata = metadata,
2054
+ .output_path = ".zig-cache/test-package-macos-mode/ModeTest.app",
2055
+ .binary_path = source_path,
2056
+ .assets_dir = ".zig-cache/test-package-macos-mode/assets",
2057
+ });
2058
+
2059
+ var app_dir = try cwd.openDir(std.testing.io, ".zig-cache/test-package-macos-mode/ModeTest.app", .{});
2060
+ defer app_dir.close(std.testing.io);
2061
+ var executable = try app_dir.openFile(std.testing.io, "Contents/MacOS/mode-test", .{});
2062
+ defer executable.close(std.testing.io);
2063
+ const permissions = (try executable.stat(std.testing.io)).permissions;
2064
+ try std.testing.expect((permissions.toMode() & 0o111) != 0);
2065
+ }
2066
+
2067
+ test "desktop chromium packages are rejected before CEF layout checks" {
2068
+ const metadata: manifest_tool.Metadata = .{
2069
+ .id = "dev.demo",
2070
+ .name = "demo",
2071
+ .version = "0.1.0",
2072
+ };
2073
+
2074
+ try std.testing.expectError(error.UnsupportedWebEngine, createPackage(std.testing.allocator, std.testing.io, .{
2075
+ .metadata = metadata,
2076
+ .target = .linux,
2077
+ .output_path = ".zig-cache/test-package-linux-chromium",
2078
+ .web_engine = .chromium,
2079
+ .cef_dir = ".zig-cache/missing-linux-cef",
2080
+ }));
2081
+ try std.testing.expectError(error.UnsupportedWebEngine, createPackage(std.testing.allocator, std.testing.io, .{
2082
+ .metadata = metadata,
2083
+ .target = .windows,
2084
+ .output_path = ".zig-cache/test-package-windows-chromium",
2085
+ .web_engine = .chromium,
2086
+ .cef_dir = ".zig-cache/missing-windows-cef",
2087
+ }));
2088
+ }
2089
+
2090
+ test "package report records target signing and assets" {
2091
+ const metadata: manifest_tool.Metadata = .{ .id = "dev.example.app", .name = "demo", .version = "1.2.3" };
2092
+ var cwd = std.Io.Dir.cwd();
2093
+ try cwd.createDirPath(std.testing.io, ".zig-cache/test-package-report");
2094
+ var dir = try cwd.openDir(std.testing.io, ".zig-cache/test-package-report", .{});
2095
+ defer dir.close(std.testing.io);
2096
+ try writeReport(std.testing.allocator, dir, std.testing.io, "package-manifest.zon", .{
2097
+ .metadata = metadata,
2098
+ .target = .linux,
2099
+ .output_path = ".zig-cache/test-package-report",
2100
+ .signing = .{ .mode = .none },
2101
+ }, "demo", 2);
2102
+ var buffer: [512]u8 = undefined;
2103
+ var file = try dir.openFile(std.testing.io, "package-manifest.zon", .{});
2104
+ defer file.close(std.testing.io);
2105
+ const len = try file.readPositionalAll(std.testing.io, &buffer, 0);
2106
+ try std.testing.expect(std.mem.indexOf(u8, buffer[0..len], ".target = \"linux\"") != null);
2107
+ try std.testing.expect(std.mem.indexOf(u8, buffer[0..len], ".asset_count = 2") != null);
2108
+ }
2109
+
2110
+ // ---------------------------------------------------------------------------
2111
+ // App icon pipeline tests
2112
+ // ---------------------------------------------------------------------------
2113
+
2114
+ /// Write a solid full-bleed square PNG source for icon tests.
2115
+ fn writeTestIconSource(gpa: std.mem.Allocator, io: std.Io, path: []const u8, extent: usize) !void {
2116
+ const pixels = try gpa.alloc(u8, extent * extent * 4);
2117
+ defer gpa.free(pixels);
2118
+ var index: usize = 0;
2119
+ while (index < extent * extent) : (index += 1) {
2120
+ pixels[index * 4 + 0] = 40;
2121
+ pixels[index * 4 + 1] = 90;
2122
+ pixels[index * 4 + 2] = 220;
2123
+ pixels[index * 4 + 3] = 255;
2124
+ }
2125
+ const encoded = try app_icon_tool.encodePng(gpa, pixels, extent, extent);
2126
+ defer gpa.free(encoded);
2127
+ try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = encoded });
2128
+ }
2129
+
2130
+ test "macos package generates a full icns family from a png source" {
2131
+ const gpa = std.testing.allocator;
2132
+ var cwd = std.Io.Dir.cwd();
2133
+ const root = ".zig-cache/test-package-icon-gen";
2134
+ try cwd.deleteTree(std.testing.io, root);
2135
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2136
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2137
+ try writeTestIconSource(gpa, std.testing.io, root ++ "/assets/icon.png", 128);
2138
+
2139
+ const metadata: manifest_tool.Metadata = .{
2140
+ .id = "dev.example.app",
2141
+ .name = "demo",
2142
+ .version = "1.0.0",
2143
+ .icons = &.{root ++ "/assets/icon.png"},
2144
+ };
2145
+ _ = try createMacosApp(gpa, std.testing.io, .{
2146
+ .metadata = metadata,
2147
+ .output_path = root ++ "/Demo.app",
2148
+ .assets_dir = root ++ "/assets",
2149
+ });
2150
+
2151
+ const icns = try readPath(gpa, std.testing.io, root ++ "/Demo.app/Contents/Resources/AppIcon.icns");
2152
+ defer gpa.free(icns);
2153
+ var iterator = app_icon_tool.IcnsIterator.init(icns) orelse return error.TestUnexpectedResult;
2154
+ var seen: usize = 0;
2155
+ while (iterator.next()) |member| {
2156
+ const slot = app_icon_tool.icns_slots[seen];
2157
+ try std.testing.expectEqualSlices(u8, &slot.kind, &member.kind);
2158
+ switch (slot.payload) {
2159
+ .png => {
2160
+ const header = app_icon_tool.pngHeader(member.data) orelse return error.TestUnexpectedResult;
2161
+ try std.testing.expectEqual(slot.size, header.width);
2162
+ try std.testing.expectEqual(slot.size, header.height);
2163
+ },
2164
+ .argb => {
2165
+ const rgba = try app_icon_tool.decodeArgb(gpa, member.data, slot.size, slot.size);
2166
+ defer gpa.free(rgba);
2167
+ try std.testing.expectEqual(slot.size * slot.size * 4, rgba.len);
2168
+ },
2169
+ }
2170
+ seen += 1;
2171
+ }
2172
+ try std.testing.expectEqual(app_icon_tool.icns_slots.len, seen);
2173
+
2174
+ // The Info.plist references the generated name, not the source name.
2175
+ const plist = try readPath(gpa, std.testing.io, root ++ "/Demo.app/Contents/Info.plist");
2176
+ defer gpa.free(plist);
2177
+ try std.testing.expect(std.mem.indexOf(u8, plist, "AppIcon.icns") != null);
2178
+ try std.testing.expect(std.mem.indexOf(u8, plist, "icon.png") == null);
2179
+ }
2180
+
2181
+ test "a prebuilt icns wins untouched over a png source" {
2182
+ const gpa = std.testing.allocator;
2183
+ var cwd = std.Io.Dir.cwd();
2184
+ const root = ".zig-cache/test-package-icon-precedence";
2185
+ try cwd.deleteTree(std.testing.io, root);
2186
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2187
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2188
+ try writeTestIconSource(gpa, std.testing.io, root ++ "/assets/icon.png", 64);
2189
+ const prebuilt = "icns\x00\x00\x00\x0cJUNK";
2190
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/assets/icon.icns", .data = prebuilt });
2191
+
2192
+ const metadata: manifest_tool.Metadata = .{
2193
+ .id = "dev.example.app",
2194
+ .name = "demo",
2195
+ .version = "1.0.0",
2196
+ // Source listed FIRST: the prebuilt .icns must still win.
2197
+ .icons = &.{ root ++ "/assets/icon.png", root ++ "/assets/icon.icns" },
2198
+ };
2199
+ _ = try createMacosApp(gpa, std.testing.io, .{
2200
+ .metadata = metadata,
2201
+ .output_path = root ++ "/Demo.app",
2202
+ .assets_dir = root ++ "/assets",
2203
+ });
2204
+
2205
+ const copied = try readPath(gpa, std.testing.io, root ++ "/Demo.app/Contents/Resources/icon.icns");
2206
+ defer gpa.free(copied);
2207
+ try std.testing.expectEqualStrings(prebuilt, copied);
2208
+ }
2209
+
2210
+ test "macos package without icons ships the default icon" {
2211
+ const gpa = std.testing.allocator;
2212
+ var cwd = std.Io.Dir.cwd();
2213
+ const root = ".zig-cache/test-package-icon-default";
2214
+ try cwd.deleteTree(std.testing.io, root);
2215
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2216
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2217
+
2218
+ const metadata: manifest_tool.Metadata = .{ .id = "dev.example.app", .name = "demo", .version = "1.0.0" };
2219
+ _ = try createMacosApp(gpa, std.testing.io, .{
2220
+ .metadata = metadata,
2221
+ .output_path = root ++ "/Demo.app",
2222
+ .assets_dir = root ++ "/assets",
2223
+ });
2224
+ const icns = try readPath(gpa, std.testing.io, root ++ "/Demo.app/Contents/Resources/AppIcon.icns");
2225
+ defer gpa.free(icns);
2226
+ try std.testing.expect(app_icon_tool.IcnsIterator.init(icns) != null);
2227
+ }
2228
+
2229
+ test "a non-square icon source fails packaging with the teaching error" {
2230
+ const gpa = std.testing.allocator;
2231
+ var cwd = std.Io.Dir.cwd();
2232
+ const root = ".zig-cache/test-package-icon-nonsquare";
2233
+ try cwd.deleteTree(std.testing.io, root);
2234
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2235
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2236
+ const pixels = try gpa.alloc(u8, 6 * 4 * 4);
2237
+ defer gpa.free(pixels);
2238
+ @memset(pixels, 255);
2239
+ const encoded = try app_icon_tool.encodePng(gpa, pixels, 6, 4);
2240
+ defer gpa.free(encoded);
2241
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/assets/icon.png", .data = encoded });
2242
+
2243
+ const metadata: manifest_tool.Metadata = .{
2244
+ .id = "dev.example.app",
2245
+ .name = "demo",
2246
+ .version = "1.0.0",
2247
+ .icons = &.{root ++ "/assets/icon.png"},
2248
+ };
2249
+ try std.testing.expectError(error.InvalidIconSource, createMacosApp(gpa, std.testing.io, .{
2250
+ .metadata = metadata,
2251
+ .output_path = root ++ "/Demo.app",
2252
+ .assets_dir = root ++ "/assets",
2253
+ }));
2254
+ }
2255
+
2256
+ test "linux artifact installs the hicolor icon size set" {
2257
+ const gpa = std.testing.allocator;
2258
+ var cwd = std.Io.Dir.cwd();
2259
+ const root = ".zig-cache/test-package-icon-linux";
2260
+ try cwd.deleteTree(std.testing.io, root);
2261
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2262
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2263
+ try writeTestIconSource(gpa, std.testing.io, root ++ "/assets/icon.png", 64);
2264
+
2265
+ const metadata: manifest_tool.Metadata = .{
2266
+ .id = "dev.example.app",
2267
+ .name = "demo",
2268
+ .version = "1.0.0",
2269
+ .icons = &.{root ++ "/assets/icon.png"},
2270
+ };
2271
+ _ = try createPackage(gpa, std.testing.io, .{
2272
+ .metadata = metadata,
2273
+ .target = .linux,
2274
+ .output_path = root ++ "/demo-linux",
2275
+ .assets_dir = root ++ "/assets",
2276
+ });
2277
+
2278
+ inline for (app_icon_tool.linux_sizes) |size| {
2279
+ const icon_path = try std.fmt.allocPrint(gpa, "{s}/demo-linux/share/icons/hicolor/{d}x{d}/apps/app-icon.png", .{ root, size, size });
2280
+ defer gpa.free(icon_path);
2281
+ const encoded = try readPath(gpa, std.testing.io, icon_path);
2282
+ defer gpa.free(encoded);
2283
+ const header = app_icon_tool.pngHeader(encoded) orelse return error.TestUnexpectedResult;
2284
+ try std.testing.expectEqual(@as(usize, size), header.width);
2285
+ try std.testing.expectEqual(@as(usize, size), header.height);
2286
+ }
2287
+ }
2288
+
2289
+ test "windows artifact gets a generated multi-size ico" {
2290
+ const gpa = std.testing.allocator;
2291
+ var cwd = std.Io.Dir.cwd();
2292
+ const root = ".zig-cache/test-package-icon-windows";
2293
+ try cwd.deleteTree(std.testing.io, root);
2294
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2295
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2296
+ try writeTestIconSource(gpa, std.testing.io, root ++ "/assets/icon.png", 64);
2297
+
2298
+ const metadata: manifest_tool.Metadata = .{
2299
+ .id = "dev.example.app",
2300
+ .name = "demo",
2301
+ .version = "1.0.0",
2302
+ .icons = &.{root ++ "/assets/icon.png"},
2303
+ };
2304
+ _ = try createPackage(gpa, std.testing.io, .{
2305
+ .metadata = metadata,
2306
+ .target = .windows,
2307
+ .output_path = root ++ "/demo-windows",
2308
+ .assets_dir = root ++ "/assets",
2309
+ });
2310
+
2311
+ const ico = try readPath(gpa, std.testing.io, root ++ "/demo-windows/app-icon.ico");
2312
+ defer gpa.free(ico);
2313
+ var iterator = app_icon_tool.IcoIterator.init(ico) orelse return error.TestUnexpectedResult;
2314
+ var seen: usize = 0;
2315
+ while (iterator.next()) |entry| {
2316
+ try std.testing.expectEqual(app_icon_tool.ico_sizes[seen], entry.size);
2317
+ const header = app_icon_tool.pngHeader(entry.data) orelse return error.TestUnexpectedResult;
2318
+ try std.testing.expectEqual(app_icon_tool.ico_sizes[seen], header.width);
2319
+ seen += 1;
2320
+ }
2321
+ try std.testing.expectEqual(app_icon_tool.ico_sizes.len, seen);
2322
+ }
2323
+
2324
+ test "ios artifact carries the asset-catalog icon set" {
2325
+ const gpa = std.testing.allocator;
2326
+ var cwd = std.Io.Dir.cwd();
2327
+ const root = ".zig-cache/test-package-icon-ios";
2328
+ try cwd.deleteTree(std.testing.io, root);
2329
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2330
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2331
+ try writeTestIconSource(gpa, std.testing.io, root ++ "/assets/icon.png", 64);
2332
+
2333
+ const metadata: manifest_tool.Metadata = .{
2334
+ .id = "dev.example.app",
2335
+ .name = "demo",
2336
+ .version = "1.0.0",
2337
+ .icons = &.{root ++ "/assets/icon.png"},
2338
+ };
2339
+ _ = try createPackage(gpa, std.testing.io, .{
2340
+ .metadata = metadata,
2341
+ .target = .ios,
2342
+ .output_path = root ++ "/demo-ios",
2343
+ .assets_dir = root ++ "/assets",
2344
+ });
2345
+
2346
+ const contents = try readPath(gpa, std.testing.io, root ++ "/demo-ios/Assets.xcassets/AppIcon.appiconset/Contents.json");
2347
+ defer gpa.free(contents);
2348
+ try std.testing.expect(std.mem.indexOf(u8, contents, "1024x1024") != null);
2349
+ const icon = try readPath(gpa, std.testing.io, root ++ "/demo-ios/Assets.xcassets/AppIcon.appiconset/AppIcon.png");
2350
+ defer gpa.free(icon);
2351
+ const header = app_icon_tool.pngHeader(icon) orelse return error.TestUnexpectedResult;
2352
+ try std.testing.expectEqual(app_icon_tool.ios_icon_size, header.width);
2353
+ }
2354
+
2355
+ test "android artifact carries launcher mipmaps and references them" {
2356
+ const gpa = std.testing.allocator;
2357
+ var cwd = std.Io.Dir.cwd();
2358
+ const root = ".zig-cache/test-package-icon-android";
2359
+ try cwd.deleteTree(std.testing.io, root);
2360
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2361
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2362
+ try writeTestIconSource(gpa, std.testing.io, root ++ "/assets/icon.png", 64);
2363
+
2364
+ const metadata: manifest_tool.Metadata = .{
2365
+ .id = "dev.example.app",
2366
+ .name = "demo",
2367
+ .version = "1.0.0",
2368
+ .icons = &.{root ++ "/assets/icon.png"},
2369
+ };
2370
+ _ = try createPackage(gpa, std.testing.io, .{
2371
+ .metadata = metadata,
2372
+ .target = .android,
2373
+ .output_path = root ++ "/demo-android",
2374
+ .assets_dir = root ++ "/assets",
2375
+ });
2376
+
2377
+ inline for (app_icon_tool.android_densities) |density| {
2378
+ const icon_path = try std.fmt.allocPrint(gpa, "{s}/demo-android/res/mipmap-{s}/ic_launcher.png", .{ root, density.name });
2379
+ defer gpa.free(icon_path);
2380
+ const encoded = try readPath(gpa, std.testing.io, icon_path);
2381
+ defer gpa.free(encoded);
2382
+ const header = app_icon_tool.pngHeader(encoded) orelse return error.TestUnexpectedResult;
2383
+ try std.testing.expectEqual(@as(usize, density.size), header.width);
2384
+ }
2385
+ const manifest = try readPath(gpa, std.testing.io, root ++ "/demo-android/AndroidManifest.xml");
2386
+ defer gpa.free(manifest);
2387
+ try std.testing.expect(std.mem.indexOf(u8, manifest, "android:icon=\"@mipmap/ic_launcher\"") != null);
2388
+ }