-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathcocoapods-service.ts
571 lines (515 loc) · 16.2 KB
/
cocoapods-service.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import { EOL } from "os";
import * as path from "path";
import * as _ from "lodash";
import { PlatformTypes, PODFILE_NAME, NS_BASE_PODFILE } from "../constants";
import { regExpEscape, getHash } from "../common/helpers";
import { IPluginData } from "../definitions/plugins";
import {
IRubyFunction,
IProjectData,
ICocoaPodsPlatformManager,
ICocoaPodsService,
IPodfilePlatformData,
} from "../definitions/project";
import { IPlatformData } from "../definitions/platform";
import { IConfiguration, IXcconfigService } from "../declarations";
import {
IFileSystem,
IChildProcess,
IErrors,
ISpawnResult,
} from "../common/declarations";
import { injector } from "../common/yok";
export class CocoaPodsService implements ICocoaPodsService {
private static PODFILE_POST_INSTALL_SECTION_NAME = "post_install";
private static INSTALLER_BLOCK_PARAMETER_NAME = "installer";
private getCocoaPodsFromPodfile: Function;
constructor(
private $cocoaPodsPlatformManager: ICocoaPodsPlatformManager,
private $fs: IFileSystem,
private $childProcess: IChildProcess,
private $errors: IErrors,
private $logger: ILogger,
private $config: IConfiguration,
private $xcconfigService: IXcconfigService
) {
this.getCocoaPodsFromPodfile = _.memoize(
this._getCocoaPodsFromPodfile,
getHash
);
}
public getPodfileHeader(targetName: string): string {
return `use_frameworks!${EOL}${EOL}target "${targetName}" do${EOL}`;
}
public getPodfileFooter(): string {
return `${EOL}end`;
}
public getProjectPodfilePath(projectRoot: string): string {
return path.join(projectRoot, PODFILE_NAME);
}
public async executePodInstall(
projectRoot: string,
xcodeProjPath: string
): Promise<ISpawnResult> {
this.$logger.info("Installing pods...");
const podTool = this.$config.USE_POD_SANDBOX ? "sandbox-pod" : "pod";
// cocoapods print a lot of non-error information on stderr. Pipe the `stderr` to `stdout`, so we won't polute CLI's stderr output.
const podInstallResult = await this.$childProcess.spawnFromEvent(
podTool,
["install"],
"close",
{ cwd: projectRoot, stdio: ["pipe", process.stdout, process.stdout] },
{ throwError: false }
);
if (podInstallResult.exitCode !== 0) {
// https://github.com/CocoaPods/CocoaPods/blob/92aaf0f1120d32f3487960b485fb69fcaf61486c/lib/cocoapods/resolver.rb#L498
// TODO add article
const versionResolutionHint =
podInstallResult.exitCode === 31
? `For more information on resolving CocoaPod issues in NativeScript read.`
: "";
this.$errors.fail(`'${podTool} install' command failed.${
podInstallResult.stderr ? " Error is: " + podInstallResult.stderr : ""
}
${versionResolutionHint}`);
}
return podInstallResult;
}
public async mergePodXcconfigFile(
projectData: IProjectData,
platformData: IPlatformData
): Promise<void> {
const podFilesRootDirName = path.join(
"Pods",
"Target Support Files",
`Pods-${projectData.projectName}`
);
const podFolder = path.join(platformData.projectRoot, podFilesRootDirName);
if (this.$fs.exists(podFolder)) {
const pluginsXcconfigFilePaths = this.$xcconfigService.getPluginsXcconfigFilePaths(
platformData.projectRoot
);
for (const configuration in pluginsXcconfigFilePaths) {
const pluginsXcconfigFilePath = pluginsXcconfigFilePaths[configuration];
const podXcconfigFilePath = path.join(
podFolder,
`Pods-${projectData.projectName}.${configuration}.xcconfig`
);
await this.$xcconfigService.mergeFiles(
podXcconfigFilePath,
pluginsXcconfigFilePath
);
}
}
}
public async applyPodfileFromAppResources(
projectData: IProjectData,
platformData: IPlatformData
): Promise<void> {
const { projectRoot, normalizedPlatformName } = platformData;
const mainPodfilePath = path.join(
projectData.appResourcesDirectoryPath,
normalizedPlatformName,
PODFILE_NAME
);
const projectPodfilePath = this.getProjectPodfilePath(projectRoot);
if (
this.$fs.exists(projectPodfilePath) ||
this.$fs.exists(mainPodfilePath)
) {
await this.applyPodfileToProject(
NS_BASE_PODFILE,
mainPodfilePath,
projectData,
platformData
);
}
}
public async applyPodfileArchExclusions(
projectData: IProjectData,
platformData: IPlatformData
): Promise<void> {
const { projectRoot } = platformData;
const exclusionsPodfile = path.join(projectRoot, "Podfile-exclusions");
if (!this.$fs.exists(exclusionsPodfile)) {
const exclusions = `
post_install do |installer|
installer.pods_project.build_configurations.each do |config|
config.build_settings["EXCLUDED_ARCHS_x86_64"] = "arm64 arm64e"
config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "i386 armv6 armv7 armv7s armv8 $(EXCLUDED_ARCHS_$(NATIVE_ARCH_64_BIT))"
config.build_settings["EXCLUDED_ARCHS[sdk=iphoneos*]"] = "i386 armv6 armv7 armv7s armv8 x86_64"
end
end`.trim();
this.$fs.writeFile(exclusionsPodfile, exclusions);
}
await this.applyPodfileToProject(
"NativeScript-CLI-Architecture-Exclusions",
exclusionsPodfile,
projectData,
platformData
);
// clean up
this.$fs.deleteFile(exclusionsPodfile);
}
public async applyPodfileToProject(
moduleName: string,
podfilePath: string,
projectData: IProjectData,
platformData: IPlatformData
): Promise<void> {
const nativeProjectPath = platformData.projectRoot;
if (!this.$fs.exists(podfilePath)) {
this.removePodfileFromProject(
moduleName,
podfilePath,
projectData,
nativeProjectPath
);
return;
}
const {
podfileContent,
replacedFunctions,
podfilePlatformData,
} = this.buildPodfileContent(
podfilePath,
moduleName,
projectData,
platformData
);
const pathToProjectPodfile = this.getProjectPodfilePath(nativeProjectPath);
const projectPodfileContent = this.$fs.exists(pathToProjectPodfile)
? this.$fs.readText(pathToProjectPodfile).trim()
: "";
if (projectPodfileContent.indexOf(podfileContent) === -1) {
// Remove old occurences of the plugin from the project's Podfile.
this.removePodfileFromProject(
moduleName,
podfilePath,
projectData,
nativeProjectPath
);
let finalPodfileContent = this.$fs.exists(pathToProjectPodfile)
? this.getPodfileContentWithoutTarget(
projectData,
this.$fs.readText(pathToProjectPodfile)
)
: "";
if (
podfileContent.indexOf(
CocoaPodsService.PODFILE_POST_INSTALL_SECTION_NAME
) !== -1
) {
finalPodfileContent = this.addPostInstallHook(
replacedFunctions,
finalPodfileContent,
podfileContent
);
}
if (podfilePlatformData) {
finalPodfileContent = this.$cocoaPodsPlatformManager.addPlatformSection(
projectData,
podfilePlatformData,
finalPodfileContent
);
}
finalPodfileContent = `${finalPodfileContent.trim()}${EOL}${EOL}${podfileContent.trim()}${EOL}`;
this.saveProjectPodfile(
projectData,
finalPodfileContent,
nativeProjectPath
);
}
}
public removePodfileFromProject(
moduleName: string,
podfilePath: string,
projectData: IProjectData,
projectRoot: string
): void {
if (this.$fs.exists(this.getProjectPodfilePath(projectRoot))) {
let projectPodFileContent = this.$fs.readText(
this.getProjectPodfilePath(projectRoot)
);
// Remove the data between #Begin Podfile and #EndPodfile
const regExpToRemove = new RegExp(
`${this.getPluginPodfileHeader(
podfilePath
)}[\\s\\S]*?${this.getPluginPodfileEnd()}`,
"mg"
);
projectPodFileContent = projectPodFileContent.replace(regExpToRemove, "");
projectPodFileContent = this.removePostInstallHook(
moduleName,
projectPodFileContent
);
projectPodFileContent = this.$cocoaPodsPlatformManager.removePlatformSection(
moduleName,
projectPodFileContent,
podfilePath
);
const defaultPodfileBeginning = this.getPodfileHeader(
projectData.projectName
);
const defaultContentWithPostInstallHook = `${defaultPodfileBeginning}${this.getPostInstallHookHeader()}end${EOL}end`;
const defaultContentWithoutPostInstallHook = `${defaultPodfileBeginning}${EOL}end`;
const trimmedProjectPodFileContent = projectPodFileContent.trim();
if (
!trimmedProjectPodFileContent ||
trimmedProjectPodFileContent === defaultContentWithPostInstallHook ||
trimmedProjectPodFileContent === defaultContentWithoutPostInstallHook
) {
this.$fs.deleteFile(this.getProjectPodfilePath(projectRoot));
} else {
this.$fs.writeFile(
this.getProjectPodfilePath(projectRoot),
projectPodFileContent
);
}
}
}
public getPluginPodfilePath(pluginData: IPluginData): string {
const pluginPlatformsFolderPath = pluginData.pluginPlatformsFolderPath(
PlatformTypes.ios
);
const pluginPodFilePath = path.join(
pluginPlatformsFolderPath,
PODFILE_NAME
);
return pluginPodFilePath;
}
private addPostInstallHook(
replacedFunctions: IRubyFunction[],
finalPodfileContent: string,
pluginPodfileContent: string
): string {
const postInstallHookStart = this.getPostInstallHookHeader();
let postInstallHookContent = "";
_.each(replacedFunctions, (rubyFunction) => {
let functionExecution = rubyFunction.functionName;
if (
rubyFunction.functionParameters &&
rubyFunction.functionParameters.length
) {
functionExecution = `${functionExecution} ${CocoaPodsService.INSTALLER_BLOCK_PARAMETER_NAME}`;
}
postInstallHookContent += ` ${functionExecution}${EOL}`;
});
if (postInstallHookContent) {
const index = finalPodfileContent.indexOf(postInstallHookStart);
if (index !== -1) {
const regExp = new RegExp(
`(${regExpEscape(postInstallHookStart)}[\\s\\S]*?)(\\bend\\b)`,
"m"
);
finalPodfileContent = finalPodfileContent.replace(
regExp,
`$1${postInstallHookContent.trimRight()}${EOL}$2`
);
} else {
if (finalPodfileContent.length > 0) {
finalPodfileContent += `${EOL}${EOL}`;
}
const postInstallHook = `${postInstallHookStart}${postInstallHookContent}end`;
finalPodfileContent = `${finalPodfileContent}${postInstallHook}`;
}
}
return finalPodfileContent;
}
private getPodfileContentWithoutTarget(
projectData: IProjectData,
projectPodfileContent: string
): string {
const podFileHeader = this.getPodfileHeader(projectData.projectName);
if (_.startsWith(projectPodfileContent, podFileHeader)) {
projectPodfileContent = projectPodfileContent.substr(
podFileHeader.length
);
const podFileFooter = this.getPodfileFooter();
// Only remove the final end in case the file starts with the podFileHeader
if (_.endsWith(projectPodfileContent, podFileFooter)) {
projectPodfileContent = projectPodfileContent.substr(
0,
projectPodfileContent.length - podFileFooter.length
);
}
}
return projectPodfileContent.trim();
}
private saveProjectPodfile(
projectData: IProjectData,
projectPodfileContent: string,
projectRoot: string
): void {
projectPodfileContent = this.getPodfileContentWithoutTarget(
projectData,
projectPodfileContent
);
const podFileHeader = this.getPodfileHeader(projectData.projectName);
const podFileFooter = this.getPodfileFooter();
const contentToWrite = `${podFileHeader}${projectPodfileContent}${podFileFooter}`;
const projectPodfilePath = this.getProjectPodfilePath(projectRoot);
this.$fs.writeFile(projectPodfilePath, contentToWrite);
}
private removePostInstallHook(
moduleName: string,
projectPodFileContent: string
): string {
const regExp = new RegExp(
`^.*?${this.getHookBasicFuncNameForPlugin(
CocoaPodsService.PODFILE_POST_INSTALL_SECTION_NAME,
moduleName
)}.*?$\\r?\\n`,
"gm"
);
projectPodFileContent = projectPodFileContent.replace(regExp, "");
return projectPodFileContent;
}
private getHookBasicFuncNameForPlugin(
hookName: string,
pluginName: string
): string {
// nativescript-hook and nativescript_hook should have different names, so replace all _ with ___ first and then replace all special symbols with _
// This will lead to a clash in case plugins are called nativescript-hook and nativescript___hook
const replacedPluginName = pluginName
.replace(/_/g, "___")
.replace(/[^A-Za-z0-9_]/g, "_");
return `${hookName}${replacedPluginName}`;
}
private replaceHookContent(
hookName: string,
podfileContent: string,
pluginName: string
): { replacedContent: string; newFunctions: IRubyFunction[] } {
const hookStart = `${hookName} do`;
const hookDefinitionRegExp = new RegExp(
`${hookStart} *(\\|(\\w+)\\|)?`,
"g"
);
const newFunctions: IRubyFunction[] = [];
const replacedContent = podfileContent.replace(
hookDefinitionRegExp,
(
substring: string,
firstGroup: string,
secondGroup: string,
index: number
): string => {
const newFunctionName = `${this.getHookBasicFuncNameForPlugin(
hookName,
pluginName
)}_${newFunctions.length}`;
let newDefinition = `def ${newFunctionName}`;
const rubyFunction: IRubyFunction = { functionName: newFunctionName };
// firstGroup is the block parameter, secondGroup is the block parameter name.
if (firstGroup && secondGroup) {
newDefinition = `${newDefinition} (${secondGroup})`;
rubyFunction.functionParameters = secondGroup;
}
newFunctions.push(rubyFunction);
return newDefinition;
}
);
return { replacedContent, newFunctions };
}
private getPluginPodfileHeader(pluginPodFilePath: string): string {
return `# Begin Podfile - ${pluginPodFilePath}`;
}
private getPluginPodfileEnd(): string {
return `# End Podfile${EOL}`;
}
private getPostInstallHookHeader() {
return `${CocoaPodsService.PODFILE_POST_INSTALL_SECTION_NAME} do |${CocoaPodsService.INSTALLER_BLOCK_PARAMETER_NAME}|${EOL}`;
}
private buildPodfileContent(
pluginPodFilePath: string,
pluginName: string,
projectData: IProjectData,
platformData: IPlatformData
): {
podfileContent: string;
replacedFunctions: IRubyFunction[];
podfilePlatformData: IPodfilePlatformData;
} {
const pluginPodfileContent = this.$fs.readText(pluginPodFilePath);
const data = this.replaceHookContent(
CocoaPodsService.PODFILE_POST_INSTALL_SECTION_NAME,
pluginPodfileContent,
pluginName
);
const cocoapodsData = this.$cocoaPodsPlatformManager.replacePlatformRow(
data.replacedContent,
pluginPodFilePath
);
const podfilePlatformData = cocoapodsData.podfilePlatformData;
let replacedContent = cocoapodsData.replacedContent;
if (
projectData.nsConfig &&
projectData.nsConfig.overridePods &&
!this.isMainPodFile(pluginPodFilePath, projectData, platformData)
) {
replacedContent = this.overridePodsFromFile(
replacedContent,
projectData,
platformData
);
}
return {
podfileContent: `${this.getPluginPodfileHeader(
pluginPodFilePath
)}${EOL}${replacedContent}${EOL}${this.getPluginPodfileEnd()}`,
replacedFunctions: data.newFunctions,
podfilePlatformData,
};
}
private getMainPodFilePath(
projectData: IProjectData,
platformData: IPlatformData
): string {
return path.join(
projectData.appResourcesDirectoryPath,
platformData.normalizedPlatformName,
PODFILE_NAME
);
}
private isMainPodFile(
podFilePath: string,
projectData: IProjectData,
platformData: IPlatformData
): boolean {
const mainPodfilePath = this.getMainPodFilePath(projectData, platformData);
return podFilePath === mainPodfilePath;
}
private overridePodsFromFile(
podfileContent: string,
projectData: IProjectData,
platformData: IPlatformData
): string {
const mainPodfilePath = this.getMainPodFilePath(projectData, platformData);
if (this.$fs.exists(mainPodfilePath)) {
const mainPodfileContent = this.$fs.readText(mainPodfilePath);
const pods = this.getCocoaPodsFromPodfile(mainPodfileContent);
_.forEach(pods, (pod) => {
podfileContent = podfileContent.replace(
new RegExp(`^[ ]*pod\\s*["']${pod}['"].*$`, "gm"),
"#$&"
);
});
}
return podfileContent;
}
private _getCocoaPodsFromPodfile(podfileContent: string): Array<string> {
const pods = [];
const podsRegex = /^\s*pod\s*["'](.*?)['"].*$/gm;
let match = podsRegex.exec(podfileContent);
while (match != null) {
const podName: string = match[1];
if (podName) {
pods.push(podName);
}
match = podsRegex.exec(podfileContent);
}
return pods;
}
}
injector.register("cocoapodsService", CocoaPodsService);