-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-project-service.ts
613 lines (506 loc) · 27 KB
/
ios-project-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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
///<reference path="../.d.ts"/>
"use strict";
import * as path from "path";
import * as shell from "shelljs";
import * as util from "util";
import * as os from "os";
import * as semver from "semver";
import * as xcode from "xcode";
import * as constants from "../constants";
import * as helpers from "../common/helpers";
import * as projectServiceBaseLib from "./platform-project-service-base";
export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServiceBase implements IPlatformProjectService {
private static XCODE_PROJECT_EXT_NAME = ".xcodeproj";
private static XCODEBUILD_MIN_VERSION = "6.0";
private static IOS_PROJECT_NAME_PLACEHOLDER = "__PROJECT_NAME__";
private static IOS_PLATFORM_NAME = "ios";
private static PODFILE_POST_INSTALL_SECTION_NAME = "post_install";
private get $npmInstallationManager(): INpmInstallationManager {
return this.$injector.resolve("npmInstallationManager");
}
constructor(private $projectData: IProjectData,
$fs: IFileSystem,
private $childProcess: IChildProcess,
private $errors: IErrors,
private $logger: ILogger,
private $iOSEmulatorServices: Mobile.IEmulatorPlatformServices,
private $options: IOptions,
private $injector: IInjector,
private $projectDataService: IProjectDataService) {
super($fs);
}
public get platformData(): IPlatformData {
let projectRoot = path.join(this.$projectData.platformsDir, "ios");
return {
frameworkPackageName: "tns-ios",
normalizedPlatformName: "iOS",
appDestinationDirectoryPath: path.join(projectRoot, this.$projectData.projectName),
platformProjectService: this,
emulatorServices: this.$iOSEmulatorServices,
projectRoot: projectRoot,
deviceBuildOutputPath: path.join(projectRoot, "build", "device"),
emulatorBuildOutputPath: path.join(projectRoot, "build", "emulator"),
validPackageNamesForDevice: [
this.$projectData.projectName + ".ipa"
],
validPackageNamesForEmulator: [
this.$projectData.projectName + ".app"
],
frameworkFilesExtensions: [".a", ".framework", ".bin"],
frameworkDirectoriesExtensions: [".framework"],
frameworkDirectoriesNames: ["Metadata", "metadataGenerator"],
targetedOS: ['darwin'],
configurationFileName: "Info.plist",
configurationFilePath: path.join(projectRoot, this.$projectData.projectName, this.$projectData.projectName+"-Info.plist"),
mergeXmlConfig: [{ "nodename": "plist", "attrname": "*" }, {"nodename": "dict", "attrname": "*"}]
};
}
public getAppResourcesDestinationDirectoryPath(): IFuture<string> {
return (() => {
this.$projectDataService.initialize(this.$projectData.projectDir);
let frameworkVersion = this.$projectDataService.getValue(this.platformData.frameworkPackageName).wait()["version"];
if(semver.lt(frameworkVersion, "1.3.0")) {
return path.join(this.platformData.projectRoot, this.$projectData.projectName, "Resources", "icons");
}
return path.join(this.platformData.projectRoot, this.$projectData.projectName, "Resources");
}).future<string>()();
}
public validate(): IFuture<void> {
return (() => {
try {
this.$childProcess.exec("which xcodebuild").wait();
} catch(error) {
this.$errors.fail("Xcode is not installed. Make sure you have Xcode installed and added to your PATH");
}
let xcodeBuildVersion = this.$childProcess.exec("xcodebuild -version | head -n 1 | sed -e 's/Xcode //'").wait();
let splitedXcodeBuildVersion = xcodeBuildVersion.split(".");
if(splitedXcodeBuildVersion.length === 3) {
xcodeBuildVersion = util.format("%s.%s", splitedXcodeBuildVersion[0], splitedXcodeBuildVersion[1]);
}
if(helpers.versionCompare(xcodeBuildVersion, IOSProjectService.XCODEBUILD_MIN_VERSION) < 0) {
this.$errors.fail("NativeScript can only run in Xcode version %s or greater", IOSProjectService.XCODEBUILD_MIN_VERSION);
}
}).future<void>()();
}
public createProject(projectRoot: string, frameworkDir: string): IFuture<void> {
return (() => {
this.$fs.ensureDirectoryExists(path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER)).wait();
if(this.$options.symlink) {
let xcodeProjectName = util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER);
shell.cp("-R", path.join(frameworkDir, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, "*"), path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER));
shell.cp("-R", path.join(frameworkDir, xcodeProjectName), projectRoot);
let directoryContent = this.$fs.readDirectory(frameworkDir).wait();
let frameworkFiles = _.difference(directoryContent, [IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, xcodeProjectName]);
_.each(frameworkFiles, (file: string) => {
this.$fs.symlink(path.join(frameworkDir, file), path.join(projectRoot, file)).wait();
});
} else {
shell.cp("-R", path.join(frameworkDir, "*"), projectRoot);
}
}).future<void>()();
}
public interpolateData(projectRoot: string): IFuture<void> {
return (() => {
let infoPlistFilePath = path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, util.format("%s-%s", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, "Info.plist"));
shell.sed('-i', "__CFBUNDLEIDENTIFIER__", this.$projectData.projectId, infoPlistFilePath);
this.replaceFileName("-Info.plist", path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER)).wait();
this.replaceFileName("-Prefix.pch", path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER)).wait();
this.replaceFileName(IOSProjectService.XCODE_PROJECT_EXT_NAME, projectRoot).wait();
let pbxprojFilePath = path.join(projectRoot, this.$projectData.projectName + IOSProjectService.XCODE_PROJECT_EXT_NAME, "project.pbxproj");
this.replaceFileContent(pbxprojFilePath).wait();
let mainFilePath = path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, "main.m");
this.replaceFileContent(mainFilePath).wait();
}).future<void>()();
}
public afterCreateProject(projectRoot: string): IFuture<void> {
return (() => {
this.$fs.rename(path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER),
path.join(projectRoot, this.$projectData.projectName)).wait();
}).future<void>()();
}
public buildProject(projectRoot: string): IFuture<void> {
return (() => {
let basicArgs = [
"-configuration", this.$options.release ? "Release" : "Debug",
"build",
'SHARED_PRECOMPS_DIR=' + path.join(projectRoot, 'build', 'sharedpch')
];
let xcworkspacePath = path.join(projectRoot, this.$projectData.projectName + ".xcworkspace");
if(this.$fs.exists(xcworkspacePath).wait()) {
basicArgs.push("-workspace", xcworkspacePath);
basicArgs.push("-scheme", this.$projectData.projectName);
} else {
basicArgs.push("-project", path.join(projectRoot, this.$projectData.projectName + ".xcodeproj"));
basicArgs.push("-target", this.$projectData.projectName);
}
let args: string[] = [];
if(this.$options.forDevice) {
args = basicArgs.concat([
"-xcconfig", path.join(projectRoot, this.$projectData.projectName, "build.xcconfig"),
"-sdk", "iphoneos",
'ARCHS=armv7 arm64',
'VALID_ARCHS=armv7 arm64',
"CONFIGURATION_BUILD_DIR=" + path.join(projectRoot, "build", "device")
]);
} else {
args = basicArgs.concat([
"-sdk", "iphonesimulator",
"-arch", "i386",
"VALID_ARCHS=\"i386\"",
"CONFIGURATION_BUILD_DIR=" + path.join(projectRoot, "build", "emulator")
]);
}
this.$childProcess.spawnFromEvent("xcodebuild", args, "exit", {cwd: this.$options, stdio: 'inherit'}).wait();
if(this.$options.forDevice) {
let buildOutputPath = path.join(projectRoot, "build", "device");
// Produce ipa file
let xcrunArgs = [
"-sdk", "iphoneos",
"PackageApplication",
"-v", path.join(buildOutputPath, this.$projectData.projectName + ".app"),
"-o", path.join(buildOutputPath, this.$projectData.projectName + ".ipa")
];
this.$childProcess.spawnFromEvent("xcrun", xcrunArgs, "exit", {cwd: this.$options, stdio: 'inherit'}).wait();
}
}).future<void>()();
}
public isPlatformPrepared(projectRoot: string): IFuture<boolean> {
return this.$fs.exists(path.join(projectRoot, this.$projectData.projectName, constants.APP_FOLDER_NAME));
}
public addLibrary(libraryPath: string): IFuture<void> {
return (() => {
let extension = path.extname(libraryPath);
if (extension === ".framework") {
this.addDynamicFramework(libraryPath);
} else {
this.$errors.failWithoutHelp(`The bundle at ${libraryPath} does not appear to be a dynamic framework package.`);
}
}).future<void>()();
}
private addDynamicFramework(frameworkPath: string): IFuture<void> {
return (() => {
this.validateFramework(frameworkPath).wait();
let targetPath = path.join("lib", this.platformData.normalizedPlatformName);
let fullTargetPath = path.join(this.$projectData.projectDir, targetPath);
this.$fs.ensureDirectoryExists(fullTargetPath).wait();
shell.cp("-R", frameworkPath, fullTargetPath);
let project = this.createPbxProj();
let frameworkName = path.basename(frameworkPath, path.extname(frameworkPath));
let frameworkBinaryPath = path.join(frameworkPath, frameworkName);
let isDynamic = _.contains(this.$childProcess.exec(`otool -Vh ${frameworkBinaryPath}`).wait(), " DYLIB ");
let frameworkAddOptions: xcode.Options = { customFramework: true };
if(isDynamic) {
frameworkAddOptions["embed"] = true;
project.updateBuildProperty("IPHONEOS_DEPLOYMENT_TARGET", "8.0");
this.$logger.info("The iOS Deployment Target is now 8.0 in order to support Cocoa Touch Frameworks.");
}
let frameworkRelativePath = this.getLibSubpathRelativeToProjectPath(path.basename(frameworkPath));
project.addFramework(frameworkRelativePath, frameworkAddOptions);
this.savePbxProj(project).wait();
}).future<void>()();
}
private addStaticLibrary(staticLibPath: string): IFuture<void> {
return (() => {
this.validateStaticLibrary(staticLibPath).wait();
// Copy files to lib folder.
let libraryName = path.basename(staticLibPath, ".a");
let libDestinationPath = path.join(this.$projectData.projectDir, path.join("lib", this.platformData.normalizedPlatformName));
let headersSubpath = path.join("include", libraryName);
this.$fs.ensureDirectoryExists(path.join(libDestinationPath, headersSubpath)).wait();
shell.cp("-Rf", staticLibPath, libDestinationPath);
shell.cp("-Rf", path.join(path.dirname(staticLibPath), headersSubpath), path.join(libDestinationPath, "include"));
// Add static library to project file and setup header search paths
let project = this.createPbxProj();
let relativeStaticLibPath = this.getLibSubpathRelativeToProjectPath(path.basename(staticLibPath));
project.addFramework(relativeStaticLibPath);
let relativeHeaderSearchPath = path.join(this.getLibSubpathRelativeToProjectPath(headersSubpath));
project.addToHeaderSearchPaths({ relativePath: relativeHeaderSearchPath });
this.generateMobulemap(path.join(libDestinationPath, headersSubpath), libraryName);
this.savePbxProj(project).wait();
}).future<void>()();
}
public canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean> {
return (() => {
let currentXcodeProjectFile = this.buildPathToXcodeProjectFile(currentVersion);
let currentXcodeProjectFileContent = this.$fs.readFile(currentXcodeProjectFile).wait();
let newXcodeProjectFile = this.buildPathToXcodeProjectFile(newVersion);
let newXcodeProjectFileContent = this.$fs.readFile(newXcodeProjectFile).wait();
return currentXcodeProjectFileContent === newXcodeProjectFileContent;
}).future<boolean>()();
}
public updatePlatform(currentVersion: string, newVersion: string): IFuture<void> {
return (() => {
// Copy old file to options["profile-dir"]
let sourceFile = path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName));
let destinationFile = path.join(this.$options.profileDir, "xcodeproj");
this.$fs.deleteDirectory(destinationFile).wait();
shell.cp("-R", path.join(sourceFile, "*"), destinationFile);
this.$logger.info("Backup file %s at location %s", sourceFile, destinationFile);
this.$fs.deleteDirectory(path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName))).wait();
// Copy xcodeProject file
let cachedPackagePath = path.join(this.$npmInstallationManager.getCachedPackagePath(this.platformData.frameworkPackageName, newVersion), constants.PROJECT_FRAMEWORK_FOLDER_NAME, util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER));
shell.cp("-R", path.join(cachedPackagePath, "*"), path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName)));
this.$logger.info("Copied from %s at %s.", cachedPackagePath, this.platformData.projectRoot);
let pbxprojFilePath = path.join(this.platformData.projectRoot, this.$projectData.projectName + IOSProjectService.XCODE_PROJECT_EXT_NAME, "project.pbxproj");
this.replaceFileContent(pbxprojFilePath).wait();
}).future<void>()();
}
public prepareProject(): IFuture<void> {
return (() => {
let project = this.createPbxProj();
let resources = project.pbxGroupByName("Resources");
if(resources) {
let references = project.pbxFileReferenceSection();
let xcodeProjectImages = _.map(<any[]>resources.children, resource => this.replace(references[resource.value].name));
this.$logger.trace("Images from Xcode project");
this.$logger.trace(xcodeProjectImages);
let appResourcesImages = this.$fs.readDirectory(this.getAppResourcesDestinationDirectoryPath().wait()).wait();
this.$logger.trace("Current images from App_Resources");
this.$logger.trace(appResourcesImages);
let imagesToAdd = _.difference(appResourcesImages, xcodeProjectImages);
this.$logger.trace(`New images to add into xcode project: ${imagesToAdd.join(", ")}`);
_.each(imagesToAdd, image => project.addResourceFile(path.relative(this.platformData.projectRoot, path.join(this.getAppResourcesDestinationDirectoryPath().wait(), image))));
let imagesToRemove = _.difference(xcodeProjectImages, appResourcesImages);
this.$logger.trace(`Images to remove from xcode project: ${imagesToRemove.join(", ")}`);
_.each(imagesToRemove, image => project.removeResourceFile(path.join(this.getAppResourcesDestinationDirectoryPath().wait(), image)));
this.savePbxProj(project).wait();
}
}).future<void>()();
}
public prepareAppResources(appResourcesDirectoryPath: string): IFuture<void> {
return (() => {
this.$fs.deleteDirectory(this.getAppResourcesDestinationDirectoryPath().wait()).wait();
}).future<void>()();
}
private get projectPodFilePath(): string {
return path.join(this.platformData.projectRoot, "Podfile");
}
private get projectXcconfigFilePath(): string {
return path.join(this.platformData.appDestinationDirectoryPath, "build.xcconfig");
}
private replace(name: string): string {
if(_.startsWith(name, '"')) {
name = name.substr(1, name.length-2);
}
return name.replace(/\\\"/g, "\"");
}
private getLibSubpathRelativeToProjectPath(subPath: string): string {
let targetPath = path.join("lib", this.platformData.normalizedPlatformName);
let frameworkPath = path.relative("platforms/ios", path.join(targetPath, subPath));
return frameworkPath;
}
private get pbxProjPath(): string {
return path.join(this.platformData.projectRoot, this.$projectData.projectName + ".xcodeproj", "project.pbxproj");
}
private createPbxProj(): any {
let project = new xcode.project(this.pbxProjPath);
project.parseSync();
return project;
}
private savePbxProj(project: any): IFuture<void> {
return this.$fs.writeFile(this.pbxProjPath, project.writeSync());
}
public preparePluginNativeCode(pluginData: IPluginData, opts?: any): IFuture<void> {
return (() => {
let pluginPlatformsFolderPath = pluginData.pluginPlatformsFolderPath(IOSProjectService.IOS_PLATFORM_NAME);
this.prepareFrameworks(pluginPlatformsFolderPath, pluginData).wait();
this.prepareStaticLibs(pluginPlatformsFolderPath, pluginData).wait();
this.prepareCocoapods(pluginPlatformsFolderPath).wait();
this.prepareXcconfigFile(pluginPlatformsFolderPath).wait();
}).future<void>()();
}
public removePluginNativeCode(pluginData: IPluginData): IFuture<void> {
return (() => {
let pluginPlatformsFolderPath = pluginData.pluginPlatformsFolderPath(IOSProjectService.IOS_PLATFORM_NAME);
this.removeFrameworks(pluginPlatformsFolderPath, pluginData).wait();
this.removeStaticLibs(pluginPlatformsFolderPath, pluginData).wait();
this.removeCocoapods(pluginPlatformsFolderPath).wait();
this.removeXcconfigFile(pluginPlatformsFolderPath).wait();
}).future<void>()();
}
public afterPrepareAllPlugins(): IFuture<void> {
return (() => {
if(this.$fs.exists(this.projectPodFilePath).wait()) {
// Check availability
try {
this.$childProcess.exec("gem which cocoapods").wait();
this.$childProcess.exec("gem which xcodeproj").wait();
} catch(e) {
this.$errors.failWithoutHelp("CocoaPods or ruby gem 'xcodeproj' is not installed. Run `sudo gem install cocoapods` and try again.");
}
let projectPodfileContent = this.$fs.readText(this.projectPodFilePath).wait();
this.$logger.trace("Project Podfile content");
this.$logger.trace(projectPodfileContent);
let firstPostInstallIndex = projectPodfileContent.indexOf(IOSProjectService.PODFILE_POST_INSTALL_SECTION_NAME);
if(firstPostInstallIndex !== -1 && firstPostInstallIndex !== projectPodfileContent.lastIndexOf(IOSProjectService.PODFILE_POST_INSTALL_SECTION_NAME)) {
this.$logger.warn(`Podfile contains more than one post_install sections. You need to open ${this.projectPodFilePath} file and manually resolve this issue.`);
}
let pbxprojFilePath = path.join(this.platformData.projectRoot, this.$projectData.projectName + IOSProjectService.XCODE_PROJECT_EXT_NAME, "xcuserdata");
if(!this.$fs.exists(pbxprojFilePath).wait()) {
this.$logger.info("Creating project scheme...");
let createSchemeRubyScript = `ruby -e "require 'xcodeproj'; xcproj = Xcodeproj::Project.open('${this.$projectData.projectName}.xcodeproj'); xcproj.recreate_user_schemes; xcproj.save"`;
this.$childProcess.exec(createSchemeRubyScript, { cwd: this.platformData.projectRoot }).wait();
}
this.executePodInstall().wait();
}
}).future<void>()();
}
private getAllLibsForPluginWithFileExtension(pluginData: IPluginData, fileExtension: string): IFuture<string[]> {
let filterCallback = (fileName: string, pluginPlatformsFolderPath: string) => path.extname(fileName) === fileExtension;
return this.getAllNativeLibrariesForPlugin(pluginData, IOSProjectService.IOS_PLATFORM_NAME, filterCallback);
};
private buildPathToXcodeProjectFile(version: string): string {
return path.join(this.$npmInstallationManager.getCachedPackagePath(this.platformData.frameworkPackageName, version), constants.PROJECT_FRAMEWORK_FOLDER_NAME, util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER), "project.pbxproj");
}
private validateFramework(libraryPath: string): IFuture<void> {
return (() => {
let infoPlistPath = path.join(libraryPath, "Info.plist");
if (!this.$fs.exists(infoPlistPath).wait()) {
this.$errors.failWithoutHelp("The bundle at %s does not contain an Info.plist file.", libraryPath);
}
let packageType = this.$childProcess.exec(`/usr/libexec/PlistBuddy -c "Print :CFBundlePackageType" "${infoPlistPath}"`).wait().trim();
if (packageType !== "FMWK") {
this.$errors.failWithoutHelp("The bundle at %s does not appear to be a dynamic framework.", libraryPath);
}
}).future<void>()();
}
private validateStaticLibrary(libraryPath: string): IFuture<void> {
return (() => {
if (path.extname(libraryPath) !== ".a") {
this.$errors.failWithoutHelp(`The bundle at ${libraryPath} does not contain a valid static library in the '.a' file format.`);
}
let expectedArchs = ["armv7", "arm64", "i386"];
let archsInTheFatFile = this.$childProcess.exec("lipo -i " + libraryPath).wait();
expectedArchs.forEach(expectedArch => {
if (archsInTheFatFile.indexOf(expectedArch) < 0) {
this.$errors.failWithoutHelp(`The static library at ${libraryPath} is not built for one or more of the following required architectures:
${expectedArchs.join(", ")}. The static library must be built for all required architectures.`);
}
});
}).future<void>()();
}
private replaceFileContent(file: string): IFuture<void> {
return (() => {
let fileContent = this.$fs.readText(file).wait();
let replacedContent = helpers.stringReplaceAll(fileContent, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, this.$projectData.projectName);
this.$fs.writeFile(file, replacedContent).wait();
}).future<void>()();
}
private replaceFileName(fileNamePart: string, fileRootLocation: string): IFuture<void> {
return (() => {
let oldFileName = IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER + fileNamePart;
let newFileName = this.$projectData.projectName + fileNamePart;
this.$fs.rename(path.join(fileRootLocation, oldFileName), path.join(fileRootLocation, newFileName)).wait();
}).future<void>()();
}
private executePodInstall(): IFuture<any> {
this.$logger.info("Installing pods...");
return this.$childProcess.exec("pod install", { cwd: this.platformData.projectRoot });
}
private prepareFrameworks(pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> {
return (() => {
_.each(this.getAllLibsForPluginWithFileExtension(pluginData, ".framework").wait(), fileName => this.addDynamicFramework(path.join(pluginPlatformsFolderPath, fileName)).wait());
}).future<void>()();
}
private prepareStaticLibs(pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> {
return (() => {
_.each(this.getAllLibsForPluginWithFileExtension(pluginData, ".a").wait(), fileName => this.addStaticLibrary(path.join(pluginPlatformsFolderPath, fileName)).wait());
}).future<void>()();
}
private prepareCocoapods(pluginPlatformsFolderPath: string, opts?: any): IFuture<void> {
return (() => {
let pluginPodFilePath = path.join(pluginPlatformsFolderPath, "Podfile");
if(this.$fs.exists(pluginPodFilePath).wait()) {
if(!this.$fs.exists(this.projectPodFilePath).wait()) {
this.$fs.writeFile(this.projectPodFilePath, "use_frameworks!\n").wait();
}
let pluginPodFileContent = this.$fs.readText(pluginPodFilePath).wait();
let contentToWrite = this.buildPodfileContent(pluginPodFilePath, pluginPodFileContent);
this.$fs.appendFile(this.projectPodFilePath, contentToWrite).wait();
}
if(opts && opts.executePodInstall && this.$fs.exists(pluginPodFilePath).wait()) {
this.executePodInstall().wait();
}
}).future<void>()();
}
private prepareXcconfigFile(pluginPlatformsFolderPath: string): IFuture<void> {
return (() => {
let pluginXcconfigFilePath = path.join(pluginPlatformsFolderPath, "build.xcconfig");
if(this.$fs.exists(pluginXcconfigFilePath).wait()) {
let contentToWrite = this.buildXcconfigContent(pluginXcconfigFilePath);
this.$fs.appendFile(this.projectXcconfigFilePath, contentToWrite).wait();
}
}).future<void>()();
}
private removeFrameworks(pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> {
return (() => {
let project = this.createPbxProj();
_.each(this.getAllLibsForPluginWithFileExtension(pluginData, ".framework").wait(), fileName => {
let relativeFrameworkPath = this.getLibSubpathRelativeToProjectPath(fileName);
project.removeFramework(relativeFrameworkPath, { customFramework: true, embed: true });
});
this.savePbxProj(project).wait();
}).future<void>()();
}
private removeStaticLibs(pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> {
return (() => {
let project = this.createPbxProj();
_.each(this.getAllLibsForPluginWithFileExtension(pluginData, ".a").wait(), fileName => {
let staticLibPath = path.join(pluginPlatformsFolderPath, fileName);
let relativeStaticLibPath = this.getLibSubpathRelativeToProjectPath(path.basename(staticLibPath));
project.removeFramework(relativeStaticLibPath);
let headersSubpath = path.join("include", path.basename(staticLibPath, ".a"));
let relativeHeaderSearchPath = path.join(this.getLibSubpathRelativeToProjectPath(headersSubpath));
project.removeFromHeaderSearchPaths({ relativePath: relativeHeaderSearchPath });
});
this.savePbxProj(project).wait();
}).future<void>()();
}
private removeCocoapods(pluginPlatformsFolderPath: string): IFuture<void> {
return (() => {
let pluginPodFilePath = path.join(pluginPlatformsFolderPath, "Podfile");
if(this.$fs.exists(pluginPodFilePath).wait()) {
let pluginPodFileContent = this.$fs.readText(pluginPodFilePath).wait();
let projectPodFileContent = this.$fs.readText(this.projectPodFilePath).wait();
let contentToRemove= this.buildPodfileContent(pluginPodFilePath, pluginPodFileContent);
projectPodFileContent = helpers.stringReplaceAll(projectPodFileContent, contentToRemove, "");
if(projectPodFileContent.trim() === "use_frameworks!") {
this.$fs.deleteFile(this.projectPodFilePath).wait();
} else {
this.$fs.writeFile(this.projectPodFilePath, projectPodFileContent).wait();
}
}
}).future<void>()();
}
private removeXcconfigFile(pluginPlatformsFolderPath: string): IFuture<void> {
return (() => {
let pluginXcconfigFilePath = path.join(pluginPlatformsFolderPath, "build.xcconfig");
if(this.$fs.exists(pluginXcconfigFilePath).wait()) {
let projectXcconfigFileContent = this.$fs.readText(this.projectXcconfigFilePath).wait();
let contentToRemove = this.buildXcconfigContent(pluginXcconfigFilePath);
projectXcconfigFileContent = helpers.stringReplaceAll(projectXcconfigFileContent, contentToRemove, "");
this.$fs.writeFile(this.projectXcconfigFilePath, projectXcconfigFileContent).wait();
}
}).future<void>()();
}
private buildPodfileContent(pluginPodFilePath: string, pluginPodFileContent: string): string {
return `# Begin Podfile - ${pluginPodFilePath} ${os.EOL} ${pluginPodFileContent} ${os.EOL} # End Podfile ${os.EOL}`;
}
private generateMobulemap(headersFolderPath: string, libraryName: string): void {
let headersFilter = (fileName: string, containingFolderPath: string) => (path.extname(fileName) === ".h" && this.$fs.getFsStats(path.join(containingFolderPath, fileName)).wait().isFile());
let headersFolderContents = this.$fs.readDirectory(headersFolderPath).wait();
let headers = _(headersFolderContents).filter(item => headersFilter(item, headersFolderPath)).value();
if (!headers.length) {
this.$fs.deleteFile(path.join(headersFolderPath, "module.modulemap")).wait();
return;
}
headers = _.map(headers, value => `header "${value}"`);
let modulemap = `module ${libraryName} { explicit module ${libraryName} { ${headers.join(" ")} } }`;
this.$fs.writeFile(path.join(headersFolderPath, "module.modulemap"), modulemap).wait();
}
private buildXcconfigContent(xcconfigFilePath: string): string {
let relativePluginXcconfigFilePath = path.relative(path.dirname(this.projectXcconfigFilePath), xcconfigFilePath);
return `${os.EOL}#include "${relativePluginXcconfigFilePath}"${os.EOL}`;
}
}
$injector.register("iOSProjectService", IOSProjectService);