Skip to content

Commit fc5fa87

Browse files
FatmeFatme
authored andcommitted
Merge pull request #214 from NativeScript/fatme/emulate-and-run-as-hierarchical-commands
Implement emulate and run as hierarchical commands
2 parents 16d9dc9 + a947306 commit fc5fa87

File tree

9 files changed

+152
-61
lines changed

9 files changed

+152
-61
lines changed

lib/bootstrap.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ $injector.requireCommand("platform|update", "./commands/update-platform");
2929
$injector.requireCommand("run", "./commands/run");
3030
$injector.requireCommand("debug", "./commands/debug");
3131
$injector.requireCommand("prepare", "./commands/prepare");
32-
$injector.requireCommand("build", "./commands/build");
32+
$injector.requireCommand("build|ios", "./commands/build");
33+
$injector.requireCommand("build|android", "./commands/build");
3334
$injector.requireCommand("deploy", "./commands/deploy");
34-
$injector.requireCommand("emulate", "./commands/emulate");
35+
$injector.requireCommand("emulate|android", "./commands/emulate");
36+
$injector.requireCommand("emulate|ios", "./commands/emulate");
3537

3638
$injector.require("npm", "./node-package-manager");
3739
$injector.require("lockfile", "./lockfile");

lib/commands/build.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
///<reference path="../.d.ts"/>
22
"use strict";
33

4-
export class BuildCommand implements ICommand {
5-
constructor(private $platformService: IPlatformService,
6-
private $platformCommandParameter: ICommandParameter) { }
4+
export class BuildCommandBase {
5+
constructor(private $platformService: IPlatformService) { }
76

8-
execute(args: string[]): IFuture<void> {
9-
return this.$platformService.buildPlatform(args[0]);
7+
executeCore(args: string[]): IFuture<void> {
8+
return this.$platformService.buildPlatform(args[0]);
109
}
10+
}
11+
12+
export class BuildIosCommand extends BuildCommandBase implements ICommand {
13+
constructor($platformService: IPlatformService,
14+
private $platformsData: IPlatformsData) {
15+
super($platformService);
16+
}
17+
18+
public allowedParameters: ICommandParameter[] = [];
19+
20+
public execute(args: string[]): IFuture<void> {
21+
return this.executeCore([this.$platformsData.availablePlatforms.iOS]);
22+
}
23+
}
24+
$injector.registerCommand("build|ios", BuildIosCommand);
25+
26+
27+
export class BuildAndroidCommand extends BuildCommandBase implements ICommand {
28+
constructor($platformService: IPlatformService,
29+
private $platformsData: IPlatformsData) {
30+
super($platformService);
31+
}
32+
33+
public allowedParameters: ICommandParameter[] = [];
1134

12-
allowedParameters = [this.$platformCommandParameter];
35+
public execute(args: string[]): IFuture<void> {
36+
return this.executeCore([this.$platformsData.availablePlatforms.Android]);
37+
}
1338
}
14-
$injector.registerCommand("build", BuildCommand);
39+
$injector.registerCommand("build|android", BuildAndroidCommand);

lib/commands/emulate.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
11
///<reference path="../.d.ts"/>
22
"use strict";
33

4-
export class EmulateCommand implements ICommand {
5-
constructor(private $platformService: IPlatformService,
6-
private $platformCommandParameter: ICommandParameter) { }
4+
export class EmulateCommandBase {
5+
constructor(private $platformService: IPlatformService) { }
76

8-
execute(args: string[]): IFuture<void> { return this.$platformService.deployOnEmulator(args[0]); }
7+
executeCore(args: string[]): IFuture<void> {
8+
return this.$platformService.deployOnEmulator(args[0]);
9+
}
10+
}
11+
12+
export class EmulateIosCommand extends EmulateCommandBase implements ICommand {
13+
constructor($platformService: IPlatformService,
14+
private $platformsData: IPlatformsData) {
15+
super($platformService);
16+
}
17+
18+
public allowedParameters: ICommandParameter[] = [];
19+
20+
public execute(args: string[]): IFuture<void> {
21+
return this.executeCore([this.$platformsData.availablePlatforms.iOS]);
22+
}
23+
}
24+
$injector.registerCommand("emulate|ios", EmulateIosCommand);
25+
26+
export class EmulateAndroidCommand extends EmulateCommandBase implements ICommand {
27+
constructor($platformService: IPlatformService,
28+
private $platformsData: IPlatformsData) {
29+
super($platformService);
30+
}
31+
32+
public allowedParameters: ICommandParameter[] = [];
933

10-
allowedParameters = [this.$platformCommandParameter];
34+
public execute(args: string[]): IFuture<void> {
35+
return this.executeCore([this.$platformsData.availablePlatforms.Android]);
36+
}
1137
}
12-
$injector.registerCommand("emulate", EmulateCommand);
38+
$injector.registerCommand("emulate|android", EmulateAndroidCommand);

lib/commands/run.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
///<reference path="../.d.ts"/>
22
"use strict";
33

4-
export class RunCommand implements ICommand {
5-
constructor(private $platformService: IPlatformService,
6-
private $platformCommandParameter: ICommandParameter) { }
7-
8-
execute(args: string[]): IFuture<void> {
9-
return (() => {
10-
this.$platformService.runPlatform(args[0]).wait();
11-
}).future<void>()();
4+
export class RunCommandBase {
5+
constructor(private $platformService: IPlatformService) { }
6+
7+
public executeCore(args: string[]): IFuture<void> {
8+
return this.$platformService.runPlatform(args[0]);
9+
}
10+
}
11+
12+
export class RunIosCommand extends RunCommandBase implements ICommand {
13+
constructor($platformService: IPlatformService,
14+
private $platformsData: IPlatformsData) {
15+
super($platformService);
16+
}
17+
18+
public allowedParameters: ICommandParameter[] = [];
19+
20+
public execute(args: string[]): IFuture<void> {
21+
return this.executeCore([this.$platformsData.availablePlatforms.iOS]);
22+
}
23+
}
24+
$injector.registerCommand("run|ios", RunIosCommand);
25+
26+
export class RunAndroidCommand extends RunCommandBase implements ICommand {
27+
constructor($platformService: IPlatformService,
28+
private $platformsData: IPlatformsData) {
29+
super($platformService);
1230
}
1331

14-
allowedParameters = [this.$platformCommandParameter];
32+
public allowedParameters: ICommandParameter[] = [];
33+
34+
public execute(args: string[]): IFuture<void> {
35+
return this.executeCore([this.$platformsData.availablePlatforms.Android]);
36+
}
1537
}
16-
$injector.registerCommand("run", RunCommand);
38+
$injector.registerCommand("run|android", RunAndroidCommand);

lib/definitions/platform.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ interface IPlatformService {
55
getPreparedPlatforms(): IFuture<string[]>;
66
removePlatforms(platforms: string[]): IFuture<void>;
77
updatePlatforms(platforms: string[]): IFuture<void>;
8-
runPlatform(platform: string): IFuture<void>;
9-
debugPlatform(platform: string): IFuture<void>;
8+
runPlatform(platform: string): IFuture<void>;
9+
debugPlatform(platform: string): IFuture<void>;
1010
preparePlatform(platform: string): IFuture<void>;
1111
buildPlatform(platform: string): IFuture<void>;
1212
deployOnDevice(platform: string): IFuture<void>;
@@ -30,6 +30,7 @@ interface IPlatformData {
3030
}
3131

3232
interface IPlatformsData {
33+
availablePlatforms: any;
3334
platformsNames: string[];
3435
getPlatformData(platform: string): IPlatformData;
3536
}

lib/platforms-data.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,12 @@ export class PlatformsData implements IPlatformsData {
2020
public getPlatformData(platform: string): IPlatformData {
2121
return this.platformsData[platform];
2222
}
23+
24+
public get availablePlatforms(): any {
25+
return {
26+
iOS: "ios",
27+
Android: "android"
28+
};
29+
}
2330
}
2431
$injector.register("platformsData", PlatformsData);

lib/services/platform-service.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export class PlatformService implements IPlatformService {
165165
public buildPlatform(platform: string): IFuture<void> {
166166
return (() => {
167167
platform = platform.toLowerCase();
168-
this.preparePlatform(platform);
168+
this.preparePlatform(platform).wait();
169169

170170
var platformData = this.$platformsData.getPlatformData(platform);
171171
platformData.platformProjectService.buildProject(platformData.projectRoot).wait();
@@ -178,57 +178,57 @@ export class PlatformService implements IPlatformService {
178178
platform = platform.toLowerCase();
179179

180180
this.preparePlatform(platform).wait();
181-
if (options.emulator) {
181+
if (options.emulator) {
182182
this.deployOnEmulator(platform).wait();
183183
} else {
184184
this.deployOnDevice(platform).wait();
185185
}
186186
}).future<void>()();
187-
}
187+
}
188188

189-
public debugPlatform(platform: string): IFuture<void> {
190-
platform = platform.toLowerCase();
189+
public debugPlatform(platform: string): IFuture<void> {
190+
platform = platform.toLowerCase();
191191

192-
var ret = options.emulator
193-
? this.debugOnEmulator(platform)
194-
: this.debugOnDevice(platform);
195-
196-
return ret;
197-
}
192+
var ret = options.emulator
193+
? this.debugOnEmulator(platform)
194+
: this.debugOnDevice(platform);
198195

199-
public debugOnEmulator(platform: string): IFuture<void> {
200-
return (() => {
196+
return ret;
197+
}
201198

202-
// a bit redundant
203-
this.deployOnEmulator(platform).wait();
199+
public debugOnEmulator(platform: string): IFuture<void> {
200+
return (() => {
204201

205-
this.debugOnDevice(platform).wait();
206-
}).future<void>()();
207-
}
202+
// a bit redundant
203+
this.deployOnEmulator(platform).wait();
208204

209-
public debugOnDevice(platform: string): IFuture<void> {
210-
return (() => {
211-
platform = platform.toLowerCase();
205+
this.debugOnDevice(platform).wait();
206+
}).future<void>()();
207+
}
212208

213-
var packageFile = "";
209+
public debugOnDevice(platform: string): IFuture<void> {
210+
return (() => {
211+
platform = platform.toLowerCase();
214212

215-
if (options["debug-brk"]) {
216-
this.preparePlatform(platform).wait();
213+
var packageFile = "";
217214

218-
var platformData = this.$platformsData.getPlatformData(platform);
215+
if (options["debug-brk"]) {
216+
this.preparePlatform(platform).wait();
219217

220-
this.buildPlatform(platform).wait();
218+
var platformData = this.$platformsData.getPlatformData(platform);
221219

222-
packageFile = this.getLatestApplicationPackageForDevice(platformData).wait().packageName;
223-
this.$logger.out("Using ", packageFile);
224-
}
220+
this.buildPlatform(platform).wait();
221+
222+
packageFile = this.getLatestApplicationPackageForDevice(platformData).wait().packageName;
223+
this.$logger.out("Using ", packageFile);
224+
}
225225

226-
this.$devicesServices.initialize({platform: platform, deviceId: options.device}).wait();
227-
var action = (device: Mobile.IDevice): IFuture<void> => { return device.debug(packageFile, this.$projectData.projectId); };
228-
this.$devicesServices.execute(action).wait();
226+
this.$devicesServices.initialize({platform: platform, deviceId: options.device}).wait();
227+
var action = (device: Mobile.IDevice): IFuture<void> => { return device.debug(packageFile, this.$projectData.projectId); };
228+
this.$devicesServices.execute(action).wait();
229229

230-
}).future<void>()();
231-
}
230+
}).future<void>()();
231+
}
232232

233233
public removePlatforms(platforms: string[]): IFuture<void> {
234234
return (() => {

test/platform-commands.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ class PlatformsData implements IPlatformsData {
6363

6464
return null;
6565
}
66+
67+
public get availablePlatforms(): any {
68+
return undefined;
69+
}
6670
}
6771

6872
function createTestInjector() {

test/stubs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ export class PlatformsDataStub implements IPlatformsData {
211211
frameworkFilesExtensions: []
212212
};
213213
}
214+
215+
public get availablePlatforms(): any {
216+
return undefined;
217+
}
214218
}
215219

216220
export class PlatformProjectServiceStub implements IPlatformProjectService {

0 commit comments

Comments
 (0)