@ng-native/desktop 0.26.0 → 0.27.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 (256) hide show
  1. package/README.md +6 -3
  2. package/bin/app-config.mjs +1 -0
  3. package/bin/archive.mjs +166 -0
  4. package/bin/autolink.mjs +61 -30
  5. package/bin/cli.mjs +127 -27
  6. package/bin/host.mjs +255 -58
  7. package/bin/package-linux.mjs +1394 -0
  8. package/bin/package.mjs +171 -35
  9. package/bin/plist.mjs +6 -2
  10. package/bin/png-icon.mjs +315 -0
  11. package/bin/prebuilt.mjs +147 -26
  12. package/bin/toolchain.mjs +258 -19
  13. package/native/CMakeLists.txt +64 -6
  14. package/native/appkit/CMakeLists.txt +2 -1
  15. package/native/appkit/appkit-backend.h +1 -0
  16. package/native/appkit/appkit-backend.mm +13 -0
  17. package/native/appkit/header-toolbar.mm +28 -25
  18. package/native/appkit/host-view.h +10 -0
  19. package/native/appkit/host-view.mm +146 -40
  20. package/native/appkit/input-replay.h +6 -0
  21. package/native/appkit/input-replay.mm +21 -12
  22. package/native/appkit/main.mm +11 -7
  23. package/native/appkit/native-controls.mm +13 -1
  24. package/native/appkit/split-view.mm +7 -13
  25. package/native/appkit/text-input-field.h +4 -0
  26. package/native/appkit/text-input-field.mm +20 -1
  27. package/native/cmake/curl.cmake +79 -0
  28. package/native/cmake/libraries/react-native-reanimated.cmake +5 -0
  29. package/native/cmake/libraries/react-native-worklets.cmake +5 -0
  30. package/native/cmake/linked-modules.cmake +20 -4
  31. package/native/cmake/react-native.cmake +18 -1
  32. package/native/core/include/ngn/accent-colors.h +38 -0
  33. package/native/core/include/ngn/accessibility-relations.h +65 -0
  34. package/native/core/include/ngn/accessibility.h +26 -0
  35. package/native/core/include/ngn/animated-frame-loop.h +56 -0
  36. package/native/core/include/ngn/app-info.h +24 -8
  37. package/native/core/include/ngn/app-menus.h +14 -1
  38. package/native/core/include/ngn/app-windows.h +15 -2
  39. package/native/core/include/ngn/aria-role.h +96 -0
  40. package/native/core/include/ngn/audio.h +2 -0
  41. package/native/core/include/ngn/camera.h +9 -0
  42. package/native/core/include/ngn/control-sizes.h +7 -2
  43. package/native/core/include/ngn/crash-report.h +31 -0
  44. package/native/core/include/ngn/curl-network.h +93 -0
  45. package/native/core/include/ngn/dbus-menu.h +39 -0
  46. package/native/core/include/ngn/default-menu.h +36 -0
  47. package/native/core/include/ngn/desktop-expo.h +7 -1
  48. package/native/core/include/ngn/desktop-modules.h +22 -0
  49. package/native/core/include/ngn/desktop-props.h +21 -1
  50. package/native/core/include/ngn/desktop-view-props.h +6 -0
  51. package/native/core/include/ngn/focus-order.h +10 -0
  52. package/native/core/include/ngn/header-bar.h +22 -0
  53. package/native/core/include/ngn/host-os.h +6 -0
  54. package/native/core/include/ngn/host-reload.h +21 -0
  55. package/native/core/include/ngn/host-state.h +23 -0
  56. package/native/core/include/ngn/image-source.h +26 -1
  57. package/native/core/include/ngn/input-script.h +3 -3
  58. package/native/core/include/ngn/input.h +4 -1
  59. package/native/core/include/ngn/launch-frame.h +21 -0
  60. package/native/core/include/ngn/launch-settings.h +23 -5
  61. package/native/core/include/ngn/mounting-backend.h +5 -0
  62. package/native/core/include/ngn/mounting-manager.h +1 -0
  63. package/native/core/include/ngn/native-controls.h +26 -5
  64. package/native/core/include/ngn/notification-schedule.h +74 -0
  65. package/native/core/include/ngn/notifications.h +2 -1
  66. package/native/core/include/ngn/openssl-update-crypto.h +15 -0
  67. package/native/core/include/ngn/photo-library.h +60 -20
  68. package/native/core/include/ngn/posix-locale.h +43 -0
  69. package/native/core/include/ngn/runtime.h +5 -0
  70. package/native/core/include/ngn/safe-areas.h +14 -2
  71. package/native/core/include/ngn/split-view.h +78 -0
  72. package/native/core/include/ngn/surface-metrics.h +7 -0
  73. package/native/core/include/ngn/system.h +14 -0
  74. package/native/core/include/ngn/text-input.h +13 -0
  75. package/native/core/include/ngn/text-units.h +38 -0
  76. package/native/core/include/ngn/updates.h +14 -2
  77. package/native/core/include/ngn/view-box.h +40 -0
  78. package/native/core/include/ngn/view-transform.h +15 -0
  79. package/native/core/include/ngn/web-browser.h +46 -0
  80. package/native/core/src/accent-colors.cpp +88 -0
  81. package/native/core/src/accessibility-relations.cpp +138 -0
  82. package/native/core/src/accessibility.cpp +50 -0
  83. package/native/core/src/animated-frame-loop.cpp +166 -0
  84. package/native/core/src/app-menus.cpp +26 -9
  85. package/native/core/src/app-windows.cpp +27 -6
  86. package/native/core/src/aria-role.cpp +127 -0
  87. package/native/core/src/control-sizes.cpp +9 -5
  88. package/native/core/src/crash-report.cpp +196 -0
  89. package/native/core/src/curl-network.cpp +1562 -0
  90. package/native/core/src/dbus-menu.cpp +71 -0
  91. package/native/core/src/default-menu.cpp +134 -0
  92. package/native/core/src/desktop-view-props.cpp +14 -0
  93. package/native/core/src/focus-order.cpp +19 -0
  94. package/native/core/src/header-bar.cpp +20 -0
  95. package/native/core/src/host-os.cpp +19 -0
  96. package/native/core/src/host-reload.cpp +45 -0
  97. package/native/core/src/host-state.cpp +8 -0
  98. package/native/core/src/image-source.cpp +90 -0
  99. package/native/core/src/input-dispatcher.cpp +27 -5
  100. package/native/core/src/input-script.cpp +3 -2
  101. package/native/core/src/launch-settings.cpp +76 -0
  102. package/native/core/src/libraries/react-native-reanimated.cpp +6 -1
  103. package/native/core/src/modules/app-info.cpp +55 -6
  104. package/native/core/src/modules/app-modules.cpp +1 -1
  105. package/native/core/src/modules/audio.cpp +4 -2
  106. package/native/core/src/modules/camera.cpp +8 -11
  107. package/native/core/src/modules/desktop-expo.cpp +10 -5
  108. package/native/core/src/modules/desktop-modules.cpp +106 -41
  109. package/native/core/src/modules/file-photo-picker.cpp +408 -0
  110. package/native/core/src/modules/notifications.cpp +13 -17
  111. package/native/core/src/modules/photo-library.cpp +42 -13
  112. package/native/core/src/modules/web-browser.cpp +89 -0
  113. package/native/core/src/mounting-manager.cpp +12 -0
  114. package/native/core/src/native-controls.cpp +42 -1
  115. package/native/core/src/notification-schedule.cpp +429 -0
  116. package/native/core/src/openssl-update-crypto.cpp +83 -0
  117. package/native/core/src/posix-locale.cpp +192 -0
  118. package/native/core/src/runtime.cpp +30 -1
  119. package/native/core/src/safe-areas.cpp +19 -4
  120. package/native/core/src/spawn-rules.cpp +4 -1
  121. package/native/core/src/split-view.cpp +93 -0
  122. package/native/core/src/system.cpp +19 -0
  123. package/native/core/src/text-input.cpp +18 -1
  124. package/native/core/src/text-units.cpp +99 -0
  125. package/native/core/src/updates.cpp +76 -17
  126. package/native/core/src/view-box.cpp +75 -40
  127. package/native/core/src/view-transform.cpp +28 -0
  128. package/native/docker/Dockerfile +38 -0
  129. package/native/docker/Dockerfile.fedora +36 -0
  130. package/native/docker/glibc-floor.sh +49 -0
  131. package/native/gtk/CMakeLists.txt +137 -0
  132. package/native/gtk/accent-colour.cpp +57 -0
  133. package/native/gtk/accent-colour.h +41 -0
  134. package/native/gtk/accessibility-settings.cpp +159 -0
  135. package/native/gtk/accessibility-settings.h +62 -0
  136. package/native/gtk/accessible-text.cpp +151 -0
  137. package/native/gtk/accessible-text.h +49 -0
  138. package/native/gtk/accessible.cpp +306 -0
  139. package/native/gtk/accessible.h +75 -0
  140. package/native/gtk/alerts.cpp +267 -0
  141. package/native/gtk/alerts.h +30 -0
  142. package/native/gtk/animation-frames.cpp +159 -0
  143. package/native/gtk/animation-frames.h +20 -0
  144. package/native/gtk/app-info.cpp +91 -0
  145. package/native/gtk/app-info.h +25 -0
  146. package/native/gtk/app-windows.cpp +439 -0
  147. package/native/gtk/app-windows.h +150 -0
  148. package/native/gtk/audio.cpp +370 -0
  149. package/native/gtk/audio.h +54 -0
  150. package/native/gtk/auth-session.cpp +73 -0
  151. package/native/gtk/auth-session.h +16 -0
  152. package/native/gtk/battery.cpp +110 -0
  153. package/native/gtk/battery.h +19 -0
  154. package/native/gtk/box-paint.cpp +262 -0
  155. package/native/gtk/box-paint.h +32 -0
  156. package/native/gtk/cairo-canvas.cpp +755 -0
  157. package/native/gtk/cairo-canvas.h +49 -0
  158. package/native/gtk/camera.cpp +909 -0
  159. package/native/gtk/camera.h +69 -0
  160. package/native/gtk/capture.cpp +46 -0
  161. package/native/gtk/capture.h +19 -0
  162. package/native/gtk/clock-format.cpp +151 -0
  163. package/native/gtk/clock-format.h +36 -0
  164. package/native/gtk/crypto.cpp +75 -0
  165. package/native/gtk/crypto.h +14 -0
  166. package/native/gtk/dbus-properties.cpp +131 -0
  167. package/native/gtk/dbus-properties.h +61 -0
  168. package/native/gtk/drag-and-drop.cpp +339 -0
  169. package/native/gtk/drag-and-drop.h +30 -0
  170. package/native/gtk/file-dialogs.cpp +239 -0
  171. package/native/gtk/file-dialogs.h +21 -0
  172. package/native/gtk/file-system.cpp +212 -0
  173. package/native/gtk/file-system.h +25 -0
  174. package/native/gtk/focus.cpp +282 -0
  175. package/native/gtk/focus.h +37 -0
  176. package/native/gtk/fonts.cpp +89 -0
  177. package/native/gtk/fonts.h +22 -0
  178. package/native/gtk/gst-frames.cpp +150 -0
  179. package/native/gtk/gst-frames.h +47 -0
  180. package/native/gtk/gst-playback.cpp +496 -0
  181. package/native/gtk/gst-playback.h +81 -0
  182. package/native/gtk/gtk-backend.cpp +690 -0
  183. package/native/gtk/gtk-backend.h +134 -0
  184. package/native/gtk/host-sources.cpp +354 -0
  185. package/native/gtk/host-sources.h +46 -0
  186. package/native/gtk/image-manipulator.cpp +254 -0
  187. package/native/gtk/image-manipulator.h +30 -0
  188. package/native/gtk/image-view.cpp +256 -0
  189. package/native/gtk/image-view.h +54 -0
  190. package/native/gtk/input-replay.cpp +299 -0
  191. package/native/gtk/input-replay.h +36 -0
  192. package/native/gtk/launcher-badge.cpp +29 -0
  193. package/native/gtk/launcher-badge.h +21 -0
  194. package/native/gtk/locales.cpp +131 -0
  195. package/native/gtk/locales.h +23 -0
  196. package/native/gtk/location.cpp +274 -0
  197. package/native/gtk/location.h +19 -0
  198. package/native/gtk/main.cpp +1092 -0
  199. package/native/gtk/menus.cpp +447 -0
  200. package/native/gtk/menus.h +100 -0
  201. package/native/gtk/native-controls.cpp +831 -0
  202. package/native/gtk/native-controls.h +58 -0
  203. package/native/gtk/network-state.cpp +72 -0
  204. package/native/gtk/network-state.h +24 -0
  205. package/native/gtk/notifications.cpp +235 -0
  206. package/native/gtk/notifications.h +30 -0
  207. package/native/gtk/pango-text.cpp +274 -0
  208. package/native/gtk/pango-text.h +73 -0
  209. package/native/gtk/portal.cpp +451 -0
  210. package/native/gtk/portal.h +71 -0
  211. package/native/gtk/printing.cpp +338 -0
  212. package/native/gtk/printing.h +34 -0
  213. package/native/gtk/proxy-resolver.cpp +74 -0
  214. package/native/gtk/proxy-resolver.h +20 -0
  215. package/native/gtk/screens.cpp +1461 -0
  216. package/native/gtk/screens.h +274 -0
  217. package/native/gtk/scroll-view.cpp +414 -0
  218. package/native/gtk/scroll-view.h +82 -0
  219. package/native/gtk/secret-store.cpp +120 -0
  220. package/native/gtk/secret-store.h +27 -0
  221. package/native/gtk/settings-portal.cpp +129 -0
  222. package/native/gtk/settings-portal.h +47 -0
  223. package/native/gtk/split-view.cpp +1105 -0
  224. package/native/gtk/split-view.h +195 -0
  225. package/native/gtk/status-notifier.cpp +487 -0
  226. package/native/gtk/status-notifier.h +43 -0
  227. package/native/gtk/surface-input.cpp +251 -0
  228. package/native/gtk/surface-input.h +57 -0
  229. package/native/gtk/system-access.cpp +295 -0
  230. package/native/gtk/system-access.h +37 -0
  231. package/native/gtk/text-field.cpp +827 -0
  232. package/native/gtk/text-field.h +78 -0
  233. package/native/gtk/video.cpp +527 -0
  234. package/native/gtk/video.h +47 -0
  235. package/native/gtk/view-widget.cpp +431 -0
  236. package/native/gtk/view-widget.h +105 -0
  237. package/native/gtk/window-scale.cpp +95 -0
  238. package/native/gtk/window-scale.h +35 -0
  239. package/native/scripts/atspi-check.py +310 -0
  240. package/native/scripts/atspi-check.test.py +48 -0
  241. package/native/scripts/build.sh +20 -3
  242. package/native/scripts/compilers.sh +17 -0
  243. package/native/scripts/deb-depends-check.sh +22 -0
  244. package/native/scripts/fetch.sh +23 -2
  245. package/native/scripts/launch-cell.sh +157 -0
  246. package/native/scripts/launch-probe.sh +14 -3
  247. package/native/scripts/make-prebuilt.mjs +133 -44
  248. package/native/scripts/prebuilt-host-cache.mjs +71 -40
  249. package/native/scripts/reset-for-compilers.sh +44 -0
  250. package/native/scripts/start-xvfb.sh +35 -0
  251. package/native/shim/paragraph-platform/react/renderer/components/text/HostPlatformParagraphProps.h +35 -0
  252. package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +5 -2
  253. package/native/shim/worklets/linux-reanimated-props.h +14 -0
  254. package/native/shim/worklets/linux-worklets.h +23 -0
  255. package/package.json +6 -3
  256. package/prebuilt.json +70 -6
package/README.md CHANGED
@@ -24,13 +24,16 @@ the details, and the Nx and Angular CLI generators that do it for you.
24
24
  ```sh
25
25
  npm start # the Expo CLI's dev server, in one terminal
26
26
  npx ng-native-desktop run # builds the host the first time, then opens the app
27
+ npx ng-native-desktop run --os linux # the same on Linux, in GTK
27
28
  npx ng-native-desktop package # dist/macos/<name>.app; --format dmg or mas to ship it
28
29
  ```
29
30
 
30
31
  The first run for a React Native version builds the host from source, which needs Xcode 16 or its
31
- Command Line Tools, CMake and Ninja, unless the package has a prebuilt host for that version and
32
- the app has no native desktop code of its own: `run` downloads the debug host, with React Native's
33
- debugger, and `package` the release one.
32
+ Command Line Tools, CMake and Ninja on a Mac, and clang, lld, CMake, Ninja and the GTK 4 and
33
+ libadwaita development packages on Linux, unless the package has a prebuilt host for that version
34
+ (and on Linux that architecture) and the app has no native desktop code of its own: `run` downloads
35
+ the debug host, with React Native's debugger, and `package` the release one. The command checks the
36
+ toolchain first and names what to install.
34
37
 
35
38
  ## Docs
36
39
 
@@ -18,6 +18,7 @@ export const PLUGIN_NAME = '@ng-native/desktop';
18
18
  *
19
19
  * @typedef {{
20
20
  * name?: string,
21
+ * description?: string,
21
22
  * slug?: string,
22
23
  * version?: string,
23
24
  * scheme?: string | string[],
@@ -0,0 +1,166 @@
1
+ /**
2
+ * The archives Linux packaging writes, in JavaScript so a Mac makes the same bytes as Linux with no
3
+ * tools: a gzipped tar (the `app` format's `.tar.gz`, a `.deb`'s members) and the `ar` archive a
4
+ * `.deb` is. Every entry is owned by root, as a package's files must be when they install, with no
5
+ * extended attributes or macOS metadata, and a fixed time, so the same input makes the same file.
6
+ */
7
+ import { gzipSync } from 'node:zlib';
8
+
9
+ /**
10
+ * @typedef {{ path: string, mode: number, data?: Uint8Array, link?: string }} ArchiveEntry
11
+ * `path` is relative, with `/` between parts, and a folder's ends in `/`; a file has `data`, and a
12
+ * symbolic link the path it points to in `link`
13
+ */
14
+
15
+ const BLOCK = 512;
16
+
17
+ /**
18
+ * Writes `value` as a NUL-terminated octal field of `length` bytes at `offset`.
19
+ *
20
+ * @param {Buffer} header
21
+ * @param {number} offset
22
+ * @param {number} length
23
+ * @param {number} value
24
+ */
25
+ function octal(header, offset, length, value) {
26
+ header.write(`${value.toString(8).padStart(length - 1, '0')}\0`, offset, length, 'ascii');
27
+ }
28
+
29
+ /**
30
+ * A path's ustar `name` and `prefix`, splitting a long one at a `/`. Throws for a path no split fits.
31
+ *
32
+ * @param {string} name
33
+ */
34
+ function ustarName(name) {
35
+ if (Buffer.byteLength(name) <= 100) return { name, prefix: '' };
36
+ for (let at = name.indexOf('/'); at !== -1; at = name.indexOf('/', at + 1)) {
37
+ const prefix = name.slice(0, at);
38
+ const rest = name.slice(at + 1);
39
+ if (Buffer.byteLength(prefix) <= 155 && Buffer.byteLength(rest) <= 100) {
40
+ return { name: rest, prefix };
41
+ }
42
+ }
43
+ throw new Error(`${name} is too long for a tar archive.`);
44
+ }
45
+
46
+ /**
47
+ * An entry's type flag and contents: a folder (`5`) and a link (`2`) have none.
48
+ *
49
+ * @param {ArchiveEntry} entry
50
+ */
51
+ function contentsOf(entry) {
52
+ if (entry.path.endsWith('/')) return { type: '5', data: Buffer.alloc(0) };
53
+ if (entry.link !== undefined) return { type: '2', data: Buffer.alloc(0) };
54
+ return { type: '0', data: Buffer.from(entry.data ?? new Uint8Array()) };
55
+ }
56
+
57
+ /**
58
+ * An entry's ustar header, owned by root:root.
59
+ *
60
+ * @param {ArchiveEntry} entry
61
+ * @param {{ type: string, size: number, time: number }} fields
62
+ */
63
+ function tarHeader(entry, { type, size, time }) {
64
+ const header = Buffer.alloc(BLOCK);
65
+ const { name, prefix } = ustarName(entry.path);
66
+ header.write(name, 0, 100, 'utf8');
67
+ octal(header, 100, 8, entry.mode & 0o7777);
68
+ octal(header, 108, 8, 0);
69
+ octal(header, 116, 8, 0);
70
+ octal(header, 124, 12, size);
71
+ octal(header, 136, 12, time);
72
+ // The checksum is summed with its own field as spaces.
73
+ header.fill(' ', 148, 156);
74
+ header.write(type, 156, 1, 'ascii');
75
+ const link = entry.link ?? '';
76
+ if (Buffer.byteLength(link) > 100) throw new Error(`${link} is too long for a tar link.`);
77
+ header.write(link, 157, 100, 'utf8');
78
+ header.write('ustar\0' + '00', 257, 8, 'ascii');
79
+ header.write('root', 265, 32, 'ascii');
80
+ header.write('root', 297, 32, 'ascii');
81
+ header.write(prefix, 345, 155, 'utf8');
82
+ let sum = 0;
83
+ for (const byte of header) sum += byte;
84
+ header.write(`${sum.toString(8).padStart(6, '0')}\0 `, 148, 8, 'ascii');
85
+ return header;
86
+ }
87
+
88
+ /**
89
+ * A ustar archive of `entries`, in their order, each owned by root:root.
90
+ *
91
+ * @param {readonly ArchiveEntry[]} entries
92
+ * @param {{ time?: number }} [options] the entries' modification time, in seconds
93
+ */
94
+ export function tar(entries, { time = 0 } = {}) {
95
+ /** @type {Buffer[]} */
96
+ const parts = [];
97
+ for (const entry of entries) {
98
+ const { type, data } = contentsOf(entry);
99
+ parts.push(tarHeader(entry, { type, size: data.length, time }), data);
100
+ const pad = (BLOCK - (data.length % BLOCK)) % BLOCK;
101
+ if (pad > 0) parts.push(Buffer.alloc(pad));
102
+ }
103
+ // Two empty blocks end the archive.
104
+ parts.push(Buffer.alloc(BLOCK * 2));
105
+ return Buffer.concat(parts);
106
+ }
107
+
108
+ /**
109
+ * `tar(entries)`, gzipped.
110
+ *
111
+ * @param {readonly ArchiveEntry[]} entries
112
+ */
113
+ export function tarGz(entries) {
114
+ return gzipSync(tar(entries), { level: 9 });
115
+ }
116
+
117
+ /**
118
+ * The folders each entry is in, as entries of their own, before it: a package manager makes a
119
+ * folder only when the archive names it.
120
+ *
121
+ * @param {readonly ArchiveEntry[]} entries
122
+ * @returns {ArchiveEntry[]}
123
+ */
124
+ export function withFolders(entries) {
125
+ const seen = new Set(entries.filter((entry) => entry.path.endsWith('/')).map((e) => e.path));
126
+ /** @type {ArchiveEntry[]} */
127
+ const out = [];
128
+ for (const entry of entries) {
129
+ const parts = entry.path.split('/').filter(Boolean);
130
+ for (let depth = 1; depth < parts.length; depth++) {
131
+ const folder = `${parts.slice(0, depth).join('/')}/`;
132
+ if (!seen.has(folder)) {
133
+ seen.add(folder);
134
+ out.push({ path: folder, mode: 0o755 });
135
+ }
136
+ }
137
+ out.push(entry);
138
+ }
139
+ return out;
140
+ }
141
+
142
+ /**
143
+ * An `ar` archive of `members`, as a `.deb` is: the common format, with names of 16 bytes at most.
144
+ *
145
+ * @param {readonly { name: string, data: Uint8Array }[]} members
146
+ */
147
+ export function ar(members) {
148
+ /** @type {Buffer[]} */
149
+ const parts = [Buffer.from('!<arch>\n', 'ascii')];
150
+ for (const { name, data } of members) {
151
+ if (name.length > 16) throw new Error(`${name} is too long for an ar member's name.`);
152
+ const field = (/** @type {string} */ value, /** @type {number} */ width) =>
153
+ value.padEnd(width, ' ');
154
+ const header =
155
+ field(name, 16) +
156
+ field('0', 12) +
157
+ field('0', 6) +
158
+ field('0', 6) +
159
+ field('100644', 8) +
160
+ field(String(data.length), 10) +
161
+ '`\n';
162
+ parts.push(Buffer.from(header, 'ascii'), Buffer.from(data));
163
+ if (data.length % 2 === 1) parts.push(Buffer.from('\n', 'ascii'));
164
+ }
165
+ return Buffer.concat(parts);
166
+ }
package/bin/autolink.mjs CHANGED
@@ -2,19 +2,25 @@
2
2
  * Autolinking for the desktop build: which native code the host compiles in, found the way Expo's
3
3
  * autolinking finds a library's iOS and Android code.
4
4
  *
5
- * - A library opts in through its `expo-module.config.json`: `macos` among its `platforms`, and a
6
- * `macos` entry naming the CMake target its desktop code builds and the folder that holds its
7
- * CMake fragment (`desktop` unless it says otherwise):
5
+ * Everything below is per desktop OS (`DESKTOP_OSES`), named by the key it is configured under:
6
+ * `macos` or `linux`.
8
7
  *
9
- * { "platforms": ["apple", "android", "macos"],
10
- * "macos": { "cmakeTarget": "haptics_desktop", "cmakeDir": "desktop" } }
8
+ * - A library opts in through its `expo-module.config.json`: the OS among its `platforms`, and an
9
+ * entry of that name naming the CMake target its desktop code builds and the folder that holds
10
+ * its CMake fragment (`desktop` unless it says otherwise). A library that builds for both lists
11
+ * both, and its fragment can be one folder for both or one for each:
11
12
  *
12
- * - The app's own modules are each `modules/<name>/macos/`, beside `ios/` and `android/`, compiled
13
- * in with nothing to register. The module's `expo-module.config.json` may name its CMake target.
13
+ * { "platforms": ["apple", "android", "macos", "linux"],
14
+ * "macos": { "cmakeTarget": "haptics_desktop", "cmakeDir": "desktop" },
15
+ * "linux": { "cmakeTarget": "haptics_desktop", "cmakeDir": "desktop" } }
16
+ *
17
+ * - The app's own modules are each `modules/<name>/<os>/`, beside `ios/` and `android/`, compiled
18
+ * in with nothing to register. The module's `expo-module.config.json` may name its CMake target
19
+ * under the OS's key.
14
20
  * - A package in the plugin options' `deny` list stays out, whatever it ships; the packages it
15
21
  * depends on are still walked.
16
- * - A dependency with native code and none for the desktop is listed, so `run` and `package` can
17
- * say its native modules are absent on macOS.
22
+ * - A dependency with native code and none for the OS is listed, so `run` and `package` can
23
+ * say its native modules are absent there.
18
24
  *
19
25
  * Plain Node with no dependencies, so it runs before anything is built.
20
26
  */
@@ -22,7 +28,18 @@ import { existsSync, readdirSync, readFileSync, realpathSync, statSync } from 'n
22
28
  import path from 'node:path';
23
29
 
24
30
  /**
25
- * Packages whose native side is answered another way on macOS, so they are neither linked nor
31
+ * The desktop OSes autolinking links native code for: the desktop members of `OS_NAMES` in
32
+ * `@ng-native/device`, which a test keeps equal. `macos` is the default wherever an OS is optional.
33
+ *
34
+ * @typedef {'macos' | 'linux'} DesktopOs
35
+ */
36
+ export const DESKTOP_OSES = /** @type {const} */ (['macos', 'linux']);
37
+
38
+ /** How each OS is written in a message. */
39
+ const OS_LABELS = { macos: 'macOS', linux: 'Linux' };
40
+
41
+ /**
42
+ * Packages whose native side is answered another way on a desktop OS, so they are neither linked nor
26
43
  * reported: React Native and React themselves, Expo (`expo` and `expo-*` resolve to empty modules
27
44
  * there, and the host's own modules answer the services that use them) and this framework.
28
45
  *
@@ -44,6 +61,7 @@ function suppliedOtherwise(name) {
44
61
  * @typedef {{ name: string, dir: string, cmakeTarget: string, cmakeDir: string }} DesktopLibrary
45
62
  * @typedef {{ name: string, dir: string, cmakeTarget: string | null }} AppModule
46
63
  * @typedef {{
64
+ * os: DesktopOs,
47
65
  * libraries: DesktopLibrary[],
48
66
  * appModules: AppModule[],
49
67
  * hostLibraries: { name: string, dir: string }[],
@@ -53,20 +71,23 @@ function suppliedOtherwise(name) {
53
71
  */
54
72
 
55
73
  /**
56
- * The desktop code an app's build compiles in, and the dependencies with native code that have
57
- * none. Walks the app's `dependencies` and theirs, as Expo's autolinking does, each package once.
74
+ * The desktop code an app's build compiles in for `os`, and the dependencies with native code that
75
+ * have none. Walks the app's `dependencies` and theirs, as Expo's autolinking does, each package once.
58
76
  *
59
77
  * @param {string} appDir
60
- * @param {{ deny?: readonly string[], builtIn?: readonly string[] }} [options] `builtIn` are the
78
+ * @param {{ os?: DesktopOs, deny?: readonly string[], builtIn?: readonly string[] }} [options]
79
+ * `os`: the OS the build is for (macos unless said); `builtIn` are the
61
80
  * libraries the host compiles in itself, which are neither linked nor reported: `hostLibraries`
62
81
  * says where each one the walk finds is installed, under a dependency's node_modules too
63
82
  * @returns {DesktopLinks}
64
83
  */
65
- export function desktopLinks(appDir, { deny = [], builtIn = [] } = {}) {
84
+ export function desktopLinks(appDir, { os = 'macos', deny = [], builtIn = [] } = {}) {
85
+ assertDesktopOs(os);
66
86
  /** @type {DesktopLinks} */
67
87
  const links = {
88
+ os,
68
89
  libraries: [],
69
- appModules: appModules(appDir),
90
+ appModules: appModules(appDir, os),
70
91
  hostLibraries: [],
71
92
  withoutDesktop: [],
72
93
  problems: [],
@@ -95,66 +116,76 @@ export function desktopLinks(appDir, { deny = [], builtIn = [] } = {}) {
95
116
  return links;
96
117
  }
97
118
 
119
+ /** @param {string} os */
120
+ function assertDesktopOs(os) {
121
+ if (!DESKTOP_OSES.some((desktop) => desktop === os)) {
122
+ throw new Error(`Autolinking is for ${DESKTOP_OSES.join(' and ')}, not '${os}'.`);
123
+ }
124
+ }
125
+
98
126
  /**
99
127
  * @param {DesktopLinks} links
100
128
  * @param {string} name
101
129
  * @param {string} dir
102
130
  */
103
131
  function classify(links, name, dir) {
132
+ const { os } = links;
104
133
  const config = readJson(path.join(dir, 'expo-module.config.json'));
105
- const macos = config?.platforms?.includes('macos') ? config.macos : undefined;
106
- if (macos === undefined) {
134
+ const entry = config?.platforms?.includes(os) ? config[os] : undefined;
135
+ if (entry === undefined) {
107
136
  if (hasNativeCode(dir, config)) links.withoutDesktop.push(name);
108
137
  return;
109
138
  }
110
- if (typeof macos?.cmakeTarget !== 'string' || macos.cmakeTarget === '') {
139
+ if (typeof entry?.cmakeTarget !== 'string' || entry.cmakeTarget === '') {
111
140
  links.problems.push(
112
- `${name} lists macos in expo-module.config.json without a macos.cmakeTarget, so it is left out.`,
141
+ `${name} lists ${os} in expo-module.config.json without a ${os}.cmakeTarget, so it is left out.`,
113
142
  );
114
143
  return;
115
144
  }
116
- const cmakeDir = path.join(dir, typeof macos.cmakeDir === 'string' ? macos.cmakeDir : 'desktop');
145
+ const cmakeDir = path.join(dir, typeof entry.cmakeDir === 'string' ? entry.cmakeDir : 'desktop');
117
146
  if (!existsSync(path.join(cmakeDir, 'CMakeLists.txt'))) {
118
147
  links.problems.push(
119
- `${name} names ${path.relative(dir, cmakeDir)}/CMakeLists.txt for macos, which it does not have, so it is left out.`,
148
+ `${name} names ${path.relative(dir, cmakeDir)}/CMakeLists.txt for ${os}, which it does not have, so it is left out.`,
120
149
  );
121
150
  return;
122
151
  }
123
- links.libraries.push({ name, dir, cmakeTarget: macos.cmakeTarget, cmakeDir });
152
+ links.libraries.push({ name, dir, cmakeTarget: entry.cmakeTarget, cmakeDir });
124
153
  }
125
154
 
126
155
  /**
127
- * The app's own modules: each `modules/<name>/macos/` folder.
156
+ * The app's own modules: each `modules/<name>/<os>/` folder.
128
157
  *
129
158
  * @param {string} appDir
159
+ * @param {DesktopOs} os
130
160
  * @returns {AppModule[]}
131
161
  */
132
- function appModules(appDir) {
162
+ function appModules(appDir, os) {
133
163
  const root = path.join(appDir, 'modules');
134
164
  if (!isDirectory(root)) return [];
135
165
  return readdirSync(root)
136
166
  .sort()
137
- .filter((name) => isDirectory(path.join(root, name, 'macos')))
167
+ .filter((name) => isDirectory(path.join(root, name, os)))
138
168
  .map((name) => {
139
169
  const config = readJson(path.join(root, name, 'expo-module.config.json'));
140
- const target = config?.macos?.cmakeTarget;
170
+ const target = config?.[os]?.cmakeTarget;
141
171
  return {
142
172
  name,
143
- dir: path.join(root, name, 'macos'),
173
+ dir: path.join(root, name, os),
144
174
  cmakeTarget: typeof target === 'string' && target !== '' ? target : null,
145
175
  };
146
176
  });
147
177
  }
148
178
 
149
179
  /**
150
- * Whether a package ships native code for a phone: an Expo module for iOS or Android, a React
180
+ * Whether a package ships native code: an Expo module for iOS, Android or a desktop OS, a React
151
181
  * Native codegen spec, a podspec, or an `ios` or `android` folder.
152
182
  *
153
183
  * @param {string} dir
154
184
  * @param {{ platforms?: string[] } | null} config its expo-module.config.json
155
185
  */
156
186
  function hasNativeCode(dir, config) {
157
- if (config?.platforms?.some((platform) => ['apple', 'ios', 'android'].includes(platform))) {
187
+ const native = ['apple', 'ios', 'android', ...DESKTOP_OSES];
188
+ if (config?.platforms?.some((platform) => native.includes(platform))) {
158
189
  return true;
159
190
  }
160
191
  if (readJson(path.join(dir, 'package.json'))?.codegenConfig) return true;
@@ -216,7 +247,7 @@ export function linkWarnings(links) {
216
247
  if (links.withoutDesktop.length > 0) {
217
248
  lines.push(
218
249
  `No desktop code in ${links.withoutDesktop.join(', ')}: their native modules are absent on ` +
219
- 'macOS. List a package in the deny option of the @ng-native/desktop plugin to keep it out ' +
250
+ `${OS_LABELS[links.os]}. List a package in the deny option of the @ng-native/desktop plugin to keep it out ` +
220
251
  'of this check.',
221
252
  );
222
253
  }
package/bin/cli.mjs CHANGED
@@ -2,19 +2,21 @@
2
2
  * The `ng-native-desktop` command: builds the desktop host when it needs building and launches it,
3
3
  * or packages the app (`package.mjs`).
4
4
  *
5
- * ng-native-desktop run [bundle path or URL] [--module <name>] [--port <port>] [--url <link>] [--universal]
5
+ * ng-native-desktop run [bundle path or URL] [--os macos|linux] [--module <name>] [--port <port>] [--url <link>] [--universal]
6
6
  * ng-native-desktop package [--format app|dmg|mas] [--output <dir>] [--feed-url <url>]
7
7
  *
8
- * The host is built in the per-user cache (see `cacheDir`) from the pinned sources
9
- * `native/scripts/fetch.sh` fetches, against the app's own `react-native`. With no bundle it loads
10
- * the app from the dev server `npm start` runs, by URL, so reload and Fast Refresh work. The app's
11
- * Expo config must list `macos`; the command says how to add it when it does not.
8
+ * The host is the prebuilt one when one fits the app, or is built in the per-user cache (see
9
+ * `cacheDir`) from the pinned sources `native/scripts/fetch.sh` fetches, against the app's own
10
+ * `react-native`: the AppKit host on a Mac, the GTK host on Linux. With no bundle it loads the app
11
+ * from the dev server `npm start` runs, by URL, so reload and Fast Refresh work. The app's Expo
12
+ * config must list the OS; the command says how to add it when it does not.
12
13
  */
13
14
  import path from 'node:path';
14
15
  import { parseArgs } from 'node:util';
15
16
 
16
17
  import { hostCapabilities } from './app-config.mjs';
17
18
  import { prepareHost, system, UsageError } from './host.mjs';
19
+ import { linuxRunAppId } from './package-linux.mjs';
18
20
  import { PACKAGE_USAGE, packageApp, parsePackage } from './package.mjs';
19
21
 
20
22
  export {
@@ -26,46 +28,119 @@ export {
26
28
  UsageError,
27
29
  } from './host.mjs';
28
30
 
29
- export const USAGE = `Usage: ng-native-desktop run [bundle path or URL] [--module <name>] [--port <port>] [--url <link>]
30
- [--universal]
31
+ export const USAGE = `Usage: ng-native-desktop run [bundle path or URL] [--os macos|linux] [--module <name>] [--port <port>]
32
+ [--url <link>] [--universal]
31
33
 
32
- Builds the macOS desktop host if it needs building, then opens the app in it: from the dev server
33
- (start it with npm start) unless a bundle path or URL is given.
34
+ Builds the desktop host if it needs building, then opens the app in it: from the dev server (start
35
+ it with npm start) unless a bundle path or URL is given. The macOS host runs on a Mac, the Linux
36
+ (GTK) host on Linux.
34
37
 
38
+ --os <os> the OS to run on: macos or linux (default: this machine's)
35
39
  --module <name> the registered app to start (default: main)
36
40
  --port <port> the dev server's port (default: 8081)
37
41
  --url <link> a link to open the app on, as Linking's initial URL (myapp://settings)
38
- --universal build for arm64 and x86_64 (default: this Mac's architecture)
42
+ --universal with --os macos, build for arm64 and x86_64 (default: this Mac's architecture)
43
+
44
+ Usage: ng-native-desktop host [--os macos|linux]
45
+
46
+ Gets the debug host run uses, as run gets it, and prints its executable's path. A Linux host comes
47
+ prebuilt on any machine, and builds only on Linux. The in-app test pool's Linux launcher runs it.
39
48
 
40
49
  ${PACKAGE_USAGE}`;
41
50
 
42
51
  /**
43
- * The dev server URL of an app's bundle for macOS. The entry is the one Expo's own clients load,
44
- * which resolves the app's `main` (`/index.bundle` would need an index file at the root).
52
+ * @typedef {import('./autolink.mjs').DesktopOs} DesktopOs
53
+ * @typedef {{ command: 'run', os: DesktopOs, bundle: string | undefined, module: string, port: number, universal: boolean, url?: string }} RunCommand
54
+ * @typedef {{ command: 'host', os: DesktopOs }} HostCommand
55
+ */
56
+
57
+ /**
58
+ * The dev server URL of an app's bundle for a desktop OS. The entry is the one Expo's own clients
59
+ * load, which resolves the app's `main` (`/index.bundle` would need an index file at the root).
45
60
  *
46
61
  * @param {number} port
62
+ * @param {DesktopOs} [os]
47
63
  */
48
- export function devServerBundleUrl(port) {
49
- return `http://localhost:${port}/.expo/.virtual-metro-entry.bundle?platform=macos&dev=true`;
64
+ export function devServerBundleUrl(port, os = 'macos') {
65
+ return `http://localhost:${port}/.expo/.virtual-metro-entry.bundle?platform=${os}&dev=true`;
50
66
  }
51
67
 
52
68
  /**
53
69
  * @param {string[]} argv the arguments after the command name
54
- * @returns {{ command: 'help' } | { command: 'run', bundle: string | undefined, module: string, port: number, universal: boolean, url?: string } | import('./package.mjs').PackageCommand}
70
+ * @param {string} [platform] this machine's, which `run --os` and `package --os` default to
71
+ * @returns {{ command: 'help' } | RunCommand | HostCommand | import('./package.mjs').PackageCommand}
55
72
  */
56
- export function parseCommand(argv) {
73
+ export function parseCommand(argv, platform = process.platform) {
57
74
  const [command, ...rest] = argv;
58
75
  if (command === undefined || command === 'help' || command === '--help' || command === '-h') {
59
76
  return { command: 'help' };
60
77
  }
61
- if (command === 'package') return parsePackage(rest);
78
+ if (command === 'package') return parsePackage(rest, platform);
79
+ if (command === 'host') return parseHost(rest, platform);
62
80
  if (command !== 'run') {
63
81
  throw new UsageError(`Unknown command "${command}".`);
64
82
  }
83
+ return parseRun(rest, platform);
84
+ }
85
+
86
+ /**
87
+ * @param {string[]} args the arguments after `host`
88
+ * @param {string} platform
89
+ * @returns {HostCommand}
90
+ */
91
+ function parseHost(args, platform) {
92
+ const { values } = parseArgs({ args, options: { os: { type: 'string' } } });
93
+ return { command: 'host', os: parseOs(values.os, platform) };
94
+ }
95
+
96
+ /**
97
+ * The OS `--os` names, this machine's when it names none.
98
+ *
99
+ * @param {string | undefined} value
100
+ * @param {string} platform
101
+ * @returns {DesktopOs}
102
+ */
103
+ function parseOs(value, platform) {
104
+ const os = value ?? (platform === 'linux' ? 'linux' : 'macos');
105
+ if (os !== 'macos' && os !== 'linux') {
106
+ throw new UsageError(`--os takes macos or linux, not "${os}".`);
107
+ }
108
+ return os;
109
+ }
110
+
111
+ /**
112
+ * Gets the debug host `run` uses for `os`, as `run` gets it, and returns its executable. Unlike
113
+ * `run`, it takes a prebuilt Linux host off Linux, where a container runs it.
114
+ *
115
+ * @param {{ os: DesktopOs }} options
116
+ * @param {typeof system} [sys]
117
+ */
118
+ export function hostExecutable({ os }, sys = system) {
119
+ const { buildDir } = prepareHost(sys, { os, universal: false });
120
+ return executableIn(buildDir, os);
121
+ }
122
+
123
+ /**
124
+ * @param {string} buildDir
125
+ * @param {DesktopOs} os
126
+ */
127
+ function executableIn(buildDir, os) {
128
+ return os === 'linux'
129
+ ? path.join(buildDir, 'gtk', 'ngn-linux')
130
+ : path.join(buildDir, 'appkit', 'ngn-macos');
131
+ }
132
+
133
+ /**
134
+ * @param {string[]} args the arguments after `run`
135
+ * @param {string} platform
136
+ * @returns {RunCommand}
137
+ */
138
+ function parseRun(args, platform) {
65
139
  const { values, positionals } = parseArgs({
66
- args: rest,
140
+ args,
67
141
  allowPositionals: true,
68
142
  options: {
143
+ os: { type: 'string' },
69
144
  module: { type: 'string', default: 'main' },
70
145
  port: { type: 'string', default: '8081' },
71
146
  universal: { type: 'boolean', default: false },
@@ -75,12 +150,19 @@ export function parseCommand(argv) {
75
150
  if (positionals.length > 1) {
76
151
  throw new UsageError('run takes at most one bundle path or URL.');
77
152
  }
153
+ const os = parseOs(values.os, platform);
154
+ if (os === 'linux' && values.universal) {
155
+ throw new UsageError(
156
+ '--universal is for --os macos only: a Linux host is built for this machine.',
157
+ );
158
+ }
78
159
  const port = Number(values.port);
79
160
  if (!Number.isInteger(port) || port <= 0 || port > 65535) {
80
161
  throw new UsageError(`--port takes a port number, not "${values.port}".`);
81
162
  }
82
163
  return {
83
164
  command: 'run',
165
+ os,
84
166
  bundle: positionals[0],
85
167
  module: values.module,
86
168
  port,
@@ -98,16 +180,28 @@ export function isUrl(bundle) {
98
180
  * Builds the host (a no-op when nothing changed) and runs it on the bundle. Returns the host's
99
181
  * exit status.
100
182
  *
101
- * @param {{ bundle?: string, module: string, port?: number, universal: boolean, url?: string }} options
183
+ * @param {{ os?: DesktopOs, bundle?: string, module: string, port?: number, universal: boolean, url?: string }} options
184
+ * `os` is macos unless it says
102
185
  * @param {typeof system} [sys]
103
186
  */
104
187
  export async function run(options, sys = system) {
105
- const { cwd, expo, config, buildDir } = prepareHost(sys, options);
106
- const appId = config.bundleIdentifier ?? expo.slug ?? config.name;
107
- // What Processes may run and read, as a packaged app's Info.plist gives it.
188
+ const os = options.os ?? 'macos';
189
+ if (os === 'linux' && sys.platform !== 'linux') {
190
+ // Before the host is fetched or built: it would have no display to draw on here.
191
+ throw new UsageError(
192
+ 'The Linux desktop host runs on Linux only for now. Run npm run linux on a Linux machine ' +
193
+ 'or VM; from this machine, npm run package -- --os linux makes the Linux packages.',
194
+ );
195
+ }
196
+ const { cwd, expo, config, buildDir } = prepareHost(sys, { ...options, os });
197
+ const appId =
198
+ os === 'linux'
199
+ ? linuxRunAppId(expo, config)
200
+ : (config.bundleIdentifier ?? expo.slug ?? config.name);
201
+ // What Processes may run and read, as a packaged app's Info.plist or embedded metadata gives it.
108
202
  const capabilities = hostCapabilities(config.options);
109
- return sys.host(path.join(buildDir, 'appkit', 'ngn-macos'), [
110
- bundleArgument(options, cwd),
203
+ return sys.host(executableIn(buildDir, os), [
204
+ bundleArgument(options, os, cwd),
111
205
  '--module',
112
206
  options.module,
113
207
  '--title',
@@ -124,10 +218,11 @@ export async function run(options, sys = system) {
124
218
  * connected for reload and Fast Refresh.
125
219
  *
126
220
  * @param {{ bundle?: string, port?: number }} options
221
+ * @param {DesktopOs} os
127
222
  * @param {string} cwd
128
223
  */
129
- function bundleArgument(options, cwd) {
130
- if (!options.bundle) return devServerBundleUrl(options.port ?? 8081);
224
+ function bundleArgument(options, os, cwd) {
225
+ if (!options.bundle) return devServerBundleUrl(options.port ?? 8081, os);
131
226
  return isUrl(options.bundle) ? options.bundle : path.resolve(cwd, options.bundle);
132
227
  }
133
228
 
@@ -137,12 +232,17 @@ function bundleArgument(options, cwd) {
137
232
  */
138
233
  export async function main(argv, sys = system) {
139
234
  try {
140
- const command = parseCommand(argv);
235
+ const command = parseCommand(argv, sys.platform);
141
236
  if (command.command === 'help') {
142
237
  console.log(USAGE);
143
238
  return 0;
144
239
  }
145
240
  if (command.command === 'package') return await packageApp(command, sys);
241
+ if (command.command === 'host') {
242
+ // Alone on stdout, which the launcher reads: the build's own output goes to stderr.
243
+ console.log(hostExecutable(command, sys));
244
+ return 0;
245
+ }
146
246
  return await run(command, sys);
147
247
  } catch (error) {
148
248
  if (