Skip to content

Commit 54fa5fc

Browse files
committed
wip: reorder build script phases PoC for Expo config plugin
1 parent f4a44b8 commit 54fa5fc

4 files changed

Lines changed: 70 additions & 25 deletions

File tree

packages/cli/src/brownfield/commands/packageIos.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export const packageIosCommand = curryOptions(
7777
'swift'
7878
);
7979

80+
// TODO: ARTUR install pods here
81+
8082
await packageIosAction(
8183
options,
8284
{
@@ -89,10 +91,22 @@ export const packageIosCommand = curryOptions(
8991
usePrebuiltRNCore: false, // for brownfield, it is required to build RN from source
9092
packageDir, // the output directory for artifacts
9193
skipCache: true, // cache is dependent on existence of Rock config file
94+
// TODO: ARTUR disable installing pods in packageIosAction
9295
},
9396
platformConfig
9497
);
9598

99+
// TODO: ARTUR - below:
100+
// const existingTarget = project.pbxTargetByName(frameworkName);
101+
// const frameworkTargetUUID = Object.entries(
102+
// project.pbxNativeTargetSection()
103+
// ).find(
104+
// ([_key, value]) =>
105+
// (value as any)?.productReference === frameworkName
106+
// )?.[0];
107+
// load xcodeproj from platformConfig?.xcodeProject?.path using 'xcode' package
108+
// ensureExpoPre55ShellPatchScriptPhaseIsOrdered(project, frameworkTargetUUID)
109+
96110
if (hasBrownie) {
97111
const productsPath = path.join(options.buildFolder, 'Build', 'Products');
98112
const brownieOutputPath = path.join(packageDir, 'Brownie.xcframework');

packages/cli/src/brownfield/utils/expo.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ export async function runExpoPrebuildIfNeeded({
5252
return false;
5353
}
5454

55-
logger.info(`Expo project detected. Running expo prebuild for ${platform}...`);
55+
logger.info(
56+
`Expo project detected. Running expo prebuild for ${platform}...`
57+
);
5658

5759
const args = ['expo', 'prebuild', '--platform', platform];
5860
if (platform === 'ios') {

packages/react-native-brownfield/src/expo-config-plugin/ios/withBrownfieldIos.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ export const withBrownfieldIos: ConfigPlugin<
3131
config = withXcodeProject(config, (xcodeConfig) => {
3232
const { modResults: project, modRequest } = xcodeConfig;
3333

34-
const frameworkTargetUUIDIfAdded = addFrameworkTarget(
34+
const { frameworkTargetUUID, targetAlreadyExists } = addFrameworkTarget(
3535
project,
3636
modRequest,
3737
props.ios
3838
);
3939

40-
if (!frameworkTargetUUIDIfAdded) {
40+
const expoMajor = config.sdkVersion
41+
? parseInt(config.sdkVersion.split('.')[0], 10)
42+
: -1;
43+
const isExpoPre55 = expoMajor < 55;
44+
45+
if (targetAlreadyExists) {
4146
Logger.logDebug(
4247
`Skipping further Xcode modifications as framework target was already present`
4348
);
@@ -46,20 +51,17 @@ export const withBrownfieldIos: ConfigPlugin<
4651
}
4752

4853
// copy the "Bundle React Native code and images" build phase from the main target to the framework target
49-
copyBundleReactNativePhase(project, frameworkTargetUUIDIfAdded);
54+
copyBundleReactNativePhase(project, frameworkTargetUUID);
5055

5156
// for Expo SDK versions < 55, add a script phase to patch ExpoModulesProvider.swift
52-
const major = config.sdkVersion
53-
? parseInt(config.sdkVersion.split('.')[0], 10)
54-
: -1;
55-
if (major < 55) {
57+
if (isExpoPre55) {
5658
Logger.logDebug(
5759
`Adding ExpoModulesProvider patch phase for Expo SDK ${config.sdkVersion}`
5860
);
5961

6062
addExpoPre55ShellPatchScriptPhase(project, {
6163
frameworkName: props.ios.frameworkName,
62-
frameworkTargetUUID: frameworkTargetUUIDIfAdded,
64+
frameworkTargetUUID: frameworkTargetUUID,
6365
});
6466
} else {
6567
Logger.logDebug(

packages/react-native-brownfield/src/expo-config-plugin/ios/xcodeHelpers.ts

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export function addFrameworkTarget(
1818
project: XcodeProject,
1919
modRequest: ModProps<XcodeProject>,
2020
options: ResolvedBrownfieldPluginIosConfig
21-
): string | null {
21+
): {
22+
frameworkTargetUUID: string;
23+
targetAlreadyExists: boolean;
24+
} {
2225
const { frameworkName, bundleIdentifier } = options;
2326

2427
// check if target already exists
@@ -28,7 +31,23 @@ export function addFrameworkTarget(
2831
`Framework target "${frameworkName}" already exists, skipping creation`
2932
);
3033

31-
return null;
34+
const frameworkTargetUUID = Object.entries(
35+
project.pbxNativeTargetSection()
36+
).find(
37+
([_key, value]) =>
38+
(value as any)?.productReference === existingTarget.productReference
39+
)?.[0];
40+
41+
if (!frameworkTargetUUID) {
42+
throw new SourceModificationError(
43+
`Failed to find framework target UUID for ${frameworkName}, although it can be resolved by name`
44+
);
45+
}
46+
47+
return {
48+
frameworkTargetUUID,
49+
targetAlreadyExists: true,
50+
};
3251
}
3352

3453
Logger.logDebug(`Adding iOS framework target: ${frameworkName}`);
@@ -130,7 +149,10 @@ export function addFrameworkTarget(
130149

131150
Logger.logInfo(`Successfully added framework target: ${frameworkName}`);
132151

133-
return frameworkTarget.uuid;
152+
return {
153+
frameworkTargetUUID: frameworkTarget.uuid,
154+
targetAlreadyExists: false,
155+
};
134156
}
135157

136158
/**
@@ -265,22 +287,24 @@ export function addExpoPre55ShellPatchScriptPhase(
265287
}),
266288
}
267289
);
290+
}
268291

269-
// make sure the patch phase is after the expo configure phase,
270-
// otherwise the patched file will be overwritten by the expo configure phase
292+
/**
293+
* Makes sure the patch expo modules provider phase is after the expo configure phase,
294+
* otherwise the patched file would be overwritten by the expo configure phase
295+
* @param project The Xcode project
296+
* @param frameworkTargetUUID The UUID of the framework target
297+
* @returns True if the build phases were modified, false otherwise
298+
*/
299+
export function ensureExpoPre55ShellPatchScriptPhaseIsOrdered(
300+
project: XcodeProject,
301+
frameworkTargetUUID: string
302+
) {
303+
let modified = false;
271304
const nativeTargetSection = project.pbxNativeTargetSection();
272305

273-
const brownfieldTarget = Object.entries(nativeTargetSection).find(
274-
([_key, value]) =>
275-
typeof value === 'object' &&
276-
(value as any)?.productType.includes(
277-
'com.apple.product-type.framework'
278-
) &&
279-
(value as any)?.name === frameworkName
280-
)![0];
281-
282306
const buildPhases: { value: string; comment?: string }[] =
283-
nativeTargetSection[brownfieldTarget].buildPhases;
307+
nativeTargetSection[frameworkTargetUUID].buildPhases;
284308

285309
const expoConfigurePhaseIndex = buildPhases.findIndex(
286310
(phase) =>
@@ -301,9 +325,12 @@ export function addExpoPre55ShellPatchScriptPhase(
301325
1
302326
)[0]; // pop the element at patchExpoModulesProviderPhaseIndex
303327
buildPhases.splice(expoConfigurePhaseIndex, 0, element); // insert the element at expoConfigurePhaseIndex ("after")
328+
modified = true;
304329
}
305330

306-
nativeTargetSection[brownfieldTarget].buildPhases = buildPhases;
331+
nativeTargetSection[frameworkTargetUUID].buildPhases = buildPhases;
307332

308333
project.writeSync();
334+
335+
return modified;
309336
}

0 commit comments

Comments
 (0)