-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-project-service.ts
481 lines (408 loc) · 18.9 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
import Future = require("fibers/future");
import * as path from "path";
import * as ChildProcessLib from "../lib/common/child-process";
import * as ConfigLib from "../lib/config";
import * as ErrorsLib from "../lib/common/errors";
import * as FileSystemLib from "../lib/common/file-system";
import * as HostInfoLib from "../lib/common/host-info";
import * as iOSProjectServiceLib from "../lib/services/ios-project-service";
import {IOSProjectService} from "../lib/services/ios-project-service";
import * as LoggerLib from "../lib/common/logger";
import * as OptionsLib from "../lib/options";
import * as yok from "../lib/common/yok";
import {DevicesService} from "../lib/common/mobile/mobile-core/devices-service";
import {MobileHelper} from "../lib/common/mobile/mobile-helper";
import {Messages} from "../lib/common/messages/messages";
import {MobilePlatformsCapabilities} from "../lib/mobile-platforms-capabilities";
import {DeviceLogProvider} from "../lib/common/mobile/device-log-provider";
import {LogFilter} from "../lib/common/mobile/log-filter";
import {LoggingLevels} from "../lib/common/mobile/logging-levels";
import {DeviceDiscovery} from "../lib/common/mobile/mobile-core/device-discovery";
import {IOSDeviceDiscovery} from "../lib/common/mobile/mobile-core/ios-device-discovery";
import {AndroidDeviceDiscovery} from "../lib/common/mobile/mobile-core/android-device-discovery";
import {PluginVariablesService} from "../lib/services/plugin-variables-service";
import {PluginVariablesHelper} from "../lib/common/plugin-variables-helper";
import {Utils} from "../lib/common/utils";
import {CocoaPodsService} from "../lib/services/cocoapods-service";
import {assert} from "chai";
import temp = require("temp");
temp.track();
class IOSSimulatorDiscoveryMock extends DeviceDiscovery {
public startLookingForDevices(): IFuture<void> {
return Future.fromResult();
}
public checkForDevices(): IFuture<void> {
return Future.fromResult();
}
}
function createTestInjector(projectPath: string, projectName: string): IInjector {
let testInjector = new yok.Yok();
testInjector.register("childProcess", ChildProcessLib.ChildProcess);
testInjector.register("config", ConfigLib.Configuration);
testInjector.register("errors", ErrorsLib.Errors);
testInjector.register("fs", FileSystemLib.FileSystem);
testInjector.register("adb", {});
testInjector.register("hostInfo", HostInfoLib.HostInfo);
testInjector.register("injector", testInjector);
testInjector.register("iOSEmulatorServices", {});
testInjector.register("cocoapodsService", CocoaPodsService);
testInjector.register("iOSProjectService", iOSProjectServiceLib.IOSProjectService);
testInjector.register("logger", LoggerLib.Logger);
testInjector.register("options", OptionsLib.Options);
testInjector.register("projectData", {
platformsDir: path.join(projectPath, "platforms"),
projectName: projectName,
projectPath: projectPath,
projectFilePath: path.join(projectPath, "package.json")
});
testInjector.register("projectHelper", {});
testInjector.register("xcodeSelectService", {});
testInjector.register("staticConfig", ConfigLib.StaticConfig);
testInjector.register("projectDataService", {});
testInjector.register("prompter", {});
testInjector.register("devicePlatformsConstants", { iOS: "iOS" });
testInjector.register("devicesService", DevicesService);
testInjector.register("iOSDeviceDiscovery", IOSDeviceDiscovery);
testInjector.register("iOSSimulatorDiscovery", IOSSimulatorDiscoveryMock);
testInjector.register("iOSSimResolver", {});
testInjector.register("androidDeviceDiscovery", AndroidDeviceDiscovery);
testInjector.register("messages", Messages);
testInjector.register("mobileHelper", MobileHelper);
testInjector.register("mobilePlatformsCapabilities", MobilePlatformsCapabilities);
testInjector.register("deviceLogProvider", DeviceLogProvider);
testInjector.register("logFilter", LogFilter);
testInjector.register("loggingLevels", LoggingLevels);
testInjector.register("utils", Utils);
testInjector.register("iTunesValidator", {});
testInjector.register("xcprojService", {});
testInjector.register("pluginVariablesService", PluginVariablesService);
testInjector.register("pluginVariablesHelper", PluginVariablesHelper);
testInjector.register("androidProcessService", {});
testInjector.register("processService", {});
return testInjector;
}
function createPackageJson(testInjector: IInjector, projectPath: string, projectName: string) {
let packageJsonData = {
"name": projectName,
"version": "0.1.0",
"nativescript": {
"tns-ios": {
"version": "1.0.0"
},
"tns-android": {
"version": "1.0.0"
}
}
};
testInjector.resolve("fs").writeJson(path.join(projectPath, "package.json"), packageJsonData).wait();
}
function expectOption(args: string[], option: string, value: string, message?: string): void {
let index = args.indexOf(option);
assert.ok(index >= 0, "Expected " + option + " to be set.");
assert.ok(args.length > index + 1, "Expected " + option + " to have value");
assert.equal(args[index + 1], value, message);
}
function readOption(args: string[], option: string): string {
let index = args.indexOf(option);
assert.ok(index >= 0, "Expected " + option + " to be set.");
assert.ok(args.length > index + 1, "Expected " + option + " to have value");
return args[index + 1];
}
describe("iOSProjectService", () => {
describe("archive", () => {
function setupArchive(options?: { archivePath?: string }): { run: () => void, assert: () => void } {
let hasCustomArchivePath = options && options.archivePath;
let projectName = "projectDirectory";
let projectPath = temp.mkdirSync(projectName);
let testInjector = createTestInjector(projectPath, projectName);
let iOSProjectService = <IOSProjectService>testInjector.resolve("iOSProjectService");
let childProcess = testInjector.resolve("childProcess");
let xcodebuildExeced = false;
let archivePath: string;
childProcess.spawnFromEvent = (cmd: string, args: string[]) => {
assert.equal(cmd, "xcodebuild", "Expected iOSProjectService.archive to call xcodebuild.archive");
xcodebuildExeced = true;
if (hasCustomArchivePath) {
archivePath = path.resolve(options.archivePath);
} else {
archivePath = path.join(projectPath, "platforms", "ios", "build", "archive", projectName + ".xcarchive");
}
assert.ok(args.indexOf("archive") >= 0, "Expected xcodebuild to be executed with archive param.");
expectOption(args, "-archivePath", archivePath, hasCustomArchivePath ? "Wrong path passed to xcarchive" : "Default xcarchive path is wrong.");
expectOption(args, "-project", path.join(projectPath, "platforms", "ios", projectName + ".xcodeproj"), "Path to Xcode project is wrong.");
expectOption(args, "-scheme", projectName, "The provided scheme is wrong.");
return Future.fromResult();
};
let resultArchivePath: string;
return {
run() {
if (hasCustomArchivePath) {
resultArchivePath = iOSProjectService.archive({ archivePath: options.archivePath }).wait();
} else {
resultArchivePath = iOSProjectService.archive().wait();
}
},
assert() {
assert.ok(xcodebuildExeced, "Expected xcodebuild archive to be executed");
assert.equal(resultArchivePath, archivePath, "iOSProjectService.archive expected to return the path to the archive");
}
};
}
it("by default exports xcodearchive to platforms/ios/build/archive/<projname>.xcarchive", () => {
let setup = setupArchive();
setup.run();
setup.assert();
});
it("can pass archivePath to xcodebuild -archivePath", () => {
let setup = setupArchive({ archivePath: "myarchive.xcarchive" });
setup.run();
setup.assert();
});
});
describe("exportArchive", () => {
let noTeamPlist = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>method</key>
<string>app-store</string>
<key>uploadBitcode</key>
<false/>
<key>uploadSymbols</key>
<false/>
</dict>
</plist>`;
let myTeamPlist = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>teamID</key>
<string>MyTeam</string>
<key>method</key>
<string>app-store</string>
<key>uploadBitcode</key>
<false/>
<key>uploadSymbols</key>
<false/>
</dict>
</plist>`;
function testExportArchive(options: { teamID?: string }, expectedPlistContent: string): void {
let projectName = "projectDirectory";
let projectPath = temp.mkdirSync(projectName);
let testInjector = createTestInjector(projectPath, projectName);
let iOSProjectService = <IOSProjectService>testInjector.resolve("iOSProjectService");
let archivePath = path.join(projectPath, "platforms", "ios", "build", "archive", projectName + ".xcarchive");
let childProcess = testInjector.resolve("childProcess");
let fs = <IFileSystem>testInjector.resolve("fs");
let xcodebuildExeced = false;
childProcess.spawnFromEvent = (cmd: string, args: string[]) => {
assert.equal(cmd, "xcodebuild", "Expected xcodebuild to be called");
xcodebuildExeced = true;
assert.ok(args.indexOf("-exportArchive") >= 0, "Expected -exportArchive to be set on xcodebuild.");
expectOption(args, "-archivePath", archivePath, "Expected the -archivePath to be passed to xcodebuild.");
expectOption(args, "-exportPath", path.join(projectPath, "platforms", "ios", "build", "archive"), "Expected the -archivePath to be passed to xcodebuild.");
let plist = readOption(args, "-exportOptionsPlist");
assert.ok(plist);
let plistContent = fs.readFile(plist).wait().toString();
// There may be better way to equal property lists
assert.equal(plistContent, expectedPlistContent, "Mismatch in exportOptionsPlist content");
return Future.fromResult();
};
let resultIpa = iOSProjectService.exportArchive({ archivePath, teamID: options.teamID }).wait();
let expectedIpa = path.join(projectPath, "platforms", "ios", "build", "archive", projectName + ".ipa");
assert.equal(resultIpa, expectedIpa, "Expected IPA at the specified location");
assert.ok(xcodebuildExeced, "Expected xcodebuild to be executed");
}
it("calls xcodebuild -exportArchive to produce .IPA", () => {
testExportArchive({}, noTeamPlist);
});
it("passes the --team-id option down the xcodebuild -exportArchive throug the -exportOptionsPlist", () => {
testExportArchive({ teamID: "MyTeam" }, myTeamPlist);
});
});
});
describe("Cocoapods support", () => {
if (require("os").platform() !== "darwin") {
console.log("Skipping Cocoapods tests. They cannot work on windows");
} else {
it("adds plugin with Podfile", () => {
let projectName = "projectDirectory";
let projectPath = temp.mkdirSync(projectName);
let testInjector = createTestInjector(projectPath, projectName);
let fs: IFileSystem = testInjector.resolve("fs");
let packageJsonData = {
"name": "myProject",
"version": "0.1.0",
"nativescript": {
"id": "org.nativescript.myProject",
"tns-ios": {
"version": "1.0.0"
}
}
};
fs.writeJson(path.join(projectPath, "package.json"), packageJsonData).wait();
let platformsFolderPath = path.join(projectPath, "platforms", "ios");
fs.createDirectory(platformsFolderPath).wait();
let iOSProjectService = testInjector.resolve("iOSProjectService");
iOSProjectService.prepareFrameworks = (pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> => {
return Future.fromResult();
};
iOSProjectService.prepareStaticLibs = (pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> => {
return Future.fromResult();
};
iOSProjectService.createPbxProj = () => {
return {
updateBuildProperty: () => { return {}; },
pbxXCBuildConfigurationSection: () => { return {}; },
};
};
iOSProjectService.savePbxProj = (): IFuture<void> => Future.fromResult();
let pluginPath = temp.mkdirSync("pluginDirectory");
let pluginPlatformsFolderPath = path.join(pluginPath, "platforms", "ios");
let pluginPodfilePath = path.join(pluginPlatformsFolderPath, "Podfile");
let pluginPodfileContent = ["source 'https://github.com/CocoaPods/Specs.git'", "platform :ios, '8.1'", "pod 'GoogleMaps'"].join("\n");
fs.writeFile(pluginPodfilePath, pluginPodfileContent).wait();
let pluginData = {
pluginPlatformsFolderPath(platform: string): string {
return pluginPlatformsFolderPath;
}
};
iOSProjectService.preparePluginNativeCode(pluginData).wait();
let projectPodfilePath = path.join(platformsFolderPath, "Podfile");
assert.isTrue(fs.exists(projectPodfilePath).wait());
let actualProjectPodfileContent = fs.readText(projectPodfilePath).wait();
let expectedProjectPodfileContent = ["use_frameworks!\n",
`target "${projectName}" do`,
`# Begin Podfile - ${pluginPodfilePath} `,
` ${pluginPodfileContent} `,
" # End Podfile \n",
"end"]
.join("\n");
assert.equal(actualProjectPodfileContent, expectedProjectPodfileContent);
});
it("adds and removes plugin with Podfile", () => {
let projectName = "projectDirectory2";
let projectPath = temp.mkdirSync(projectName);
let testInjector = createTestInjector(projectPath, projectName);
let fs: IFileSystem = testInjector.resolve("fs");
let packageJsonData = {
"name": "myProject2",
"version": "0.1.0",
"nativescript": {
"id": "org.nativescript.myProject2",
"tns-ios": {
"version": "1.0.0"
}
}
};
fs.writeJson(path.join(projectPath, "package.json"), packageJsonData).wait();
let platformsFolderPath = path.join(projectPath, "platforms", "ios");
fs.createDirectory(platformsFolderPath).wait();
let iOSProjectService = testInjector.resolve("iOSProjectService");
iOSProjectService.prepareFrameworks = (pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> => {
return Future.fromResult();
};
iOSProjectService.prepareStaticLibs = (pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> => {
return Future.fromResult();
};
iOSProjectService.removeFrameworks = (pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> => {
return Future.fromResult();
};
iOSProjectService.removeStaticLibs = (pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> => {
return Future.fromResult();
};
iOSProjectService.createPbxProj = () => {
return {
updateBuildProperty: () => { return {}; },
pbxXCBuildConfigurationSection: () => { return {}; },
};
};
iOSProjectService.savePbxProj = (): IFuture<void> => Future.fromResult();
let pluginPath = temp.mkdirSync("pluginDirectory");
let pluginPlatformsFolderPath = path.join(pluginPath, "platforms", "ios");
let pluginPodfilePath = path.join(pluginPlatformsFolderPath, "Podfile");
let pluginPodfileContent = ["source 'https://github.com/CocoaPods/Specs.git'", "platform :ios, '8.1'", "pod 'GoogleMaps'"].join("\n");
fs.writeFile(pluginPodfilePath, pluginPodfileContent).wait();
let pluginData = {
pluginPlatformsFolderPath(platform: string): string {
return pluginPlatformsFolderPath;
}
};
iOSProjectService.preparePluginNativeCode(pluginData).wait();
let projectPodfilePath = path.join(platformsFolderPath, "Podfile");
assert.isTrue(fs.exists(projectPodfilePath).wait());
let actualProjectPodfileContent = fs.readText(projectPodfilePath).wait();
let expectedProjectPodfileContent = ["use_frameworks!\n",
`target "${projectName}" do`,
`# Begin Podfile - ${pluginPodfilePath} `,
` ${pluginPodfileContent} `,
" # End Podfile \n",
"end"]
.join("\n");
assert.equal(actualProjectPodfileContent, expectedProjectPodfileContent);
iOSProjectService.removePluginNativeCode(pluginData).wait();
assert.isFalse(fs.exists(projectPodfilePath).wait());
});
}
});
describe("Static libraries support", () => {
if (require("os").platform() !== "darwin") {
console.log("Skipping static library tests. They work only on darwin.");
return;
}
let projectName = "projectDirectory";
let projectPath = temp.mkdirSync(projectName);
let libraryName = "testLibrary1";
let headers = ["TestHeader1.h", "TestHeader2.h"];
let testInjector = createTestInjector(projectPath, projectName);
let fs: IFileSystem = testInjector.resolve("fs");
let staticLibraryPath = path.join(path.join(temp.mkdirSync("pluginDirectory"), "platforms", "ios"));
let staticLibraryHeadersPath = path.join(staticLibraryPath,"include", libraryName);
it("checks validation of header files", () => {
let iOSProjectService = testInjector.resolve("iOSProjectService");
fs.ensureDirectoryExists(staticLibraryHeadersPath).wait();
_.each(headers, header => { fs.writeFile(path.join(staticLibraryHeadersPath, header), "").wait(); });
// Add all header files.
fs.writeFile(path.join(staticLibraryHeadersPath, libraryName + ".a"), "").wait();
let error: any;
try {
iOSProjectService.validateStaticLibrary(path.join(staticLibraryPath, libraryName + ".a")).wait();
} catch(err) {
error = err;
}
assert.instanceOf(error, Error, "Expect to fail, the .a file is not a static library.");
});
it("checks generation of modulemaps", () => {
let iOSProjectService = testInjector.resolve("iOSProjectService");
fs.ensureDirectoryExists(staticLibraryHeadersPath).wait();
_.each(headers, header => { fs.writeFile(path.join(staticLibraryHeadersPath, header), "").wait(); });
iOSProjectService.generateModulemap(staticLibraryHeadersPath, libraryName);
// Read the generated modulemap and verify it.
let modulemap = fs.readFile(path.join(staticLibraryHeadersPath, "module.modulemap")).wait();
let headerCommands = _.map(headers, value => `header "${value}"`);
let modulemapExpectation = `module ${libraryName} { explicit module ${libraryName} { ${headerCommands.join(" ")} } }`;
assert.equal(modulemap, modulemapExpectation);
// Delete all header files. And try to regenerate modulemap.
_.each(headers, header => { fs.deleteFile(path.join(staticLibraryHeadersPath, header)).wait(); });
iOSProjectService.generateModulemap(staticLibraryHeadersPath, libraryName);
let error: any;
try {
modulemap = fs.readFile(path.join(staticLibraryHeadersPath, "module.modulemap")).wait();
} catch(err) {
error = err;
}
assert.instanceOf(error, Error, "Expect to fail, there shouldn't be a module.modulemap file.");
});
});
describe("Relative paths", () => {
it("checks for correct calculation of relative paths", () => {
let projectName = "projectDirectory";
let projectPath = temp.mkdirSync(projectName);
let subpath = path.join(projectPath, "sub", "path");
let testInjector = createTestInjector(projectPath, projectName);
createPackageJson(testInjector, projectPath, projectName);
let iOSProjectService = testInjector.resolve("iOSProjectService");
let result = iOSProjectService.getLibSubpathRelativeToProjectPath(subpath);
assert.equal(result, path.join("..", "..", "sub", "path"));
});
});