From baaea894c79c55684007865bbf2e303181d7e275 Mon Sep 17 00:00:00 2001 From: Emil Tabakov Date: Wed, 15 Mar 2017 15:53:38 +0200 Subject: [PATCH 1/4] Move clean command logic to platform service --- lib/commands/platform-clean.ts | 3 +-- lib/definitions/platform.d.ts | 2 ++ lib/services/platform-service.ts | 5 +++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/commands/platform-clean.ts b/lib/commands/platform-clean.ts index 2918d9c396..579e4b1040 100644 --- a/lib/commands/platform-clean.ts +++ b/lib/commands/platform-clean.ts @@ -9,8 +9,7 @@ export class CleanCommand implements ICommand { } public async execute(args: string[]): Promise { - await this.$platformService.removePlatforms(args, this.$projectData); - await this.$platformService.addPlatforms(args, this.$options.platformTemplate, this.$projectData, { provision: this.$options.provision, sdk: this.$options.sdk }); + await this.$platformService.cleanPlatforms(args, this.$options.platformTemplate, this.$projectData, {provision: this.$options.provision, sdk: this.$options.sdk }); } public async canExecute(args: string[]): Promise { diff --git a/lib/definitions/platform.d.ts b/lib/definitions/platform.d.ts index 9268d5cca8..3ace46f88a 100644 --- a/lib/definitions/platform.d.ts +++ b/lib/definitions/platform.d.ts @@ -1,4 +1,6 @@ interface IPlatformService extends NodeJS.EventEmitter { + cleanPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, framework?: string): Promise; + addPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, frameworkPath?: string): Promise; /** diff --git a/lib/services/platform-service.ts b/lib/services/platform-service.ts index eb5f71fd18..088ab14335 100644 --- a/lib/services/platform-service.ts +++ b/lib/services/platform-service.ts @@ -46,6 +46,11 @@ export class PlatformService extends EventEmitter implements IPlatformService { super(); } + public async cleanPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, framworkPath?: string): Promise { + await this.removePlatforms(platforms, projectData); + await this.addPlatforms(platforms, platformTemplate, projectData, platformSpecificData); + } + public async addPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, frameworkPath?: string): Promise { let platformsDir = projectData.platformsDir; this.$fs.ensureDirectoryExists(platformsDir); From 5984ed44fc6f21d636341276e1cedda5368a718c Mon Sep 17 00:00:00 2001 From: Emil Tabakov Date: Wed, 15 Mar 2017 18:12:44 +0200 Subject: [PATCH 2/4] Preserve the version of {N} on clean --- lib/services/platform-service.ts | 18 +++++++++++++++--- test/platform-service.ts | 30 ++++++++++++++++++++++++++++++ test/stubs.ts | 4 ++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/lib/services/platform-service.ts b/lib/services/platform-service.ts index 088ab14335..85f6fd8dea 100644 --- a/lib/services/platform-service.ts +++ b/lib/services/platform-service.ts @@ -47,8 +47,20 @@ export class PlatformService extends EventEmitter implements IPlatformService { } public async cleanPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, framworkPath?: string): Promise { - await this.removePlatforms(platforms, projectData); - await this.addPlatforms(platforms, platformTemplate, projectData, platformSpecificData); + for (let platform of platforms) { + // TODO: Copy pasted - refactor as a common function + let platformData = this.$platformsData.getPlatformData(platform, projectData); + let currentPlatformData: any = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName); + let version: string; + if (currentPlatformData && currentPlatformData[constants.VERSION_STRING]) { + version = currentPlatformData[constants.VERSION_STRING]; + }; + + let platformWithVersion: string = platform + "@" + version; + + await this.removePlatforms([platform], projectData); + await this.addPlatforms([platformWithVersion], platformTemplate, projectData, platformSpecificData); + } } public async addPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, frameworkPath?: string): Promise { @@ -75,7 +87,7 @@ export class PlatformService extends EventEmitter implements IPlatformService { let platformData = this.$platformsData.getPlatformData(platform, projectData); let currentPlatformData: any = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName); - if (currentPlatformData && currentPlatformData[constants.VERSION_STRING]) { + if (version === undefined && currentPlatformData && currentPlatformData[constants.VERSION_STRING]) { version = currentPlatformData[constants.VERSION_STRING]; } diff --git a/test/platform-service.ts b/test/platform-service.ts index 94aca1a1e1..67f0930677 100644 --- a/test/platform-service.ts +++ b/test/platform-service.ts @@ -245,6 +245,36 @@ describe('Platform Service Tests', () => { }); }); + describe("clean platform unit tests", () => { + it("should preserve the specified in the project nativescript version", async () => { + const versionString = "2.4.1"; + let fs = testInjector.resolve("fs"); + fs.exists = () => false; + + let nsValueObject: any = {}; + nsValueObject[VERSION_STRING] = versionString; + let projectDataService = testInjector.resolve("projectDataService"); + projectDataService.getNSValue = () => nsValueObject; + + let npmInstallationManager = testInjector.resolve("npmInstallationManager"); + npmInstallationManager.install = (packageName: string, packageDir: string, options: INpmInstallOptions) => { + assert.deepEqual(options.version, versionString); + return ""; + }; + + let projectData: IProjectData = testInjector.resolve("projectData"); + platformService.removePlatforms = (platforms: string[], prjctData: IProjectData): Promise => { + nsValueObject[VERSION_STRING] = undefined; + return Promise.resolve(); + }; + + await platformService.cleanPlatforms(["android"], "", projectData, null); + + nsValueObject[VERSION_STRING] = versionString; + await platformService.cleanPlatforms(["ios"], "", projectData, null); + }); + }); + // TODO: Commented as it doesn't seem correct. Check what's the case and why it's been expected to fail. // describe("list platform unit tests", () => { // it("fails when platforms are not added", () => { diff --git a/test/stubs.ts b/test/stubs.ts index f3aa5bce99..800c41d1d4 100644 --- a/test/stubs.ts +++ b/test/stubs.ts @@ -581,6 +581,10 @@ export class PlatformServiceStub extends EventEmitter implements IPlatformServic return Promise.resolve(true); } + public cleanPlatforms(platforms: string[]): Promise { + return Promise.resolve(); + } + public addPlatforms(platforms: string[]): Promise { return Promise.resolve(); } From fc31514f9065e55825f7c4d1458fb04d4bdcaab9 Mon Sep 17 00:00:00 2001 From: Emil Tabakov Date: Wed, 15 Mar 2017 18:23:58 +0200 Subject: [PATCH 3/4] extract function that gets project version --- lib/services/platform-service.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/services/platform-service.ts b/lib/services/platform-service.ts index 85f6fd8dea..bcd3ad0c12 100644 --- a/lib/services/platform-service.ts +++ b/lib/services/platform-service.ts @@ -48,14 +48,7 @@ export class PlatformService extends EventEmitter implements IPlatformService { public async cleanPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, framworkPath?: string): Promise { for (let platform of platforms) { - // TODO: Copy pasted - refactor as a common function - let platformData = this.$platformsData.getPlatformData(platform, projectData); - let currentPlatformData: any = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName); - let version: string; - if (currentPlatformData && currentPlatformData[constants.VERSION_STRING]) { - version = currentPlatformData[constants.VERSION_STRING]; - }; - + let version: string = this.getCurrentPlatformVersion(platform, projectData); let platformWithVersion: string = platform + "@" + version; await this.removePlatforms([platform], projectData); @@ -72,6 +65,17 @@ export class PlatformService extends EventEmitter implements IPlatformService { } } + private getCurrentPlatformVersion(platform: string, projectData: IProjectData) : string { + let platformData = this.$platformsData.getPlatformData(platform, projectData); + let currentPlatformData: any = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName); + let version: string; + if (currentPlatformData && currentPlatformData[constants.VERSION_STRING]) { + version = currentPlatformData[constants.VERSION_STRING]; + }; + + return version; + } + private async addPlatform(platformParam: string, platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, frameworkPath?: string): Promise { let data = platformParam.split("@"), platform = data[0].toLowerCase(), @@ -86,9 +90,9 @@ export class PlatformService extends EventEmitter implements IPlatformService { } let platformData = this.$platformsData.getPlatformData(platform, projectData); - let currentPlatformData: any = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName); - if (version === undefined && currentPlatformData && currentPlatformData[constants.VERSION_STRING]) { - version = currentPlatformData[constants.VERSION_STRING]; + + if (version === undefined) { + version = this.getCurrentPlatformVersion(platform, projectData); } // Copy platform specific files in platforms dir From 1b2d112d8d7a94a2061eb0a966bd0f36a7ed4b3b Mon Sep 17 00:00:00 2001 From: Emil Tabakov Date: Wed, 15 Mar 2017 18:30:10 +0200 Subject: [PATCH 4/4] Do not submit undefined if version is missing --- lib/services/platform-service.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/services/platform-service.ts b/lib/services/platform-service.ts index bcd3ad0c12..92531e2a0a 100644 --- a/lib/services/platform-service.ts +++ b/lib/services/platform-service.ts @@ -49,7 +49,11 @@ export class PlatformService extends EventEmitter implements IPlatformService { public async cleanPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, framworkPath?: string): Promise { for (let platform of platforms) { let version: string = this.getCurrentPlatformVersion(platform, projectData); - let platformWithVersion: string = platform + "@" + version; + + let platformWithVersion: string = platform; + if (version !== undefined) { + platformWithVersion += "@" + version; + } await this.removePlatforms([platform], projectData); await this.addPlatforms([platformWithVersion], platformTemplate, projectData, platformSpecificData);