diff --git a/packages/cli-doctor/src/commands/info.ts b/packages/cli-doctor/src/commands/info.ts index 08117e2ef..9d609834a 100644 --- a/packages/cli-doctor/src/commands/info.ts +++ b/packages/cli-doctor/src/commands/info.ts @@ -8,6 +8,7 @@ import getEnvironmentInfo from '../tools/envinfo'; import {logger, version} from '@react-native-community/cli-tools'; import {Config} from '@react-native-community/cli-types'; +import {getArchitecture} from '@react-native-community/cli-platform-ios'; import {readFile} from 'fs-extra'; import path from 'path'; import {stringify} from 'yaml'; @@ -52,16 +53,11 @@ const info = async function getInfo(_argv: Array, ctx: Config) { } try { - const project = await readFile( - path.join( - ctx.project.ios.sourceDir, - '/Pods/Pods.xcodeproj/project.pbxproj', - ), + const isNewArchitecture = await getArchitecture( + ctx.project.ios.sourceDir, ); - platforms.iOS.newArchEnabled = project.includes( - '-DRCT_NEW_ARCH_ENABLED=1', - ); + platforms.iOS.newArchEnabled = isNewArchitecture; } catch { platforms.iOS.newArchEnabled = notFound; } diff --git a/packages/cli-platform-ios/src/commands/buildIOS/index.ts b/packages/cli-platform-ios/src/commands/buildIOS/index.ts index 1da8bb8f4..6c4c5b342 100644 --- a/packages/cli-platform-ios/src/commands/buildIOS/index.ts +++ b/packages/cli-platform-ios/src/commands/buildIOS/index.ts @@ -12,12 +12,20 @@ import {BuildFlags, buildOptions} from './buildOptions'; import {getConfiguration} from './getConfiguration'; import {getXcodeProjectAndDir} from './getXcodeProjectAndDir'; import resolvePods from '../../tools/pods'; +import getArchitecture from '../../tools/getArchitecture'; async function buildIOS(_: Array, ctx: Config, args: BuildFlags) { const {xcodeProject, sourceDir} = getXcodeProjectAndDir(ctx.project.ios); + const isAppRunningNewArchitecture = ctx.project.ios?.sourceDir + ? await getArchitecture(ctx.project.ios?.sourceDir) + : undefined; + // check if pods need to be installed - await resolvePods(ctx.root, ctx.dependencies, {forceInstall: args.forcePods}); + await resolvePods(ctx.root, ctx.dependencies, { + forceInstall: args.forcePods, + newArchEnabled: isAppRunningNewArchitecture, + }); process.chdir(sourceDir); diff --git a/packages/cli-platform-ios/src/commands/runIOS/index.ts b/packages/cli-platform-ios/src/commands/runIOS/index.ts index f57077367..020ba9ec8 100644 --- a/packages/cli-platform-ios/src/commands/runIOS/index.ts +++ b/packages/cli-platform-ios/src/commands/runIOS/index.ts @@ -31,6 +31,7 @@ import {promptForDeviceSelection} from '../../tools/prompts'; import getSimulators from '../../tools/getSimulators'; import {getXcodeProjectAndDir} from '../buildIOS/getXcodeProjectAndDir'; import resolvePods from '../../tools/pods'; +import getArchitecture from '../../tools/getArchitecture'; export interface FlagsT extends BuildFlags { simulator?: string; @@ -48,8 +49,15 @@ async function runIOS(_: Array, ctx: Config, args: FlagsT) { let {packager, port} = args; + const isAppRunningNewArchitecture = ctx.project.ios?.sourceDir + ? await getArchitecture(ctx.project.ios?.sourceDir) + : undefined; + // check if pods need to be installed - await resolvePods(ctx.root, ctx.dependencies, {forceInstall: args.forcePods}); + await resolvePods(ctx.root, ctx.dependencies, { + forceInstall: args.forcePods, + newArchEnabled: isAppRunningNewArchitecture, + }); const packagerStatus = await isPackagerRunning(port); diff --git a/packages/cli-platform-ios/src/index.ts b/packages/cli-platform-ios/src/index.ts index 5242bb9fe..67fccf425 100644 --- a/packages/cli-platform-ios/src/index.ts +++ b/packages/cli-platform-ios/src/index.ts @@ -4,4 +4,5 @@ export {default as commands} from './commands'; export {projectConfig, dependencyConfig, findPodfilePaths} from './config'; +export {default as getArchitecture} from './tools/getArchitecture'; export {default as installPods} from './tools/installPods'; diff --git a/packages/cli-platform-ios/src/tools/getArchitecture.ts b/packages/cli-platform-ios/src/tools/getArchitecture.ts new file mode 100644 index 000000000..cd5b1c864 --- /dev/null +++ b/packages/cli-platform-ios/src/tools/getArchitecture.ts @@ -0,0 +1,14 @@ +import {readFile} from 'fs-extra'; +import path from 'path'; + +export default async function getArchitecture(iosSourceDir: string) { + try { + const project = await readFile( + path.join(iosSourceDir, '/Pods/Pods.xcodeproj/project.pbxproj'), + ); + + return project.includes('-DRCT_NEW_ARCH_ENABLED=1'); + } catch { + return false; + } +} diff --git a/packages/cli-platform-ios/src/tools/installPods.ts b/packages/cli-platform-ios/src/tools/installPods.ts index d90f8319d..16e7a6d00 100644 --- a/packages/cli-platform-ios/src/tools/installPods.ts +++ b/packages/cli-platform-ios/src/tools/installPods.ts @@ -13,20 +13,29 @@ import runBundleInstall from './runBundleInstall'; interface PodInstallOptions { skipBundleInstall?: boolean; + newArchEnabled?: boolean; + iosFolderPath?: string; } -async function runPodInstall( - loader: Ora, - shouldHandleRepoUpdate: boolean = true, -) { +interface RunPodInstallOptions { + shouldHandleRepoUpdate?: boolean; + newArchEnabled?: boolean; +} + +async function runPodInstall(loader: Ora, options?: RunPodInstallOptions) { + const shouldHandleRepoUpdate = options?.shouldHandleRepoUpdate || true; try { loader.start( - `Installing CocoaPods dependencies ${chalk.dim( - '(this may take a few minutes)', - )}`, + `Installing CocoaPods dependencies ${chalk.bold( + options?.newArchEnabled ? 'with New Architecture' : '', + )} ${chalk.dim('(this may take a few minutes)')}`, ); - await execa('bundle', ['exec', 'pod', 'install']); + await execa('bundle', ['exec', 'pod', 'install'], { + env: { + RCT_NEW_ARCH_ENABLED: options?.newArchEnabled ? '1' : '0', + }, + }); } catch (error) { // "pod" command outputs errors to stdout (at least some of them) const stderr = (error as any).stderr || (error as any).stdout; @@ -40,7 +49,10 @@ async function runPodInstall( */ if (stderr.includes('pod repo update') && shouldHandleRepoUpdate) { await runPodUpdate(loader); - await runPodInstall(loader, false); + await runPodInstall(loader, { + shouldHandleRepoUpdate: false, + newArchEnabled: options?.newArchEnabled, + }); } else { loader.fail(); logger.error(stderr); @@ -110,18 +122,14 @@ async function installCocoaPods(loader: Ora) { } } -async function installPods( - loader?: Ora, - iosFolderPath?: string, - options?: PodInstallOptions, -) { +async function installPods(loader?: Ora, options?: PodInstallOptions) { loader = loader || new NoopLoader(); try { - if (!iosFolderPath && !fs.existsSync('ios')) { + if (!options?.iosFolderPath && !fs.existsSync('ios')) { return; } - process.chdir(iosFolderPath ?? 'ios'); + process.chdir(options?.iosFolderPath ?? 'ios'); const hasPods = fs.existsSync('Podfile'); @@ -143,7 +151,7 @@ async function installPods( await installCocoaPods(loader); } - await runPodInstall(loader); + await runPodInstall(loader, {newArchEnabled: options?.newArchEnabled}); } finally { process.chdir('..'); } diff --git a/packages/cli-platform-ios/src/tools/pods.ts b/packages/cli-platform-ios/src/tools/pods.ts index 7bc871bb3..df38114c9 100644 --- a/packages/cli-platform-ios/src/tools/pods.ts +++ b/packages/cli-platform-ios/src/tools/pods.ts @@ -16,6 +16,7 @@ import { interface ResolvePodsOptions { forceInstall?: boolean; + newArchEnabled?: boolean; } interface NativeDependencies { @@ -65,8 +66,9 @@ async function install( ) { const loader = getLoader('Installing CocoaPods...'); try { - await installPods(loader, iosFolderPath, { + await installPods(loader, { skipBundleInstall: !!cachedDependenciesHash, + iosFolderPath, }); cacheManager.set(packageJson.name, 'dependencies', currentDependenciesHash); loader.succeed(); @@ -114,11 +116,26 @@ export default async function resolvePods( !compareMd5Hashes(currentDependenciesHash, cachedDependenciesHash) || !arePodsInstalled ) { - await install( - packageJson, - cachedDependenciesHash, - currentDependenciesHash, - iosFolderPath, - ); + const loader = getLoader('Installing CocoaPods...'); + try { + await installPods(loader, { + skipBundleInstall: !!cachedDependenciesHash, + newArchEnabled: options?.newArchEnabled, + iosFolderPath, + }); + cacheManager.set( + packageJson.name, + 'dependencies', + currentDependenciesHash, + ); + loader.succeed(); + } catch { + loader.fail(); + throw new CLIError( + `Something when wrong while installing CocoaPods. Please run ${chalk.bold( + 'pod install', + )} manually`, + ); + } } }