|
| 1 | +const { withAppDelegate, withXcodeProject, WarningAggregator } = require('expo/config-plugins'); |
| 2 | +const { getAppDelegate } = require('@expo/config-plugins/build/ios/Paths'); |
| 3 | +const path = require('path'); |
| 4 | +const fs = require('fs'); |
| 5 | + |
| 6 | +function iosApplyImplementation( |
| 7 | + appDelegate, |
| 8 | + find, |
| 9 | + add, |
| 10 | + replace, |
| 11 | +) { |
| 12 | + if (appDelegate.includes(add)) { |
| 13 | + return appDelegate; |
| 14 | + } |
| 15 | + |
| 16 | + if (appDelegate.includes(find)) { |
| 17 | + return appDelegate.replace(find, replace ? add : `${find}\n${add}`); |
| 18 | + } |
| 19 | + |
| 20 | + WarningAggregator.addWarningIOS( |
| 21 | + 'withCodePushIos', |
| 22 | + ` |
| 23 | + Failed to detect "${find.replace(/\n/g, '').trim()}" in the AppDelegate.(m|swift). |
| 24 | + Please ${replace ? 'replace' : 'add'} "${add.replace(/\n/g, '').trim()}" to the AppDelegate.(m|swift). |
| 25 | + Supported format: Expo SDK default template. |
| 26 | +
|
| 27 | + iOS manual setup: https://github.com/Soomgo-Mobile/react-native-code-push#2-ios-setup |
| 28 | + `, |
| 29 | + ); |
| 30 | + |
| 31 | + return appDelegate; |
| 32 | +} |
| 33 | + |
| 34 | +function getBridgingHeaderFileFromXcode(project) { |
| 35 | + const buildConfigs = project.pbxXCBuildConfigurationSection(); |
| 36 | + |
| 37 | + for (const key in buildConfigs) { |
| 38 | + const config = buildConfigs[key]; |
| 39 | + if ( |
| 40 | + typeof config === 'object' && |
| 41 | + config.buildSettings && |
| 42 | + config.buildSettings['SWIFT_OBJC_BRIDGING_HEADER'] |
| 43 | + ) { |
| 44 | + const bridgingHeaderFile = config.buildSettings[ |
| 45 | + 'SWIFT_OBJC_BRIDGING_HEADER' |
| 46 | + ].replace(/"/g, ''); |
| 47 | + |
| 48 | + return bridgingHeaderFile; |
| 49 | + } |
| 50 | + } |
| 51 | + return null; |
| 52 | +} |
| 53 | + |
| 54 | +const withIosAppDelegateDependency = (config) => { |
| 55 | + return withAppDelegate(config, (action) => { |
| 56 | + const language = action.modResults.language; |
| 57 | + |
| 58 | + if (['objc', 'objcpp'].includes(language)) { |
| 59 | + action.modResults.contents = iosApplyImplementation( |
| 60 | + action.modResults.contents, |
| 61 | + `#import "AppDelegate.h"`, |
| 62 | + `#import <CodePush/CodePush.h>`, |
| 63 | + ); |
| 64 | + action.modResults.contents = iosApplyImplementation( |
| 65 | + action.modResults.contents, |
| 66 | + `return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];`, |
| 67 | + `return [CodePush bundleURL];`, |
| 68 | + true, |
| 69 | + ); |
| 70 | + |
| 71 | + return action; |
| 72 | + } |
| 73 | + |
| 74 | + if (language === 'swift') { |
| 75 | + action.modResults.contents = iosApplyImplementation( |
| 76 | + action.modResults.contents, |
| 77 | + `return Bundle.main.url(forResource: "main", withExtension: "jsbundle")`, |
| 78 | + `return CodePush.bundleURL()`, |
| 79 | + true, |
| 80 | + ); |
| 81 | + |
| 82 | + return action; |
| 83 | + } |
| 84 | + |
| 85 | + WarningAggregator.addWarningIOS( |
| 86 | + 'withIosAppDelegate', |
| 87 | + `${language} AppDelegate file is not supported yet.`, |
| 88 | + ); |
| 89 | + |
| 90 | + return action; |
| 91 | + }); |
| 92 | +}; |
| 93 | + |
| 94 | +const withIosBridgingHeader = (config) => { |
| 95 | + return withXcodeProject(config, (action) => { |
| 96 | + const projectRoot = action.modRequest.projectRoot; |
| 97 | + const appDelegate = getAppDelegate(projectRoot); |
| 98 | + |
| 99 | + if (appDelegate.language === 'swift') { |
| 100 | + const bridgingHeaderFile = getBridgingHeaderFileFromXcode( |
| 101 | + action.modResults, |
| 102 | + ); |
| 103 | + |
| 104 | + const bridgingHeaderPath = path.join( |
| 105 | + action.modRequest.platformProjectRoot, |
| 106 | + bridgingHeaderFile, |
| 107 | + ); |
| 108 | + |
| 109 | + if (fs.existsSync(bridgingHeaderPath)) { |
| 110 | + let content = fs.readFileSync(bridgingHeaderPath, 'utf8'); |
| 111 | + const codePushImport = '#import <CodePush/CodePush.h>'; |
| 112 | + |
| 113 | + if (!content.includes(codePushImport)) { |
| 114 | + content += `${codePushImport}\n`; |
| 115 | + fs.writeFileSync(bridgingHeaderPath, content); |
| 116 | + } |
| 117 | + |
| 118 | + return action; |
| 119 | + } |
| 120 | + |
| 121 | + WarningAggregator.addWarningIOS( |
| 122 | + 'withIosBridgingHeader', |
| 123 | + ` |
| 124 | + Failed to detect ${bridgingHeaderPath} file. |
| 125 | + Please add CodePush integration manually: |
| 126 | + #import <CodePush/CodePush.h> |
| 127 | +
|
| 128 | + Supported format: Expo SDK default template. |
| 129 | + iOS manual setup: https://github.com/Soomgo-Mobile/react-native-code-push#2-edit-appdelegate-code |
| 130 | + ` |
| 131 | + ); |
| 132 | + |
| 133 | + return action; |
| 134 | + } |
| 135 | + |
| 136 | + return action; |
| 137 | + }); |
| 138 | +}; |
| 139 | + |
| 140 | +module.exports = { |
| 141 | + withIosAppDelegateDependency, |
| 142 | + withIosBridgingHeader, |
| 143 | +}; |
0 commit comments