|
1 | 1 | import { withAppDelegate, ConfigPlugin } from '@expo/config-plugins'; |
2 | | -import { isExpo53OrLater } from '../expo-version'; |
| 2 | +import { assertExpo53OrLater } from '../expo-version'; |
3 | 3 |
|
4 | 4 | const codeModIOs = require('@expo/config-plugins/build/ios/codeMod'); |
5 | 5 |
|
6 | | -const withAppDelegateSwift: ConfigPlugin = rootConfig => { |
7 | | - return withAppDelegate(rootConfig, config => { |
8 | | - let { contents } = config.modResults; |
9 | | - |
10 | | - if (!contents.includes('RNAppAuthAuthorizationFlowManager')) { |
11 | | - const replaceText = 'class AppDelegate: ExpoAppDelegate'; |
12 | | - contents = contents.replace(replaceText, `${replaceText}, RNAppAuthAuthorizationFlowManager`); |
13 | | - |
14 | | - const replaceText2 = |
15 | | - 'return super.application(app, open: url, options: options) || RCTLinkingManager.application(app, open: url, options: options)'; |
16 | | - contents = contents.replace( |
17 | | - replaceText2, |
18 | | - `if let authorizationFlowManagerDelegate = self.authorizationFlowManagerDelegate { |
| 6 | +const APP_AUTH_PROTOCOL = 'RNAppAuthAuthorizationFlowManager'; |
| 7 | +const APP_AUTH_DELEGATE_PROPERTY = |
| 8 | + 'public weak var authorizationFlowManagerDelegate: RNAppAuthAuthorizationFlowManagerDelegate?'; |
| 9 | +const APP_AUTH_RESUME_BLOCK = `if let authorizationFlowManagerDelegate = self.authorizationFlowManagerDelegate { |
19 | 10 | if authorizationFlowManagerDelegate.resumeExternalUserAgentFlow(with: url) { |
20 | | - return true |
| 11 | + return true |
21 | 12 | } |
| 13 | + }`; |
| 14 | + |
| 15 | +export const applyExpo53AppDelegatePatch = (contents: string): string => { |
| 16 | + contents = contents.replace( |
| 17 | + /^(\s*(?:public\s+)?class\s+AppDelegate\s*:\s*ExpoAppDelegate)([^{]*)(\{)/m, |
| 18 | + (match, declaration, conformances, openingBrace) => { |
| 19 | + const trimmedConformances = conformances.trim(); |
| 20 | + if (trimmedConformances.includes(APP_AUTH_PROTOCOL)) { |
| 21 | + return match; |
| 22 | + } |
| 23 | + |
| 24 | + return `${declaration}${trimmedConformances}, ${APP_AUTH_PROTOCOL} ${openingBrace}`; |
22 | 25 | } |
23 | | - ${replaceText2}` |
24 | | - ); |
| 26 | + ); |
25 | 27 |
|
26 | | - const replaceText3 = 'var reactNativeFactory: RCTReactNativeFactory?'; |
| 28 | + if (!contents.includes(APP_AUTH_DELEGATE_PROPERTY)) { |
| 29 | + const reactNativeFactoryPattern = |
| 30 | + /^(\s*)(?:public\s+)?var\s+reactNativeFactory\s*:\s*RCTReactNativeFactory\?\s*$/m; |
| 31 | + const factoryMatch = contents.match(reactNativeFactoryPattern); |
| 32 | + if (factoryMatch) { |
| 33 | + const indent = factoryMatch[1]; |
27 | 34 | contents = contents.replace( |
28 | | - replaceText3, |
29 | | - `${replaceText3}\n\n public weak var authorizationFlowManagerDelegate: RNAppAuthAuthorizationFlowManagerDelegate?` |
| 35 | + reactNativeFactoryPattern, |
| 36 | + match => `${match}\n\n${indent}${APP_AUTH_DELEGATE_PROPERTY}` |
30 | 37 | ); |
31 | 38 | } |
| 39 | + } |
32 | 40 |
|
33 | | - config.modResults.contents = contents; |
| 41 | + if (!contents.includes('resumeExternalUserAgentFlow(with: url)')) { |
| 42 | + contents = contents.replace( |
| 43 | + /((?:public\s+)?override\s+func\s+application\s*\([\s\S]*?open\s+url\s*:\s*URL[\s\S]*?\)\s*->\s*Bool\s*\{)/m, |
| 44 | + match => `${match}\n ${APP_AUTH_RESUME_BLOCK}\n` |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + return contents; |
| 49 | +}; |
| 50 | + |
| 51 | +const withAppDelegateSwift: ConfigPlugin = rootConfig => { |
| 52 | + return withAppDelegate(rootConfig, config => { |
| 53 | + assertExpo53OrLater(config, config.modRequest.projectRoot); |
| 54 | + config.modResults.contents = applyExpo53AppDelegatePatch(config.modResults.contents); |
34 | 55 | return config; |
35 | 56 | }); |
36 | 57 | }; |
37 | 58 |
|
38 | 59 | export const withAppAuthAppDelegate: ConfigPlugin = rootConfig => { |
39 | | - if (isExpo53OrLater(rootConfig)) { |
40 | | - return withAppDelegateSwift(rootConfig); |
41 | | - } |
| 60 | + return withAppDelegateSwift(rootConfig); |
| 61 | +}; |
42 | 62 |
|
| 63 | +export const withLegacyAppAuthAppDelegate: ConfigPlugin = rootConfig => { |
43 | 64 | return withAppDelegate(rootConfig, config => { |
44 | 65 | let { contents } = config.modResults; |
45 | 66 |
|
|
0 commit comments