@blotch/angular-lynx-native 22.3.1 → 22.3.3

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 (123) hide show
  1. package/README.md +47 -15
  2. package/dist/android/project.d.ts +114 -1
  3. package/dist/android/project.d.ts.map +1 -1
  4. package/dist/android/project.js +440 -5
  5. package/dist/eject.d.ts +15 -1
  6. package/dist/eject.d.ts.map +1 -1
  7. package/dist/eject.js +14 -2
  8. package/dist/features.d.ts +70 -0
  9. package/dist/features.d.ts.map +1 -0
  10. package/dist/features.js +115 -0
  11. package/dist/files.d.ts +24 -0
  12. package/dist/files.d.ts.map +1 -0
  13. package/dist/files.js +41 -0
  14. package/dist/generate.d.ts +34 -3
  15. package/dist/generate.d.ts.map +1 -1
  16. package/dist/generate.js +78 -19
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +16 -2
  20. package/dist/ios/build.d.ts.map +1 -1
  21. package/dist/ios/build.js +8 -2
  22. package/dist/ios/frameworks.d.ts +6 -5
  23. package/dist/ios/frameworks.d.ts.map +1 -1
  24. package/dist/ios/frameworks.js +46 -15
  25. package/dist/ios/project.d.ts +192 -11
  26. package/dist/ios/project.d.ts.map +1 -1
  27. package/dist/ios/project.js +823 -33
  28. package/dist/options.d.ts +208 -1
  29. package/dist/options.d.ts.map +1 -1
  30. package/dist/options.js +154 -0
  31. package/dist/permissions.d.ts +18 -1
  32. package/dist/permissions.d.ts.map +1 -1
  33. package/dist/permissions.js +79 -4
  34. package/dist/plugins.d.ts +245 -0
  35. package/dist/plugins.d.ts.map +1 -0
  36. package/dist/plugins.js +1244 -0
  37. package/package.json +1 -1
  38. package/templates/android/README.md +46 -5
  39. package/templates/android/angular-lynx.properties +2 -0
  40. package/templates/android/app/build.gradle.kts +78 -11
  41. package/templates/android/app/src/main/AndroidManifest.xml +19 -3
  42. package/templates/android/app/src/main/java/dev/angularlynx/shell/AngularLynxPlugins.kt +13 -0
  43. package/templates/android/app/src/main/java/dev/angularlynx/shell/AudioRecorderModule.kt +520 -0
  44. package/templates/android/app/src/main/java/dev/angularlynx/shell/BarcodeScannerModule.kt +144 -0
  45. package/templates/android/app/src/main/java/dev/angularlynx/shell/BiometricsModule.kt +433 -0
  46. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapPresenter.kt +98 -0
  47. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapRendererContext.kt +175 -0
  48. package/templates/android/app/src/main/java/dev/angularlynx/shell/BluetoothModule.kt +1288 -0
  49. package/templates/android/app/src/main/java/dev/angularlynx/shell/CalendarModule.kt +127 -0
  50. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DContext.kt +1557 -0
  51. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DOps.kt +63 -0
  52. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasFilter.kt +357 -0
  53. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +4 -3
  54. package/templates/android/app/src/main/java/dev/angularlynx/shell/EdgeToEdgeOverlay.kt +24 -0
  55. package/templates/android/app/src/main/java/dev/angularlynx/shell/EngineThreadHint.kt +156 -0
  56. package/templates/android/app/src/main/java/dev/angularlynx/shell/FilePickerModule.kt +263 -0
  57. package/templates/android/app/src/main/java/dev/angularlynx/shell/GeolocationModule.kt +557 -0
  58. package/templates/android/app/src/main/java/dev/angularlynx/shell/HapticsModule.kt +82 -0
  59. package/templates/android/app/src/main/java/dev/angularlynx/shell/HostElementUI.kt +331 -0
  60. package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePickerModule.kt +143 -0
  61. package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePipeline.kt +83 -0
  62. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +523 -9
  63. package/templates/android/app/src/main/java/dev/angularlynx/shell/ModuleBytes.kt +58 -0
  64. package/templates/android/app/src/main/java/dev/angularlynx/shell/MotionModule.kt +332 -0
  65. package/templates/android/app/src/main/java/dev/angularlynx/shell/NetworkStatus.kt +121 -0
  66. package/templates/android/app/src/main/java/dev/angularlynx/shell/NfcModule.kt +274 -0
  67. package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +35 -0
  68. package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +355 -0
  69. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareTargetModule.kt +234 -0
  70. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +7 -8
  71. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +58 -4
  72. package/templates/android/app/src/main/java/dev/angularlynx/shell/StoreReviewModule.kt +66 -0
  73. package/templates/android/app/src/main/java/dev/angularlynx/shell/SystemFontFamilies.kt +68 -0
  74. package/templates/android/app/src/main/java/dev/angularlynx/shell/TrackingModule.kt +74 -0
  75. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoFrameTarget.kt +429 -0
  76. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoUI.kt +676 -0
  77. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +128 -13
  78. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +493 -65
  79. package/templates/android/app/src/main/java/dev/angularlynx/shell/WifiModule.kt +429 -0
  80. package/templates/android/app/src/main/res/values/biometrics.xml +9 -0
  81. package/templates/android/app/src/main/res/values/share_target.xml +11 -0
  82. package/templates/android/app/src/main/res/xml/backup_rules.xml +5 -3
  83. package/templates/android/app/src/main/res/xml/data_extraction_rules.xml +6 -3
  84. package/templates/android/app/src/main/res/xml/share_paths.xml +7 -2
  85. package/templates/android/build.gradle.kts +1 -1
  86. package/templates/android/gradle/wrapper/gradle-wrapper.properties +2 -2
  87. package/templates/ios/App/AngularLynxBitmapRendererContext.m +188 -0
  88. package/templates/ios/App/AngularLynxCanvas2DContext.m +2467 -0
  89. package/templates/ios/App/AngularLynxCanvas2DOps.h +61 -0
  90. package/templates/ios/App/AngularLynxPlugins.swift +22 -0
  91. package/templates/ios/App/AngularLynxVideo.h +37 -0
  92. package/templates/ios/App/AngularLynxVideo.m +435 -0
  93. package/templates/ios/App/AngularLynxWebGL.h +69 -7
  94. package/templates/ios/App/AngularLynxWebGL.m +538 -29
  95. package/templates/ios/App/AngularLynxWebGLContext.m +148 -46
  96. package/templates/ios/App/AnimationFrameRate.swift +66 -0
  97. package/templates/ios/App/AppDelegate.swift +37 -3
  98. package/templates/ios/App/AppleSignInButton.swift +106 -0
  99. package/templates/ios/App/AppleSignInModule.swift +222 -0
  100. package/templates/ios/App/AudioRecorderModule.swift +433 -0
  101. package/templates/ios/App/BarcodeScannerModule.swift +284 -0
  102. package/templates/ios/App/BiometricsModule.swift +256 -0
  103. package/templates/ios/App/BluetoothModule.swift +1086 -0
  104. package/templates/ios/App/CalendarModule.swift +158 -0
  105. package/templates/ios/App/FilePickerModule.swift +240 -0
  106. package/templates/ios/App/GeolocationModule.swift +455 -0
  107. package/templates/ios/App/HapticsModule.swift +90 -0
  108. package/templates/ios/App/HostElementUI.swift +270 -0
  109. package/templates/ios/App/ImagePickerModule.swift +160 -1
  110. package/templates/ios/App/Info.plist +2 -0
  111. package/templates/ios/App/LynxViewController.swift +274 -9
  112. package/templates/ios/App/MotionModule.swift +430 -0
  113. package/templates/ios/App/NetworkStatus.swift +41 -0
  114. package/templates/ios/App/NfcModule.swift +346 -0
  115. package/templates/ios/App/PhotoLibraryModule.swift +169 -0
  116. package/templates/ios/App/ShareTargetModule.swift +150 -0
  117. package/templates/ios/App/ShellModule.swift +17 -0
  118. package/templates/ios/App/StoreReviewModule.swift +61 -0
  119. package/templates/ios/App/TrackingModule.swift +121 -0
  120. package/templates/ios/App/WifiModule.swift +237 -0
  121. package/templates/ios/App/XElementRegistry.swift +13 -3
  122. package/templates/ios/ShareExtension/Info.plist +42 -0
  123. package/templates/ios/ShareExtension/ShareViewController.swift +387 -0
@@ -1,8 +1,11 @@
1
- import { copyFileSync, readFileSync, writeFileSync } from 'node:fs';
1
+ import { copyFileSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
2
2
  import path from 'node:path';
3
+ import { leaveOutWifi, removeMarkedLines, removeTemplateFiles, } from "../features.js";
4
+ import { ANGLE_IOS_FRAMEWORKS } from "./frameworks.js";
3
5
  import { readPngSize } from "../png.js";
4
6
  import { APP_NAME_PLACEHOLDER, shortcutIdentifier, } from "../options.js";
5
7
  import { PERMISSION_MAP } from "../permissions.js";
8
+ import { checkIosResources, checkSwiftFileNames, checkSwiftTypes, copyPluginSources, iosPluginsSwift, readNativePlugins, } from "../plugins.js";
6
9
  import { IOS_IMAGE_SET, writeIosSplash } from "../splash.js";
7
10
  import { escapeXml, replaceMarker } from "../xml.js";
8
11
  const ORIENTATIONS = {
@@ -21,10 +24,23 @@ const ORIENTATIONS = {
21
24
  * Writes the app options into the iOS template: build settings go in
22
25
  * Config/App.xcconfig (which App.xcodeproj reads), Info.plist entries go
23
26
  * between the template's `angular-lynx:info` markers, the shortcuts
24
- * become App/AppShortcuts.swift, the app link domains become
25
- * Config/App.entitlements, and the icon and the splash go in the asset
26
- * catalog.
27
+ * become App/AppShortcuts.swift, the options that need a capability
28
+ * (IOS_ENTITLEMENTS) become Config/App.entitlements, and the icon and the splash go in the asset
29
+ * catalog. The barcode scanner's module stays only in an app with the
30
+ * `camera` permission, the Bluetooth module only in an app with the
31
+ * `bluetooth` permission, the store review module only in an app with
32
+ * `storeReview`, the calendar module only in one with `calendar`, the
33
+ * Sign in with Apple module only in one with `signInWithApple`, the
34
+ * tracking module only in one with `permissions.tracking`, the NFC module
35
+ * only in one with `permissions.nfc`, the audio recorder's module only in one
36
+ * with `permissions.microphone`, the geolocation module only in one with
37
+ * `permissions.location`, the motion module only in one with `motion`,
38
+ * the Share Extension, its target and
39
+ * its module only in one with `shareTarget`, and the Wi-Fi module only in
40
+ * one with `wifi`, with only the parts it turns on.
27
41
  */
42
+ /** LynxBarcodeScanner's module, which needs the `camera` permission. */
43
+ export const IOS_BARCODE_SCANNER_SOURCE = 'App/BarcodeScannerModule.swift';
28
44
  export const iosGenerator = {
29
45
  configure: (dir, app, projectRoot, development = false) => {
30
46
  writeFileSync(path.join(dir, 'Config/App.xcconfig'), xcconfig(app));
@@ -35,13 +51,41 @@ export const iosGenerator = {
35
51
  if (app.shortcuts.length > 0) {
36
52
  writeFileSync(path.join(dir, 'App/AppShortcuts.swift'), appShortcutsSwift(app));
37
53
  }
54
+ // App Store Connect flags an app whose code uses the camera without
55
+ // NSCameraUsageDescription (ITMS-90683), which only the `camera`
56
+ // permission writes. So an app that doesn't ask for the camera leaves
57
+ // the scanner's module out, and on Android its module and the Play
58
+ // services library behind it (see androidGenerator). LynxViewController
59
+ // registers the module by name only when it's compiled in.
60
+ if (!app.permissions.camera) {
61
+ rmSync(path.join(dir, IOS_BARCODE_SCANNER_SOURCE), { force: true });
62
+ }
63
+ // App Store Connect flags an app whose code uses CoreBluetooth without
64
+ // NSBluetoothAlwaysUsageDescription (ITMS-90683), which only the
65
+ // `bluetooth` permission writes. So an app that doesn't ask for Bluetooth
66
+ // leaves the module out, and ships no Bluetooth code at all, on Android
67
+ // as well (see androidGenerator). LynxViewController registers the module
68
+ // by name only when it's compiled in.
69
+ if (!app.permissions.bluetooth) {
70
+ rmSync(path.join(dir, 'App/BluetoothModule.swift'), { force: true });
71
+ }
38
72
  // In Config/, beside the xcconfig that points the target at it, rather
39
73
  // than in App/: Xcode would copy a file in that synchronized folder into
40
- // the app as a resource. An app without app links signs with no
41
- // entitlements, as the template does.
42
- if (app.appLinkDomains.length > 0) {
74
+ // the app as a resource. An app whose options need no capability signs
75
+ // with no entitlements, as the template does.
76
+ if (hasEntitlements(app)) {
43
77
  writeFileSync(path.join(dir, ENTITLEMENTS), entitlementsPlist(app));
44
78
  }
79
+ // The Share Extension, with its own target, Info.plist and entitlements.
80
+ // The app's half (App Groups, the share scheme and ShareTargetModule) is
81
+ // in the entitlements and Info.plist written above.
82
+ if (app.shareTarget) {
83
+ const projectFile = path.join(dir, PROJECT_FILE);
84
+ writeFileSync(projectFile, withShareExtension(readFileSync(projectFile, 'utf8'), app));
85
+ const extensionPlist = path.join(dir, IOS_SHARE_EXTENSION_DIR, 'Info.plist');
86
+ writeFileSync(extensionPlist, replaceMarker(readFileSync(extensionPlist, 'utf8'), 'share-extension', shareExtensionPlistEntries(app)));
87
+ writeFileSync(path.join(dir, SHARE_EXTENSION_ENTITLEMENTS), shareExtensionEntitlementsPlist(app));
88
+ }
45
89
  if (app.icon) {
46
90
  const icon = path.resolve(projectRoot, app.icon);
47
91
  const size = readPngSize(icon);
@@ -52,10 +96,360 @@ export const iosGenerator = {
52
96
  }
53
97
  writeIosSplash(dir, app, projectRoot);
54
98
  },
99
+ // The plugins' Swift, under App/ like the shell's own: App/ is a
100
+ // synchronized folder, which compiles every source file in it, so the
101
+ // project file needs no change. Their resources come with them, as the
102
+ // folder's: asset catalogs compiled with the shell's, other files copied
103
+ // into the app. The registration replaces the template's, which registers
104
+ // nothing.
105
+ writePlugins: (dir, app, projectRoot) => {
106
+ const plugins = readNativePlugins(app.nativePlugins, projectRoot);
107
+ checkSwiftFileNames(plugins, path.join(dir, 'App'));
108
+ checkSwiftTypes(plugins, path.join(dir, 'App'));
109
+ checkIosResources(plugins, path.join(dir, 'App'));
110
+ copyPluginSources(plugins, 'ios', path.join(dir, IOS_PLUGIN_SOURCES));
111
+ writeFileSync(path.join(dir, 'App/AngularLynxPlugins.swift'), iosPluginsSwift(plugins));
112
+ },
113
+ leaveOut: (dir, features, app) => {
114
+ // Face ID, Touch ID and the Keychain items behind them are only for an
115
+ // app that says why it uses them: without NSFaceIDUsageDescription Face
116
+ // ID fails, and App Review asks about LocalAuthentication code an app
117
+ // gives no reason for. LynxViewController registers the module by name,
118
+ // so the shell builds without it.
119
+ if (!app.permissions.biometrics) {
120
+ removeTemplateFiles(dir, IOS_BIOMETRICS_SOURCES);
121
+ }
122
+ // The document picker is only for an app that turns it on with
123
+ // `filePicker`; LynxViewController registers it by name too.
124
+ if (!app.filePicker) {
125
+ removeTemplateFiles(dir, IOS_FILE_PICKER_SOURCES);
126
+ }
127
+ // An app that doesn't ask for ratings in the app (angular.json's
128
+ // `storeReview`) ships no StoreKit review code it never calls.
129
+ // LynxViewController registers the module by name too.
130
+ if (!app.storeReview) {
131
+ removeTemplateFiles(dir, [IOS_STORE_REVIEW_SOURCE]);
132
+ }
133
+ // Nor does an app that doesn't add events to the calendar (`calendar`)
134
+ // ship EventKit code it never calls.
135
+ if (!app.calendar) {
136
+ removeTemplateFiles(dir, [IOS_CALENDAR_SOURCE]);
137
+ }
138
+ // Sign in with Apple's sheet goes with its entitlement (see
139
+ // IOS_ENTITLEMENTS): an app that doesn't offer it has neither, and
140
+ // LynxViewController registers the module by name.
141
+ if (!app.signInWithApple) {
142
+ removeTemplateFiles(dir, IOS_APPLE_SIGN_IN_SOURCES);
143
+ }
144
+ // App Tracking Transparency's prompt is only for an app that says why it
145
+ // tracks (`permissions.tracking`): iOS ends an app that asks without
146
+ // NSUserTrackingUsageDescription.
147
+ if (!app.permissions.tracking) {
148
+ removeTemplateFiles(dir, [IOS_TRACKING_SOURCE]);
149
+ }
150
+ // An app that isn't a share target has no extension, no App Group and
151
+ // no module reading one; the project has no target for the extension
152
+ // either (configure adds it only with shareTarget).
153
+ if (!app.shareTarget) {
154
+ removeTemplateFiles(dir, [IOS_SHARE_TARGET_SOURCE]);
155
+ rmSync(path.join(dir, IOS_SHARE_EXTENSION_DIR), { recursive: true });
156
+ }
157
+ // Core NFC goes with its usage description and entitlement, which only
158
+ // `permissions.nfc` writes: an app without them would be ended, or
159
+ // refused, as it opened a session, and App Review asks about NFC code an
160
+ // app gives no reason for.
161
+ if (!app.permissions.nfc) {
162
+ removeTemplateFiles(dir, [IOS_NFC_SOURCE]);
163
+ }
164
+ // App Store Connect flags an app whose code uses the microphone without
165
+ // NSMicrophoneUsageDescription (ITMS-90683), which only
166
+ // `permissions.microphone` writes, so the recorder goes with it.
167
+ // LynxViewController registers the module by name.
168
+ if (!app.permissions.microphone) {
169
+ removeTemplateFiles(dir, [IOS_AUDIO_RECORDER_SOURCE]);
170
+ }
171
+ // Core Location goes with its usage description, which only
172
+ // `permissions.location` writes: App Store Connect flags an app whose
173
+ // code uses Core Location without NSLocationWhenInUseUsageDescription
174
+ // (ITMS-90683), and iOS shows no prompt without it.
175
+ if (!app.permissions.location) {
176
+ removeTemplateFiles(dir, [IOS_GEOLOCATION_SOURCE]);
177
+ }
178
+ // Saving to the photo library is only for an app that says why, with
179
+ // its own reason for the prompt. Every app has a reason for the share
180
+ // sheet's Save Image (see infoPlistEntries), but App Review asks about
181
+ // Photos code an app has no use for. Registered by name, as above.
182
+ if (!app.permissions.savePhotos) {
183
+ removeTemplateFiles(dir, IOS_PHOTO_LIBRARY_SOURCES);
184
+ }
185
+ // The Wi-Fi module goes with its entitlements (see IOS_ENTITLEMENTS),
186
+ // each part with its own: an app keeps only the code of the parts it
187
+ // turns on (`// angular-lynx:wifi-read`, `// angular-lynx:wifi-join`).
188
+ // The location code especially, which App Store Connect flags in an app
189
+ // without a location usage description.
190
+ leaveOutWifi(dir, app.wifi, IOS_WIFI_SOURCE);
191
+ // LynxImagePicker's camera goes with NSCameraUsageDescription, which
192
+ // only `permissions.camera` writes: App Store Connect flags an app whose
193
+ // code uses the camera without it (ITMS-90683), and iOS ends one that
194
+ // opens the camera without it. Every other app keeps only the library
195
+ // picker (`// angular-lynx:image-picker-camera`).
196
+ if (!app.permissions.camera) {
197
+ removeMarkedLines(path.join(dir, IOS_IMAGE_PICKER_SOURCE), 'image-picker-camera');
198
+ }
199
+ // An app that doesn't read the motion sensors (angular.json's `motion`)
200
+ // ships no Core Motion or compass code it never calls. LynxViewController
201
+ // registers the module by name, so the shell builds without it.
202
+ if (!app.motion) {
203
+ removeTemplateFiles(dir, [IOS_MOTION_SOURCE]);
204
+ }
205
+ if (features.webgl)
206
+ return;
207
+ if (features.canvas) {
208
+ // A 2D canvas keeps the module and the canvas's view, without the
209
+ // WebGL2 context and the module's lines that make one.
210
+ removeTemplateFiles(dir, IOS_WEBGL_SOURCES);
211
+ for (const file of IOS_WEBGL_MARKED) {
212
+ removeMarkedLines(path.join(dir, file), 'webgl');
213
+ }
214
+ }
215
+ else {
216
+ removeTemplateFiles(dir, IOS_CANVAS_SOURCES);
217
+ }
218
+ // Only the WebGL2 context draws with ANGLE. The canvas's view is still
219
+ // a CAMetalLayer, which is QuartzCore's: a 2D context shows its bitmap
220
+ // in a CALayer over it.
221
+ const projectFile = path.join(dir, PROJECT_FILE);
222
+ writeFileSync(projectFile, withoutFrameworks(readFileSync(projectFile, 'utf8'), ANGLE_IOS_FRAMEWORKS));
223
+ },
55
224
  bundleDir: (dir) => path.join(dir, 'LynxBundle'),
56
225
  };
226
+ /** The module behind `LynxStoreReview`, which only `storeReview` keeps. */
227
+ export const IOS_STORE_REVIEW_SOURCE = 'App/StoreReviewModule.swift';
228
+ /** The module behind `LynxCalendar`, which only `calendar` keeps. */
229
+ export const IOS_CALENDAR_SOURCE = 'App/CalendarModule.swift';
230
+ /**
231
+ * The module behind `LynxAppleSignIn` and the `<apple-sign-in-button>`
232
+ * element, which only `signInWithApple` keeps.
233
+ */
234
+ export const IOS_APPLE_SIGN_IN_SOURCES = [
235
+ 'App/AppleSignInModule.swift',
236
+ 'App/AppleSignInButton.swift',
237
+ ];
238
+ /**
239
+ * The module behind `LynxAudioRecorder`, which only `permissions.microphone`
240
+ * keeps.
241
+ */
242
+ export const IOS_AUDIO_RECORDER_SOURCE = 'App/AudioRecorderModule.swift';
243
+ /** The module behind `LynxNfc`, which only `permissions.nfc` keeps. */
244
+ export const IOS_NFC_SOURCE = 'App/NfcModule.swift';
245
+ /**
246
+ * LynxImagePicker's module, whose camera (`// angular-lynx:image-picker-camera`)
247
+ * only an app with `permissions.camera` keeps.
248
+ */
249
+ export const IOS_IMAGE_PICKER_SOURCE = 'App/ImagePickerModule.swift';
250
+ /** The module behind `LynxWifi`, which only `wifi` keeps. */
251
+ export const IOS_WIFI_SOURCE = 'App/WifiModule.swift';
252
+ /** The module behind `LynxMotion`, which only `motion` keeps. */
253
+ export const IOS_MOTION_SOURCE = 'App/MotionModule.swift';
254
+ /**
255
+ * The module behind `LynxGeolocation`, which only `permissions.location`
256
+ * keeps.
257
+ */
258
+ export const IOS_GEOLOCATION_SOURCE = 'App/GeolocationModule.swift';
259
+ /**
260
+ * The module behind `LynxTracking`, which only an app with
261
+ * `permissions.tracking` keeps.
262
+ */
263
+ export const IOS_TRACKING_SOURCE = 'App/TrackingModule.swift';
264
+ /**
265
+ * The shell's biometrics module, `AngularLynxBiometrics` (LynxBiometrics, and
266
+ * the values LynxSecureStorage keeps behind Face ID or Touch ID), which only
267
+ * an app with `permissions.biometrics` keeps.
268
+ */
269
+ export const IOS_BIOMETRICS_SOURCES = ['App/BiometricsModule.swift'];
270
+ /**
271
+ * The shell's file picker module, `AngularLynxFilePicker` (LynxFilePicker),
272
+ * which only an app with `filePicker` keeps.
273
+ */
274
+ export const IOS_FILE_PICKER_SOURCES = ['App/FilePickerModule.swift'];
275
+ /** Where the plugins' Swift goes, a folder per plugin. */
276
+ export const IOS_PLUGIN_SOURCES = 'App/Plugins';
277
+ /**
278
+ * The shell's photo library module, `AngularLynxPhotoLibrary`
279
+ * (LynxPhotoLibrary), which only an app with `permissions.savePhotos` keeps.
280
+ */
281
+ export const IOS_PHOTO_LIBRARY_SOURCES = [
282
+ 'App/PhotoLibraryModule.swift',
283
+ ];
284
+ /**
285
+ * The shell's `<canvas>`, its `AngularLynxWebGL` module and its contexts, and
286
+ * `<video>`'s readable frames: about 176 kB of the executable. Swift reaches
287
+ * them only by name (NSClassFromString in LynxViewController and
288
+ * XElementRegistry), so without them the shell registers no canvas, keeps
289
+ * XElement's own `<video>`, and still builds: App/ is a synchronized folder,
290
+ * which compiles what's in it.
291
+ */
292
+ export const IOS_CANVAS_SOURCES = [
293
+ 'App/AngularLynxBitmapRendererContext.m',
294
+ 'App/AngularLynxCanvas2DContext.m',
295
+ 'App/AngularLynxCanvas2DOps.h',
296
+ 'App/AngularLynxVideo.h',
297
+ 'App/AngularLynxVideo.m',
298
+ 'App/AngularLynxWebGL.h',
299
+ 'App/AngularLynxWebGL.m',
300
+ 'App/AngularLynxWebGLContext.m',
301
+ 'App/AngularLynxWebGLOps.h',
302
+ ];
303
+ /**
304
+ * The WebGL2 context, which an app that draws only in 2D leaves out with
305
+ * ANGLE (issue #847).
306
+ */
307
+ export const IOS_WEBGL_SOURCES = [
308
+ 'App/AngularLynxWebGLContext.m',
309
+ 'App/AngularLynxWebGLOps.h',
310
+ ];
311
+ /**
312
+ * The files whose lines between `// angular-lynx:webgl` markers name the
313
+ * WebGL2 context, and go with it: the class's declaration, and the module's
314
+ * methods that make and run one. Nothing else names it.
315
+ */
316
+ const IOS_WEBGL_MARKED = [
317
+ 'App/AngularLynxWebGL.h',
318
+ 'App/AngularLynxWebGL.m',
319
+ ];
320
+ const PROJECT_FILE = 'App.xcodeproj/project.pbxproj';
321
+ /**
322
+ * The project without the XCFrameworks named, which it then neither links
323
+ * nor embeds: ANGLE's, about 6 MB of an app, are only for WebGL. Each
324
+ * reference to a framework is one line of the project (its file, its build
325
+ * files, and their places in the group, the link phase and the embed phase).
326
+ */
327
+ export const withoutFrameworks = (project, frameworks) => {
328
+ const names = frameworks.map((name) => `${name}.xcframework`);
329
+ // A renamed framework would otherwise stay in every app unnoticed.
330
+ for (const name of names) {
331
+ if (!project.includes(name)) {
332
+ throw new Error(`The project has no ${name} to leave out.`);
333
+ }
334
+ }
335
+ return project
336
+ .split('\n')
337
+ .filter((line) => !names.some((name) => line.includes(name)))
338
+ .join('\n');
339
+ };
57
340
  /** The app's entitlements, from the project folder (see xcconfig). */
58
341
  const ENTITLEMENTS = 'Config/App.entitlements';
342
+ /**
343
+ * Each option that needs an iOS capability, and the entitlements it adds to
344
+ * Config/App.entitlements. A feature that needs one (Wi-Fi, in-app
345
+ * purchases, push...) adds its entry here, rather than writing the file: the generator
346
+ * writes the file, and points the target at it, only when an entry adds
347
+ * something, and {@link mergeEntitlements} joins two options' lists under
348
+ * the same key, as Associated Domains holds both `applinks:` and
349
+ * `webcredentials:` domains.
350
+ */
351
+ export const IOS_ENTITLEMENTS = [
352
+ {
353
+ // Universal Links: iOS opens the app for an https link to one of the
354
+ // domains, once the domain's apple-app-site-association names the app
355
+ // (see appLinkFiles). iOS fetches that file through Apple's CDN when the
356
+ // app is installed.
357
+ option: 'appLinkDomains',
358
+ capability: 'Associated Domains',
359
+ entitlements: (app) => app.appLinkDomains.length === 0
360
+ ? {}
361
+ : {
362
+ 'com.apple.developer.associated-domains': app.appLinkDomains.map((domain) => `applinks:${domain}`),
363
+ },
364
+ },
365
+ {
366
+ // ASAuthorizationAppleIDProvider's sheet fails without it
367
+ // (ASAuthorizationError 1000). "Default" is the only value Apple
368
+ // defines: the app is its own primary App ID for the sign-in.
369
+ option: 'signInWithApple',
370
+ capability: 'Sign In with Apple',
371
+ entitlements: (app) => app.signInWithApple
372
+ ? { 'com.apple.developer.applesignin': ['Default'] }
373
+ : {},
374
+ },
375
+ {
376
+ // The container the Share Extension leaves each share in for the app
377
+ // (ShareTargetModule). The extension has the same group in its own
378
+ // entitlements (shareExtensionEntitlementsPlist).
379
+ option: 'shareTarget',
380
+ capability: 'App Groups',
381
+ entitlements: (app) => app.shareTarget
382
+ ? { 'com.apple.security.application-groups': [shareGroup(app)] }
383
+ : {},
384
+ },
385
+ {
386
+ // Core NFC's NFCNDEFReaderSession, behind LynxNfc. TAG is the only
387
+ // format an app for iOS 13 and later may declare: it covers NDEF
388
+ // sessions too, and Xcode rejects the older NDEF value.
389
+ option: 'permissions.nfc',
390
+ capability: 'NFC Tag Reading',
391
+ entitlements: (app) => app.permissions.nfc
392
+ ? { 'com.apple.developer.nfc.readersession.formats': ['TAG'] }
393
+ : {},
394
+ },
395
+ {
396
+ // NEHotspotConfigurationManager's apply fails without it, behind
397
+ // LynxWifi.join().
398
+ option: 'wifi.join',
399
+ capability: 'Hotspot Configuration',
400
+ entitlements: (app) => app.wifi.join
401
+ ? { 'com.apple.developer.networking.HotspotConfiguration': true }
402
+ : {},
403
+ },
404
+ {
405
+ // NEHotspotNetwork.fetchCurrent answers nil without it, behind
406
+ // LynxWifi.currentNetwork().
407
+ option: 'wifi.readCurrentNetwork',
408
+ capability: 'Access WiFi Information',
409
+ entitlements: (app) => app.wifi.readCurrentNetwork
410
+ ? { 'com.apple.developer.networking.wifi-info': true }
411
+ : {},
412
+ },
413
+ ];
414
+ /**
415
+ * Joins the entitlements several options add into one dictionary, its keys
416
+ * sorted, as Xcode writes them. Two lists under one key become one, each
417
+ * value once, in the order the options name them. Two different flags or
418
+ * strings under one key can't both be signed, so they throw, naming the
419
+ * options.
420
+ */
421
+ export const mergeEntitlements = (contributions) => {
422
+ const merged = new Map();
423
+ for (const { option, entitlements } of contributions) {
424
+ for (const [key, value] of Object.entries(entitlements)) {
425
+ const seen = merged.get(key);
426
+ if (!seen) {
427
+ merged.set(key, {
428
+ value: Array.isArray(value) ? [...new Set(value)] : value,
429
+ from: option,
430
+ });
431
+ }
432
+ else if (Array.isArray(seen.value) && Array.isArray(value)) {
433
+ seen.value = [...new Set([...seen.value, ...value])];
434
+ seen.from = `${seen.from}, ${option}`;
435
+ }
436
+ else if (seen.value !== value) {
437
+ throw new Error(`${seen.from} and ${option} set the ${key} entitlement to different values (${JSON.stringify(seen.value)}, ${JSON.stringify(value)}).`);
438
+ }
439
+ }
440
+ }
441
+ return Object.fromEntries([...merged.keys()].sort().map((key) => [key, merged.get(key).value]));
442
+ };
443
+ /**
444
+ * The options that add entitlements to `app`, in IOS_ENTITLEMENTS' order.
445
+ */
446
+ const entitlementContributions = (app) => IOS_ENTITLEMENTS.map(({ option, entitlements }) => ({
447
+ option,
448
+ entitlements: entitlements(app),
449
+ })).filter(({ entitlements }) => Object.keys(entitlements).length > 0);
450
+ /** The app's entitlements, from every option that adds some. */
451
+ export const iosEntitlements = (app) => mergeEntitlements(entitlementContributions(app));
452
+ const hasEntitlements = (app) => entitlementContributions(app).length > 0;
59
453
  /**
60
454
  * xcconfig values can't be quoted; `$` and `//` would start a variable or a comment.
61
455
  */
@@ -70,37 +464,54 @@ export const xcconfig = (app) => [
70
464
  `MARKETING_VERSION = ${app.version}`,
71
465
  `CURRENT_PROJECT_VERSION = ${app.buildNumber}`,
72
466
  `DEVELOPMENT_TEAM = ${app.ios.teamId ?? ''}`,
73
- // Only with app links: the only entitlement the generator writes.
74
- ...(app.appLinkDomains.length > 0
467
+ // Only when an option needs a capability (IOS_ENTITLEMENTS): an
468
+ // entitlement nobody asked for could keep a free Personal Team from
469
+ // signing the app.
470
+ ...(hasEntitlements(app)
75
471
  ? [`CODE_SIGN_ENTITLEMENTS = ${ENTITLEMENTS}`]
76
472
  : []),
77
473
  '',
78
474
  ].join('\n');
79
475
  /**
80
- * Config/App.entitlements: the Associated Domains entitlement, which has iOS
81
- * open the app for an https link to one of the domains (a Universal Link),
82
- * once the domain's apple-app-site-association names the app (see
83
- * appLinkFiles). iOS fetches that file through Apple's CDN when the app is
84
- * installed.
476
+ * Config/App.entitlements: the capabilities the app's options ask for
477
+ * ({@link IOS_ENTITLEMENTS}), merged.
85
478
  *
86
- * Signing it needs a team in the paid Apple Developer Program: a free
87
- * Personal Team can't sign Associated Domains. The simulator app needs no
88
- * team.
479
+ * Signing most of them needs a team in the paid Apple Developer Program: a
480
+ * free Personal Team can't sign Associated Domains, Sign in with Apple, NFC
481
+ * Tag Reading or App Groups (explainSigningFailure in build.ts names the
482
+ * option). The simulator app
483
+ * needs no team.
89
484
  */
90
- export const entitlementsPlist = (app) => [
91
- '<?xml version="1.0" encoding="UTF-8"?>',
92
- '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
93
- '<!-- Written by @blotch/angular-lynx-native from appLinkDomains in angular.json. -->',
94
- '<plist version="1.0">',
95
- '<dict>',
96
- '\t<key>com.apple.developer.associated-domains</key>',
97
- '\t<array>',
98
- ...app.appLinkDomains.map((domain) => `\t\t<string>applinks:${escapeXml(domain)}</string>`),
99
- '\t</array>',
100
- '</dict>',
101
- '</plist>',
102
- '',
103
- ].join('\n');
485
+ export const entitlementsPlist = (app) => {
486
+ const options = entitlementContributions(app).map(({ option }) => option);
487
+ const value = (entitlement) => {
488
+ if (typeof entitlement === 'boolean') {
489
+ return [entitlement ? '\t<true/>' : '\t<false/>'];
490
+ }
491
+ if (typeof entitlement === 'string') {
492
+ return [`\t<string>${escapeXml(entitlement)}</string>`];
493
+ }
494
+ return [
495
+ '\t<array>',
496
+ ...entitlement.map((item) => `\t\t<string>${escapeXml(item)}</string>`),
497
+ '\t</array>',
498
+ ];
499
+ };
500
+ return [
501
+ '<?xml version="1.0" encoding="UTF-8"?>',
502
+ '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
503
+ `<!-- Written by @blotch/angular-lynx-native from ${options.join(', ')} in angular.json. -->`,
504
+ '<plist version="1.0">',
505
+ '<dict>',
506
+ ...Object.entries(iosEntitlements(app)).flatMap(([key, entitlement]) => [
507
+ `\t<key>${escapeXml(key)}</key>`,
508
+ ...value(entitlement),
509
+ ]),
510
+ '</dict>',
511
+ '</plist>',
512
+ '',
513
+ ].join('\n');
514
+ };
104
515
  /** What iOS shows the first time someone taps Save Image, without `savePhotos`. */
105
516
  export const DEFAULT_SAVE_PHOTOS_REASON = 'Saves the images you choose to your photo library.';
106
517
  /**
@@ -126,6 +537,12 @@ export const infoPlistEntries = (app, { development = false } = {}) => {
126
537
  // Read by LynxViewController, which lays the LynxView out over the whole
127
538
  // screen or inside the safe area.
128
539
  lines.push('<key>AngularLynxEdgeToEdge</key>', app.edgeToEdge ? '<true/>' : '<false/>');
540
+ // Read by LynxViewController, which sets the LynxView's font scale from
541
+ // Dynamic Type, capped at the max, or keeps it at 1.
542
+ lines.push('<key>AngularLynxTextScaling</key>', app.textScaling.enabled ? '<true/>' : '<false/>');
543
+ if (app.textScaling.max !== undefined) {
544
+ lines.push('<key>AngularLynxMaxTextScale</key>', `<real>${app.textScaling.max}</real>`);
545
+ }
129
546
  // The share sheet LynxShare opens has Save Image for an image, which adds
130
547
  // it to the photo library, and iOS ends an app that does so without a
131
548
  // reason in Info.plist. So every app has one, which `savePhotos` changes.
@@ -150,11 +567,27 @@ export const infoPlistEntries = (app, { development = false } = {}) => {
150
567
  if (development && !app.permissions.localNetwork) {
151
568
  lines.push('<key>NSLocalNetworkUsageDescription</key>', `<string>${escapeXml(LOCAL_NETWORK_REASON)}</string>`);
152
569
  }
570
+ // The app's Documents folder in the Files app, under On My iPhone, where
571
+ // LynxFilePicker's save() can put a file and pick() find it again. iOS
572
+ // lists the folder only with both keys.
573
+ if (app.fileSharing) {
574
+ lines.push('<key>UIFileSharingEnabled</key>', '<true/>', '<key>LSSupportsOpeningDocumentsInPlace</key>', '<true/>');
575
+ }
153
576
  // The schemes LinkingModule's canOpenURL can ask about: iOS answers false
154
577
  // for any other. UIApplication.open needs none of them.
155
578
  array('LSApplicationQueriesSchemes', app.canOpenURLSchemes);
156
- if (app.deepLinkSchemes.length > 0) {
157
- lines.push('<key>CFBundleURLTypes</key>', '<array>', '\t<dict>', '\t\t<key>CFBundleURLName</key>', `\t\t<string>${escapeXml(app.id)}</string>`, '\t\t<key>CFBundleURLSchemes</key>', '\t\t<array>', ...app.deepLinkSchemes.map((scheme) => `\t\t\t<string>${escapeXml(scheme)}</string>`), '\t\t</array>', '\t</dict>', '</array>');
579
+ // The Share Extension opens the app with a scheme of its own, which
580
+ // SceneDelegate tells from a deep link, and leaves the share in the App
581
+ // Group ShareTargetModule reads.
582
+ const schemes = [
583
+ ...app.deepLinkSchemes,
584
+ ...(app.shareTarget ? [shareScheme(app)] : []),
585
+ ];
586
+ if (app.shareTarget) {
587
+ lines.push('<key>AngularLynxShareGroup</key>', `<string>${escapeXml(shareGroup(app))}</string>`, '<key>AngularLynxShareScheme</key>', `<string>${escapeXml(shareScheme(app))}</string>`);
588
+ }
589
+ if (schemes.length > 0) {
590
+ lines.push('<key>CFBundleURLTypes</key>', '<array>', '\t<dict>', '\t\t<key>CFBundleURLName</key>', `\t\t<string>${escapeXml(app.id)}</string>`, '\t\t<key>CFBundleURLSchemes</key>', '\t\t<array>', ...schemes.map((scheme) => `\t\t\t<string>${escapeXml(scheme)}</string>`), '\t\t</array>', '\t</dict>', '</array>');
158
591
  }
159
592
  return lines.join('\n');
160
593
  };
@@ -238,3 +671,360 @@ export const appShortcutsSwift = (app) => {
238
671
  '',
239
672
  ].join('\n');
240
673
  };
674
+ // ─── shareTarget: the Share Extension ────────────────────────────────────────
675
+ /** LynxShareTarget's module in the app, which only `shareTarget` keeps. */
676
+ export const IOS_SHARE_TARGET_SOURCE = 'App/ShareTargetModule.swift';
677
+ /**
678
+ * The Share Extension's folder: the extension's code and its Info.plist,
679
+ * which only `shareTarget` keeps, with the target the generator adds to the
680
+ * project for it ({@link withShareExtension}).
681
+ */
682
+ export const IOS_SHARE_EXTENSION_DIR = 'ShareExtension';
683
+ /** The extension's entitlements, beside the app's (see ENTITLEMENTS). */
684
+ const SHARE_EXTENSION_ENTITLEMENTS = 'Config/ShareExtension.entitlements';
685
+ /**
686
+ * The App Group the app and its Share Extension share, where the extension
687
+ * leaves each share for the app: `group.` and the app's bundle ID, as Xcode
688
+ * names a new group.
689
+ */
690
+ export const shareGroup = (app) => `group.${app.id}`;
691
+ /**
692
+ * The URL scheme the Share Extension opens the app with once it has left a
693
+ * share: one of the app's own, from its bundle ID, so no other app answers
694
+ * to it. SceneDelegate takes it for the share, never for a deep link.
695
+ */
696
+ export const shareScheme = (app) => `angularlynx-share.${app.id.toLowerCase().replace(/[^a-z0-9.-]/g, '-')}`;
697
+ /**
698
+ * The activation rule's keys for `mimeTypes`: which kinds of things in a
699
+ * share sheet show the app. Apple's rule can't name a MIME type, only kinds,
700
+ * so a type that isn't text, a link, an image or a video allows files, and
701
+ * the extension then checks each file's own type against the list.
702
+ */
703
+ export const shareActivationRule = (mimeTypes) => {
704
+ const takes = (kind) => mimeTypes.some((type) => type === '*/*' || type === `${kind}/*` || type.startsWith(`${kind}/`));
705
+ // Version 2 shows the app when the share holds one thing it takes. The
706
+ // first version asks that it take everything shared, and Safari shares a
707
+ // page as both a link and text, so a text-only app wasn't listed there.
708
+ const rule = {
709
+ NSExtensionActivationDictionaryVersion: 2,
710
+ };
711
+ // The most of each kind iOS offers the app for. It only decides whether
712
+ // the app shows in the share sheet: the extension's 20 MiB of files per
713
+ // share is what limits a share, so this is high enough that a selection
714
+ // of small photos that fits is never refused for its count alone.
715
+ const most = 100;
716
+ if (mimeTypes.some((type) => type === '*/*' || type === 'text/*' || type === 'text/plain')) {
717
+ rule['NSExtensionActivationSupportsText'] = true;
718
+ }
719
+ // Links for text/plain too, as Android shares them: iOS doesn't count a
720
+ // page's link as text, and the extension hands a link to an app that
721
+ // takes only text as its text.
722
+ if (mimeTypes.some((type) => type === '*/*' ||
723
+ type === 'text/*' ||
724
+ type === 'text/uri-list' ||
725
+ type === 'text/plain')) {
726
+ rule['NSExtensionActivationSupportsWebURLWithMaxCount'] = 1;
727
+ rule['NSExtensionActivationSupportsWebPageWithMaxCount'] = 1;
728
+ }
729
+ if (takes('image')) {
730
+ rule['NSExtensionActivationSupportsImageWithMaxCount'] = most;
731
+ }
732
+ if (takes('video')) {
733
+ rule['NSExtensionActivationSupportsMovieWithMaxCount'] = most;
734
+ }
735
+ if (mimeTypes.some((type) => !type.startsWith('image/') &&
736
+ !type.startsWith('video/') &&
737
+ type !== 'text/plain' &&
738
+ type !== 'text/uri-list')) {
739
+ rule['NSExtensionActivationSupportsFileWithMaxCount'] = most;
740
+ }
741
+ return Object.fromEntries(Object.keys(rule)
742
+ .sort()
743
+ .map((key) => [key, rule[key]]));
744
+ };
745
+ /**
746
+ * The Share Extension's Info.plist entries, between its template's
747
+ * `angular-lynx:share-extension` markers: the extension point, the
748
+ * activation rule for `mimeTypes`, and what the extension reads (the types,
749
+ * the App Group and the app's share scheme).
750
+ */
751
+ export const shareExtensionPlistEntries = (app) => {
752
+ const { mimeTypes } = app.shareTarget;
753
+ return [
754
+ '<key>AngularLynxShareGroup</key>',
755
+ `<string>${escapeXml(shareGroup(app))}</string>`,
756
+ '<key>AngularLynxShareScheme</key>',
757
+ `<string>${escapeXml(shareScheme(app))}</string>`,
758
+ '<key>AngularLynxShareMimeTypes</key>',
759
+ '<array>',
760
+ ...mimeTypes.map((type) => `\t<string>${escapeXml(type)}</string>`),
761
+ '</array>',
762
+ '<key>NSExtension</key>',
763
+ '<dict>',
764
+ '\t<key>NSExtensionAttributes</key>',
765
+ '\t<dict>',
766
+ '\t\t<key>NSExtensionActivationRule</key>',
767
+ '\t\t<dict>',
768
+ ...Object.entries(shareActivationRule(mimeTypes)).flatMap(([key, value]) => [
769
+ `\t\t\t<key>${key}</key>`,
770
+ typeof value === 'boolean'
771
+ ? '\t\t\t<true/>'
772
+ : `\t\t\t<integer>${value}</integer>`,
773
+ ]),
774
+ '\t\t</dict>',
775
+ '\t</dict>',
776
+ '\t<key>NSExtensionPointIdentifier</key>',
777
+ '\t<string>com.apple.share-services</string>',
778
+ '\t<key>NSExtensionPrincipalClass</key>',
779
+ '\t<string>$(PRODUCT_MODULE_NAME).ShareViewController</string>',
780
+ '</dict>',
781
+ ].join('\n');
782
+ };
783
+ /**
784
+ * Config/ShareExtension.entitlements: the App Group alone, which is all the
785
+ * extension needs to leave a share where the app reads it.
786
+ */
787
+ export const shareExtensionEntitlementsPlist = (app) => [
788
+ '<?xml version="1.0" encoding="UTF-8"?>',
789
+ '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
790
+ '<!-- Written by @blotch/angular-lynx-native from shareTarget in angular.json. -->',
791
+ '<plist version="1.0">',
792
+ '<dict>',
793
+ '\t<key>com.apple.security.application-groups</key>',
794
+ '\t<array>',
795
+ `\t\t<string>${escapeXml(shareGroup(app))}</string>`,
796
+ '\t</array>',
797
+ '</dict>',
798
+ '</plist>',
799
+ '',
800
+ ].join('\n');
801
+ /**
802
+ * The IDs of what {@link withShareExtension} adds to the project. Any 24 hex
803
+ * digits do, as long as they're not the template's: fixed ones keep the
804
+ * project the same from one build to the next, which Xcode's incremental
805
+ * build state relies on.
806
+ */
807
+ const SHARE_IDS = {
808
+ target: '9D15F9546F164C486D749216',
809
+ product: '57C1796F1E1D5E76E66D6B1C',
810
+ embedFile: '7F7C1C4CBB392ABDC1101D98',
811
+ embedPhase: '7AAA86580D6ADC71A7F9AEF7',
812
+ exceptions: '5CCA31789B464279D620DE7A',
813
+ group: 'B63CC0C639B6A1AFBB041D9A',
814
+ frameworks: '37E44BB912BEB62673EF446D',
815
+ resources: '845DC63496E277892316007B',
816
+ sources: 'CF43D7AA4D8685F579119344',
817
+ proxy: '9C2EA95DB61269E81E8FEF29',
818
+ dependency: 'BF032A8D37EBD074ECB8FB5F',
819
+ debug: 'B32A48D083991B263D7C3F38',
820
+ release: '200485B5D20BCAA13A707402',
821
+ configurations: '9BE5D382CCC82CC4917AEC6A',
822
+ };
823
+ /** The template's own objects that the extension's are tied to. */
824
+ const TEMPLATE_IDS = {
825
+ project: '0C772889932C3A1BA02CADB1',
826
+ appTarget: 'FAC36A2364C8DD5EE47C8F83',
827
+ xcconfig: '33520879069FA35A78AACE2A',
828
+ };
829
+ /**
830
+ * The project with the Share Extension's target added: built from the
831
+ * synchronized ShareExtension/ folder, embedded in the app (Embed Foundation
832
+ * Extensions), and built before it. The target reads App.xcconfig, as the
833
+ * app's does, for the version, the team and the display name, and has
834
+ * settings of its own for its bundle ID (the app's, then `.ShareExtension`,
835
+ * as Apple requires), its Info.plist and its entitlements. It links none of
836
+ * Lynx's frameworks: it can't run Lynx.
837
+ *
838
+ * Xcode's project format has no includes, so the target's objects are
839
+ * written into the sections they belong to, and each list that names them
840
+ * (the project's targets, the app's build phases and dependencies, the
841
+ * groups) gets a line after a line of the template's it must find once.
842
+ */
843
+ export const withShareExtension = (project, app) => {
844
+ const ids = SHARE_IDS;
845
+ const lines = project.split('\n');
846
+ /**
847
+ * Inserts `added` after the one line that is `after`, trimmed.
848
+ */
849
+ const insertAfter = (after, added) => {
850
+ const found = lines.flatMap((line, index) => line.trim() === after ? [index] : []);
851
+ if (found.length !== 1) {
852
+ throw new Error(`The project template has ${found.length} lines "${after}", where the Share Extension's target needs one.`);
853
+ }
854
+ const indent = /^\s*/.exec(lines[found[0]])[0];
855
+ lines.splice(found[0] + 1, 0, ...added.map((line) => `${indent}${line}`));
856
+ };
857
+ /**
858
+ * Inserts a section of its own after the section `after` ends.
859
+ */
860
+ const section = (after, name, objects) => {
861
+ const end = lines.findIndex((line) => line === `/* End ${after} section */`);
862
+ if (end === -1) {
863
+ throw new Error(`The project template has no ${after} section.`);
864
+ }
865
+ lines.splice(end + 1, 0, '', `/* Begin ${name} section */`, ...objects, `/* End ${name} section */`);
866
+ };
867
+ /**
868
+ * Adds objects at the end of a section the template has.
869
+ */
870
+ const intoSection = (name, objects) => {
871
+ const end = lines.findIndex((line) => line.trim() === `/* End ${name} section */`);
872
+ if (end === -1) {
873
+ throw new Error(`The project template has no ${name} section.`);
874
+ }
875
+ lines.splice(end, 0, ...objects);
876
+ };
877
+ const settings = (configuration) => [
878
+ `\t\t${ids[configuration === 'Debug' ? 'debug' : 'release']} /* ${configuration} */ = {`,
879
+ '\t\t\tisa = XCBuildConfiguration;',
880
+ `\t\t\tbaseConfigurationReference = ${TEMPLATE_IDS.xcconfig} /* Config/App.xcconfig */;`,
881
+ '\t\t\tbuildSettings = {',
882
+ '\t\t\t\tAPPLICATION_EXTENSION_API_ONLY = YES;',
883
+ `\t\t\t\tCODE_SIGN_ENTITLEMENTS = ${SHARE_EXTENSION_ENTITLEMENTS};`,
884
+ '\t\t\t\tCODE_SIGN_STYLE = Automatic;',
885
+ '\t\t\t\tGENERATE_INFOPLIST_FILE = NO;',
886
+ `\t\t\t\tINFOPLIST_FILE = ${IOS_SHARE_EXTENSION_DIR}/Info.plist;`,
887
+ '\t\t\t\tLD_RUNPATH_SEARCH_PATHS = (',
888
+ '\t\t\t\t\t"$(inherited)",',
889
+ '\t\t\t\t\t"@executable_path/Frameworks",',
890
+ '\t\t\t\t\t"@executable_path/../../Frameworks",',
891
+ '\t\t\t\t);',
892
+ `\t\t\t\tPRODUCT_BUNDLE_IDENTIFIER = "${app.id}.ShareExtension";`,
893
+ '\t\t\t\tPRODUCT_NAME = ShareExtension;',
894
+ '\t\t\t\tSKIP_INSTALL = YES;',
895
+ '\t\t\t\tTARGETED_DEVICE_FAMILY = 1;',
896
+ '\t\t\t};',
897
+ `\t\t\tname = ${configuration};`,
898
+ '\t\t};',
899
+ ];
900
+ intoSection('PBXBuildFile', [
901
+ `\t\t${ids.embedFile} /* ShareExtension.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = ${ids.product} /* ShareExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };`,
902
+ ]);
903
+ section('PBXBuildFile', 'PBXContainerItemProxy', [
904
+ `\t\t${ids.proxy} /* PBXContainerItemProxy */ = {`,
905
+ '\t\t\tisa = PBXContainerItemProxy;',
906
+ `\t\t\tcontainerPortal = ${TEMPLATE_IDS.project} /* Project object */;`,
907
+ '\t\t\tproxyType = 1;',
908
+ `\t\t\tremoteGlobalIDString = ${ids.target};`,
909
+ '\t\t\tremoteInfo = ShareExtension;',
910
+ '\t\t};',
911
+ ]);
912
+ intoSection('PBXCopyFilesBuildPhase', [
913
+ `\t\t${ids.embedPhase} /* Embed Foundation Extensions */ = {`,
914
+ '\t\t\tisa = PBXCopyFilesBuildPhase;',
915
+ '\t\t\tbuildActionMask = 2147483647;',
916
+ '\t\t\tdstPath = "";',
917
+ '\t\t\tdstSubfolderSpec = 13;',
918
+ '\t\t\tfiles = (',
919
+ `\t\t\t\t${ids.embedFile} /* ShareExtension.appex in Embed Foundation Extensions */,`,
920
+ '\t\t\t);',
921
+ '\t\t\tname = "Embed Foundation Extensions";',
922
+ '\t\t\trunOnlyForDeploymentPostprocessing = 0;',
923
+ '\t\t};',
924
+ ]);
925
+ intoSection('PBXFileReference', [
926
+ `\t\t${ids.product} /* ShareExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = ShareExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; };`,
927
+ ]);
928
+ intoSection('PBXFileSystemSynchronizedBuildFileExceptionSet', [
929
+ `\t\t${ids.exceptions} /* PBXFileSystemSynchronizedBuildFileExceptionSet */ = {`,
930
+ '\t\t\tisa = PBXFileSystemSynchronizedBuildFileExceptionSet;',
931
+ '\t\t\tmembershipExceptions = (',
932
+ '\t\t\t\tInfo.plist,',
933
+ '\t\t\t);',
934
+ `\t\t\ttarget = ${ids.target} /* ShareExtension */;`,
935
+ '\t\t};',
936
+ ]);
937
+ intoSection('PBXFileSystemSynchronizedRootGroup', [
938
+ `\t\t${ids.group} /* ShareExtension */ = {`,
939
+ '\t\t\tisa = PBXFileSystemSynchronizedRootGroup;',
940
+ '\t\t\texceptions = (',
941
+ `\t\t\t\t${ids.exceptions} /* PBXFileSystemSynchronizedBuildFileExceptionSet */,`,
942
+ '\t\t\t);',
943
+ `\t\t\tpath = ${IOS_SHARE_EXTENSION_DIR};`,
944
+ '\t\t\tsourceTree = "<group>";',
945
+ '\t\t};',
946
+ ]);
947
+ const phase = (id, isa, name) => [
948
+ `\t\t${id} /* ${name} */ = {`,
949
+ `\t\t\tisa = ${isa};`,
950
+ '\t\t\tbuildActionMask = 2147483647;',
951
+ '\t\t\tfiles = (',
952
+ '\t\t\t);',
953
+ '\t\t\trunOnlyForDeploymentPostprocessing = 0;',
954
+ '\t\t};',
955
+ ];
956
+ intoSection('PBXFrameworksBuildPhase', phase(ids.frameworks, 'PBXFrameworksBuildPhase', 'Frameworks'));
957
+ intoSection('PBXNativeTarget', [
958
+ `\t\t${ids.target} /* ShareExtension */ = {`,
959
+ '\t\t\tisa = PBXNativeTarget;',
960
+ `\t\t\tbuildConfigurationList = ${ids.configurations} /* Build configuration list for PBXNativeTarget "ShareExtension" */;`,
961
+ '\t\t\tbuildPhases = (',
962
+ `\t\t\t\t${ids.sources} /* Sources */,`,
963
+ `\t\t\t\t${ids.frameworks} /* Frameworks */,`,
964
+ `\t\t\t\t${ids.resources} /* Resources */,`,
965
+ '\t\t\t);',
966
+ '\t\t\tbuildRules = (',
967
+ '\t\t\t);',
968
+ '\t\t\tdependencies = (',
969
+ '\t\t\t);',
970
+ '\t\t\tfileSystemSynchronizedGroups = (',
971
+ `\t\t\t\t${ids.group} /* ShareExtension */,`,
972
+ '\t\t\t);',
973
+ '\t\t\tname = ShareExtension;',
974
+ '\t\t\tproductName = ShareExtension;',
975
+ `\t\t\tproductReference = ${ids.product} /* ShareExtension.appex */;`,
976
+ '\t\t\tproductType = "com.apple.product-type.app-extension";',
977
+ '\t\t};',
978
+ ]);
979
+ intoSection('PBXResourcesBuildPhase', phase(ids.resources, 'PBXResourcesBuildPhase', 'Resources'));
980
+ intoSection('PBXSourcesBuildPhase', phase(ids.sources, 'PBXSourcesBuildPhase', 'Sources'));
981
+ section('PBXSourcesBuildPhase', 'PBXTargetDependency', [
982
+ `\t\t${ids.dependency} /* PBXTargetDependency */ = {`,
983
+ '\t\t\tisa = PBXTargetDependency;',
984
+ `\t\t\ttarget = ${ids.target} /* ShareExtension */;`,
985
+ `\t\t\ttargetProxy = ${ids.proxy} /* PBXContainerItemProxy */;`,
986
+ '\t\t};',
987
+ ]);
988
+ intoSection('XCBuildConfiguration', [
989
+ ...settings('Debug'),
990
+ ...settings('Release'),
991
+ ]);
992
+ intoSection('XCConfigurationList', [
993
+ `\t\t${ids.configurations} /* Build configuration list for PBXNativeTarget "ShareExtension" */ = {`,
994
+ '\t\t\tisa = XCConfigurationList;',
995
+ '\t\t\tbuildConfigurations = (',
996
+ `\t\t\t\t${ids.debug} /* Debug */,`,
997
+ `\t\t\t\t${ids.release} /* Release */,`,
998
+ '\t\t\t);',
999
+ '\t\t\tdefaultConfigurationIsVisible = 0;',
1000
+ '\t\t\tdefaultConfigurationName = Release;',
1001
+ '\t\t};',
1002
+ ]);
1003
+ // The lists that name the new objects.
1004
+ insertAfter('D84EC48E5D673EC745AF0F7D /* LynxBundle */,', [
1005
+ `${ids.group} /* ShareExtension */,`,
1006
+ ]);
1007
+ insertAfter('15B4BAB9D017652EC88BA5A1 /* App.app */,', [
1008
+ `${ids.product} /* ShareExtension.appex */,`,
1009
+ ]);
1010
+ // The app embeds the extension after its frameworks, and builds it first.
1011
+ insertAfter('60782C70CAE3611E1ABED20A /* Embed Frameworks */,', [
1012
+ `${ids.embedPhase} /* Embed Foundation Extensions */,`,
1013
+ ]);
1014
+ const appTarget = lines.findIndex((line) => line.trim().startsWith(`${TEMPLATE_IDS.appTarget} /* App */ = {`));
1015
+ const dependencies = lines.findIndex((line, index) => index > appTarget && line.trim() === 'dependencies = (');
1016
+ if (appTarget === -1 || dependencies === -1) {
1017
+ throw new Error("The project template's App target has no dependencies.");
1018
+ }
1019
+ const indent = /^\s*/.exec(lines[dependencies])[0];
1020
+ lines.splice(dependencies + 1, 0, `${indent}\t${ids.dependency} /* PBXTargetDependency */,`);
1021
+ insertAfter(`${TEMPLATE_IDS.appTarget} /* App */,`, [
1022
+ `${ids.target} /* ShareExtension */,`,
1023
+ ]);
1024
+ insertAfter('TargetAttributes = {', [
1025
+ `\t${ids.target} = {`,
1026
+ '\t\tCreatedOnToolsVersion = 27.0;',
1027
+ '\t};',
1028
+ ]);
1029
+ return lines.join('\n');
1030
+ };