Skip to content

Commit 81be681

Browse files
committed
Improve coding style. Fix lint errors.
1 parent 4f3561b commit 81be681

File tree

3 files changed

+108
-91
lines changed

3 files changed

+108
-91
lines changed

lib/definitions/xcode.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ declare module "xcode" {
66
embed?: boolean;
77
relativePath?: string;
88
}
9-
9+
1010
class project {
1111
constructor(filename: string);
1212

lib/services/ios-project-service.ts

+39-39
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,22 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
197197
public isPlatformPrepared(projectRoot: string): IFuture<boolean> {
198198
return this.$fs.exists(path.join(projectRoot, this.$projectData.projectName, constants.APP_FOLDER_NAME));
199199
}
200-
200+
201201
public addLibrary(libraryPath: string): IFuture<void> {
202202
return (() => {
203203
let extension = path.extname(libraryPath);
204204
if (extension === ".framework") {
205205
this.addDynamicFramework(libraryPath);
206206
} else {
207-
this.$errors.failWithoutHelp("The bundle at %s does not appear to be a dynamic framework package.", libraryPath);
207+
this.$errors.failWithoutHelp(`The bundle at ${libraryPath} does not appear to be a dynamic framework package.`);
208208
}
209209
}).future<void>()();
210210
}
211-
211+
212212
private addDynamicFramework(frameworkPath: string): IFuture<void> {
213213
return (() => {
214214
this.validateFramework(frameworkPath).wait();
215-
215+
216216
let targetPath = path.join("lib", this.platformData.normalizedPlatformName);
217217
let fullTargetPath = path.join(this.$projectData.projectDir, targetPath);
218218
this.$fs.ensureDirectoryExists(fullTargetPath).wait();
@@ -232,33 +232,34 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
232232
}
233233

234234
let frameworkRelativePath = this.getLibSubpathRelativeToProjectPath(path.basename(frameworkPath));
235-
project.addFramework(frameworkRelativePath, { customFramework: true, embed: true });
235+
project.addFramework(frameworkRelativePath, frameworkAddOptions);
236236
this.savePbxProj(project).wait();
237237
}).future<void>()();
238238
}
239-
239+
240240
private addStaticLibrary(staticLibPath: string): IFuture<void> {
241241
return (() => {
242-
this.validateStaticLibrary(staticLibPath).wait();
242+
this.validateStaticLibrary(staticLibPath).wait();
243243
// Copy files to lib folder.
244+
let libraryName = path.basename(staticLibPath, ".a");
244245
let libDestinationPath = path.join(this.$projectData.projectDir, path.join("lib", this.platformData.normalizedPlatformName));
245-
let headersSubpath = path.join("include", path.basename(staticLibPath, ".a"));
246+
let headersSubpath = path.join("include", libraryName);
246247
this.$fs.ensureDirectoryExists(path.join(libDestinationPath, headersSubpath)).wait();
247248
shell.cp("-Rf", staticLibPath, libDestinationPath);
248249
shell.cp("-Rf", path.join(path.dirname(staticLibPath), headersSubpath), path.join(libDestinationPath, "include"));
249-
250+
250251
// Add static library to project file and setup header search paths
251252
let project = this.createPbxProj();
252253
let relativeStaticLibPath = this.getLibSubpathRelativeToProjectPath(path.basename(staticLibPath));
253254
project.addFramework(relativeStaticLibPath);
254-
255+
255256
let relativeHeaderSearchPath = path.join(this.getLibSubpathRelativeToProjectPath(headersSubpath));
256257
project.addToHeaderSearchPaths({ relativePath: relativeHeaderSearchPath });
257-
258-
this.generateMobulemap(path.join(libDestinationPath, headersSubpath), path.basename(staticLibPath, ".a"));
258+
259+
this.generateMobulemap(path.join(libDestinationPath, headersSubpath), libraryName);
259260
this.savePbxProj(project).wait();
260-
}).future<void>()();
261-
}
261+
}).future<void>()();
262+
}
262263

263264
public canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean> {
264265
return (() => {
@@ -364,7 +365,7 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
364365
public preparePluginNativeCode(pluginData: IPluginData, opts?: any): IFuture<void> {
365366
return (() => {
366367
let pluginPlatformsFolderPath = pluginData.pluginPlatformsFolderPath(IOSProjectService.IOS_PLATFORM_NAME);
367-
368+
368369
this.prepareFrameworks(pluginPlatformsFolderPath, pluginData).wait();
369370
this.prepareStaticLibs(pluginPlatformsFolderPath, pluginData).wait();
370371
this.prepareCocoapods(pluginPlatformsFolderPath).wait();
@@ -374,7 +375,7 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
374375
public removePluginNativeCode(pluginData: IPluginData): IFuture<void> {
375376
return (() => {
376377
let pluginPlatformsFolderPath = pluginData.pluginPlatformsFolderPath(IOSProjectService.IOS_PLATFORM_NAME);
377-
378+
378379
this.removeFrameworks(pluginPlatformsFolderPath, pluginData).wait();
379380
this.removeStaticLibs(pluginPlatformsFolderPath, pluginData).wait();
380381
this.removeCocoapods(pluginPlatformsFolderPath).wait();
@@ -417,7 +418,7 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
417418
let filterCallback = (fileName: string, pluginPlatformsFolderPath: string) => path.extname(fileName) === fileExtension;
418419
return this.getAllNativeLibrariesForPlugin(pluginData, IOSProjectService.IOS_PLATFORM_NAME, filterCallback);
419420
};
420-
421+
421422
private buildPathToXcodeProjectFile(version: string): string {
422423
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");
423424
}
@@ -435,19 +436,20 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
435436
}
436437
}).future<void>()();
437438
}
438-
439+
439440
private validateStaticLibrary(libraryPath: string): IFuture<void> {
440441
return (() => {
441442
if (path.extname(libraryPath) !== ".a") {
442-
this.$errors.failWithoutHelp("The bundle at %s does not contain valid '.a' extension.", libraryPath);
443+
this.$errors.failWithoutHelp(`The bundle at ${libraryPath} does not contain a valid static library in the '.a' file format.`);
443444
}
444-
445-
let expectedArchs = ["armv7", "arm64", "x86_64", "i386"];
445+
446+
let expectedArchs = ["armv7", "arm64", "i386"];
446447
let archsInTheFatFile = this.$childProcess.exec("lipo -i " + libraryPath).wait();
447-
448-
expectedArchs.forEach(expectedArch => {
448+
449+
expectedArchs.forEach(expectedArch => {
449450
if (archsInTheFatFile.indexOf(expectedArch) < 0) {
450-
this.$errors.failWithoutHelp("The static library at %s is not build for all required architectures - %s.", libraryPath, expectedArchs);
451+
this.$errors.failWithoutHelp(`The static library at ${libraryPath} is not built for one or more of the following required architectures:
452+
${expectedArchs.join(", ")}. The static library must be built for all required architectures.`);
451453
}
452454
});
453455
}).future<void>()();
@@ -480,7 +482,7 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
480482
_.each(this.getAllLibsForPluginWithFileExtension(pluginData, ".framework").wait(), fileName => this.addDynamicFramework(path.join(pluginPlatformsFolderPath, fileName)).wait());
481483
}).future<void>()();
482484
}
483-
485+
484486
private prepareStaticLibs(pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> {
485487
return (() => {
486488
_.each(this.getAllLibsForPluginWithFileExtension(pluginData, ".a").wait(), fileName => this.addStaticLibrary(path.join(pluginPlatformsFolderPath, fileName)).wait());
@@ -490,7 +492,7 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
490492
private prepareCocoapods(pluginPlatformsFolderPath: string, opts?: any): IFuture<void> {
491493
return (() => {
492494
let pluginPodFilePath = path.join(pluginPlatformsFolderPath, "Podfile");
493-
495+
494496
if(this.$fs.exists(pluginPodFilePath).wait()) {
495497
if(!this.$fs.exists(this.projectPodFilePath).wait()) {
496498
this.$fs.writeFile(this.projectPodFilePath, "use_frameworks!\n").wait();
@@ -518,16 +520,16 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
518520
this.savePbxProj(project).wait();
519521
}).future<void>()();
520522
}
521-
523+
522524
private removeStaticLibs(pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> {
523525
return (() => {
524526
let project = this.createPbxProj();
525-
527+
526528
_.each(this.getAllLibsForPluginWithFileExtension(pluginData, ".a").wait(), fileName => {
527529
let staticLibPath = path.join(pluginPlatformsFolderPath, fileName);
528530
let relativeStaticLibPath = this.getLibSubpathRelativeToProjectPath(path.basename(staticLibPath));
529-
project.removeFramework(relativeStaticLibPath);
530-
531+
project.removeFramework(relativeStaticLibPath);
532+
531533
let headersSubpath = path.join("include", path.basename(staticLibPath, ".a"));
532534
let relativeHeaderSearchPath = path.join(this.getLibSubpathRelativeToProjectPath(headersSubpath));
533535
project.removeFromHeaderSearchPaths({ relativePath: relativeHeaderSearchPath });
@@ -557,23 +559,21 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
557559
private buildPodfileContent(pluginPodFilePath: string, pluginPodFileContent: string): string {
558560
return `# Begin Podfile - ${pluginPodFilePath} ${os.EOL} ${pluginPodFileContent} ${os.EOL} # End Podfile ${os.EOL}`;
559561
}
560-
562+
561563
private generateMobulemap(headersFolderPath: string, libraryName: string): void {
562564
let headersFilter = (fileName: string, containingFolderPath: string) => (path.extname(fileName) === ".h" && this.$fs.getFsStats(path.join(containingFolderPath, fileName)).wait().isFile());
563565
let headersFolderContents = this.$fs.readDirectory(headersFolderPath).wait();
564-
let headers = _(headersFolderContents).filter(item => headersFilter(item, headersFolderPath)).value();
565-
566+
let headers = _(headersFolderContents).filter(item => headersFilter(item, headersFolderPath)).value();
567+
566568
if (!headers.length) {
567569
this.$fs.deleteFile(path.join(headersFolderPath, "module.modulemap")).wait();
568570
return;
569571
}
570-
571-
headers.forEach(function(currentValue, index, array) {
572-
array[index] = "header \"" + currentValue + "\"";
573-
});
574-
572+
573+
headers = _.map(headers, value => `header "${value}"`);
574+
575575
let modulemap = `module ${libraryName} { explicit module ${libraryName} { ${headers.join(" ")} } }`;
576-
this.$fs.writeFile(path.join(headersFolderPath, "module.modulemap"), modulemap, "utf8").wait();
576+
this.$fs.writeFile(path.join(headersFolderPath, "module.modulemap"), modulemap).wait();
577577
}
578578
}
579579

test/ios-project-service.ts

+68-51
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ describe("Cocoapods support", () => {
7272
iOSProjectService.prepareFrameworks = (pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> => {
7373
return Future.fromResult();
7474
};
75+
iOSProjectService.prepareStaticLibs = (pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> => {
76+
return Future.fromResult();
77+
};
7578

7679
let pluginPath = temp.mkdirSync("pluginDirectory");
7780
let pluginPlatformsFolderPath = path.join(pluginPath, "platforms", "ios");
@@ -124,9 +127,15 @@ describe("Cocoapods support", () => {
124127
iOSProjectService.prepareFrameworks = (pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> => {
125128
return Future.fromResult();
126129
};
130+
iOSProjectService.prepareStaticLibs = (pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> => {
131+
return Future.fromResult();
132+
};
127133
iOSProjectService.removeFrameworks = (pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> => {
128134
return Future.fromResult();
129135
};
136+
iOSProjectService.removeStaticLibs = (pluginPlatformsFolderPath: string, pluginData: IPluginData): IFuture<void> => {
137+
return Future.fromResult();
138+
};
130139

131140
let pluginPath = temp.mkdirSync("pluginDirectory");
132141
let pluginPlatformsFolderPath = path.join(pluginPath, "platforms", "ios");
@@ -162,66 +171,74 @@ describe("Cocoapods support", () => {
162171

163172
describe("Static libraries support", () => {
164173
if (require("os").platform() !== "darwin") {
165-
console.log("Skipping static library tests. They cannot work on windows.");
166-
} else {
167-
it("checks static libraries support", () => {
168-
let projectName = "projectDirectory";
169-
let projectPath = temp.mkdirSync(projectName);
170-
let libraryName = "testLibrary1";
171-
let headers = ["TestHeader1.h", "TestHeader2.h"];
172-
173-
let testInjector = createTestInjector(projectPath, projectName);
174-
let fs: IFileSystem = testInjector.resolve("fs");
175-
let iOSProjectService = testInjector.resolve("iOSProjectService");
176-
177-
let staticLibraryPath = path.join(path.join(temp.mkdirSync("pluginDirectory"), "platforms", "ios"));
178-
let staticLibraryHeadersPath = path.join(staticLibraryPath,"include", libraryName);
179-
fs.createDirectory(staticLibraryHeadersPath).wait();
180-
181-
_.each(headers, header => { fs.writeFile(path.join(staticLibraryHeadersPath, header), "").wait(); });
182-
// Add all header files.
183-
fs.writeFile(path.join(staticLibraryHeadersPath, libraryName + ".a"), "").wait();
184-
185-
try {
186-
iOSProjectService.validateStaticLibrary(path.join(staticLibraryPath, libraryName + ".a")).wait();
187-
} catch(error) {
188-
// Expect to fail, the .a file is not a static library.
189-
assert.isTrue(error !== null);
190-
}
191-
192-
iOSProjectService.generateMobulemap(staticLibraryHeadersPath, libraryName);
193-
let modulemap = fs.readFile(path.join(staticLibraryHeadersPath, "module.modulemap")).wait();
194-
let headerCommands: string[] = [];
195-
headers.forEach(function(currentValue, index, array) {
196-
headerCommands.push("header \"" + currentValue + "\"");
197-
});
198-
199-
let modulemapExpectation = `module ${libraryName} { explicit module ${libraryName} { ${headerCommands.join(" ")} } }`;
200-
assert.equal(modulemap, modulemapExpectation);
201-
202-
// Delete all header files.
203-
_.each(headers, header => { fs.deleteFile(path.join(staticLibraryHeadersPath, header)).wait(); });
204-
iOSProjectService.generateMobulemap(staticLibraryHeadersPath, libraryName);
205-
try {
206-
modulemap = fs.readFile(path.join(staticLibraryHeadersPath, "module.modulemap")).wait();
207-
} catch(error) {
208-
// Expect to fail, there shouldn't be a module.modulemap file.
209-
assert.isTrue(error !== null);
210-
}
211-
});
174+
console.log("Skipping static library tests. They work only on darwin.");
175+
return;
212176
}
177+
178+
let projectName = "projectDirectory";
179+
let projectPath = temp.mkdirSync(projectName);
180+
let libraryName = "testLibrary1";
181+
let headers = ["TestHeader1.h", "TestHeader2.h"];
182+
let testInjector = createTestInjector(projectPath, projectName);
183+
let fs: IFileSystem = testInjector.resolve("fs");
184+
let staticLibraryPath = path.join(path.join(temp.mkdirSync("pluginDirectory"), "platforms", "ios"));
185+
let staticLibraryHeadersPath = path.join(staticLibraryPath,"include", libraryName);
186+
187+
it("checks validation of header files", () => {
188+
let iOSProjectService = testInjector.resolve("iOSProjectService");
189+
fs.ensureDirectoryExists(staticLibraryHeadersPath).wait();
190+
_.each(headers, header => { fs.writeFile(path.join(staticLibraryHeadersPath, header), "").wait(); });
191+
192+
// Add all header files.
193+
fs.writeFile(path.join(staticLibraryHeadersPath, libraryName + ".a"), "").wait();
194+
195+
let error: any;
196+
try {
197+
iOSProjectService.validateStaticLibrary(path.join(staticLibraryPath, libraryName + ".a")).wait();
198+
} catch(err) {
199+
error = err;
200+
}
201+
202+
assert.instanceOf(error, Error, "Expect to fail, the .a file is not a static library.");
203+
});
204+
205+
it("checks generation of modulemaps", () => {
206+
let iOSProjectService = testInjector.resolve("iOSProjectService");
207+
fs.ensureDirectoryExists(staticLibraryHeadersPath).wait();
208+
_.each(headers, header => { fs.writeFile(path.join(staticLibraryHeadersPath, header), "").wait(); });
209+
210+
iOSProjectService.generateMobulemap(staticLibraryHeadersPath, libraryName);
211+
// Read the generated modulemap and verify it.
212+
let modulemap = fs.readFile(path.join(staticLibraryHeadersPath, "module.modulemap")).wait();
213+
let headerCommands = _.map(headers, value => `header "${value}"`);
214+
let modulemapExpectation = `module ${libraryName} { explicit module ${libraryName} { ${headerCommands.join(" ")} } }`;
215+
216+
assert.equal(modulemap, modulemapExpectation);
217+
218+
// Delete all header files. And try to regenerate modulemap.
219+
_.each(headers, header => { fs.deleteFile(path.join(staticLibraryHeadersPath, header)).wait(); });
220+
iOSProjectService.generateMobulemap(staticLibraryHeadersPath, libraryName);
221+
222+
let error: any;
223+
try {
224+
modulemap = fs.readFile(path.join(staticLibraryHeadersPath, "module.modulemap")).wait();
225+
} catch(err) {
226+
error = err;
227+
}
228+
229+
assert.instanceOf(error, Error, "Expect to fail, there shouldn't be a module.modulemap file.");
230+
});
213231
});
214232

215233
describe("Relative paths", () => {
216234
it("checks for correct calculation of relative paths", () => {
217235
let projectName = "projectDirectory";
218236
let projectPath = temp.mkdirSync(projectName);
219-
let subpath = "sub/path";
220-
237+
let subpath = "sub/path";
238+
221239
let testInjector = createTestInjector(projectPath, projectName);
222-
let fs: IFileSystem = testInjector.resolve("fs");
223240
let iOSProjectService = testInjector.resolve("iOSProjectService");
224-
241+
225242
let result = iOSProjectService.getLibSubpathRelativeToProjectPath(subpath);
226243
assert.equal(result, path.join("../../lib/iOS/", subpath));
227244
});

0 commit comments

Comments
 (0)