Skip to content

Commit f76df9f

Browse files
committed
fix(apple): compare also Podfile and Podfile.lock when deciding to install Cocoapods
1 parent 767f9dc commit f76df9f

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

packages/cli-platform-apple/src/commands/buildCommand/createBuild.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@ const createBuild =
2727
? await getArchitecture(platformConfig.sourceDir)
2828
: undefined;
2929

30-
await resolvePods(ctx.root, ctx.dependencies, platformName, {
31-
forceInstall: args.forcePods,
32-
newArchEnabled: isAppRunningNewArchitecture,
33-
});
30+
await resolvePods(
31+
ctx.root,
32+
platformConfig.sourceDir,
33+
ctx.dependencies,
34+
platformName,
35+
{
36+
forceInstall: args.forcePods,
37+
newArchEnabled: isAppRunningNewArchitecture,
38+
},
39+
);
3440

3541
installedPods = true;
3642
}

packages/cli-platform-apple/src/commands/runCommand/createRun.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,16 @@ const createRun =
7272
? await getArchitecture(platformConfig.sourceDir)
7373
: undefined;
7474

75-
await resolvePods(ctx.root, ctx.dependencies, platformName, {
76-
forceInstall: args.forcePods,
77-
newArchEnabled: isAppRunningNewArchitecture,
78-
});
75+
await resolvePods(
76+
ctx.root,
77+
platformConfig.sourceDir,
78+
ctx.dependencies,
79+
platformName,
80+
{
81+
forceInstall: args.forcePods,
82+
newArchEnabled: isAppRunningNewArchitecture,
83+
},
84+
);
7985

8086
installedPods = true;
8187
}

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

+28-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
getLoader,
99
} from '@react-native-community/cli-tools';
1010
import installPods from './installPods';
11-
import findPodfilePath from '../config/findPodfilePath';
1211
import {
1312
DependencyConfig,
1413
IOSDependencyConfig,
@@ -61,7 +60,7 @@ export function generateMd5Hash(text: string) {
6160
return createHash('md5').update(text).digest('hex');
6261
}
6362

64-
export function compareMd5Hashes(hash1: string, hash2: string) {
63+
export function compareMd5Hashes(hash1?: string, hash2?: string) {
6564
return hash1 === hash2;
6665
}
6766

@@ -91,12 +90,15 @@ async function install(
9190

9291
export default async function resolvePods(
9392
root: string,
93+
sourceDir: string,
9494
nativeDependencies: NativeDependencies,
9595
platformName: ApplePlatform,
9696
options?: ResolvePodsOptions,
9797
) {
9898
const packageJson = getPackageJson(root);
99-
const podfilePath = findPodfilePath(root, platformName);
99+
const podfilePath = path.join(sourceDir, 'Podfile'); // sourceDir is calculated based on Podfile location, see getProjectConfig()
100+
101+
const podfileLockPath = path.join(sourceDir, 'Podfile.lock');
100102
const platformFolderPath = podfilePath
101103
? podfilePath.slice(0, podfilePath.lastIndexOf('/'))
102104
: path.join(root, platformName);
@@ -108,23 +110,44 @@ export default async function resolvePods(
108110
);
109111
const dependenciesString = dependenciesToString(platformDependencies);
110112
const currentDependenciesHash = generateMd5Hash(dependenciesString);
113+
// Users can manually add dependencies to Podfile, so we can entirely rely on `dependencies` from `config`'s output.
114+
const currentPodfileHash = fs.existsSync(podfilePath)
115+
? generateMd5Hash(fs.readFileSync(podfilePath, 'utf8'))
116+
: undefined;
117+
const currentPodfileLockHash = fs.existsSync(podfileLockPath)
118+
? generateMd5Hash(fs.readFileSync(podfileLockPath, 'utf8'))
119+
: undefined;
120+
121+
const cachedPodfileHash = cacheManager.get(packageJson.name, 'podfile');
122+
const cachedPodfileLockHash = cacheManager.get(
123+
packageJson.name,
124+
'podfileLock',
125+
);
126+
111127
const cachedDependenciesHash = cacheManager.get(
112128
packageJson.name,
113129
'dependencies',
114130
);
115131

132+
const isCacheValid =
133+
cachedDependenciesHash === undefined &&
134+
cachedPodfileHash === undefined &&
135+
cachedPodfileLockHash === undefined;
136+
116137
if (options?.forceInstall) {
117138
await install(
118139
packageJson,
119140
cachedDependenciesHash,
120141
currentDependenciesHash,
121142
platformFolderPath,
122143
);
123-
} else if (arePodsInstalled && cachedDependenciesHash === undefined) {
144+
} else if (arePodsInstalled && isCacheValid) {
124145
cacheManager.set(packageJson.name, 'dependencies', currentDependenciesHash);
125146
} else if (
126-
!cachedDependenciesHash ||
147+
!isCacheValid ||
127148
!compareMd5Hashes(currentDependenciesHash, cachedDependenciesHash) ||
149+
!compareMd5Hashes(currentPodfileHash, cachedPodfileHash) ||
150+
!compareMd5Hashes(currentPodfileLockHash, cachedPodfileLockHash) ||
128151
!arePodsInstalled
129152
) {
130153
const loader = getLoader('Installing CocoaPods...');

packages/cli-tools/src/cacheManager.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type CacheKey =
1010
| 'lastChecked'
1111
| 'latestVersion'
1212
| 'dependencies'
13+
| 'podfile'
14+
| 'podfileLock'
1315
| 'lastUsedIOSDeviceId';
1416
type Cache = {[key in CacheKey]?: string};
1517

0 commit comments

Comments
 (0)