Skip to content

Add validation for command parameters #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ $injector.require("analyticsSettingsService", "./services/analytics-settings-ser

$injector.require("emulatorSettingsService", "./services/emulator-settings-service");

$injector.require("platformCommandParameter", "./platform-command-param");
$injector.requireCommand("create", "./commands/create-project");
$injector.requireCommand("platform|*list", "./commands/list-platforms");
$injector.requireCommand("platform|add", "./commands/add-platform");
Expand Down
18 changes: 17 additions & 1 deletion lib/commands/add-platform.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
///<reference path="../.d.ts"/>
"use strict";

export class AddPlatformCommand implements ICommand {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the file is changed in this PR, we can add the missing "use strict"

constructor(private $platformService: IPlatformService) { }
constructor(private $platformService: IPlatformService,
private $errors: IErrors) { }

execute(args: string[]): IFuture<void> {
return (() => {
this.$platformService.addPlatforms(args).wait();
}).future<void>()();
}

allowedParameters: ICommandParameter[] = [];

canExecute(args: string[]): IFuture<boolean> {
return (() => {
if(!args || args.length === 0) {
this.$errors.fail("No platform specified. Please specify a platform to add");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if we list available platforms

}

_.each(args, arg => this.$platformService.validatePlatform(arg));

return true;
}).future<boolean>()();
}
}
$injector.registerCommand("platform|add", AddPlatformCommand);
6 changes: 5 additions & 1 deletion lib/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
///<reference path="../.d.ts"/>
"use strict";

export class BuildCommand implements ICommand {
constructor(private $platformService: IPlatformService) { }
constructor(private $platformService: IPlatformService,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use strict

private $platformCommandParameter: ICommandParameter) { }

execute(args: string[]): IFuture<void> {
return (() => {
this.$platformService.buildPlatform(args[0]).wait();
}).future<void>()();
}

allowedParameters = [this.$platformCommandParameter];
}
$injector.registerCommand("build", BuildCommand);
23 changes: 22 additions & 1 deletion lib/commands/create-project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
///<reference path="../.d.ts"/>
"use strict";

export class ProjectCommandParameter implements ICommandParameter {
constructor(private $errors: IErrors,
private $projectNameValidator: IProjectNameValidator) { }

mandatory = true;
validate(value: string): IFuture<boolean> {
return (() => {
if(!value) {
this.$errors.fail("You must specify <App name> when creating a new project.");
}

return this.$projectNameValidator.validate(value);
}).future<boolean>()();
}
}

export class CreateProjectCommand implements ICommand {
constructor(private $projectService: IProjectService) { }
constructor(private $projectService: IProjectService,
private $errors: IErrors,
private $projectNameValidator: IProjectNameValidator) { }

public enableHooks = false;

Expand All @@ -10,5 +29,7 @@ export class CreateProjectCommand implements ICommand {
this.$projectService.createProject(args[0]).wait();
}).future<void>()();
}

allowedParameters = [new ProjectCommandParameter(this.$errors, this.$projectNameValidator) ]
}
$injector.registerCommand("create", CreateProjectCommand);
8 changes: 6 additions & 2 deletions lib/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
///<reference path="../.d.ts"/>
"use strict";

export class DeployOnDeviceCommand implements ICommand {
constructor(private $platformService: IPlatformService) { }
constructor(private $platformService: IPlatformService,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use strict

private $platformCommandParameter: ICommandParameter) { }

execute(args: string[]): IFuture<void> {
return this.$platformService.deployOnDevice(args[0]);
}

allowedParameters = [this.$platformCommandParameter];
}
$injector.registerCommand("deploy", DeployOnDeviceCommand);
$injector.registerCommand("deploy", DeployOnDeviceCommand);
9 changes: 6 additions & 3 deletions lib/commands/emulate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
///<reference path="../.d.ts"/>

export class EmulateCommand implements ICommand {
constructor(private $platformService: IPlatformService) { }
constructor(private $platformService: IPlatformService,
private $platformCommandParameter: ICommandParameter) { }

execute(args: string[]): IFuture<void> { return this.$platformService.deployOnEmulator(args[0]);}
execute(args: string[]): IFuture<void> { return this.$platformService.deployOnEmulator(args[0]); }

allowedParameters = [this.$platformCommandParameter];
}
$injector.registerCommand("emulate", EmulateCommand);
$injector.registerCommand("emulate", EmulateCommand);
5 changes: 4 additions & 1 deletion lib/commands/list-devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import util = require("util")

export class ListDevicesCommand implements ICommand {
constructor(private $devicesServices: Mobile.IDevicesServices,
private $logger: ILogger) { }
private $logger: ILogger,
private $stringParameter: ICommandParameter) { }

execute(args: string[]): IFuture<void> {
return (() => {
Expand All @@ -17,5 +18,7 @@ export class ListDevicesCommand implements ICommand {
this.$devicesServices.execute(action, undefined, {allowNoDevices: true}).wait();
}).future<void>()();
}

allowedParameters = [this.$stringParameter];
}
$injector.registerCommand("list-devices", ListDevicesCommand);
2 changes: 2 additions & 0 deletions lib/commands/list-platforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ export class ListPlatformsCommand implements ICommand {
}
}).future<void>()();
}

allowedParameters: ICommandParameter[] = [];
}
$injector.registerCommand("platform|*list", ListPlatformsCommand);
2 changes: 2 additions & 0 deletions lib/commands/post-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ export class PostInstallCommand implements ICommand {
this.$autoCompletionService.enableAutoCompletion().wait();
}).future<void>()();
}

public allowedParameters: ICommandParameter[] = [];
}
$injector.registerCommand("dev-post-install", PostInstallCommand);
5 changes: 4 additions & 1 deletion lib/commands/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
///<reference path="../.d.ts"/>

export class PrepareCommand implements ICommand {
constructor(private $platformService: IPlatformService) { }
constructor(private $platformService: IPlatformService,
private $platformCommandParameter: ICommandParameter) { }

execute(args: string[]): IFuture<void> {
return (() => {
this.$platformService.preparePlatform(args[0]).wait();
}).future<void>()();
}

allowedParameters = [this.$platformCommandParameter];
}
$injector.registerCommand("prepare", PrepareCommand);
18 changes: 17 additions & 1 deletion lib/commands/remove-platform.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
///<reference path="../.d.ts"/>
"use strict";

export class RemovePlatformCommand implements ICommand {
constructor(private $platformService: IPlatformService) { }
constructor(private $platformService: IPlatformService,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use strict

private $errors: IErrors) { }

execute(args: string[]): IFuture<void> {
return (() => {
this.$platformService.removePlatforms(args).wait();
}).future<void>()();
}

canExecute(args: string[]): IFuture<boolean> {
return (() => {
if(!args || args.length === 0) {
this.$errors.fail("No platform specified. Please specify a platform to remove");
}

_.each(args, arg => this.$platformService.validatePlatformInstalled(arg));

return true;
}).future<boolean>()();
}

allowedParameters: ICommandParameter[] = [];
}
$injector.registerCommand("platform|remove", RemovePlatformCommand);
6 changes: 5 additions & 1 deletion lib/commands/run.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
///<reference path="../.d.ts"/>
"use strict";

export class RunCommand implements ICommand {
constructor(private $platformService: IPlatformService) { }
constructor(private $platformService: IPlatformService,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use strict

private $platformCommandParameter: ICommandParameter) { }

execute(args: string[]): IFuture<void> {
return (() => {
this.$platformService.runPlatform(args[0]).wait();
}).future<void>()();
}

allowedParameters = [this.$platformCommandParameter];
}
$injector.registerCommand("run", RunCommand);
18 changes: 17 additions & 1 deletion lib/commands/update-platform.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
///<reference path="../.d.ts"/>
"use strict";

export class UpdatePlatformCommand implements ICommand {
constructor(private $platformService: IPlatformService) { }
constructor(private $platformService: IPlatformService,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"use strict";

private $errors:IErrors) { }

execute(args: string[]): IFuture<void> {
return (() => {
this.$platformService.updatePlatforms(args).wait();
}).future<void>()();
}

canExecute(args: string[]): IFuture<boolean> {
return (() => {
if(!args || args.length === 0) {
this.$errors.fail("No platform specified. Please specify platforms to update.");
}

_.each(args, arg => this.$platformService.validatePlatformInstalled(arg));

return true;
}).future<boolean>()();
}

allowedParameters: ICommandParameter[] = [];
}
$injector.registerCommand("platform|update", UpdatePlatformCommand);
2 changes: 2 additions & 0 deletions lib/definitions/platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface IPlatformService {
buildPlatform(platform: string): IFuture<void>;
deployOnDevice(platform: string): IFuture<void>;
deployOnEmulator(platform: string): IFuture<void>;
validatePlatformInstalled(platform: string): void;
validatePlatform(platform: string): void;
}

interface IPlatformData {
Expand Down
14 changes: 14 additions & 0 deletions lib/platform-command-param.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
///<reference path=".d.ts"/>
"use strict";

export class PlatformCommandParameter implements ICommandParameter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"use strict";

constructor(private $platformService: IPlatformService) { }
mandatory = true;
validate(value: string): IFuture<boolean> {
return (() => {
this.$platformService.validatePlatformInstalled(value);
return true;
}).future<boolean>()();
}
}
$injector.register("platformCommandParameter", PlatformCommandParameter);
20 changes: 2 additions & 18 deletions lib/services/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ export class PlatformService implements IPlatformService {

public addPlatforms(platforms: string[]): IFuture<void> {
return (() => {
if(!platforms || platforms.length === 0) {
this.$errors.fail("No platform specified. Please specify a platform to add");
}

var platformsDir = this.$projectData.platformsDir;
this.$fs.ensureDirectoryExists(platformsDir).wait();

Expand Down Expand Up @@ -136,7 +132,6 @@ export class PlatformService implements IPlatformService {

public preparePlatform(platform: string): IFuture<void> {
return (() => {
this.validatePlatformInstalled(platform);
platform = platform.toLowerCase();

var platformData = this.$platformsData.getPlatformData(platform);
Expand Down Expand Up @@ -166,7 +161,6 @@ export class PlatformService implements IPlatformService {

public buildPlatform(platform: string): IFuture<void> {
return (() => {
this.validatePlatformInstalled(platform);
platform = platform.toLowerCase();

var platformData = this.$platformsData.getPlatformData(platform);
Expand All @@ -177,7 +171,6 @@ export class PlatformService implements IPlatformService {

public runPlatform(platform: string): IFuture<void> {
return (() => {
this.validatePlatformInstalled(platform);
platform = platform.toLowerCase();

this.preparePlatform(platform).wait();
Expand All @@ -191,10 +184,6 @@ export class PlatformService implements IPlatformService {

public removePlatforms(platforms: string[]): IFuture<void> {
return (() => {
if(!platforms || platforms.length === 0) {
this.$errors.fail("No platform specified. Please specify a platform to remove");
}

_.each(platforms, platform => {
this.validatePlatformInstalled(platform);

Expand All @@ -207,10 +196,6 @@ export class PlatformService implements IPlatformService {

public updatePlatforms(platforms: string[]): IFuture<void> {
return (() => {
if(!platforms || platforms.length === 0) {
this.$errors.fail("No platform specified. Please specify a platform to remove");
}

_.each(platforms, platform => {
var parts = platform.split("@");
platform = parts[0].toLowerCase();
Expand All @@ -224,7 +209,6 @@ export class PlatformService implements IPlatformService {

public deployOnDevice(platform: string): IFuture<void> {
return (() => {
this.validatePlatformInstalled(platform);
platform = platform.toLowerCase();

var platformData = this.$platformsData.getPlatformData(platform);
Expand Down Expand Up @@ -267,7 +251,7 @@ export class PlatformService implements IPlatformService {
}).future<void>()();
}

private validatePlatform(platform: string): void {
public validatePlatform(platform: string): void {
if(!platform) {
this.$errors.fail("No platform specified.")
}
Expand All @@ -283,7 +267,7 @@ export class PlatformService implements IPlatformService {
}
}

private validatePlatformInstalled(platform: string): void {
public validatePlatformInstalled(platform: string): void {
this.validatePlatform(platform);

if (!this.isPlatformInstalled(platform).wait()) {
Expand Down
Loading