8
8
getLoader ,
9
9
} from '@react-native-community/cli-tools' ;
10
10
import installPods from './installPods' ;
11
- import findPodfilePath from '../config/findPodfilePath' ;
12
11
import {
13
12
DependencyConfig ,
14
13
IOSDependencyConfig ,
@@ -61,10 +60,30 @@ export function generateMd5Hash(text: string) {
61
60
return createHash ( 'md5' ) . update ( text ) . digest ( 'hex' ) ;
62
61
}
63
62
64
- export function compareMd5Hashes ( hash1 : string , hash2 : string ) {
63
+ export function compareMd5Hashes ( hash1 ? : string , hash2 ? : string ) {
65
64
return hash1 === hash2 ;
66
65
}
67
66
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
+
68
87
async function install (
69
88
packageJson : Record < string , any > ,
70
89
cachedDependenciesHash : string | undefined ,
@@ -79,24 +98,28 @@ async function install(
79
98
} ) ;
80
99
cacheManager . set ( packageJson . name , 'dependencies' , currentDependenciesHash ) ;
81
100
loader . succeed ( ) ;
82
- } catch {
101
+ } catch ( error ) {
83
102
loader . fail ( ) ;
84
103
throw new CLIError (
85
104
`Something when wrong while installing CocoaPods. Please run ${ chalk . bold (
86
105
'pod install' ,
87
106
) } manually`,
107
+ error as Error ,
88
108
) ;
89
109
}
90
110
}
91
111
92
112
export default async function resolvePods (
93
113
root : string ,
114
+ sourceDir : string ,
94
115
nativeDependencies : NativeDependencies ,
95
116
platformName : ApplePlatform ,
96
117
options ?: ResolvePodsOptions ,
97
118
) {
98
119
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' ) ;
100
123
const platformFolderPath = podfilePath
101
124
? podfilePath . slice ( 0 , podfilePath . lastIndexOf ( '/' ) )
102
125
: path . join ( root , platformName ) ;
@@ -108,6 +131,18 @@ export default async function resolvePods(
108
131
) ;
109
132
const dependenciesString = dependenciesToString ( platformDependencies ) ;
110
133
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
+
111
146
const cachedDependenciesHash = cacheManager . get (
112
147
packageJson . name ,
113
148
'dependencies' ,
@@ -120,13 +155,20 @@ export default async function resolvePods(
120
155
currentDependenciesHash ,
121
156
platformFolderPath ,
122
157
) ;
123
- } else if ( arePodsInstalled && cachedDependenciesHash === undefined ) {
124
- cacheManager . set ( packageJson . name , 'dependencies' , currentDependenciesHash ) ;
125
158
} else if (
126
- ! cachedDependenciesHash ||
127
- ! compareMd5Hashes ( currentDependenciesHash , cachedDependenciesHash ) ||
128
- ! arePodsInstalled
159
+ arePodsInstalled &&
160
+ compareMd5Hashes ( currentDependenciesHash , cachedDependenciesHash ) &&
161
+ compareMd5Hashes ( currentPodfileHash , cachedPodfileHash ) &&
162
+ compareMd5Hashes ( currentPodfileLockChecksum , cachedPodfileLockChecksum )
129
163
) {
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 {
130
172
const loader = getLoader ( 'Installing CocoaPods...' ) ;
131
173
try {
132
174
await installPods ( loader , {
@@ -139,13 +181,22 @@ export default async function resolvePods(
139
181
'dependencies' ,
140
182
currentDependenciesHash ,
141
183
) ;
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
+ ) ;
142
192
loader . succeed ( ) ;
143
- } catch {
193
+ } catch ( error ) {
144
194
loader . fail ( ) ;
145
195
throw new CLIError (
146
196
`Something when wrong while installing CocoaPods. Please run ${ chalk . bold (
147
197
'pod install' ,
148
198
) } manually`,
199
+ error as Error ,
149
200
) ;
150
201
}
151
202
}
0 commit comments