Skip to content

Commit 8115218

Browse files
committed
refactor: improve bridge header path detection in plugin config
1 parent 4437501 commit 8115218

File tree

1 file changed

+56
-34
lines changed

1 file changed

+56
-34
lines changed

expo/withAppsFlyerIos.js

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1-
const { withDangerousMod, withAppDelegate, WarningAggregator } = require('@expo/config-plugins');
1+
const { withAppDelegate, withDangerousMod, withXcodeProject, WarningAggregator } = require('@expo/config-plugins');
22
const { mergeContents } = require('@expo/config-plugins/build/utils/generateCode');
33
const { getAppDelegate } = require('@expo/config-plugins/build/ios/Paths');
44
const fs = require('fs');
55
const path = require('path');
66

7+
function getBridgingHeaderPathFromXcode(project) {
8+
const buildConfigs = project.pbxXCBuildConfigurationSection();
9+
10+
for (const key in buildConfigs) {
11+
const config = buildConfigs[key];
12+
if (
13+
typeof config === 'object' &&
14+
config.buildSettings &&
15+
config.buildSettings['SWIFT_OBJC_BRIDGING_HEADER']
16+
) {
17+
const bridgingHeaderPath = config.buildSettings[
18+
'SWIFT_OBJC_BRIDGING_HEADER'
19+
].replace(/"/g, '');
20+
21+
return bridgingHeaderPath;
22+
}
23+
}
24+
25+
return null;
26+
}
27+
728
function modifyObjcAppDelegate(appDelegate) {
829
const RNAPPSFLYER_IMPORT = `#import <RNAppsFlyer.h>\n`;
930
const RNAPPSFLYER_CONTINUE_USER_ACTIVITY_IDENTIFIER = `- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {`;
@@ -88,47 +109,48 @@ function withAppsFlyerAppDelegate(config) {
88109
});
89110
}
90111

91-
function withIosBridgingHeader(config) {
92-
return withDangerousMod(config, [
93-
'ios',
94-
async (config) => {
95-
const projectRoot = config.modRequest.projectRoot;
96-
const projectName = config.modRequest.projectName || config.name;
97-
const appDelegate = getAppDelegate(projectRoot);
98-
99-
if (appDelegate.language === 'swift') {
100-
const bridgingHeaderPath = path.join(
101-
config.modRequest.platformProjectRoot,
102-
config.name,
103-
`${projectName}-Bridging-Header.h`,
104-
);
105-
106-
if (fs.existsSync(bridgingHeaderPath)) {
107-
let content = fs.readFileSync(bridgingHeaderPath, 'utf8');
108-
const appsFlyerImport = '#import <RNAppsFlyer.h>';
109-
110-
if (!content.includes(appsFlyerImport)) {
111-
content += `${appsFlyerImport}\n`;
112-
fs.writeFileSync(bridgingHeaderPath, content);
113-
}
114-
} else {
115-
WarningAggregator.addWarningIOS(
116-
'withIosBridgingHeader',
112+
const withIosBridgingHeader = (config) => {
113+
return withXcodeProject(config, (action) => {
114+
const projectRoot = action.modRequest.projectRoot;
115+
const appDelegate = getAppDelegate(projectRoot);
116+
117+
if (appDelegate.language === 'swift') {
118+
const bridgingHeaderPath = getBridgingHeaderPathFromXcode(
119+
action.modResults,
120+
);
121+
122+
const bridgingHeaderFilePath = path.join(
123+
action.modRequest.platformProjectRoot,
124+
bridgingHeaderPath,
125+
);
126+
127+
if (fs.existsSync(bridgingHeaderFilePath)) {
128+
let content = fs.readFileSync(bridgingHeaderFilePath, 'utf8');
129+
const appsFlyerImport = '#import <RNAppsFlyer.h>';
130+
131+
if (!content.includes(appsFlyerImport)) {
132+
content += `${appsFlyerImport}\n`;
133+
fs.writeFileSync(bridgingHeaderFilePath, content);
134+
}
135+
136+
return action;
137+
}
138+
139+
WarningAggregator.addWarningIOS(
140+
'withIosBridgingHeader',
117141
`
118142
Failed to detect ${bridgingHeaderPath} file. Please add AppsFlyer integration manually:
119143
#import <RNAppsFlyer.h>
120144
121145
Supported format: Expo SDK default template
122146
`
123-
);
124-
}
147+
);
125148

126-
return config;
127-
}
149+
return action;
150+
}
128151

129-
return config;
130-
},
131-
]);
152+
return action;
153+
});
132154
};
133155

134156
function withPodfile(config, shouldUseStrictMode) {

0 commit comments

Comments
 (0)