From 5d83cdeb876f41ee05737a921ed8e4ef31a6ac5d Mon Sep 17 00:00:00 2001 From: szymonrybczak Date: Wed, 3 Jul 2024 15:32:00 +0200 Subject: [PATCH] fix(apple): compare also `Podfile` and `Podfile.lock` when deciding to install Cocoapods --- .../src/commands/buildCommand/createBuild.ts | 14 +++++-- .../src/commands/runCommand/createRun.ts | 14 +++++-- packages/cli-platform-apple/src/tools/pods.ts | 38 +++++++++++++++---- packages/cli-tools/src/cacheManager.ts | 2 + 4 files changed, 52 insertions(+), 16 deletions(-) diff --git a/packages/cli-platform-apple/src/commands/buildCommand/createBuild.ts b/packages/cli-platform-apple/src/commands/buildCommand/createBuild.ts index 3e06b6c10..795c6bb07 100644 --- a/packages/cli-platform-apple/src/commands/buildCommand/createBuild.ts +++ b/packages/cli-platform-apple/src/commands/buildCommand/createBuild.ts @@ -27,10 +27,16 @@ const createBuild = ? await getArchitecture(platformConfig.sourceDir) : undefined; - await resolvePods(ctx.root, ctx.dependencies, platformName, { - forceInstall: args.forcePods, - newArchEnabled: isAppRunningNewArchitecture, - }); + await resolvePods( + ctx.root, + platformConfig.sourceDir, + ctx.dependencies, + platformName, + { + forceInstall: args.forcePods, + newArchEnabled: isAppRunningNewArchitecture, + }, + ); installedPods = true; } diff --git a/packages/cli-platform-apple/src/commands/runCommand/createRun.ts b/packages/cli-platform-apple/src/commands/runCommand/createRun.ts index 486b9ebf5..62525725b 100644 --- a/packages/cli-platform-apple/src/commands/runCommand/createRun.ts +++ b/packages/cli-platform-apple/src/commands/runCommand/createRun.ts @@ -72,10 +72,16 @@ const createRun = ? await getArchitecture(platformConfig.sourceDir) : undefined; - await resolvePods(ctx.root, ctx.dependencies, platformName, { - forceInstall: args.forcePods, - newArchEnabled: isAppRunningNewArchitecture, - }); + await resolvePods( + ctx.root, + platformConfig.sourceDir, + ctx.dependencies, + platformName, + { + forceInstall: args.forcePods, + newArchEnabled: isAppRunningNewArchitecture, + }, + ); installedPods = true; } diff --git a/packages/cli-platform-apple/src/tools/pods.ts b/packages/cli-platform-apple/src/tools/pods.ts index 2895c3d9f..c15f4c176 100644 --- a/packages/cli-platform-apple/src/tools/pods.ts +++ b/packages/cli-platform-apple/src/tools/pods.ts @@ -8,7 +8,6 @@ import { getLoader, } from '@react-native-community/cli-tools'; import installPods from './installPods'; -import findPodfilePath from '../config/findPodfilePath'; import { DependencyConfig, IOSDependencyConfig, @@ -61,7 +60,7 @@ export function generateMd5Hash(text: string) { return createHash('md5').update(text).digest('hex'); } -export function compareMd5Hashes(hash1: string, hash2: string) { +export function compareMd5Hashes(hash1?: string, hash2?: string) { return hash1 === hash2; } @@ -91,12 +90,15 @@ async function install( export default async function resolvePods( root: string, + sourceDir: string, nativeDependencies: NativeDependencies, platformName: ApplePlatform, options?: ResolvePodsOptions, ) { const packageJson = getPackageJson(root); - const podfilePath = findPodfilePath(root, platformName); + const podfilePath = path.join(sourceDir, 'Podfile'); // sourceDir is calculated based on Podfile location, see getProjectConfig() + + const podfileLockPath = path.join(sourceDir, 'Podfile.lock'); const platformFolderPath = podfilePath ? podfilePath.slice(0, podfilePath.lastIndexOf('/')) : path.join(root, platformName); @@ -108,6 +110,20 @@ export default async function resolvePods( ); const dependenciesString = dependenciesToString(platformDependencies); const currentDependenciesHash = generateMd5Hash(dependenciesString); + // Users can manually add dependencies to Podfile, so we can't entirely rely on `dependencies` from `config`'s output. + const currentPodfileHash = generateMd5Hash( + fs.readFileSync(podfilePath, 'utf8'), + ); + const currentPodfileLockHash = generateMd5Hash( + fs.readFileSync(podfileLockPath, 'utf8'), + ); + + const cachedPodfileHash = cacheManager.get(packageJson.name, 'podfile'); + const cachedPodfileLockHash = cacheManager.get( + packageJson.name, + 'podfileLock', + ); + const cachedDependenciesHash = cacheManager.get( packageJson.name, 'dependencies', @@ -120,13 +136,17 @@ export default async function resolvePods( currentDependenciesHash, platformFolderPath, ); - } else if (arePodsInstalled && cachedDependenciesHash === undefined) { - cacheManager.set(packageJson.name, 'dependencies', currentDependenciesHash); } else if ( - !cachedDependenciesHash || - !compareMd5Hashes(currentDependenciesHash, cachedDependenciesHash) || - !arePodsInstalled + arePodsInstalled && + compareMd5Hashes(currentDependenciesHash, cachedDependenciesHash) && + compareMd5Hashes(currentPodfileHash, cachedPodfileHash) && + compareMd5Hashes(currentPodfileLockHash, cachedPodfileLockHash) ) { + cacheManager.set(packageJson.name, 'dependencies', currentDependenciesHash); + cacheManager.set(packageJson.name, 'podfile', currentPodfileHash); + cacheManager.set(packageJson.name, 'podfileLock', currentPodfileLockHash); + } else { + console.log('eh'); const loader = getLoader('Installing CocoaPods...'); try { await installPods(loader, { @@ -139,6 +159,8 @@ export default async function resolvePods( 'dependencies', currentDependenciesHash, ); + cacheManager.set(packageJson.name, 'podfile', currentPodfileHash); + cacheManager.set(packageJson.name, 'podfileLock', currentPodfileLockHash); loader.succeed(); } catch { loader.fail(); diff --git a/packages/cli-tools/src/cacheManager.ts b/packages/cli-tools/src/cacheManager.ts index 227eb674a..dbcf48afe 100644 --- a/packages/cli-tools/src/cacheManager.ts +++ b/packages/cli-tools/src/cacheManager.ts @@ -10,6 +10,8 @@ type CacheKey = | 'lastChecked' | 'latestVersion' | 'dependencies' + | 'podfile' + | 'podfileLock' | 'lastUsedIOSDeviceId'; type Cache = {[key in CacheKey]?: string};