@native-sdk/cli 0.8.3 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/build/app.zig +905 -61
  2. package/build.zig +674 -373
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +6 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +455 -3
  7. package/packages/core/package-lock.json +11 -11
  8. package/packages/core/package.json +2 -2
  9. package/packages/core/scripts/compiler_typecheck.mjs +116 -15
  10. package/packages/core/scripts/gen_service_surface.mjs +268 -0
  11. package/packages/core/scripts/npm_static_spike.mjs +64 -0
  12. package/packages/core/scripts/run_external_core_compiler.mjs +77 -6
  13. package/packages/core/scripts/run_external_service_compiler.mjs +240 -0
  14. package/packages/core/scripts/stage_external_core.mjs +9 -1
  15. package/packages/core/scripts/stage_external_services.mjs +172 -0
  16. package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
  17. package/packages/core/scripts/vendor_service_packages.mjs +189 -0
  18. package/packages/core/sdk/core.d.ts +214 -2
  19. package/packages/core/sdk/core.ts +715 -42
  20. package/packages/core/sdk/events.d.ts +50 -0
  21. package/packages/core/sdk/events.ts +75 -0
  22. package/packages/core/src/checker.ts +528 -15
  23. package/packages/core/src/cli.ts +143 -3
  24. package/packages/core/src/contract.ts +26 -5
  25. package/packages/core/src/devhost.ts +1849 -18
  26. package/packages/core/src/devhost_journal.mjs +181 -0
  27. package/packages/core/src/devhost_resolver.mjs +17 -1
  28. package/packages/core/src/devhost_service_worker.mjs +90 -0
  29. package/packages/core/src/diagnostics.ts +84 -18
  30. package/packages/core/src/frontend.ts +204 -34
  31. package/packages/core/src/modules.ts +129 -80
  32. package/packages/core/src/service_contract.ts +597 -0
  33. package/packages/core/src/sqlite_cli.ts +93 -0
  34. package/packages/core/src/sqlite_codegen.ts +806 -0
  35. package/packages/core/src/sqlite_runtime_policy.ts +364 -0
  36. package/packages/core/src/typed_ast.ts +36 -8
  37. package/skill-data/core/SKILL.md +4 -2
  38. package/skill-data/core/references/project-anatomy.md +3 -2
  39. package/skill-data/core/references/web-engines-packaging-debugging.md +3 -1
  40. package/skill-data/native-ui/SKILL.md +55 -8
  41. package/skill-data/ts-core/SKILL.md +92 -40
  42. package/skill-data/ts-services/SKILL.md +126 -0
  43. package/skill-data/ts-services/references/service-surface.md +603 -0
  44. package/skill-data/zig/SKILL.md +1 -1
  45. package/skills/native-sdk/SKILL.md +1 -1
  46. package/src/app_runner/no_migrations.zig +3 -0
  47. package/src/app_runner/no_services.zig +20 -0
  48. package/src/app_runner/root.zig +104 -4
  49. package/src/app_runner/ts_core_main.zig +268 -4
  50. package/src/automation/snapshot.zig +23 -5
  51. package/src/embed/app_exports.zig +11 -1
  52. package/src/embed/c_api.zig +48 -2
  53. package/src/embed/conversions.zig +4 -5
  54. package/src/embed/host.zig +115 -1
  55. package/src/embed/root.zig +7 -0
  56. package/src/embed/tests.zig +213 -0
  57. package/src/embed/types.zig +51 -0
  58. package/src/embed/ui_host.zig +121 -5
  59. package/src/platform/android/NativeSdkActivity.java +131 -0
  60. package/src/platform/android/android_host.c +182 -1
  61. package/src/platform/android/native_sdk_app.h +17 -0
  62. package/src/platform/ios/native_sdk_app.h +17 -0
  63. package/src/platform/ios/uikit_host.m +115 -0
  64. package/src/platform/linux/cef_host.cpp +1 -1
  65. package/src/platform/linux/gtk_host.c +89 -17
  66. package/src/platform/linux/gtk_host.h +3 -0
  67. package/src/platform/linux/root.zig +41 -18
  68. package/src/platform/macos/appkit_host.h +26 -3
  69. package/src/platform/macos/appkit_host.m +1051 -129
  70. package/src/platform/macos/capture_info_plist.c +25 -0
  71. package/src/platform/macos/cef_host.mm +270 -36
  72. package/src/platform/macos/root.zig +312 -34
  73. package/src/platform/null_platform.zig +281 -19
  74. package/src/platform/null_platform_tests.zig +74 -0
  75. package/src/platform/root.zig +16 -0
  76. package/src/platform/types.zig +279 -15
  77. package/src/platform/windows/root.zig +243 -21
  78. package/src/platform/windows/webview2_host.cpp +464 -46
  79. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  80. package/src/primitives/app_manifest/root.zig +4 -0
  81. package/src/primitives/app_manifest/tests.zig +3 -0
  82. package/src/primitives/app_manifest/types.zig +34 -0
  83. package/src/primitives/app_manifest/validation.zig +19 -1
  84. package/src/primitives/canvas/code_tests.zig +9 -9
  85. package/src/primitives/canvas/events.zig +12 -11
  86. package/src/primitives/canvas/frame.zig +44 -9
  87. package/src/primitives/canvas/icons/mic.svg +1 -0
  88. package/src/primitives/canvas/icons.zig +2 -0
  89. package/src/primitives/canvas/root.zig +8 -0
  90. package/src/primitives/canvas/text_span_tests.zig +39 -0
  91. package/src/primitives/canvas/ui.zig +23 -4
  92. package/src/primitives/canvas/ui_markup.zig +15 -0
  93. package/src/primitives/canvas/ui_markup_compiled.zig +9 -0
  94. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1 -1
  95. package/src/primitives/canvas/ui_markup_tests.zig +5 -0
  96. package/src/primitives/canvas/ui_markup_view.zig +9 -0
  97. package/src/primitives/canvas/ui_markup_view_tests.zig +9 -6
  98. package/src/primitives/canvas/ui_schema.zig +19 -10
  99. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  100. package/src/primitives/canvas/ui_tests.zig +18 -4
  101. package/src/primitives/canvas/widget_invalidation.zig +9 -2
  102. package/src/primitives/canvas/widget_render.zig +296 -114
  103. package/src/primitives/canvas/widget_runtime_tests.zig +133 -0
  104. package/src/primitives/canvas/widget_text_input.zig +10 -10
  105. package/src/primitives/canvas/widget_tree.zig +1 -1
  106. package/src/primitives/canvas/widgets.zig +29 -5
  107. package/src/root.zig +56 -0
  108. package/src/runtime/api.zig +11 -0
  109. package/src/runtime/automation_snapshot.zig +4 -0
  110. package/src/runtime/automation_widget_dispatch.zig +1 -0
  111. package/src/runtime/bridge_payload.zig +2 -0
  112. package/src/runtime/bridge_permission_tests.zig +23 -2
  113. package/src/runtime/bridge_responses.zig +3 -1
  114. package/src/runtime/canvas_frame.zig +31 -13
  115. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  116. package/src/runtime/canvas_frame_invalidation_tests.zig +35 -10
  117. package/src/runtime/canvas_widget_anchored_tests.zig +83 -0
  118. package/src/runtime/canvas_widget_events.zig +69 -24
  119. package/src/runtime/canvas_widget_runtime.zig +101 -6
  120. package/src/runtime/canvas_widget_scroll_driver_tests.zig +47 -4
  121. package/src/runtime/canvas_widget_scroll_drivers.zig +3 -3
  122. package/src/runtime/canvas_widget_semantics_tests.zig +2 -0
  123. package/src/runtime/canvas_widget_split_tree_tests.zig +228 -0
  124. package/src/runtime/canvas_widget_state.zig +6 -1
  125. package/src/runtime/canvas_widget_text_tests.zig +112 -3
  126. package/src/runtime/clock.zig +13 -1
  127. package/src/runtime/core.zig +133 -1
  128. package/src/runtime/credentials_store.zig +115 -0
  129. package/src/runtime/effects.zig +2568 -75
  130. package/src/runtime/effects_audio_capture_tests.zig +251 -0
  131. package/src/runtime/effects_credentials_tests.zig +332 -0
  132. package/src/runtime/effects_host_tests.zig +208 -1
  133. package/src/runtime/effects_relational_tests.zig +314 -0
  134. package/src/runtime/effects_store_tests.zig +211 -0
  135. package/src/runtime/flow.zig +14 -1
  136. package/src/runtime/gpu_surface_events.zig +6 -1
  137. package/src/runtime/native_command_event_tests.zig +42 -0
  138. package/src/runtime/persist_store.zig +666 -0
  139. package/src/runtime/record_store.zig +788 -0
  140. package/src/runtime/relational_store.zig +854 -0
  141. package/src/runtime/root.zig +52 -0
  142. package/src/runtime/runtime_event_tests.zig +70 -0
  143. package/src/runtime/service_host.zig +711 -0
  144. package/src/runtime/service_pool.zig +1314 -0
  145. package/src/runtime/session_blobs.zig +1 -1
  146. package/src/runtime/session_journal.zig +102 -0
  147. package/src/runtime/session_record.zig +177 -0
  148. package/src/runtime/session_replay.zig +109 -4
  149. package/src/runtime/shell_layout.zig +1 -0
  150. package/src/runtime/shell_layout_tests.zig +120 -0
  151. package/src/runtime/sqlite_engine.zig +474 -0
  152. package/src/runtime/state.zig +6 -0
  153. package/src/runtime/system_services.zig +49 -10
  154. package/src/runtime/tests.zig +5 -0
  155. package/src/runtime/ts_core_host.zig +1189 -118
  156. package/src/runtime/ts_core_host_tests.zig +638 -11
  157. package/src/runtime/ts_ui_app.zig +537 -1
  158. package/src/runtime/ui_app.zig +160 -19
  159. package/src/runtime/ui_app_tests.zig +203 -5
  160. package/src/runtime/validation.zig +22 -0
  161. package/src/runtime/view.zig +11 -0
  162. package/src/runtime/view_widget_scroll.zig +56 -5
  163. package/src/runtime/view_widget_text.zig +24 -5
  164. package/src/runtime/view_widget_tree.zig +1 -1
  165. package/src/runtime/window_storage.zig +1 -0
  166. package/src/runtime/window_view_runtime.zig +2 -3
  167. package/src/runtime/window_views.zig +19 -0
  168. package/src/tooling/android.zig +21 -16
  169. package/src/tooling/db.zig +160 -0
  170. package/src/tooling/ios.zig +7 -1
  171. package/src/tooling/manifest.zig +1248 -2
  172. package/src/tooling/package.zig +953 -6
  173. package/src/tooling/raw_manifest.zig +65 -0
  174. package/src/tooling/root.zig +1 -0
  175. package/src/tooling/templates.zig +216 -4
  176. package/src/tooling/ts_core.zig +283 -17
  177. package/src/tooling/verbs.zig +7 -0
  178. package/third_party/sqlite/README.md +13 -0
  179. package/third_party/sqlite/sqlite3.c +269649 -0
  180. package/third_party/sqlite/sqlite3.h +14349 -0
  181. package/tools/corewire/core_abi.zig +14 -2
  182. package/tools/corewire/emit.zig +98 -29
  183. package/tools/corewire/emit_facade.zig +414 -23
  184. package/tools/corewire/emit_profile.zig +6 -2
  185. package/tools/corewire/emit_service.zig +924 -0
  186. package/tools/corewire/main.zig +122 -0
  187. package/tools/corewire/service_contract.zig +250 -0
  188. package/tools/corewire/sidecar.zig +26 -15
@@ -62,7 +62,14 @@ pub const PackageOptions = struct {
62
62
  target: PackageTarget = .macos,
63
63
  optimize: []const u8 = "Debug",
64
64
  output_path: []const u8,
65
+ /// Project root used to resolve app.zon-relative packaging inputs such
66
+ /// as a custom DMG background. The CLI derives it from --manifest.
67
+ project_dir: []const u8 = ".",
65
68
  binary_path: ?[]const u8 = null,
69
+ /// Phase-1 TypeScript service host. Desktop packages place this next
70
+ /// to the app executable so the generated runner can resolve it by
71
+ /// sibling name and both executables enter the signing boundary.
72
+ service_binary_path: ?[]const u8 = null,
66
73
  assets_dir: []const u8 = "assets",
67
74
  frontend: ?manifest_tool.FrontendMetadata = null,
68
75
  web_engine: WebEngine = .system,
@@ -84,6 +91,45 @@ pub const PackageOptions = struct {
84
91
  env_map: ?*std.process.Environ.Map = null,
85
92
  };
86
93
 
94
+ /// Whether the app source tree declares the TypeScript service class. This
95
+ /// guards CLI auto-discovery so a stale installed helper is never added to a
96
+ /// package after the app's services have been removed.
97
+ pub fn projectHasTypeScriptServices(allocator: std.mem.Allocator, io: std.Io, project_dir: []const u8) !bool {
98
+ const services_path = try std.fs.path.join(allocator, &.{ project_dir, "src", "services" });
99
+ defer allocator.free(services_path);
100
+ var dir = std.Io.Dir.cwd().openDir(io, services_path, .{ .iterate = true }) catch return false;
101
+ defer dir.close(io);
102
+ var walker = try dir.walk(allocator);
103
+ defer walker.deinit();
104
+ while (try walker.next(io)) |entry| {
105
+ if (entry.kind == .file and std.mem.endsWith(u8, entry.basename, ".ts") and
106
+ !std.mem.endsWith(u8, entry.basename, ".d.ts")) return true;
107
+ }
108
+ return false;
109
+ }
110
+
111
+ /// Locate the service host installed by a normal `native build`. Explicit
112
+ /// package inputs still win; this is the CLI's no-flag fallback twin of the
113
+ /// main executable discovery.
114
+ pub fn discoverInstalledServiceBinary(
115
+ allocator: std.mem.Allocator,
116
+ io: std.Io,
117
+ project_dir: []const u8,
118
+ app_name: []const u8,
119
+ target: PackageTarget,
120
+ ) !?[]const u8 {
121
+ const suffix: []const u8 = if (target == .windows) ".exe" else "";
122
+ const file_name = try std.fmt.allocPrint(allocator, "{s}_services{s}", .{ app_name, suffix });
123
+ defer allocator.free(file_name);
124
+ const candidate = try std.fs.path.join(allocator, &.{ project_dir, "zig-out", "bin", file_name });
125
+ var file = std.Io.Dir.cwd().openFile(io, candidate, .{}) catch {
126
+ allocator.free(candidate);
127
+ return null;
128
+ };
129
+ file.close(io);
130
+ return candidate;
131
+ }
132
+
87
133
  /// The Windows subsystem verdict the packaging posture check reaches:
88
134
  /// `gui` and `console` only when the PE optional header actually said
89
135
  /// so, `unknown` when the probe proved nothing — non-PE or truncated
@@ -162,6 +208,16 @@ pub fn createPackage(allocator: std.mem.Allocator, io: std.Io, options: PackageO
162
208
  return err;
163
209
  };
164
210
  try validateWebEngineTarget(options.target, options.web_engine);
211
+ if (options.target == .macos and options.archive) {
212
+ manifest_tool.validateDmgPackageSettings(options.metadata) catch |err| {
213
+ std.debug.print("error: app.zon dmg settings are invalid ({s})\n", .{@errorName(err)});
214
+ return err;
215
+ };
216
+ if (try manifest_tool.checkDmgSources(allocator, io, options.project_dir, options.metadata.dmg)) |message| {
217
+ std.debug.print("error: {s}\n", .{message});
218
+ return error.InvalidDmgSource;
219
+ }
220
+ }
165
221
  var stats = switch (options.target) {
166
222
  .macos => try createMacosApp(allocator, io, options),
167
223
  .windows, .linux => try createDesktopArtifact(allocator, io, options),
@@ -243,6 +299,12 @@ pub fn createMacosApp(allocator: std.mem.Allocator, io: std.Io, options: Package
243
299
  } else {
244
300
  try writeFile(package_dir, io, "Contents/MacOS/README.txt", "No app binary was supplied for this local package.\n");
245
301
  }
302
+ if (options.service_binary_path) |service_binary_path| {
303
+ const service_subpath = try std.fmt.allocPrint(allocator, "Contents/MacOS/{s}_services", .{executable_name});
304
+ defer allocator.free(service_subpath);
305
+ try copyFileToDir(allocator, io, package_dir, service_binary_path, service_subpath);
306
+ try makeExecutable(package_dir, io, service_subpath);
307
+ }
246
308
 
247
309
  const info_plist = try macosInfoPlist(allocator, options.metadata, executable_name);
248
310
  defer allocator.free(info_plist);
@@ -348,6 +410,13 @@ fn createDesktopArtifact(allocator: std.mem.Allocator, io: std.Io, options: Pack
348
410
  } else {
349
411
  try writeFile(dir, io, "bin/README.txt", "Build the app binary separately and place it here for this target.\n");
350
412
  }
413
+ if (options.service_binary_path) |service_binary_path| {
414
+ const service_suffix: []const u8 = if (options.target == .windows) ".exe" else "";
415
+ const service_subpath = try std.fmt.allocPrint(allocator, "bin/{s}_services{s}", .{ options.metadata.name, service_suffix });
416
+ defer allocator.free(service_subpath);
417
+ try copyFileToDir(allocator, io, dir, service_binary_path, service_subpath);
418
+ try makeExecutable(dir, io, service_subpath);
419
+ }
351
420
 
352
421
  const assets_output = try assetOutputPath(allocator, options.output_path, "resources", options);
353
422
  defer allocator.free(assets_output);
@@ -666,6 +735,8 @@ fn macosInfoPlist(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata
666
735
  defer allocator.free(document_types);
667
736
  const url_types = try macosUrlTypes(allocator, metadata);
668
737
  defer allocator.free(url_types);
738
+ const privacy_descriptions = try macosPrivacyUsageDescriptions(allocator, metadata);
739
+ defer allocator.free(privacy_descriptions);
669
740
  // The About panel's bottom line in packaged bundles: the manifest
670
741
  // description rides NSHumanReadableCopyright, the plist key the
671
742
  // standard About panel renders as its footer text — the same line
@@ -702,11 +773,51 @@ fn macosInfoPlist(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata
702
773
  \\ <string>{s}</string>
703
774
  \\ <key>CFBundleVersion</key>
704
775
  \\ <string>{s}</string>
705
- \\{s}{s}{s}
776
+ \\{s}{s}{s}{s}
706
777
  \\</dict>
707
778
  \\</plist>
708
779
  \\
709
- , .{ bundle_id, display_name, display_name, executable, icon, version, version, about_line, document_types, url_types });
780
+ , .{ bundle_id, display_name, display_name, executable, icon, version, version, about_line, privacy_descriptions, document_types, url_types });
781
+ }
782
+
783
+ fn metadataHasPermission(metadata: manifest_tool.Metadata, name: []const u8) bool {
784
+ for (metadata.permissions) |permission| {
785
+ if (std.mem.eql(u8, permission, name)) return true;
786
+ }
787
+ return false;
788
+ }
789
+
790
+ /// Privacy usage strings are generated from the manifest's explicit capture
791
+ /// permissions. ScreenCaptureKit historically uses the screen-recording
792
+ /// consent surface for desktop audio; newer macOS also recognizes the
793
+ /// audio-only usage key, so system capture declares both.
794
+ fn macosPrivacyUsageDescriptions(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata) ![]const u8 {
795
+ const microphone = metadataHasPermission(metadata, "microphone");
796
+ const system_audio = metadataHasPermission(metadata, "system_audio");
797
+ if (!microphone and !system_audio) return allocator.dupe(u8, "");
798
+ const display_name = try xmlEscapeAlloc(allocator, metadata.displayName());
799
+ defer allocator.free(display_name);
800
+ if (microphone and system_audio) return std.fmt.allocPrint(allocator,
801
+ \\ <key>NSMicrophoneUsageDescription</key>
802
+ \\ <string>{s} captures microphone audio when you start recording.</string>
803
+ \\ <key>NSAudioCaptureUsageDescription</key>
804
+ \\ <string>{s} captures system audio when you start recording.</string>
805
+ \\ <key>NSScreenCaptureUsageDescription</key>
806
+ \\ <string>{s} captures system audio when you start recording.</string>
807
+ \\
808
+ , .{ display_name, display_name, display_name });
809
+ if (microphone) return std.fmt.allocPrint(allocator,
810
+ \\ <key>NSMicrophoneUsageDescription</key>
811
+ \\ <string>{s} captures microphone audio when you start recording.</string>
812
+ \\
813
+ , .{display_name});
814
+ return std.fmt.allocPrint(allocator,
815
+ \\ <key>NSAudioCaptureUsageDescription</key>
816
+ \\ <string>{s} captures system audio when you start recording.</string>
817
+ \\ <key>NSScreenCaptureUsageDescription</key>
818
+ \\ <string>{s} captures system audio when you start recording.</string>
819
+ \\
820
+ , .{ display_name, display_name });
710
821
  }
711
822
 
712
823
  /// The optional NSHumanReadableCopyright entry (with trailing newline)
@@ -1960,20 +2071,492 @@ fn createArchive(allocator: std.mem.Allocator, io: std.Io, options: PackageOptio
1960
2071
  defer allocator.free(archive_command_path);
1961
2072
 
1962
2073
  const ok = switch (options.target) {
1963
- .macos => runArchiveCommand(io, &.{ "hdiutil", "create", "-volname", options.metadata.displayName(), "-srcfolder", options.output_path, "-ov", "-format", "UDZO", archive_command_path }, null),
2074
+ .macos => try createMacosDmg(allocator, io, options, archive_command_path),
1964
2075
  .windows => runArchiveCommand(io, &.{ "zip", "-r", archive_command_path, "." }, options.output_path),
1965
2076
  .linux => runArchiveCommand(io, &.{ "tar", "czf", archive_command_path, "-C", options.output_path, "." }, null),
1966
2077
  .ios, .android => unreachable,
1967
2078
  };
1968
2079
 
1969
2080
  if (!ok) {
1970
- std.debug.print("warning: archive creation failed for {s}\n", .{archive_path});
1971
- allocator.free(archive_path);
1972
- return null;
2081
+ std.debug.print("error: archive creation failed for {s}\n", .{archive_path});
2082
+ return error.ArchiveCreationFailed;
1973
2083
  }
1974
2084
  return archive_path;
1975
2085
  }
1976
2086
 
2087
+ /// Build the familiar macOS drag-to-Applications image without introducing a
2088
+ /// package-time dependency. A writable image is populated first so Finder can
2089
+ /// persist its icon-view presentation, then converted to the compressed UDZO
2090
+ /// artifact users download. If Finder automation is unavailable (for example
2091
+ /// in a headless build worker), the Applications link and background still
2092
+ /// ship and only the saved window arrangement is omitted.
2093
+ fn createMacosDmg(allocator: std.mem.Allocator, io: std.Io, options: PackageOptions, archive_path: []const u8) !bool {
2094
+ const dmg = options.metadata.dmg;
2095
+ const volume_name = dmg.volume_name orelse options.metadata.displayName();
2096
+ var nonce_bytes: [8]u8 = undefined;
2097
+ std.Io.random(io, &nonce_bytes);
2098
+ const nonce = std.mem.readInt(u64, &nonce_bytes, .little);
2099
+ const work_path = try std.fmt.allocPrint(allocator, "{s}.native-work-{x}", .{ archive_path, nonce });
2100
+ defer allocator.free(work_path);
2101
+ const work_absolute = try absolutePathAlloc(allocator, io, work_path);
2102
+ defer allocator.free(work_absolute);
2103
+ const rw_image_path = try std.fs.path.join(allocator, &.{ work_absolute, "staging.dmg" });
2104
+ defer allocator.free(rw_image_path);
2105
+ const source_path = try std.fs.path.join(allocator, &.{ work_absolute, "source" });
2106
+ defer allocator.free(source_path);
2107
+ const mount_path = try std.fs.path.join(allocator, &.{ work_absolute, "mount" });
2108
+ defer allocator.free(mount_path);
2109
+ const app_path = try absolutePathAlloc(allocator, io, options.output_path);
2110
+ defer allocator.free(app_path);
2111
+
2112
+ var cwd = std.Io.Dir.cwd();
2113
+ try cwd.createDirPath(io, work_path);
2114
+ var work_dir = try cwd.openDir(io, work_path, .{});
2115
+ defer work_dir.close(io);
2116
+ try work_dir.createDirPath(io, "source");
2117
+ try work_dir.createDirPath(io, "mount");
2118
+
2119
+ var attached = false;
2120
+ defer {
2121
+ if (attached) {
2122
+ if (runArchiveCommand(io, &.{ "hdiutil", "detach", "-quiet", mount_path }, null) or
2123
+ runArchiveCommand(io, &.{ "hdiutil", "detach", "-quiet", "-force", mount_path }, null))
2124
+ {
2125
+ attached = false;
2126
+ } else {
2127
+ std.debug.print("warning: could not detach temporary DMG at {s}; leaving {s} for manual cleanup\n", .{ mount_path, work_path });
2128
+ }
2129
+ }
2130
+ if (!attached) cwd.deleteTree(io, work_path) catch {};
2131
+ }
2132
+
2133
+ const app_name = try dmgAppBundleNameAlloc(allocator, options.metadata);
2134
+ defer allocator.free(app_name);
2135
+ var source_dir = try cwd.openDir(io, source_path, .{});
2136
+ defer source_dir.close(io);
2137
+ try stageDmgItems(allocator, io, options, source_dir, source_path, app_path, app_name);
2138
+
2139
+ const retina_background_source = try dmgRetinaBackgroundSourceAlloc(allocator, io, options.project_dir, dmg.background);
2140
+ defer if (retina_background_source) |path| allocator.free(path);
2141
+ const background_name = try dmgBackgroundNameAlloc(allocator, dmg.background, retina_background_source != null);
2142
+ defer allocator.free(background_name);
2143
+ try stageDmgBackground(allocator, io, options, source_dir, source_path, background_name, retina_background_source);
2144
+
2145
+ // Finder persists portable window presentation in the volume's
2146
+ // `.DS_Store` on HFS+. With hdiutil's current APFS default, the same
2147
+ // successful AppleScript can leave no metadata in the image at all.
2148
+ if (!runArchiveCommand(io, &.{ "hdiutil", "create", "-quiet", "-volname", volume_name, "-srcfolder", source_path, "-ov", "-format", "UDRW", "-fs", "HFS+", rw_image_path }, null)) return false;
2149
+ if (!runArchiveCommand(io, &.{ "hdiutil", "attach", "-quiet", rw_image_path, "-mountpoint", mount_path, "-nobrowse", "-noverify", "-noautoopen" }, null)) return false;
2150
+ attached = true;
2151
+
2152
+ const finder_script = try dmgFinderScriptAlloc(allocator, dmg, mount_path, app_name, background_name);
2153
+ defer allocator.free(finder_script);
2154
+ if (!runArchiveCommand(io, &.{ "osascript", "-e", finder_script }, null)) {
2155
+ std.debug.print("warning: Finder could not save the DMG window layout; the image still includes its configured items and background\n", .{});
2156
+ } else if (!try waitForDmgFinderMetadata(allocator, io, mount_path)) {
2157
+ std.debug.print("warning: Finder returned success but did not persist the DMG window layout; the image still includes its configured items and background\n", .{});
2158
+ }
2159
+
2160
+ if (!runArchiveCommand(io, &.{ "hdiutil", "detach", "-quiet", mount_path }, null)) {
2161
+ if (!runArchiveCommand(io, &.{ "hdiutil", "detach", "-quiet", "-force", mount_path }, null)) return false;
2162
+ }
2163
+ attached = false;
2164
+ return runArchiveCommand(io, &.{ "hdiutil", "convert", "-quiet", rw_image_path, "-ov", "-format", "UDZO", "-imagekey", "zlib-level=9", "-o", archive_path }, null);
2165
+ }
2166
+
2167
+ fn dmgAppBundleNameAlloc(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata) ![]u8 {
2168
+ var buffer: [255]u8 = undefined;
2169
+ return allocator.dupe(u8, try manifest_tool.dmgAppBundleName(&buffer, metadata));
2170
+ }
2171
+
2172
+ fn stageDmgItems(
2173
+ allocator: std.mem.Allocator,
2174
+ io: std.Io,
2175
+ options: PackageOptions,
2176
+ source_dir: std.Io.Dir,
2177
+ source_path: []const u8,
2178
+ app_path: []const u8,
2179
+ app_name: []const u8,
2180
+ ) !void {
2181
+ const app_destination = try std.fs.path.join(allocator, &.{ source_path, app_name });
2182
+ defer allocator.free(app_destination);
2183
+ if (!runArchiveCommand(io, &.{ "ditto", app_path, app_destination }, null)) {
2184
+ std.debug.print("error: packaged app could not be staged in the DMG\n", .{});
2185
+ return error.DmgItemCopyFailed;
2186
+ }
2187
+
2188
+ const dmg = options.metadata.dmg;
2189
+ if (dmg.items.len == 0) {
2190
+ if (dmg.applications_link) try source_dir.symLink(io, "/Applications", "Applications", .{ .is_directory = true });
2191
+ return;
2192
+ }
2193
+
2194
+ for (dmg.items) |item| switch (item.kind) {
2195
+ .app => {},
2196
+ .applications => try source_dir.symLink(io, "/Applications", "Applications", .{ .is_directory = true }),
2197
+ .file => {
2198
+ const item_path = item.path orelse return error.InvalidDmgItem;
2199
+ const item_name = manifest_tool.dmgItemDestinationName(item) orelse return error.InvalidDmgItem;
2200
+ if (std.ascii.eqlIgnoreCase(item_name, app_name)) {
2201
+ std.debug.print("error: DMG item {s} conflicts with the packaged app name\n", .{item_name});
2202
+ return error.DuplicateDmgItem;
2203
+ }
2204
+ const item_source = try std.fs.path.join(allocator, &.{ options.project_dir, item_path });
2205
+ defer allocator.free(item_source);
2206
+ const item_destination = try std.fs.path.join(allocator, &.{ source_path, item_name });
2207
+ defer allocator.free(item_destination);
2208
+ if (!runArchiveCommand(io, &.{ "ditto", item_source, item_destination }, null)) {
2209
+ std.debug.print("error: DMG item {s} could not be copied from {s}\n", .{ item_name, item_source });
2210
+ return error.DmgItemCopyFailed;
2211
+ }
2212
+ },
2213
+ .link => {
2214
+ const target = item.path orelse return error.InvalidDmgItem;
2215
+ const item_name = manifest_tool.dmgItemDestinationName(item) orelse return error.InvalidDmgItem;
2216
+ if (std.ascii.eqlIgnoreCase(item_name, app_name)) {
2217
+ std.debug.print("error: DMG item {s} conflicts with the packaged app name\n", .{item_name});
2218
+ return error.DuplicateDmgItem;
2219
+ }
2220
+ try source_dir.symLink(io, target, item_name, .{ .is_directory = true });
2221
+ },
2222
+ };
2223
+ }
2224
+
2225
+ fn dmgRetinaBackgroundSourceAlloc(allocator: std.mem.Allocator, io: std.Io, project_dir: []const u8, background: ?[]const u8) !?[]const u8 {
2226
+ const path = background orelse return null;
2227
+ const retina_relative = (try manifest_tool.dmgRetinaRelativePathAlloc(allocator, path)) orelse return null;
2228
+ defer allocator.free(retina_relative);
2229
+ const retina_source = try std.fs.path.join(allocator, &.{ project_dir, retina_relative });
2230
+ errdefer allocator.free(retina_source);
2231
+ var file = std.Io.Dir.cwd().openFile(io, retina_source, .{}) catch {
2232
+ allocator.free(retina_source);
2233
+ return null;
2234
+ };
2235
+ file.close(io);
2236
+ return retina_source;
2237
+ }
2238
+
2239
+ fn stageDmgBackground(
2240
+ allocator: std.mem.Allocator,
2241
+ io: std.Io,
2242
+ options: PackageOptions,
2243
+ source_dir: std.Io.Dir,
2244
+ source_path: []const u8,
2245
+ background_name: []const u8,
2246
+ retina_background_source: ?[]const u8,
2247
+ ) !void {
2248
+ const dmg = options.metadata.dmg;
2249
+ try source_dir.createDirPath(io, ".background");
2250
+ const output_path = try std.fs.path.join(allocator, &.{ source_path, ".background", background_name });
2251
+ defer allocator.free(output_path);
2252
+
2253
+ if (dmg.background) |background| {
2254
+ const background_source = try std.fs.path.join(allocator, &.{ options.project_dir, background });
2255
+ defer allocator.free(background_source);
2256
+ if (retina_background_source) |retina_source| {
2257
+ const extension = std.fs.path.extension(background);
2258
+ const one_x_name = try std.fmt.allocPrint(allocator, "background{s}", .{extension});
2259
+ defer allocator.free(one_x_name);
2260
+ const two_x_name = try std.fmt.allocPrint(allocator, "background@2x{s}", .{extension});
2261
+ defer allocator.free(two_x_name);
2262
+ const one_x_subpath = try std.fs.path.join(allocator, &.{ ".background", one_x_name });
2263
+ defer allocator.free(one_x_subpath);
2264
+ const two_x_subpath = try std.fs.path.join(allocator, &.{ ".background", two_x_name });
2265
+ defer allocator.free(two_x_subpath);
2266
+ try std.Io.Dir.copyFile(std.Io.Dir.cwd(), background_source, source_dir, one_x_subpath, io, .{ .make_path = true, .replace = true });
2267
+ try std.Io.Dir.copyFile(std.Io.Dir.cwd(), retina_source, source_dir, two_x_subpath, io, .{ .make_path = true, .replace = true });
2268
+ const one_x_path = try std.fs.path.join(allocator, &.{ source_path, one_x_subpath });
2269
+ defer allocator.free(one_x_path);
2270
+ const two_x_path = try std.fs.path.join(allocator, &.{ source_path, two_x_subpath });
2271
+ defer allocator.free(two_x_path);
2272
+ try assembleRetinaDmgBackground(io, one_x_path, two_x_path, output_path, "custom");
2273
+ try source_dir.deleteFile(io, one_x_subpath);
2274
+ try source_dir.deleteFile(io, two_x_subpath);
2275
+ } else {
2276
+ const output_subpath = try std.fs.path.join(allocator, &.{ ".background", background_name });
2277
+ defer allocator.free(output_subpath);
2278
+ try std.Io.Dir.copyFile(std.Io.Dir.cwd(), background_source, source_dir, output_subpath, io, .{ .make_path = true, .replace = true });
2279
+ }
2280
+ return;
2281
+ }
2282
+
2283
+ const generated_1x = try defaultDmgBackgroundAtScale(allocator, dmg, 1);
2284
+ defer allocator.free(generated_1x);
2285
+ const generated_2x = try defaultDmgBackgroundAtScale(allocator, dmg, 2);
2286
+ defer allocator.free(generated_2x);
2287
+ try source_dir.writeFile(io, .{ .sub_path = ".background/background.png", .data = generated_1x });
2288
+ try source_dir.writeFile(io, .{ .sub_path = ".background/background@2x.png", .data = generated_2x });
2289
+ const one_x_path = try std.fs.path.join(allocator, &.{ source_path, ".background", "background.png" });
2290
+ defer allocator.free(one_x_path);
2291
+ const two_x_path = try std.fs.path.join(allocator, &.{ source_path, ".background", "background@2x.png" });
2292
+ defer allocator.free(two_x_path);
2293
+ try assembleRetinaDmgBackground(io, one_x_path, two_x_path, output_path, "generated");
2294
+ try source_dir.deleteFile(io, ".background/background.png");
2295
+ try source_dir.deleteFile(io, ".background/background@2x.png");
2296
+ }
2297
+
2298
+ fn assembleRetinaDmgBackground(io: std.Io, one_x_path: []const u8, two_x_path: []const u8, output_path: []const u8, kind: []const u8) !void {
2299
+ if (!runArchiveCommandQuiet(io, &.{ "tiffutil", "-cathidpicheck", one_x_path, two_x_path, "-out", output_path }, null)) {
2300
+ std.debug.print("error: could not assemble the {s} 1x/2x DMG background with tiffutil; the @2x image must be exactly twice the base image's dimensions\n", .{kind});
2301
+ return error.DmgBackgroundAssemblyFailed;
2302
+ }
2303
+ }
2304
+
2305
+ fn dmgBackgroundNameAlloc(allocator: std.mem.Allocator, background: ?[]const u8, has_retina_sibling: bool) ![]const u8 {
2306
+ const extension = if (background) |path| if (has_retina_sibling) ".tiff" else std.fs.path.extension(path) else ".tiff";
2307
+ return std.fmt.allocPrint(allocator, "background{s}", .{extension});
2308
+ }
2309
+
2310
+ fn defaultDmgBackground(allocator: std.mem.Allocator, dmg: manifest_tool.DmgMetadata) ![]u8 {
2311
+ return defaultDmgBackgroundAtScale(allocator, dmg, 1);
2312
+ }
2313
+
2314
+ fn defaultDmgBackgroundAtScale(allocator: std.mem.Allocator, dmg: manifest_tool.DmgMetadata, scale: usize) ![]u8 {
2315
+ std.debug.assert(scale > 0);
2316
+ const width: usize = @as(usize, dmg.window_width) * scale;
2317
+ const height: usize = @as(usize, dmg.window_height) * scale;
2318
+ const pixels = try allocator.alloc(u8, width * height * 4);
2319
+ defer allocator.free(pixels);
2320
+
2321
+ for (0..height) |y| {
2322
+ const t = if (height > 1) @as(f32, @floatFromInt(y)) / @as(f32, @floatFromInt(height - 1)) else 0;
2323
+ const shade = [3]u8{
2324
+ @intFromFloat(@round(249.0 - 11.0 * t)),
2325
+ @intFromFloat(@round(250.0 - 11.0 * t)),
2326
+ @intFromFloat(@round(252.0 - 10.0 * t)),
2327
+ };
2328
+ for (0..width) |x| {
2329
+ const offset = (y * width + x) * 4;
2330
+ pixels[offset + 0] = shade[0];
2331
+ pixels[offset + 1] = shade[1];
2332
+ pixels[offset + 2] = shade[2];
2333
+ pixels[offset + 3] = 255;
2334
+ }
2335
+ }
2336
+
2337
+ if (dmgInstallPositions(dmg) != null) drawDmgArrow(pixels, width, height, dmg, scale);
2338
+ return app_icon_tool.encodePng(allocator, pixels, width, height);
2339
+ }
2340
+
2341
+ fn drawDmgArrow(pixels: []u8, width: usize, height: usize, dmg: manifest_tool.DmgMetadata, scale: usize) void {
2342
+ const install_positions = dmgInstallPositions(dmg) orelse return;
2343
+ const pixel_scale: f32 = @floatFromInt(scale);
2344
+ const app_x = @as(f32, @floatFromInt(install_positions.app.x)) * pixel_scale;
2345
+ const app_y = @as(f32, @floatFromInt(install_positions.app.y)) * pixel_scale;
2346
+ const applications_x = @as(f32, @floatFromInt(install_positions.applications.x)) * pixel_scale;
2347
+ const applications_y = @as(f32, @floatFromInt(install_positions.applications.y)) * pixel_scale;
2348
+ const dx = applications_x - app_x;
2349
+ const dy = applications_y - app_y;
2350
+ const distance = @sqrt(dx * dx + dy * dy);
2351
+ const app_margin = @as(f32, @floatFromInt(dmg.icon_size)) * pixel_scale * 0.64;
2352
+ // Finder's Applications folder glyph is optically wider than a typical
2353
+ // app glyph at the same icon-size setting. Give that end a little more
2354
+ // breathing room so the arrow reads centered between visible edges.
2355
+ const applications_margin = @as(f32, @floatFromInt(dmg.icon_size)) * pixel_scale * 0.71;
2356
+ if (distance <= app_margin + applications_margin + 28.0 * pixel_scale) return;
2357
+ const ux = dx / distance;
2358
+ const uy = dy / distance;
2359
+ const px = -uy;
2360
+ const py = ux;
2361
+ const start_x = app_x + ux * app_margin;
2362
+ const start_y = app_y + uy * app_margin;
2363
+ const end_x = applications_x - ux * applications_margin;
2364
+ const end_y = applications_y - uy * applications_margin;
2365
+ const head_length: f32 = @min(28.0 * pixel_scale, distance * 0.12);
2366
+ const head_width: f32 = head_length * 0.62;
2367
+ const head_x = end_x - ux * head_length;
2368
+ const head_y = end_y - uy * head_length;
2369
+
2370
+ const segments = [3]DmgArrowSegment{
2371
+ .{ .ax = start_x, .ay = start_y, .bx = end_x, .by = end_y },
2372
+ .{ .ax = end_x, .ay = end_y, .bx = head_x + px * head_width, .by = head_y + py * head_width },
2373
+ .{ .ax = end_x, .ay = end_y, .bx = head_x - px * head_width, .by = head_y - py * head_width },
2374
+ };
2375
+ var shadow_segments = segments;
2376
+ for (&shadow_segments) |*segment| {
2377
+ segment.ay += 2 * pixel_scale;
2378
+ segment.by += 2 * pixel_scale;
2379
+ }
2380
+
2381
+ const shadow = [3]u8{ 70, 74, 82 };
2382
+ drawDmgArrowShape(pixels, width, height, &shadow_segments, 8 * pixel_scale, shadow, 0.10);
2383
+
2384
+ const arrow = [3]u8{ 105, 110, 120 };
2385
+ drawDmgArrowShape(pixels, width, height, &segments, 5 * pixel_scale, arrow, 0.58);
2386
+ }
2387
+
2388
+ const DmgInstallPositions = struct {
2389
+ app: manifest_tool.DmgPosition,
2390
+ applications: manifest_tool.DmgPosition,
2391
+ };
2392
+
2393
+ fn dmgInstallPositions(dmg: manifest_tool.DmgMetadata) ?DmgInstallPositions {
2394
+ if (dmg.items.len == 0) {
2395
+ if (!dmg.applications_link) return null;
2396
+ return .{ .app = dmg.app_position, .applications = dmg.applications_position };
2397
+ }
2398
+ var app: ?manifest_tool.DmgPosition = null;
2399
+ var applications: ?manifest_tool.DmgPosition = null;
2400
+ for (dmg.items) |item| switch (item.kind) {
2401
+ .app => app = item.position,
2402
+ .applications => applications = item.position,
2403
+ .file, .link => {},
2404
+ };
2405
+ return .{ .app = app orelse return null, .applications = applications orelse return null };
2406
+ }
2407
+
2408
+ const DmgArrowSegment = struct {
2409
+ ax: f32,
2410
+ ay: f32,
2411
+ bx: f32,
2412
+ by: f32,
2413
+ };
2414
+
2415
+ /// Rasterize the union of rounded line segments and blend it once. Taking the
2416
+ /// maximum coverage prevents translucent joins from becoming darker, while
2417
+ /// the one-pixel analytic fringe keeps diagonal and rounded edges smooth.
2418
+ fn drawDmgArrowShape(pixels: []u8, width: usize, height: usize, segments: []const DmgArrowSegment, thickness: f32, color: [3]u8, alpha: f32) void {
2419
+ if (segments.len == 0 or thickness <= 0 or alpha <= 0) return;
2420
+ const radius = thickness * 0.5;
2421
+ for (0..height) |y| {
2422
+ for (0..width) |x| {
2423
+ const fx = @as(f32, @floatFromInt(x)) + 0.5;
2424
+ const fy = @as(f32, @floatFromInt(y)) + 0.5;
2425
+ var nearest_squared = std.math.inf(f32);
2426
+ for (segments) |segment| {
2427
+ const dx = segment.bx - segment.ax;
2428
+ const dy = segment.by - segment.ay;
2429
+ const length_squared = dx * dx + dy * dy;
2430
+ if (length_squared <= 0.001) continue;
2431
+ const projection = std.math.clamp(((fx - segment.ax) * dx + (fy - segment.ay) * dy) / length_squared, 0.0, 1.0);
2432
+ const distance_x = fx - (segment.ax + projection * dx);
2433
+ const distance_y = fy - (segment.ay + projection * dy);
2434
+ nearest_squared = @min(nearest_squared, distance_x * distance_x + distance_y * distance_y);
2435
+ }
2436
+ if (nearest_squared > (radius + 0.5) * (radius + 0.5)) continue;
2437
+ const coverage = std.math.clamp(radius + 0.5 - @sqrt(@max(nearest_squared, 0)), 0.0, 1.0);
2438
+ if (coverage <= 0) continue;
2439
+ const offset = (y * width + x) * 4;
2440
+ const pixel_alpha = alpha * coverage;
2441
+ const inverse = 1.0 - pixel_alpha;
2442
+ pixels[offset + 0] = @intFromFloat(@round(@as(f32, @floatFromInt(color[0])) * pixel_alpha + @as(f32, @floatFromInt(pixels[offset + 0])) * inverse));
2443
+ pixels[offset + 1] = @intFromFloat(@round(@as(f32, @floatFromInt(color[1])) * pixel_alpha + @as(f32, @floatFromInt(pixels[offset + 1])) * inverse));
2444
+ pixels[offset + 2] = @intFromFloat(@round(@as(f32, @floatFromInt(color[2])) * pixel_alpha + @as(f32, @floatFromInt(pixels[offset + 2])) * inverse));
2445
+ }
2446
+ }
2447
+ }
2448
+
2449
+ fn dmgFinderScriptAlloc(allocator: std.mem.Allocator, dmg: manifest_tool.DmgMetadata, mount_path: []const u8, app_name: []const u8, background_name: []const u8) ![]u8 {
2450
+ const escaped_mount = try appleScriptStringAlloc(allocator, mount_path);
2451
+ defer allocator.free(escaped_mount);
2452
+ const background_path = try std.fs.path.join(allocator, &.{ mount_path, ".background", background_name });
2453
+ defer allocator.free(background_path);
2454
+ const escaped_background_path = try appleScriptStringAlloc(allocator, background_path);
2455
+ defer allocator.free(escaped_background_path);
2456
+ const position_lines = try dmgFinderPositionLinesAlloc(allocator, dmg, app_name);
2457
+ defer allocator.free(position_lines);
2458
+ const right = 100 + @as(u32, dmg.window_width);
2459
+ // `bounds` includes Finder's title bar; the manifest dimensions describe
2460
+ // the usable icon/background canvas so artwork stays exactly W×H.
2461
+ const bottom = 100 + @as(u32, dmg.window_height) + 36;
2462
+ const inset_right = right - 10;
2463
+ const inset_bottom = bottom - 10;
2464
+ // Capture the window opened from our mount path immediately. Asking
2465
+ // Finder for the container window by disk name can select an older,
2466
+ // read-only copy of the same app DMG that the developer still has mounted.
2467
+ return std.fmt.allocPrint(allocator,
2468
+ \\tell application "Finder"
2469
+ \\ set dmgFolder to POSIX file "{s}" as alias
2470
+ \\ open dmgFolder
2471
+ \\ set dmgWindow to front window
2472
+ \\ delay 1
2473
+ \\ set current view of dmgWindow to icon view
2474
+ \\ set toolbar visible of dmgWindow to false
2475
+ \\ set statusbar visible of dmgWindow to false
2476
+ \\ set pathbar visible of dmgWindow to false
2477
+ \\ set the bounds of dmgWindow to {{100, 100, {d}, {d}}}
2478
+ \\ set theViewOptions to the icon view options of dmgWindow
2479
+ \\ set arrangement of theViewOptions to not arranged
2480
+ \\ set icon size of theViewOptions to {d}
2481
+ \\ set text size of theViewOptions to 13
2482
+ \\ set label position of theViewOptions to bottom
2483
+ \\ set background picture of theViewOptions to (POSIX file "{s}" as alias)
2484
+ \\{s} close dmgWindow
2485
+ \\ open dmgFolder
2486
+ \\ set dmgWindow to front window
2487
+ \\ delay 1
2488
+ \\ set statusbar visible of dmgWindow to false
2489
+ \\ set the bounds of dmgWindow to {{100, 100, {d}, {d}}}
2490
+ \\ delay 1
2491
+ \\ set the bounds of dmgWindow to {{100, 100, {d}, {d}}}
2492
+ \\ update dmgFolder without registering applications
2493
+ \\ delay 1
2494
+ \\ close dmgWindow
2495
+ \\ delay 2
2496
+ \\end tell
2497
+ , .{ escaped_mount, right, bottom, dmg.icon_size, escaped_background_path, position_lines, inset_right, inset_bottom, right, bottom });
2498
+ }
2499
+
2500
+ fn waitForDmgFinderMetadata(allocator: std.mem.Allocator, io: std.Io, mount_path: []const u8) !bool {
2501
+ const ds_store_path = try std.fs.path.join(allocator, &.{ mount_path, ".DS_Store" });
2502
+ defer allocator.free(ds_store_path);
2503
+ const cwd = std.Io.Dir.cwd();
2504
+ for (0..50) |_| {
2505
+ const stat = cwd.statFile(io, ds_store_path, .{}) catch {
2506
+ std.Io.sleep(io, std.Io.Duration.fromMilliseconds(100), .awake) catch return false;
2507
+ continue;
2508
+ };
2509
+ if (stat.size > 0) return true;
2510
+ std.Io.sleep(io, std.Io.Duration.fromMilliseconds(100), .awake) catch return false;
2511
+ }
2512
+ return false;
2513
+ }
2514
+
2515
+ fn dmgFinderPositionLinesAlloc(allocator: std.mem.Allocator, dmg: manifest_tool.DmgMetadata, app_name: []const u8) ![]u8 {
2516
+ var lines: std.ArrayList(u8) = .empty;
2517
+ defer lines.deinit(allocator);
2518
+
2519
+ if (dmg.items.len == 0) {
2520
+ try appendDmgFinderPositionLine(allocator, &lines, app_name, dmg.app_position);
2521
+ if (dmg.applications_link) try appendDmgFinderPositionLine(allocator, &lines, "Applications", dmg.applications_position);
2522
+ return lines.toOwnedSlice(allocator);
2523
+ }
2524
+
2525
+ for (dmg.items) |item| {
2526
+ const name = switch (item.kind) {
2527
+ .app => app_name,
2528
+ .applications => "Applications",
2529
+ .file, .link => manifest_tool.dmgItemDestinationName(item) orelse return error.InvalidDmgItem,
2530
+ };
2531
+ try appendDmgFinderPositionLine(allocator, &lines, name, item.position);
2532
+ }
2533
+ return lines.toOwnedSlice(allocator);
2534
+ }
2535
+
2536
+ fn appendDmgFinderPositionLine(allocator: std.mem.Allocator, lines: *std.ArrayList(u8), name: []const u8, position: manifest_tool.DmgPosition) !void {
2537
+ const escaped_name = try appleScriptStringAlloc(allocator, name);
2538
+ defer allocator.free(escaped_name);
2539
+ const line = try std.fmt.allocPrint(allocator, " set position of item \"{s}\" of dmgFolder to {{{d}, {d}}}\n", .{ escaped_name, position.x, position.y });
2540
+ defer allocator.free(line);
2541
+ try lines.appendSlice(allocator, line);
2542
+ }
2543
+
2544
+ fn appleScriptStringAlloc(allocator: std.mem.Allocator, value: []const u8) ![]u8 {
2545
+ var escaped: std.ArrayList(u8) = .empty;
2546
+ defer escaped.deinit(allocator);
2547
+ for (value) |byte| {
2548
+ switch (byte) {
2549
+ '\\', '"' => {
2550
+ try escaped.append(allocator, '\\');
2551
+ try escaped.append(allocator, byte);
2552
+ },
2553
+ '\n', '\r' => try escaped.append(allocator, ' '),
2554
+ else => try escaped.append(allocator, byte),
2555
+ }
2556
+ }
2557
+ return escaped.toOwnedSlice(allocator);
2558
+ }
2559
+
1977
2560
  fn absolutePathAlloc(allocator: std.mem.Allocator, io: std.Io, path: []const u8) ![]const u8 {
1978
2561
  if (std.fs.path.isAbsolute(path)) return allocator.dupe(u8, path);
1979
2562
  const cwd = try std.process.currentPathAlloc(io, allocator);
@@ -1997,6 +2580,22 @@ fn runArchiveCommand(io: std.Io, argv: []const []const u8, cwd: ?[]const u8) boo
1997
2580
  };
1998
2581
  }
1999
2582
 
2583
+ fn runArchiveCommandQuiet(io: std.Io, argv: []const []const u8, cwd: ?[]const u8) bool {
2584
+ const child_cwd: std.process.Child.Cwd = if (cwd) |path| .{ .path = path } else .inherit;
2585
+ var child = std.process.spawn(io, .{
2586
+ .argv = argv,
2587
+ .cwd = child_cwd,
2588
+ .stdin = .ignore,
2589
+ .stdout = .ignore,
2590
+ .stderr = .ignore,
2591
+ }) catch return false;
2592
+ const term = child.wait(io) catch return false;
2593
+ return switch (term) {
2594
+ .exited => |code| code == 0,
2595
+ else => false,
2596
+ };
2597
+ }
2598
+
2000
2599
  pub fn archivePath(allocator: std.mem.Allocator, options: PackageOptions) ![]const u8 {
2001
2600
  const dir = std.fs.path.dirname(options.output_path) orelse ".";
2002
2601
  return std.fmt.allocPrint(allocator, "{s}/{s}-{s}-{s}-{s}{s}", .{
@@ -2031,6 +2630,270 @@ test "archive path includes correct suffix per platform" {
2031
2630
  try std.testing.expect(std.mem.endsWith(u8, win_path, ".zip"));
2032
2631
  }
2033
2632
 
2633
+ test "default DMG background follows the configured window and draws the install arrow" {
2634
+ const gpa = std.testing.allocator;
2635
+ const dmg: manifest_tool.DmgMetadata = .{
2636
+ .window_width = 420,
2637
+ .window_height = 280,
2638
+ .icon_size = 96,
2639
+ .app_position = .{ .x = 110, .y = 140 },
2640
+ .applications_position = .{ .x = 310, .y = 140 },
2641
+ };
2642
+ const encoded = try defaultDmgBackground(gpa, dmg);
2643
+ defer gpa.free(encoded);
2644
+ const header = app_icon_tool.pngHeader(encoded) orelse return error.TestUnexpectedResult;
2645
+ try std.testing.expectEqual(@as(usize, 420), header.width);
2646
+ try std.testing.expectEqual(@as(usize, 280), header.height);
2647
+
2648
+ const retina = try defaultDmgBackgroundAtScale(gpa, dmg, 2);
2649
+ defer gpa.free(retina);
2650
+ const retina_header = app_icon_tool.pngHeader(retina) orelse return error.TestUnexpectedResult;
2651
+ try std.testing.expectEqual(@as(usize, 840), retina_header.width);
2652
+ try std.testing.expectEqual(@as(usize, 560), retina_header.height);
2653
+
2654
+ const decoded = try app_icon_tool.decodePng(gpa, encoded);
2655
+ defer decoded.deinit(gpa);
2656
+ const arrow = (140 * decoded.width + 210) * 4;
2657
+ const clear = (140 * decoded.width + 24) * 4;
2658
+ try std.testing.expect(decoded.pixels[arrow] < decoded.pixels[clear]);
2659
+ try std.testing.expect(decoded.pixels[arrow + 1] < decoded.pixels[clear + 1]);
2660
+ }
2661
+
2662
+ test "DMG arrow unions translucent joins and antialiases rounded edges" {
2663
+ var pixels: [20 * 20 * 4]u8 = @splat(255);
2664
+ const segments = [_]DmgArrowSegment{
2665
+ .{ .ax = 4, .ay = 10, .bx = 12, .by = 10 },
2666
+ .{ .ax = 12, .ay = 10, .bx = 9, .by = 7 },
2667
+ .{ .ax = 12, .ay = 10, .bx = 9, .by = 13 },
2668
+ };
2669
+ drawDmgArrowShape(&pixels, 20, 20, &segments, 5, .{ 0, 0, 0 }, 0.5);
2670
+
2671
+ const joined = (9 * 20 + 11) * 4;
2672
+ try std.testing.expectEqual(@as(u8, 128), pixels[joined]);
2673
+ try std.testing.expectEqual(pixels[joined], pixels[joined + 1]);
2674
+ try std.testing.expectEqual(pixels[joined], pixels[joined + 2]);
2675
+
2676
+ const antialiased_edge = (7 * 20 + 6) * 4;
2677
+ try std.testing.expect(pixels[antialiased_edge] > pixels[joined]);
2678
+ try std.testing.expect(pixels[antialiased_edge] < 255);
2679
+ }
2680
+
2681
+ test "DMG Finder script carries custom layout and escapes paths and names" {
2682
+ const script = try dmgFinderScriptAlloc(std.testing.allocator, .{
2683
+ .window_width = 720,
2684
+ .window_height = 440,
2685
+ .icon_size = 144,
2686
+ .app_position = .{ .x = 180, .y = 210 },
2687
+ .applications_position = .{ .x = 540, .y = 210 },
2688
+ }, "/Volumes/Demo \"Installer\"", "Demo \"App\".app", "background.jpeg");
2689
+ defer std.testing.allocator.free(script);
2690
+ try std.testing.expect(std.mem.indexOf(u8, script, "POSIX file \"/Volumes/Demo \\\"Installer\\\"\" as alias") != null);
2691
+ try std.testing.expect(std.mem.indexOf(u8, script, "set the bounds of dmgWindow to {100, 100, 820, 576}") != null);
2692
+ try std.testing.expect(std.mem.indexOf(u8, script, "set the bounds of dmgWindow to {100, 100, 810, 566}") != null);
2693
+ try std.testing.expect(std.mem.count(u8, script, "set the bounds of dmgWindow to {100, 100, 820, 576}") == 2);
2694
+ try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, script, "set dmgWindow to front window"));
2695
+ try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, script, "close dmgWindow"));
2696
+ try std.testing.expect(std.mem.indexOf(u8, script, "delay 2") != null);
2697
+ try std.testing.expect(std.mem.indexOf(u8, script, "set pathbar visible of dmgWindow to false") != null);
2698
+ try std.testing.expect(std.mem.indexOf(u8, script, "set icon size of theViewOptions to 144") != null);
2699
+ try std.testing.expect(std.mem.indexOf(u8, script, "/Volumes/Demo \\\"Installer\\\"/.background/background.jpeg") != null);
2700
+ try std.testing.expect(std.mem.indexOf(u8, script, "item \"Demo \\\"App\\\".app\"") != null);
2701
+ try std.testing.expect(std.mem.indexOf(u8, script, "item \"Applications\"") != null);
2702
+ }
2703
+
2704
+ test "DMG Finder script omits the Applications item when its link is disabled" {
2705
+ const script = try dmgFinderScriptAlloc(std.testing.allocator, .{ .applications_link = false }, "Demo", "Demo.app", "background.png");
2706
+ defer std.testing.allocator.free(script);
2707
+ try std.testing.expect(std.mem.indexOf(u8, script, "item \"Applications\"") == null);
2708
+ }
2709
+
2710
+ test "DMG Finder script positions the explicit visible item list" {
2711
+ const items = [_]manifest_tool.DmgItemMetadata{
2712
+ .{ .kind = .app, .position = .{ .x = 150, .y = 170 } },
2713
+ .{ .kind = .applications, .position = .{ .x = 510, .y = 170 } },
2714
+ .{ .kind = .file, .path = "docs/README.pdf", .name = "Read \"Me\".pdf", .position = .{ .x = 260, .y = 330 } },
2715
+ .{ .kind = .link, .path = "/Library/QuickLook", .name = "QuickLook", .position = .{ .x = 400, .y = 330 } },
2716
+ };
2717
+ const script = try dmgFinderScriptAlloc(std.testing.allocator, .{ .items = &items }, "/Volumes/Demo", "Demo.app", "background.tiff");
2718
+ defer std.testing.allocator.free(script);
2719
+ try std.testing.expect(std.mem.indexOf(u8, script, "item \"Demo.app\" of dmgFolder to {150, 170}") != null);
2720
+ try std.testing.expect(std.mem.indexOf(u8, script, "item \"Applications\" of dmgFolder to {510, 170}") != null);
2721
+ try std.testing.expect(std.mem.indexOf(u8, script, "item \"Read \\\"Me\\\".pdf\" of dmgFolder to {260, 330}") != null);
2722
+ try std.testing.expect(std.mem.indexOf(u8, script, "item \"QuickLook\" of dmgFolder to {400, 330}") != null);
2723
+ }
2724
+
2725
+ test "DMG background discovers an adjacent retina source" {
2726
+ var cwd = std.Io.Dir.cwd();
2727
+ const root = ".zig-cache/test-package-dmg-retina";
2728
+ try cwd.deleteTree(std.testing.io, root);
2729
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2730
+ try cwd.createDirPath(std.testing.io, root ++ "/art");
2731
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/art/installer@2x.png", .data = "fixture" });
2732
+
2733
+ const retina = (try dmgRetinaBackgroundSourceAlloc(std.testing.allocator, std.testing.io, root, "art/installer.png")).?;
2734
+ defer std.testing.allocator.free(retina);
2735
+ try std.testing.expectEqualStrings(root ++ "/art/installer@2x.png", retina);
2736
+ const background_name = try dmgBackgroundNameAlloc(std.testing.allocator, "art/installer.png", true);
2737
+ defer std.testing.allocator.free(background_name);
2738
+ try std.testing.expectEqualStrings("background.tiff", background_name);
2739
+ }
2740
+
2741
+ test "assembled DMG background does not retain source images" {
2742
+ if (@import("builtin").os.tag != .macos) return error.SkipZigTest;
2743
+ var cwd = std.Io.Dir.cwd();
2744
+ const root = ".zig-cache/test-package-dmg-background-cleanup";
2745
+ try cwd.deleteTree(std.testing.io, root);
2746
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2747
+ try cwd.createDirPath(std.testing.io, root ++ "/source");
2748
+
2749
+ const metadata: manifest_tool.Metadata = .{
2750
+ .id = "dev.example.demo",
2751
+ .name = "demo",
2752
+ .version = "1.0.0",
2753
+ .dmg = .{
2754
+ .window_width = 320,
2755
+ .window_height = 240,
2756
+ .applications_link = false,
2757
+ },
2758
+ };
2759
+ var source_dir = try cwd.openDir(std.testing.io, root ++ "/source", .{});
2760
+ defer source_dir.close(std.testing.io);
2761
+ try stageDmgBackground(std.testing.allocator, std.testing.io, .{
2762
+ .metadata = metadata,
2763
+ .output_path = root ++ "/Demo.app",
2764
+ }, source_dir, root ++ "/source", "background.tiff", null);
2765
+
2766
+ var background = try cwd.openFile(std.testing.io, root ++ "/source/.background/background.tiff", .{});
2767
+ background.close(std.testing.io);
2768
+ try std.testing.expectError(error.FileNotFound, cwd.openFile(std.testing.io, root ++ "/source/.background/background.png", .{}));
2769
+ try std.testing.expectError(error.FileNotFound, cwd.openFile(std.testing.io, root ++ "/source/.background/background@2x.png", .{}));
2770
+ }
2771
+
2772
+ test "macOS archive rejects an invalid DMG background before staging the app" {
2773
+ var cwd = std.Io.Dir.cwd();
2774
+ const root = ".zig-cache/test-package-dmg-invalid-background";
2775
+ try cwd.deleteTree(std.testing.io, root);
2776
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2777
+ try cwd.createDirPath(std.testing.io, root ++ "/art");
2778
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/art/installer.png", .data = "not a png" });
2779
+
2780
+ try std.testing.expectError(error.InvalidDmgSource, createPackage(std.testing.allocator, std.testing.io, .{
2781
+ .metadata = .{
2782
+ .id = "dev.example.demo",
2783
+ .name = "demo",
2784
+ .display_name = "Demo",
2785
+ .version = "1.0.0",
2786
+ .dmg = .{ .background = "art/installer.png" },
2787
+ },
2788
+ .target = .macos,
2789
+ .output_path = root ++ "/Demo.app",
2790
+ .project_dir = root,
2791
+ .archive = true,
2792
+ }));
2793
+ try std.testing.expectError(error.FileNotFound, cwd.openDir(std.testing.io, root ++ "/Demo.app", .{}));
2794
+ }
2795
+
2796
+ test "macOS archive rejects an app-name collision before staging the app" {
2797
+ var cwd = std.Io.Dir.cwd();
2798
+ const root = ".zig-cache/test-package-dmg-app-name-collision";
2799
+ try cwd.deleteTree(std.testing.io, root);
2800
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2801
+
2802
+ const items = [_]manifest_tool.DmgItemMetadata{
2803
+ .{ .kind = .app, .position = .{ .x = 170, .y = 182 } },
2804
+ .{ .kind = .link, .path = "/Applications", .name = "Demo.app", .position = .{ .x = 490, .y = 182 } },
2805
+ };
2806
+ try std.testing.expectError(error.DuplicateDmgItem, createPackage(std.testing.allocator, std.testing.io, .{
2807
+ .metadata = .{
2808
+ .id = "dev.example.demo",
2809
+ .name = "demo",
2810
+ .display_name = "Demo",
2811
+ .version = "1.0.0",
2812
+ .dmg = .{ .items = &items },
2813
+ },
2814
+ .target = .macos,
2815
+ .output_path = root ++ "/Demo.app",
2816
+ .archive = true,
2817
+ }));
2818
+ try std.testing.expectError(error.FileNotFound, cwd.openDir(std.testing.io, root ++ "/Demo.app", .{}));
2819
+ }
2820
+
2821
+ test "DMG app bundle uses the display name or explicit item override" {
2822
+ const default_name = try dmgAppBundleNameAlloc(std.testing.allocator, .{
2823
+ .id = "dev.example.demo",
2824
+ .name = "demo",
2825
+ .display_name = "Demo/Studio",
2826
+ .version = "1.0.0",
2827
+ });
2828
+ defer std.testing.allocator.free(default_name);
2829
+ try std.testing.expectEqualStrings("Demo-Studio.app", default_name);
2830
+
2831
+ const items = [_]manifest_tool.DmgItemMetadata{
2832
+ .{ .kind = .app, .name = "Branded Installer.app", .position = .{ .x = 170, .y = 182 } },
2833
+ };
2834
+ const overridden = try dmgAppBundleNameAlloc(std.testing.allocator, .{
2835
+ .id = "dev.example.demo",
2836
+ .name = "demo",
2837
+ .display_name = "Demo Studio",
2838
+ .version = "1.0.0",
2839
+ .dmg = .{ .items = &items },
2840
+ });
2841
+ defer std.testing.allocator.free(overridden);
2842
+ try std.testing.expectEqualStrings("Branded Installer.app", overridden);
2843
+ }
2844
+
2845
+ test "DMG explicit items stage the branded app, project contents, and links" {
2846
+ if (@import("builtin").os.tag != .macos) return error.SkipZigTest;
2847
+ var cwd = std.Io.Dir.cwd();
2848
+ const root = ".zig-cache/test-package-dmg-items";
2849
+ try cwd.deleteTree(std.testing.io, root);
2850
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2851
+ try cwd.createDirPath(std.testing.io, root ++ "/Demo.app/Contents");
2852
+ try cwd.createDirPath(std.testing.io, root ++ "/project/Extras");
2853
+ try cwd.createDirPath(std.testing.io, root ++ "/source");
2854
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/Demo.app/Contents/marker", .data = "app" });
2855
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/project/README.pdf", .data = "readme" });
2856
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/project/Extras/note.txt", .data = "bonus" });
2857
+
2858
+ const items = [_]manifest_tool.DmgItemMetadata{
2859
+ .{ .kind = .app, .name = "Branded Demo", .position = .{ .x = 150, .y = 170 } },
2860
+ .{ .kind = .applications, .position = .{ .x = 510, .y = 170 } },
2861
+ .{ .kind = .file, .path = "README.pdf", .name = "Read Me.pdf", .position = .{ .x = 220, .y = 330 } },
2862
+ .{ .kind = .file, .path = "Extras", .name = "Bonus", .position = .{ .x = 330, .y = 330 } },
2863
+ .{ .kind = .link, .path = "/Applications", .name = "System Applications", .position = .{ .x = 440, .y = 330 } },
2864
+ };
2865
+ const metadata: manifest_tool.Metadata = .{
2866
+ .id = "dev.example.demo",
2867
+ .name = "demo",
2868
+ .display_name = "Demo",
2869
+ .version = "1.0.0",
2870
+ .dmg = .{ .items = &items },
2871
+ };
2872
+ try manifest_tool.validateDmgSettings(metadata.dmg);
2873
+ const app_name = try dmgAppBundleNameAlloc(std.testing.allocator, metadata);
2874
+ defer std.testing.allocator.free(app_name);
2875
+ try std.testing.expectEqualStrings("Branded Demo.app", app_name);
2876
+
2877
+ var source_dir = try cwd.openDir(std.testing.io, root ++ "/source", .{});
2878
+ defer source_dir.close(std.testing.io);
2879
+ try stageDmgItems(std.testing.allocator, std.testing.io, .{
2880
+ .metadata = metadata,
2881
+ .output_path = root ++ "/Demo.app",
2882
+ .project_dir = root ++ "/project",
2883
+ }, source_dir, root ++ "/source", root ++ "/Demo.app", app_name);
2884
+
2885
+ var app_marker = try cwd.openFile(std.testing.io, root ++ "/source/Branded Demo.app/Contents/marker", .{});
2886
+ app_marker.close(std.testing.io);
2887
+ var readme = try cwd.openFile(std.testing.io, root ++ "/source/Read Me.pdf", .{});
2888
+ readme.close(std.testing.io);
2889
+ var bonus = try cwd.openFile(std.testing.io, root ++ "/source/Bonus/note.txt", .{});
2890
+ bonus.close(std.testing.io);
2891
+ var applications = try cwd.openDir(std.testing.io, root ++ "/source/Applications", .{});
2892
+ applications.close(std.testing.io);
2893
+ var system_applications = try cwd.openDir(std.testing.io, root ++ "/source/System Applications", .{});
2894
+ system_applications.close(std.testing.io);
2895
+ }
2896
+
2034
2897
  test "archive command reports nonzero exit" {
2035
2898
  if (@import("builtin").os.tag == .windows) return error.SkipZigTest;
2036
2899
  try std.testing.expect(!runArchiveCommand(std.testing.io, &.{ "sh", "-c", "exit 7" }, null));
@@ -2047,6 +2910,7 @@ test "mobile package templates ship the toolkit hosts" {
2047
2910
  try std.testing.expect(std.mem.indexOf(u8, ios_host, "native_sdk_app_text_input_state") != null);
2048
2911
  try std.testing.expect(std.mem.indexOf(u8, ios_host, "native_sdk_app_set_text_measure") != null);
2049
2912
  try std.testing.expect(std.mem.indexOf(u8, ios_host, "native_sdk_app_set_asset_root") != null);
2913
+ try std.testing.expect(std.mem.indexOf(u8, ios_host, "native_sdk_app_set_data_root") != null);
2050
2914
  try std.testing.expect(std.mem.indexOf(u8, ios_host, "native_sdk_app_widget_semantics_by_id") != null);
2051
2915
  try std.testing.expect(std.mem.indexOf(u8, ios_host, "view.safeAreaInsets") != null);
2052
2916
  try std.testing.expect(std.mem.indexOf(u8, ios_host, "_dyld_get_image_header_containing_address") != null);
@@ -2072,6 +2936,7 @@ test "mobile package templates ship the toolkit hosts" {
2072
2936
  try std.testing.expect(std.mem.indexOf(u8, android_bridge, "native_sdk_app_text_input_state") != null);
2073
2937
  try std.testing.expect(std.mem.indexOf(u8, android_bridge, "native_sdk_app_set_text_measure") != null);
2074
2938
  try std.testing.expect(std.mem.indexOf(u8, android_bridge, "native_sdk_app_set_asset_root") != null);
2939
+ try std.testing.expect(std.mem.indexOf(u8, android_bridge, "native_sdk_app_set_data_root") != null);
2075
2940
  try std.testing.expect(std.mem.indexOf(u8, android_bridge, "ANativeWindow_fromSurface") != null);
2076
2941
  try std.testing.expect(std.mem.indexOf(u8, android_bridge, "WINDOW_FORMAT_RGBA_8888") != null);
2077
2942
  }
@@ -2297,6 +3162,30 @@ test "plist template includes identity executable and version" {
2297
3162
  try std.testing.expect(std.mem.indexOf(u8, bare_plist, "NSHumanReadableCopyright") == null);
2298
3163
  }
2299
3164
 
3165
+ test "plist capture usage descriptions follow manifest permissions" {
3166
+ const capture_permissions = [_][]const u8{ "microphone", "system_audio" };
3167
+ const capture: manifest_tool.Metadata = .{
3168
+ .id = "dev.example.recorder",
3169
+ .name = "recorder",
3170
+ .display_name = "Audio & Voice",
3171
+ .version = "1.0.0",
3172
+ .permissions = &capture_permissions,
3173
+ };
3174
+ const plist = try macosInfoPlist(std.testing.allocator, capture, "recorder");
3175
+ defer std.testing.allocator.free(plist);
3176
+ try std.testing.expect(std.mem.indexOf(u8, plist, "NSMicrophoneUsageDescription") != null);
3177
+ try std.testing.expect(std.mem.indexOf(u8, plist, "NSAudioCaptureUsageDescription") != null);
3178
+ try std.testing.expect(std.mem.indexOf(u8, plist, "NSScreenCaptureUsageDescription") != null);
3179
+ try std.testing.expect(std.mem.indexOf(u8, plist, "Audio &amp; Voice captures microphone audio") != null);
3180
+
3181
+ const bare: manifest_tool.Metadata = .{ .id = "dev.example.app", .name = "demo", .version = "1.0.0" };
3182
+ const bare_plist = try macosInfoPlist(std.testing.allocator, bare, "demo");
3183
+ defer std.testing.allocator.free(bare_plist);
3184
+ try std.testing.expect(std.mem.indexOf(u8, bare_plist, "NSMicrophoneUsageDescription") == null);
3185
+ try std.testing.expect(std.mem.indexOf(u8, bare_plist, "NSAudioCaptureUsageDescription") == null);
3186
+ try std.testing.expect(std.mem.indexOf(u8, bare_plist, "NSScreenCaptureUsageDescription") == null);
3187
+ }
3188
+
2300
3189
  test "plist template includes document and URL registrations" {
2301
3190
  const extensions = [_][]const u8{ "md", ".markdown" };
2302
3191
  const mime_types = [_][]const u8{"text/markdown"};
@@ -2469,6 +3358,64 @@ test "macOS app executable is marked executable" {
2469
3358
  try std.testing.expect((permissions.toMode() & 0o111) != 0);
2470
3359
  }
2471
3360
 
3361
+ test "desktop packages place the TypeScript service host beside the app" {
3362
+ var cwd = std.Io.Dir.cwd();
3363
+ const root = ".zig-cache/test-package-service-host";
3364
+ try cwd.deleteTree(std.testing.io, root);
3365
+ defer cwd.deleteTree(std.testing.io, root) catch {};
3366
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
3367
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app", .data = "app" });
3368
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/service", .data = "service" });
3369
+
3370
+ const metadata: manifest_tool.Metadata = .{ .id = "dev.example.service", .name = "service-demo", .version = "1.0.0" };
3371
+ _ = try createPackage(std.testing.allocator, std.testing.io, .{
3372
+ .metadata = metadata,
3373
+ .target = .linux,
3374
+ .output_path = root ++ "/linux",
3375
+ .binary_path = root ++ "/app",
3376
+ .service_binary_path = root ++ "/service",
3377
+ .assets_dir = root ++ "/assets",
3378
+ });
3379
+ var linux_dir = try cwd.openDir(std.testing.io, root ++ "/linux", .{});
3380
+ defer linux_dir.close(std.testing.io);
3381
+ var linux_service = try linux_dir.openFile(std.testing.io, "bin/service-demo_services", .{});
3382
+ linux_service.close(std.testing.io);
3383
+
3384
+ _ = try createMacosApp(std.testing.allocator, std.testing.io, .{
3385
+ .metadata = metadata,
3386
+ .output_path = root ++ "/Service.app",
3387
+ .binary_path = root ++ "/app",
3388
+ .service_binary_path = root ++ "/service",
3389
+ .assets_dir = root ++ "/assets",
3390
+ });
3391
+ var mac_dir = try cwd.openDir(std.testing.io, root ++ "/Service.app", .{});
3392
+ defer mac_dir.close(std.testing.io);
3393
+ var mac_service = try mac_dir.openFile(std.testing.io, "Contents/MacOS/service-demo_services", .{});
3394
+ mac_service.close(std.testing.io);
3395
+ }
3396
+
3397
+ test "normal build service host is discovered only for a service-bearing app" {
3398
+ var cwd = std.Io.Dir.cwd();
3399
+ const root = ".zig-cache/test-package-service-discovery";
3400
+ try cwd.deleteTree(std.testing.io, root);
3401
+ defer cwd.deleteTree(std.testing.io, root) catch {};
3402
+ try cwd.createDirPath(std.testing.io, root ++ "/src/services/nested");
3403
+ try cwd.createDirPath(std.testing.io, root ++ "/zig-out/bin");
3404
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/zig-out/bin/service-demo_services", .data = "service" });
3405
+
3406
+ try std.testing.expect(!try projectHasTypeScriptServices(std.testing.allocator, std.testing.io, root));
3407
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/src/services/types.d.ts", .data = "export interface Ignored {}" });
3408
+ try std.testing.expect(!try projectHasTypeScriptServices(std.testing.allocator, std.testing.io, root));
3409
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/src/services/nested/feeds.ts", .data = "export function parse(): Uint8Array { return new Uint8Array(0); }" });
3410
+ try std.testing.expect(try projectHasTypeScriptServices(std.testing.allocator, std.testing.io, root));
3411
+ const discovered = (try discoverInstalledServiceBinary(std.testing.allocator, std.testing.io, root, "service-demo", .linux)).?;
3412
+ defer std.testing.allocator.free(discovered);
3413
+ try std.testing.expectEqualStrings(root ++ "/zig-out/bin/service-demo_services", discovered);
3414
+
3415
+ try cwd.deleteTree(std.testing.io, root ++ "/src/services");
3416
+ try std.testing.expect(!try projectHasTypeScriptServices(std.testing.allocator, std.testing.io, root));
3417
+ }
3418
+
2472
3419
  test "desktop chromium packages are rejected before CEF layout checks" {
2473
3420
  const metadata: manifest_tool.Metadata = .{
2474
3421
  .id = "dev.demo",