-
Notifications
You must be signed in to change notification settings - Fork 907
/
Copy pathpods.ts
203 lines (186 loc) · 5.67 KB
/
pods.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import path from 'path';
import fs from 'fs-extra';
import {createHash} from 'crypto';
import chalk from 'chalk';
import {
CLIError,
cacheManager,
getLoader,
} from '@react-native-community/cli-tools';
import installPods from './installPods';
import {
DependencyConfig,
IOSDependencyConfig,
} from '@react-native-community/cli-types';
import {ApplePlatform} from '../types';
interface ResolvePodsOptions {
forceInstall?: boolean;
newArchEnabled?: boolean;
}
interface NativeDependencies {
[key: string]: DependencyConfig;
}
function getPackageJson(root: string) {
try {
return require(path.join(root, 'package.json'));
} catch {
throw new CLIError(
'No package.json found. Please make sure the file exists in the current folder.',
);
}
}
export function getPlatformDependencies(
dependencies: NativeDependencies,
platformName: ApplePlatform,
) {
return Object.keys(dependencies)
.filter((dependency) => dependencies[dependency].platforms?.[platformName])
.map(
(dependency) =>
`${dependency}@${
(
dependencies[dependency].platforms?.[
platformName
] as IOSDependencyConfig
).version
}`,
)
.sort();
}
export function dependenciesToString(dependencies: string[]) {
return dependencies.join('\n');
}
export function generateMd5Hash(text: string) {
return createHash('md5').update(text).digest('hex');
}
export function compareMd5Hashes(hash1?: string, hash2?: string) {
return hash1 === hash2;
}
async function getChecksum(
podfileLockPath: string,
): Promise<string | undefined> {
try {
const file = fs.readFileSync(podfileLockPath, 'utf8');
const checksumLine = file
.split('\n')
.find((line) => line.includes('PODFILE CHECKSUM'));
if (checksumLine) {
return checksumLine.split(': ')[1];
}
return undefined;
} catch {
return undefined;
}
}
async function install(
packageJson: Record<string, any>,
cachedDependenciesHash: string | undefined,
currentDependenciesHash: string,
iosFolderPath: string,
) {
const loader = getLoader('Installing CocoaPods...');
try {
await installPods(loader, {
skipBundleInstall: !!cachedDependenciesHash,
iosFolderPath,
});
cacheManager.set(packageJson.name, 'dependencies', currentDependenciesHash);
loader.succeed();
} catch (error) {
loader.fail();
throw new CLIError(
`Something when wrong while installing CocoaPods. Please run ${chalk.bold(
'pod install',
)} manually`,
error as Error,
);
}
}
export default async function resolvePods(
root: string,
sourceDir: string,
nativeDependencies: NativeDependencies,
platformName: ApplePlatform,
options?: ResolvePodsOptions,
) {
const packageJson = getPackageJson(root);
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);
const podsPath = path.join(platformFolderPath, 'Pods');
const arePodsInstalled = fs.existsSync(podsPath);
const platformDependencies = getPlatformDependencies(
nativeDependencies,
platformName,
);
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'),
);
let currentPodfileLockChecksum = await getChecksum(podfileLockPath);
const cachedPodfileHash = cacheManager.get(packageJson.name, 'podfile');
const cachedPodfileLockChecksum = cacheManager.get(
packageJson.name,
'podfileLock',
);
const cachedDependenciesHash = cacheManager.get(
packageJson.name,
'dependencies',
);
if (options?.forceInstall) {
await install(
packageJson,
cachedDependenciesHash,
currentDependenciesHash,
platformFolderPath,
);
} else if (
arePodsInstalled &&
compareMd5Hashes(currentDependenciesHash, cachedDependenciesHash) &&
compareMd5Hashes(currentPodfileHash, cachedPodfileHash) &&
compareMd5Hashes(currentPodfileLockChecksum, cachedPodfileLockChecksum)
) {
cacheManager.set(packageJson.name, 'dependencies', currentDependenciesHash);
cacheManager.set(packageJson.name, 'podfile', currentPodfileHash);
cacheManager.set(
packageJson.name,
'podfileLock',
currentPodfileLockChecksum ?? '',
);
} else {
const loader = getLoader('Installing CocoaPods...');
try {
await installPods(loader, {
skipBundleInstall: !!cachedDependenciesHash,
newArchEnabled: options?.newArchEnabled,
iosFolderPath: platformFolderPath,
});
cacheManager.set(
packageJson.name,
'dependencies',
currentDependenciesHash,
);
cacheManager.set(packageJson.name, 'podfile', currentPodfileHash);
// We need to read again the checksum because value changed after running `pod install`
currentPodfileLockChecksum = await getChecksum(podfileLockPath);
cacheManager.set(
packageJson.name,
'podfileLock',
currentPodfileLockChecksum ?? '',
);
loader.succeed();
} catch (error) {
loader.fail();
throw new CLIError(
`Something when wrong while installing CocoaPods. Please run ${chalk.bold(
'pod install',
)} manually`,
error as Error,
);
}
}
}