|
1 | | -import { ConfigPlugin, IOSConfig, BaseMods, withMod } from 'expo/config-plugins' |
2 | | -import fs from 'fs' |
| 1 | +import { ConfigPlugin, withAppDelegate } from '@expo/config-plugins' |
| 2 | +import * as fs from 'fs' |
| 3 | +import * as path from 'path' |
3 | 4 |
|
4 | | -/** |
5 | | - * A plugin which adds new base modifiers to the prebuild config. |
6 | | - */ |
7 | | -export function withAppDelegateBaseMod(config) { |
8 | | - return ( |
9 | | - BaseMods.withGeneratedBaseMods < |
10 | | - 'appDelegate' > |
11 | | - (config, |
12 | | - { |
13 | | - platform: 'ios', |
14 | | - providers: { |
15 | | - // Append a custom rule to supply AppDelegate data to mods on `mods.ios.appDelegate` |
16 | | - appDelegate: |
17 | | - BaseMods.provider < |
18 | | - IOSConfig.Paths.AppDelegateProjectFile > |
19 | | - { |
20 | | - // Get the local filepath that should be passed to the `read` method. |
21 | | - getFilePath({ modRequest: { projectRoot } }) { |
22 | | - return IOSConfig.Paths.getAppDelegateFilePath(projectRoot) |
23 | | - }, |
24 | | - // Read the input file from the filesystem. |
25 | | - async read(filePath) { |
26 | | - return IOSConfig.Paths.getFileInfo(filePath) |
27 | | - }, |
28 | | - // Write the resulting output to the filesystem. |
29 | | - async write(filePath: string, { modResults: { contents } }) { |
30 | | - // Modify the AppDelegate.m/mm file's contents |
31 | | - const modifiedContents = modifyAppDelegate(contents) |
32 | | - await fs.promises.writeFile(filePath, modifiedContents) |
33 | | - }, |
34 | | - }, |
35 | | - }, |
36 | | - }) |
37 | | - ) |
38 | | -} |
39 | | - |
40 | | -/** |
41 | | - * Function to modify the AppDelegate.m/mm file contents. |
42 | | - */ |
43 | | -function modifyAppDelegate(contents) { |
| 5 | +// Helper function to modify AppDelegate.m |
| 6 | +function modifyAppDelegate(appDelegate: string): string { |
44 | 7 | const importStatement = `#import "RNExternalDisplayUtils.h"` |
45 | 8 |
|
46 | 9 | // Add the import statement if it's not already present |
47 | | - if (!contents.includes(importStatement)) { |
48 | | - contents = `${importStatement}\n${contents}` |
| 10 | + if (!appDelegate.includes(importStatement)) { |
| 11 | + appDelegate = `${importStatement}\n${appDelegate}` |
49 | 12 | } |
50 | 13 |
|
51 | | - const method = ` |
52 | | - - (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0)) { |
53 | | - UISceneConfiguration * configuration = |
54 | | - [RNExternalAppDelegateUtil application:application |
55 | | - configurationForConnectingSceneSession:connectingSceneSession |
56 | | - options:options |
57 | | - sceneOptions:@{ |
58 | | - @"headless": @YES |
59 | | - } |
60 | | - ]; |
61 | | - return configuration; |
62 | | - } |
63 | | - ` |
| 14 | + const customMethod = ` |
| 15 | +- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0)) { |
| 16 | + UISceneConfiguration * configuration = |
| 17 | + [RNExternalAppDelegateUtil application:application |
| 18 | + configurationForConnectingSceneSession:connectingSceneSession |
| 19 | + options:options |
| 20 | + sceneOptions:@{ |
| 21 | + @"headless": @YES |
| 22 | + } |
| 23 | + ]; |
| 24 | + return configuration; |
| 25 | +} |
| 26 | +` |
64 | 27 |
|
65 | | - // Add the method if it's not already present |
66 | | - if ( |
67 | | - !contents.includes( |
68 | | - 'application:(UIApplication *)application configurationForConnectingSceneSession', |
69 | | - ) |
70 | | - ) { |
71 | | - contents = contents.replace('@end', `${method}\n@end`) |
| 28 | + // Insert the method before the '@end' in AppDelegate |
| 29 | + if (!appDelegate.includes('configurationForConnectingSceneSession')) { |
| 30 | + appDelegate = appDelegate.replace('@end', `${customMethod}\n@end`) |
72 | 31 | } |
73 | 32 |
|
74 | | - return contents |
| 33 | + return appDelegate |
75 | 34 | } |
76 | 35 |
|
77 | | -/** |
78 | | - * (Utility) Provides the AppDelegate file for modification. |
79 | | - */ |
80 | | -export const withAppDelegate: ConfigPlugin< |
81 | | - Mod<IOSConfig.Paths.AppDelegateProjectFile>, |
82 | | -> = (config, action) => { |
83 | | - return withMod(config, { |
84 | | - platform: 'ios', |
85 | | - mod: 'appDelegate', |
86 | | - action, |
87 | | - }) |
88 | | -} |
| 36 | +// Config plugin to modify AppDelegate.m |
| 37 | +const withMultipleSceneSupport: ConfigPlugin = (config) => { |
| 38 | + return withAppDelegate(config, async (config) => { |
| 39 | + config.modResults.contents = modifyAppDelegate(config.modResults.contents) |
89 | 40 |
|
90 | | -// (Example) Log the contents of the AppDelegate mod results. |
91 | | -export const withSimpleAppDelegateMod = (config) => { |
92 | | - return withAppDelegate(config, (config) => { |
93 | | - console.log('modify AppDelegate:', config.modResults) |
94 | 41 | return config |
95 | 42 | }) |
96 | 43 | } |
97 | 44 |
|
98 | | -export default withAppDelegateBaseMod |
| 45 | +export default withMultipleSceneSupport |
0 commit comments