diff --git a/packages/cli-config/src/schema.ts b/packages/cli-config/src/schema.ts index ee4ed7074..99f9ec78e 100644 --- a/packages/cli-config/src/schema.ts +++ b/packages/cli-config/src/schema.ts @@ -156,6 +156,7 @@ export const projectConfig = t .array() .items(t.string()) .default([]), + newArchEnabled: t.bool().default(false), }) .default({}), android: t diff --git a/packages/cli-doctor/src/commands/info.ts b/packages/cli-doctor/src/commands/info.ts index 08117e2ef..e3d69eec4 100644 --- a/packages/cli-doctor/src/commands/info.ts +++ b/packages/cli-doctor/src/commands/info.ts @@ -6,7 +6,11 @@ */ import getEnvironmentInfo from '../tools/envinfo'; -import {logger, version} from '@react-native-community/cli-tools'; +import { + getArchitectureForIos, + logger, + version, +} from '@react-native-community/cli-tools'; import {Config} from '@react-native-community/cli-types'; import {readFile} from 'fs-extra'; import path from 'path'; @@ -52,16 +56,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 getArchitectureForIos( + 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..3aab96de9 100644 --- a/packages/cli-platform-ios/src/commands/buildIOS/index.ts +++ b/packages/cli-platform-ios/src/commands/buildIOS/index.ts @@ -12,12 +12,24 @@ import {BuildFlags, buildOptions} from './buildOptions'; import {getConfiguration} from './getConfiguration'; import {getXcodeProjectAndDir} from './getXcodeProjectAndDir'; import resolvePods from '../../tools/pods'; +import {getArchitectureForIos} from '@react-native-community/cli-tools'; async function buildIOS(_: Array, ctx: Config, args: BuildFlags) { const {xcodeProject, sourceDir} = getXcodeProjectAndDir(ctx.project.ios); + const isAppRunningNewArchitecture = ctx.project.ios?.sourceDir + ? await getArchitectureForIos(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, + enableNewArchitecture: + ctx.project.ios?.newArchEnabled !== undefined + ? ctx.project.ios?.newArchEnabled + : isAppRunningNewArchitecture, + isRunningNewArchitecture: 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 30f584301..e23eb872c 100644 --- a/packages/cli-platform-ios/src/commands/runIOS/index.ts +++ b/packages/cli-platform-ios/src/commands/runIOS/index.ts @@ -21,6 +21,7 @@ import { isPackagerRunning, logAlreadyRunningBundler, handlePortUnavailable, + getArchitectureForIos, } from '@react-native-community/cli-tools'; import {buildProject} from '../buildIOS/buildProject'; import {BuildFlags, buildOptions} from '../buildIOS/buildOptions'; @@ -45,11 +46,21 @@ export interface FlagsT extends BuildFlags { async function runIOS(_: Array, ctx: Config, args: FlagsT) { link.setPlatform('ios'); - let {packager, port} = args; + const isAppRunningNewArchitecture = ctx.project.ios?.sourceDir + ? await getArchitectureForIos(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, + enableNewArchitecture: + ctx.project.ios?.newArchEnabled !== undefined + ? ctx.project.ios?.newArchEnabled + : isAppRunningNewArchitecture, + isRunningNewArchitecture: isAppRunningNewArchitecture, + }); const packagerStatus = await isPackagerRunning(port); diff --git a/packages/cli-platform-ios/src/config/__tests__/getProjectConfig.test.ts b/packages/cli-platform-ios/src/config/__tests__/getProjectConfig.test.ts index a9d76a6cf..7491210a3 100644 --- a/packages/cli-platform-ios/src/config/__tests__/getProjectConfig.test.ts +++ b/packages/cli-platform-ios/src/config/__tests__/getProjectConfig.test.ts @@ -25,6 +25,7 @@ describe('ios::getProjectConfig', () => { }); expect(projectConfig('/', {})).toMatchInlineSnapshot(` Object { + "newArchEnabled": undefined, "sourceDir": "/ios", "watchModeCommandParams": undefined, "xcodeProject": null, @@ -45,6 +46,7 @@ describe('ios::getProjectConfig', () => { }); expect(projectConfig('/', {})).toMatchInlineSnapshot(` Object { + "newArchEnabled": undefined, "sourceDir": "/ios", "watchModeCommandParams": undefined, "xcodeProject": null, diff --git a/packages/cli-platform-ios/src/config/index.ts b/packages/cli-platform-ios/src/config/index.ts index 0232a4874..e0a347f7c 100644 --- a/packages/cli-platform-ios/src/config/index.ts +++ b/packages/cli-platform-ios/src/config/index.ts @@ -51,6 +51,7 @@ export function projectConfig( sourceDir, watchModeCommandParams: userConfig.watchModeCommandParams, xcodeProject, + newArchEnabled: userConfig.newArchEnabled, }; } diff --git a/packages/cli-platform-ios/src/tools/installPods.ts b/packages/cli-platform-ios/src/tools/installPods.ts index 08ecadb93..21113512e 100644 --- a/packages/cli-platform-ios/src/tools/installPods.ts +++ b/packages/cli-platform-ios/src/tools/installPods.ts @@ -13,12 +13,16 @@ import runBundleInstall from './runBundleInstall'; interface PodInstallOptions { skipBundleInstall?: boolean; + newArchEnabled?: boolean; } -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( @@ -26,7 +30,11 @@ async function runPodInstall( )}`, ); - 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 +48,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); @@ -139,7 +150,7 @@ async function installPods(loader?: Ora, options?: PodInstallOptions) { 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 cb56f1085..5fa8476c9 100644 --- a/packages/cli-platform-ios/src/tools/pods.ts +++ b/packages/cli-platform-ios/src/tools/pods.ts @@ -16,6 +16,8 @@ import { interface ResolvePodsOptions { forceInstall?: boolean; + enableNewArchitecture?: boolean; + isRunningNewArchitecture: boolean | undefined; } interface NativeDependencies { @@ -77,20 +79,30 @@ export default async function resolvePods( 'dependencies', ); + const isRunningDifferentArchitecture = + options?.enableNewArchitecture !== undefined && + options.isRunningNewArchitecture !== options?.enableNewArchitecture; + if ( !cachedDependenciesHash || !compareMd5Hashes(currentDependenciesHash, cachedDependenciesHash) || !arePodsInstalled || - options?.forceInstall + options?.forceInstall || + isRunningDifferentArchitecture ) { const loader = getLoader('Installing CocoaPods...'); try { - await installPods(loader, {skipBundleInstall: !!cachedDependenciesHash}); + await installPods(loader, { + skipBundleInstall: !!cachedDependenciesHash, + newArchEnabled: + options?.enableNewArchitecture && isRunningDifferentArchitecture, + }); cacheManager.set( packageJson.name, 'dependencies', currentDependenciesHash, ); + loader.succeed(); } catch { loader.fail(); diff --git a/packages/cli-tools/src/cacheManager.ts b/packages/cli-tools/src/cacheManager.ts index 367d7b513..863a8f5b8 100644 --- a/packages/cli-tools/src/cacheManager.ts +++ b/packages/cli-tools/src/cacheManager.ts @@ -4,7 +4,12 @@ import os from 'os'; import appDirs from 'appdirsjs'; import logger from './logger'; -type CacheKey = 'eTag' | 'lastChecked' | 'latestVersion' | 'dependencies'; +type CacheKey = + | 'eTag' + | 'lastChecked' + | 'latestVersion' + | 'dependencies' + | 'newArchEnabled'; type Cache = {[key in CacheKey]?: string}; function loadCache(name: string): Cache | undefined { diff --git a/packages/cli-tools/src/getArchitectureForIos.ts b/packages/cli-tools/src/getArchitectureForIos.ts new file mode 100644 index 000000000..3154c8693 --- /dev/null +++ b/packages/cli-tools/src/getArchitectureForIos.ts @@ -0,0 +1,14 @@ +import {readFile} from 'fs-extra'; +import path from 'path'; + +export default async function getArchitectureForIos(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-tools/src/index.ts b/packages/cli-tools/src/index.ts index d0e1b92ba..c7153b2ad 100644 --- a/packages/cli-tools/src/index.ts +++ b/packages/cli-tools/src/index.ts @@ -17,5 +17,6 @@ export {default as handlePortUnavailable} from './handlePortUnavailable'; export * from './port'; export {default as cacheManager} from './cacheManager'; export {default as runSudo} from './runSudo'; +export {default as getArchitectureForIos} from './getArchitectureForIos'; export * from './errors'; diff --git a/packages/cli-types/src/ios.ts b/packages/cli-types/src/ios.ts index 808791d44..6efa705ca 100644 --- a/packages/cli-types/src/ios.ts +++ b/packages/cli-types/src/ios.ts @@ -6,6 +6,7 @@ export interface IOSProjectParams { sourceDir?: string; watchModeCommandParams?: string[]; + newArchEnabled?: boolean; } export type IOSProjectInfo = { @@ -17,6 +18,7 @@ export interface IOSProjectConfig { sourceDir: string; xcodeProject: IOSProjectInfo | null; watchModeCommandParams?: string[]; + newArchEnabled?: boolean; } export interface IOSDependencyConfig {