Skip to content

Commit

Permalink
fix(apple): compare also Podfile and Podfile.lock when deciding t…
Browse files Browse the repository at this point in the history
…o install Cocoapods
  • Loading branch information
szymonrybczak committed Jul 3, 2024
1 parent 767f9dc commit f76df9f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 10 additions & 4 deletions packages/cli-platform-apple/src/commands/runCommand/createRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
33 changes: 28 additions & 5 deletions packages/cli-platform-apple/src/tools/pods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
getLoader,
} from '@react-native-community/cli-tools';
import installPods from './installPods';
import findPodfilePath from '../config/findPodfilePath';
import {
DependencyConfig,
IOSDependencyConfig,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -108,23 +110,44 @@ export default async function resolvePods(
);
const dependenciesString = dependenciesToString(platformDependencies);
const currentDependenciesHash = generateMd5Hash(dependenciesString);
// Users can manually add dependencies to Podfile, so we can entirely rely on `dependencies` from `config`'s output.
const currentPodfileHash = fs.existsSync(podfilePath)
? generateMd5Hash(fs.readFileSync(podfilePath, 'utf8'))
: undefined;
const currentPodfileLockHash = fs.existsSync(podfileLockPath)
? generateMd5Hash(fs.readFileSync(podfileLockPath, 'utf8'))
: undefined;

const cachedPodfileHash = cacheManager.get(packageJson.name, 'podfile');
const cachedPodfileLockHash = cacheManager.get(
packageJson.name,
'podfileLock',
);

const cachedDependenciesHash = cacheManager.get(
packageJson.name,
'dependencies',
);

const isCacheValid =
cachedDependenciesHash === undefined &&
cachedPodfileHash === undefined &&
cachedPodfileLockHash === undefined;

if (options?.forceInstall) {
await install(
packageJson,
cachedDependenciesHash,
currentDependenciesHash,
platformFolderPath,
);
} else if (arePodsInstalled && cachedDependenciesHash === undefined) {
} else if (arePodsInstalled && isCacheValid) {
cacheManager.set(packageJson.name, 'dependencies', currentDependenciesHash);
} else if (
!cachedDependenciesHash ||
!isCacheValid ||
!compareMd5Hashes(currentDependenciesHash, cachedDependenciesHash) ||
!compareMd5Hashes(currentPodfileHash, cachedPodfileHash) ||
!compareMd5Hashes(currentPodfileLockHash, cachedPodfileLockHash) ||
!arePodsInstalled
) {
const loader = getLoader('Installing CocoaPods...');
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-tools/src/cacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type CacheKey =
| 'lastChecked'
| 'latestVersion'
| 'dependencies'
| 'podfile'
| 'podfileLock'
| 'lastUsedIOSDeviceId';
type Cache = {[key in CacheKey]?: string};

Expand Down

0 comments on commit f76df9f

Please sign in to comment.