Skip to content

Commit 97e68ae

Browse files
fix: install pods on ios-commands with New Arch (#2122)
* extract getArchitecture function * install pods with correct architecture * fix: install pods on ios commands with new arch * move iosFolderPath to options
1 parent 3930008 commit 97e68ae

File tree

7 files changed

+86
-34
lines changed

7 files changed

+86
-34
lines changed

packages/cli-doctor/src/commands/info.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import getEnvironmentInfo from '../tools/envinfo';
99
import {logger, version} from '@react-native-community/cli-tools';
1010
import {Config} from '@react-native-community/cli-types';
11+
import {getArchitecture} from '@react-native-community/cli-platform-ios';
1112
import {readFile} from 'fs-extra';
1213
import path from 'path';
1314
import {stringify} from 'yaml';
@@ -52,16 +53,11 @@ const info = async function getInfo(_argv: Array<string>, ctx: Config) {
5253
}
5354

5455
try {
55-
const project = await readFile(
56-
path.join(
57-
ctx.project.ios.sourceDir,
58-
'/Pods/Pods.xcodeproj/project.pbxproj',
59-
),
56+
const isNewArchitecture = await getArchitecture(
57+
ctx.project.ios.sourceDir,
6058
);
6159

62-
platforms.iOS.newArchEnabled = project.includes(
63-
'-DRCT_NEW_ARCH_ENABLED=1',
64-
);
60+
platforms.iOS.newArchEnabled = isNewArchitecture;
6561
} catch {
6662
platforms.iOS.newArchEnabled = notFound;
6763
}

packages/cli-platform-ios/src/commands/buildIOS/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ import {BuildFlags, buildOptions} from './buildOptions';
1212
import {getConfiguration} from './getConfiguration';
1313
import {getXcodeProjectAndDir} from './getXcodeProjectAndDir';
1414
import resolvePods from '../../tools/pods';
15+
import getArchitecture from '../../tools/getArchitecture';
1516

1617
async function buildIOS(_: Array<string>, ctx: Config, args: BuildFlags) {
1718
const {xcodeProject, sourceDir} = getXcodeProjectAndDir(ctx.project.ios);
1819

20+
const isAppRunningNewArchitecture = ctx.project.ios?.sourceDir
21+
? await getArchitecture(ctx.project.ios?.sourceDir)
22+
: undefined;
23+
1924
// check if pods need to be installed
20-
await resolvePods(ctx.root, ctx.dependencies, {forceInstall: args.forcePods});
25+
await resolvePods(ctx.root, ctx.dependencies, {
26+
forceInstall: args.forcePods,
27+
newArchEnabled: isAppRunningNewArchitecture,
28+
});
2129

2230
process.chdir(sourceDir);
2331

packages/cli-platform-ios/src/commands/runIOS/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {promptForDeviceSelection} from '../../tools/prompts';
3131
import getSimulators from '../../tools/getSimulators';
3232
import {getXcodeProjectAndDir} from '../buildIOS/getXcodeProjectAndDir';
3333
import resolvePods from '../../tools/pods';
34+
import getArchitecture from '../../tools/getArchitecture';
3435

3536
export interface FlagsT extends BuildFlags {
3637
simulator?: string;
@@ -48,8 +49,15 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
4849

4950
let {packager, port} = args;
5051

52+
const isAppRunningNewArchitecture = ctx.project.ios?.sourceDir
53+
? await getArchitecture(ctx.project.ios?.sourceDir)
54+
: undefined;
55+
5156
// check if pods need to be installed
52-
await resolvePods(ctx.root, ctx.dependencies, {forceInstall: args.forcePods});
57+
await resolvePods(ctx.root, ctx.dependencies, {
58+
forceInstall: args.forcePods,
59+
newArchEnabled: isAppRunningNewArchitecture,
60+
});
5361

5462
const packagerStatus = await isPackagerRunning(port);
5563

packages/cli-platform-ios/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
export {default as commands} from './commands';
66
export {projectConfig, dependencyConfig, findPodfilePaths} from './config';
7+
export {default as getArchitecture} from './tools/getArchitecture';
78
export {default as installPods} from './tools/installPods';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {readFile} from 'fs-extra';
2+
import path from 'path';
3+
4+
export default async function getArchitecture(iosSourceDir: string) {
5+
try {
6+
const project = await readFile(
7+
path.join(iosSourceDir, '/Pods/Pods.xcodeproj/project.pbxproj'),
8+
);
9+
10+
return project.includes('-DRCT_NEW_ARCH_ENABLED=1');
11+
} catch {
12+
return false;
13+
}
14+
}

packages/cli-platform-ios/src/tools/installPods.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,29 @@ import runBundleInstall from './runBundleInstall';
1313

1414
interface PodInstallOptions {
1515
skipBundleInstall?: boolean;
16+
newArchEnabled?: boolean;
17+
iosFolderPath?: string;
1618
}
1719

18-
async function runPodInstall(
19-
loader: Ora,
20-
shouldHandleRepoUpdate: boolean = true,
21-
) {
20+
interface RunPodInstallOptions {
21+
shouldHandleRepoUpdate?: boolean;
22+
newArchEnabled?: boolean;
23+
}
24+
25+
async function runPodInstall(loader: Ora, options?: RunPodInstallOptions) {
26+
const shouldHandleRepoUpdate = options?.shouldHandleRepoUpdate || true;
2227
try {
2328
loader.start(
24-
`Installing CocoaPods dependencies ${chalk.dim(
25-
'(this may take a few minutes)',
26-
)}`,
29+
`Installing CocoaPods dependencies ${chalk.bold(
30+
options?.newArchEnabled ? 'with New Architecture' : '',
31+
)} ${chalk.dim('(this may take a few minutes)')}`,
2732
);
2833

29-
await execa('bundle', ['exec', 'pod', 'install']);
34+
await execa('bundle', ['exec', 'pod', 'install'], {
35+
env: {
36+
RCT_NEW_ARCH_ENABLED: options?.newArchEnabled ? '1' : '0',
37+
},
38+
});
3039
} catch (error) {
3140
// "pod" command outputs errors to stdout (at least some of them)
3241
const stderr = (error as any).stderr || (error as any).stdout;
@@ -40,7 +49,10 @@ async function runPodInstall(
4049
*/
4150
if (stderr.includes('pod repo update') && shouldHandleRepoUpdate) {
4251
await runPodUpdate(loader);
43-
await runPodInstall(loader, false);
52+
await runPodInstall(loader, {
53+
shouldHandleRepoUpdate: false,
54+
newArchEnabled: options?.newArchEnabled,
55+
});
4456
} else {
4557
loader.fail();
4658
logger.error(stderr);
@@ -110,18 +122,14 @@ async function installCocoaPods(loader: Ora) {
110122
}
111123
}
112124

113-
async function installPods(
114-
loader?: Ora,
115-
iosFolderPath?: string,
116-
options?: PodInstallOptions,
117-
) {
125+
async function installPods(loader?: Ora, options?: PodInstallOptions) {
118126
loader = loader || new NoopLoader();
119127
try {
120-
if (!iosFolderPath && !fs.existsSync('ios')) {
128+
if (!options?.iosFolderPath && !fs.existsSync('ios')) {
121129
return;
122130
}
123131

124-
process.chdir(iosFolderPath ?? 'ios');
132+
process.chdir(options?.iosFolderPath ?? 'ios');
125133

126134
const hasPods = fs.existsSync('Podfile');
127135

@@ -143,7 +151,7 @@ async function installPods(
143151
await installCocoaPods(loader);
144152
}
145153

146-
await runPodInstall(loader);
154+
await runPodInstall(loader, {newArchEnabled: options?.newArchEnabled});
147155
} finally {
148156
process.chdir('..');
149157
}

packages/cli-platform-ios/src/tools/pods.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616

1717
interface ResolvePodsOptions {
1818
forceInstall?: boolean;
19+
newArchEnabled?: boolean;
1920
}
2021

2122
interface NativeDependencies {
@@ -65,8 +66,9 @@ async function install(
6566
) {
6667
const loader = getLoader('Installing CocoaPods...');
6768
try {
68-
await installPods(loader, iosFolderPath, {
69+
await installPods(loader, {
6970
skipBundleInstall: !!cachedDependenciesHash,
71+
iosFolderPath,
7072
});
7173
cacheManager.set(packageJson.name, 'dependencies', currentDependenciesHash);
7274
loader.succeed();
@@ -114,11 +116,26 @@ export default async function resolvePods(
114116
!compareMd5Hashes(currentDependenciesHash, cachedDependenciesHash) ||
115117
!arePodsInstalled
116118
) {
117-
await install(
118-
packageJson,
119-
cachedDependenciesHash,
120-
currentDependenciesHash,
121-
iosFolderPath,
122-
);
119+
const loader = getLoader('Installing CocoaPods...');
120+
try {
121+
await installPods(loader, {
122+
skipBundleInstall: !!cachedDependenciesHash,
123+
newArchEnabled: options?.newArchEnabled,
124+
iosFolderPath,
125+
});
126+
cacheManager.set(
127+
packageJson.name,
128+
'dependencies',
129+
currentDependenciesHash,
130+
);
131+
loader.succeed();
132+
} catch {
133+
loader.fail();
134+
throw new CLIError(
135+
`Something when wrong while installing CocoaPods. Please run ${chalk.bold(
136+
'pod install',
137+
)} manually`,
138+
);
139+
}
123140
}
124141
}

0 commit comments

Comments
 (0)