From 9a38ec11a3d0602d3b44f0d627f04aa87ed510fd Mon Sep 17 00:00:00 2001 From: Rosen Vladimirov Date: Thu, 13 Aug 2015 16:14:42 +0300 Subject: [PATCH] Add platform on all commands When you execute `tns run `, but you have forgotten to add the platform before that, you go in inconsistent state. Make sure to add the platform for the user when one of the following is executed: * `tns prepare ` * `tns build ` * `tns run ` * `tns update @` - this will add the specified version in case you have not added the platform before that. * `tns emulate ` This way we simplify the workflow, now you can just do: 1) `tns create app1` 2) `tns run android --path app1` And the application will run on your attached device. Resolves https://github.com/NativeScript/nativescript-cli/issues/774 Part of the implementation of: https://github.com/NativeScript/nativescript-cli/issues/475 --- lib/commands/update-platform.ts | 2 +- lib/platform-command-param.ts | 2 +- lib/services/platform-service.ts | 29 +++++++++++++++++++---------- test/platform-service.ts | 12 ++++++++++-- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/commands/update-platform.ts b/lib/commands/update-platform.ts index fa2c2bbd53..635027631b 100644 --- a/lib/commands/update-platform.ts +++ b/lib/commands/update-platform.ts @@ -17,7 +17,7 @@ export class UpdatePlatformCommand implements ICommand { this.$errors.fail("No platform specified. Please specify platforms to update."); } - _.each(args, arg => this.$platformService.validatePlatformInstalled(arg.split("@")[0])); + _.each(args, arg => this.$platformService.validatePlatform(arg.split("@")[0])); return true; }).future()(); diff --git a/lib/platform-command-param.ts b/lib/platform-command-param.ts index 1e5389d7fc..ff480fca23 100644 --- a/lib/platform-command-param.ts +++ b/lib/platform-command-param.ts @@ -6,7 +6,7 @@ export class PlatformCommandParameter implements ICommandParameter { mandatory = true; validate(value: string): IFuture { return (() => { - this.$platformService.validatePlatformInstalled(value); + this.$platformService.validatePlatform(value); return true; }).future()(); } diff --git a/lib/services/platform-service.ts b/lib/services/platform-service.ts index 19876bba19..e1bbcdc5b9 100644 --- a/lib/services/platform-service.ts +++ b/lib/services/platform-service.ts @@ -146,6 +146,7 @@ export class PlatformService implements IPlatformService { this.validatePlatform(platform); platform = platform.toLowerCase(); + this.ensurePlatformInstalled(platform).wait(); var platformData = this.$platformsData.getPlatformData(platform); let appDestinationDirectoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME); @@ -233,13 +234,13 @@ export class PlatformService implements IPlatformService { public updatePlatforms(platforms: string[]): IFuture { return (() => { - _.each(platforms, platform => { - var parts = platform.split("@"); - platform = parts[0].toLowerCase(); - var version = parts[1]; - - this.validatePlatformInstalled(platform); - this.updatePlatform(platform, version).wait(); + _.each(platforms, platformParam => { + let [platform, version] = platformParam.split("@"); + if(this.isPlatformInstalled(platform).wait()) { + this.updatePlatform(platform, version).wait(); + } else { + this.addPlatform(platformParam).wait(); + } }); }).future()(); } @@ -247,7 +248,7 @@ export class PlatformService implements IPlatformService { public deployOnDevice(platform: string): IFuture { return (() => { platform = platform.toLowerCase(); - + this.ensurePlatformInstalled(platform).wait(); var platformData = this.$platformsData.getPlatformData(platform); var cachedDeviceOption = this.$options.forDevice; @@ -276,7 +277,7 @@ export class PlatformService implements IPlatformService { public deployOnEmulator(platform: string): IFuture { return (() => { - this.validatePlatformInstalled(platform); + this.ensurePlatformInstalled(platform).wait(); platform = platform.toLowerCase(); var platformData = this.$platformsData.getPlatformData(platform); @@ -333,7 +334,15 @@ export class PlatformService implements IPlatformService { } }).future()(); } - + + private ensurePlatformInstalled(platform: string): IFuture { + return (() => { + if(!this.isPlatformInstalled(platform).wait()) { + this.addPlatform(platform).wait(); + } + }).future()(); + } + private isPlatformInstalled(platform: string): IFuture { return this.$fs.exists(path.join(this.$projectData.platformsDir, platform.toLowerCase())); } diff --git a/test/platform-service.ts b/test/platform-service.ts index 7429a1416b..4a5c195654 100644 --- a/test/platform-service.ts +++ b/test/platform-service.ts @@ -227,7 +227,11 @@ describe('Platform Service Tests', () => { appResourcesDestinationDirectoryPath: appResourcesFolderPath, normalizedPlatformName: "iOS", platformProjectService: { - prepareProject: () => Future.fromResult() + prepareProject: () => Future.fromResult(), + validate: () => Future.fromResult(), + createProject: (projectRoot: string, frameworkDir: string) => Future.fromResult(), + interpolateData: (projectRoot: string) => Future.fromResult(), + afterCreateProject: (projectRoot: string) => Future.fromResult() } } }; @@ -281,7 +285,11 @@ describe('Platform Service Tests', () => { appResourcesDestinationDirectoryPath: appResourcesFolderPath, normalizedPlatformName: "Android", platformProjectService: { - prepareProject: () => Future.fromResult() + prepareProject: () => Future.fromResult(), + validate: () => Future.fromResult(), + createProject: (projectRoot: string, frameworkDir: string) => Future.fromResult(), + interpolateData: (projectRoot: string) => Future.fromResult(), + afterCreateProject: (projectRoot: string) => Future.fromResult() } } };