88 getLoader ,
99} from '@react-native-community/cli-tools' ;
1010import installPods from './installPods' ;
11- import findPodfilePath from '../config/findPodfilePath' ;
1211import {
1312 DependencyConfig ,
1413 IOSDependencyConfig ,
@@ -61,10 +60,30 @@ 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
67+ async function getChecksum (
68+ podfileLockPath : string ,
69+ ) : Promise < string | undefined > {
70+ try {
71+ const file = fs . readFileSync ( podfileLockPath , 'utf8' ) ;
72+
73+ const checksumLine = file
74+ . split ( '\n' )
75+ . find ( ( line ) => line . includes ( 'PODFILE CHECKSUM' ) ) ;
76+
77+ if ( checksumLine ) {
78+ return checksumLine . split ( ': ' ) [ 1 ] ;
79+ }
80+
81+ return undefined ;
82+ } catch {
83+ return undefined ;
84+ }
85+ }
86+
6887async function install (
6988 packageJson : Record < string , any > ,
7089 cachedDependenciesHash : string | undefined ,
@@ -79,24 +98,28 @@ async function install(
7998 } ) ;
8099 cacheManager . set ( packageJson . name , 'dependencies' , currentDependenciesHash ) ;
81100 loader . succeed ( ) ;
82- } catch {
101+ } catch ( error ) {
83102 loader . fail ( ) ;
84103 throw new CLIError (
85104 `Something when wrong while installing CocoaPods. Please run ${ chalk . bold (
86105 'pod install' ,
87106 ) } manually`,
107+ error as Error ,
88108 ) ;
89109 }
90110}
91111
92112export default async function resolvePods (
93113 root : string ,
114+ sourceDir : string ,
94115 nativeDependencies : NativeDependencies ,
95116 platformName : ApplePlatform ,
96117 options ?: ResolvePodsOptions ,
97118) {
98119 const packageJson = getPackageJson ( root ) ;
99- const podfilePath = findPodfilePath ( root , platformName ) ;
120+ const podfilePath = path . join ( sourceDir , 'Podfile' ) ; // sourceDir is calculated based on Podfile location, see getProjectConfig()
121+
122+ const podfileLockPath = path . join ( sourceDir , 'Podfile.lock' ) ;
100123 const platformFolderPath = podfilePath
101124 ? podfilePath . slice ( 0 , podfilePath . lastIndexOf ( '/' ) )
102125 : path . join ( root , platformName ) ;
@@ -108,6 +131,18 @@ export default async function resolvePods(
108131 ) ;
109132 const dependenciesString = dependenciesToString ( platformDependencies ) ;
110133 const currentDependenciesHash = generateMd5Hash ( dependenciesString ) ;
134+ // Users can manually add dependencies to Podfile, so we can't entirely rely on `dependencies` from `config`'s output.
135+ const currentPodfileHash = generateMd5Hash (
136+ fs . readFileSync ( podfilePath , 'utf8' ) ,
137+ ) ;
138+ let currentPodfileLockChecksum = await getChecksum ( podfileLockPath ) ;
139+
140+ const cachedPodfileHash = cacheManager . get ( packageJson . name , 'podfile' ) ;
141+ const cachedPodfileLockChecksum = cacheManager . get (
142+ packageJson . name ,
143+ 'podfileLock' ,
144+ ) ;
145+
111146 const cachedDependenciesHash = cacheManager . get (
112147 packageJson . name ,
113148 'dependencies' ,
@@ -120,13 +155,20 @@ export default async function resolvePods(
120155 currentDependenciesHash ,
121156 platformFolderPath ,
122157 ) ;
123- } else if ( arePodsInstalled && cachedDependenciesHash === undefined ) {
124- cacheManager . set ( packageJson . name , 'dependencies' , currentDependenciesHash ) ;
125158 } else if (
126- ! cachedDependenciesHash ||
127- ! compareMd5Hashes ( currentDependenciesHash , cachedDependenciesHash ) ||
128- ! arePodsInstalled
159+ arePodsInstalled &&
160+ compareMd5Hashes ( currentDependenciesHash , cachedDependenciesHash ) &&
161+ compareMd5Hashes ( currentPodfileHash , cachedPodfileHash ) &&
162+ compareMd5Hashes ( currentPodfileLockChecksum , cachedPodfileLockChecksum )
129163 ) {
164+ cacheManager . set ( packageJson . name , 'dependencies' , currentDependenciesHash ) ;
165+ cacheManager . set ( packageJson . name , 'podfile' , currentPodfileHash ) ;
166+ cacheManager . set (
167+ packageJson . name ,
168+ 'podfileLock' ,
169+ currentPodfileLockChecksum ?? '' ,
170+ ) ;
171+ } else {
130172 const loader = getLoader ( 'Installing CocoaPods...' ) ;
131173 try {
132174 await installPods ( loader , {
@@ -139,13 +181,22 @@ export default async function resolvePods(
139181 'dependencies' ,
140182 currentDependenciesHash ,
141183 ) ;
184+ cacheManager . set ( packageJson . name , 'podfile' , currentPodfileHash ) ;
185+ // We need to read again the checksum because value changed after running `pod install`
186+ currentPodfileLockChecksum = await getChecksum ( podfileLockPath ) ;
187+ cacheManager . set (
188+ packageJson . name ,
189+ 'podfileLock' ,
190+ currentPodfileLockChecksum ?? '' ,
191+ ) ;
142192 loader . succeed ( ) ;
143- } catch {
193+ } catch ( error ) {
144194 loader . fail ( ) ;
145195 throw new CLIError (
146196 `Something when wrong while installing CocoaPods. Please run ${ chalk . bold (
147197 'pod install' ,
148198 ) } manually`,
199+ error as Error ,
149200 ) ;
150201 }
151202 }
0 commit comments