Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit 11415a5

Browse files
authored
Merge pull request #918 from telerik/plamen5kov/refactoring
Plamen5kov/refactoring
2 parents 29dbbeb + 33a7fdf commit 11415a5

17 files changed

+356
-70
lines changed

bootstrap.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ $injector.require("messagesService", "./services/messages-service");
2222

2323
$injector.require("cancellation", "./services/cancellation");
2424
$injector.require("hooksService", "./services/hooks-service");
25+
$injector.require("emulatorImageService", "./services/emulator-image-service");
2526

2627
$injector.require("httpClient", "./http-client");
2728
$injector.require("childProcess", "./child-process");
@@ -42,8 +43,8 @@ $injector.requireCommand("autocomplete|disable", "./commands/autocompletion");
4243
$injector.requireCommand("autocomplete|status", "./commands/autocompletion");
4344

4445
$injector.requireCommand("device|*list", "./commands/device/list-devices");
45-
$injector.requireCommand("device|android", "./commands/device/list-devices");
46-
$injector.requireCommand("device|ios", "./commands/device/list-devices");
46+
$injector.requireCommand(["device|android", "devices|android"], "./commands/device/list-devices");
47+
$injector.requireCommand(["device|ios", "devices|ios"], "./commands/device/list-devices");
4748

4849
$injector.requireCommand("device|log", "./commands/device/device-log-stream");
4950
$injector.requireCommand("device|run", "./commands/device/run-application");

commands/device/device-log-stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ export class OpenDeviceLogStreamCommand implements ICommand {
2525
}
2626
}
2727

28-
$injector.registerCommand("device|log", OpenDeviceLogStreamCommand);
28+
$injector.registerCommand(["device|log", "devices|log"], OpenDeviceLogStreamCommand);

commands/device/get-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ export class GetFileCommand implements ICommand {
2222
}
2323
}
2424

25-
$injector.registerCommand("device|get-file", GetFileCommand);
25+
$injector.registerCommand(["device|get-file", "devices|get-file"], GetFileCommand);

commands/device/list-applications.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ export class ListApplicationsCommand implements ICommand {
2222
this.$logger.out(output.join(EOL));
2323
}
2424
}
25-
$injector.registerCommand("device|list-applications", ListApplicationsCommand);
25+
$injector.registerCommand(["device|list-applications", "devices|list-applications"], ListApplicationsCommand);

commands/device/list-devices.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ export class ListDevicesCommand implements ICommand {
44
constructor(private $devicesService: Mobile.IDevicesService,
55
private $logger: ILogger,
66
private $stringParameter: ICommandParameter,
7+
private $mobileHelper: Mobile.IMobileHelper,
8+
private $emulatorImageService: Mobile.IEmulatorImageService,
79
private $options: ICommonOptions) { }
810

911
public allowedParameters = [this.$stringParameter];
1012

1113
public async execute(args: string[]): Promise<void> {
14+
if (this.$options.availableDevices) {
15+
await this.$emulatorImageService.listAvailableEmulators(this.$mobileHelper.validatePlatformName(args[0]));
16+
}
17+
18+
this.$logger.out("\nConnected devices & emulators");
1219
let index = 1;
1320
await this.$devicesService.initialize({ platform: args[0], deviceId: null, skipInferPlatform: true });
1421

@@ -35,7 +42,7 @@ export class ListDevicesCommand implements ICommand {
3542
}
3643
}
3744

38-
$injector.registerCommand("device|*list", ListDevicesCommand);
45+
$injector.registerCommand(["device|*list", "devices|*list"], ListDevicesCommand);
3946

4047
class ListAndroidDevicesCommand implements ICommand {
4148
constructor(private $injector: IInjector,
@@ -50,7 +57,7 @@ class ListAndroidDevicesCommand implements ICommand {
5057
}
5158
}
5259

53-
$injector.registerCommand("device|android", ListAndroidDevicesCommand);
60+
$injector.registerCommand(["device|android", "devices|android"], ListAndroidDevicesCommand);
5461

5562
class ListiOSDevicesCommand implements ICommand {
5663
constructor(private $injector: IInjector,
@@ -65,4 +72,4 @@ class ListiOSDevicesCommand implements ICommand {
6572
}
6673
}
6774

68-
$injector.registerCommand("device|ios", ListiOSDevicesCommand);
75+
$injector.registerCommand(["device|ios", "devices|ios"], ListiOSDevicesCommand);

commands/device/list-files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ export class ListFilesCommand implements ICommand {
2323
}
2424
}
2525

26-
$injector.registerCommand("device|list-files", ListFilesCommand);
26+
$injector.registerCommand(["device|list-files", "devices|list-files"], ListFilesCommand);

commands/device/put-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ export class PutFileCommand implements ICommand {
2020
await this.$devicesService.execute(action);
2121
}
2222
}
23-
$injector.registerCommand("device|put-file", PutFileCommand);
23+
$injector.registerCommand(["device|put-file", "devices|put-file"], PutFileCommand);

commands/device/run-application.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ export class RunApplicationOnDeviceCommand implements ICommand {
1919
}
2020
}
2121

22-
$injector.registerCommand("device|run", RunApplicationOnDeviceCommand);
22+
$injector.registerCommand(["device|run", "devices|run"], RunApplicationOnDeviceCommand);

commands/device/stop-application.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ export class StopApplicationOnDeviceCommand implements ICommand {
1414
}
1515
}
1616

17-
$injector.registerCommand("device|stop", StopApplicationOnDeviceCommand);
17+
$injector.registerCommand(["device|stop", "devices|stop"], StopApplicationOnDeviceCommand);

commands/device/uninstall-application.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ export class UninstallApplicationCommand implements ICommand {
1212
await this.$devicesService.execute(action);
1313
}
1414
}
15-
$injector.registerCommand("device|uninstall", UninstallApplicationCommand);
15+
$injector.registerCommand(["device|uninstall", "devices|uninstall"], UninstallApplicationCommand);

definitions/mobile.d.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,16 @@ declare module Mobile {
351351
interface IDevicesService {
352352
hasDevices: boolean;
353353
deviceCount: number;
354+
355+
/**
356+
* Optionally starts emulator depending on the passed options.
357+
* @param {IDevicesServicesInitializationOptions} data Defines wheather to start default or specific emulator.
358+
* @return {Promise<void>}
359+
*/
360+
startEmulatorIfNecessary(data?: Mobile.IDevicesServicesInitializationOptions): Promise<void>;
361+
354362
execute(action: (device: Mobile.IDevice) => Promise<void>, canExecute?: (dev: Mobile.IDevice) => boolean, options?: { allowNoDevices?: boolean }): Promise<void>;
363+
355364
/**
356365
* Initializes DevicesService, so after that device operations could be executed.
357366
* @param {IDevicesServicesInitializationOptions} data Defines the options which will be used for whole devicesService.
@@ -363,7 +372,6 @@ declare module Mobile {
363372
getDevicesForPlatform(platform: string): Mobile.IDevice[];
364373
getDeviceInstances(): Mobile.IDevice[];
365374
getDeviceByDeviceOption(): Mobile.IDevice;
366-
getDeviceByName(name: string): Mobile.IDevice;
367375
isAndroidDevice(device: Mobile.IDevice): boolean;
368376
isiOSDevice(device: Mobile.IDevice): boolean;
369377
isiOSSimulator(device: Mobile.IDevice): boolean;
@@ -375,7 +383,7 @@ declare module Mobile {
375383
getDeviceByIdentifier(identifier: string): Mobile.IDevice;
376384
mapAbstractToTcpPort(deviceIdentifier: string, appIdentifier: string, framework: string): Promise<string>;
377385
detectCurrentlyAttachedDevices(): Promise<void>;
378-
startEmulator(platform?: string): Promise<void>;
386+
startEmulator(platform?: string, emulatorImage?: string): Promise<void>;
379387
isCompanionAppInstalledOnDevices(deviceIdentifiers: string[], framework: string): Promise<IAppInstalledInfo>[];
380388
getDebuggableApps(deviceIdentifiers: string[]): Promise<Mobile.IDeviceApplicationInformation[]>[];
381389
getDebuggableViews(deviceIdentifier: string, appIdentifier: string): Promise<Mobile.IDebugWebViewInfo[]>;
@@ -500,9 +508,10 @@ declare module Mobile {
500508
*/
501509
checkAvailability(dependsOnProject?: boolean): void;
502510

503-
startEmulator(): Promise<string>;
511+
startEmulator(emulatorImage?: string): Promise<string>
504512
runApplicationOnEmulator(app: string, emulatorOptions?: IEmulatorOptions): Promise<any>;
505513
getEmulatorId(): Promise<string>;
514+
getRunningEmulatorId(image: string): Promise<string>;
506515
}
507516

508517
interface IAndroidEmulatorServices extends IEmulatorPlatformServices {
@@ -555,6 +564,24 @@ declare module Mobile {
555564
getAllCapabilities(): IDictionary<Mobile.IPlatformCapabilities>;
556565
}
557566

567+
//todo: plamen5kov: this is a duplicate of an interface (IEmulatorPlatformService) fix after 3.0-RC. nativescript-cli/lib/definitions/emulator-platform-service.d.ts
568+
interface IEmulatorImageService {
569+
listAvailableEmulators(platform: string): Promise<void>;
570+
getEmulatorInfo(platform: string, nameOfId: string): Promise<IEmulatorInfo>;
571+
getiOSEmulators(): Promise<IEmulatorInfo[]>;
572+
getAndroidEmulators(): IEmulatorInfo[];
573+
}
574+
575+
//todo: plamen5kov: this is a duplicate of an interface (IEmulatorInfo) fix after 3.0-RC nativescript-cli/lib/definitions/emulator-platform-service.d.ts
576+
interface IEmulatorInfo {
577+
name: string;
578+
version: string;
579+
platform: string;
580+
id: string;
581+
type: string;
582+
isRunning?: boolean;
583+
}
584+
558585
interface IMobileHelper {
559586
platformNames: string[];
560587
isAndroidPlatform(platform: string): boolean;

mobile/android/android-emulator-services.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ class AndroidEmulatorServices implements Mobile.IAndroidEmulatorServices {
9999
}
100100
}
101101

102-
public async startEmulator(): Promise<string> {
102+
public async startEmulator(emulatorImage?: string): Promise<string> {
103103
if (this.$options.avd && this.$options.geny) {
104104
this.$errors.fail("You cannot specify both --avd and --geny options. Please use only one of them.");
105105
}
106106

107107
let emulatorId: string = null;
108108

109-
let image = this.getEmulatorImage();
109+
let image = this.getEmulatorImage(emulatorImage);
110110
if (image) {
111111
// start the emulator, if needed
112112
emulatorId = await this.startEmulatorInstance(image);
@@ -117,7 +117,7 @@ class AndroidEmulatorServices implements Mobile.IAndroidEmulatorServices {
117117
// unlock screen
118118
await this.unlockScreen(emulatorId);
119119
} else {
120-
this.$errors.fail("Could not find an emulator image to run your project.");
120+
this.$errors.fail(`Could not find an emulator image or identifier to run your project. Please run: "${this.$staticConfig.CLIENT_NAME} device <platform> --available-devices"`);
121121
}
122122

123123
return emulatorId;
@@ -151,8 +151,8 @@ class AndroidEmulatorServices implements Mobile.IAndroidEmulatorServices {
151151
}
152152
}
153153

154-
private getEmulatorImage(): string {
155-
let image = this.$options.avd || this.$options.geny || this.getBestFit();
154+
private getEmulatorImage(suggestedImage?: string): string {
155+
let image = this.$options.avd || this.$options.geny || this.getBestFit(suggestedImage);
156156
return image;
157157
}
158158

@@ -193,7 +193,7 @@ class AndroidEmulatorServices implements Mobile.IAndroidEmulatorServices {
193193
});
194194
}
195195

196-
private async getRunningEmulatorId(image: string): Promise<string> {
196+
public async getRunningEmulatorId(image: string): Promise<string> {
197197
let runningEmulators = await this.getRunningEmulators();
198198
if (runningEmulators.length === 0) {
199199
return "";
@@ -287,7 +287,7 @@ class AndroidEmulatorServices implements Mobile.IAndroidEmulatorServices {
287287
let emulatorId = await this.getRunningEmulatorId(image);
288288
this.endTimeEpoch = helpers.getCurrentEpochTime() + this.$utils.getMilliSecondsTimeout(AndroidEmulatorServices.TIMEOUT_SECONDS);
289289
if (emulatorId) {
290-
// If there's already a running instance of this image, we'll just deploy the app to it.
290+
// If there's already a running instance of this image, we'll just return the emulatorId.
291291
return emulatorId;
292292
}
293293

@@ -410,10 +410,15 @@ class AndroidEmulatorServices implements Mobile.IAndroidEmulatorServices {
410410
return result;
411411
}
412412

413-
private getBestFit(): string {
413+
private getBestFit(suggestedImage?: string): string {
414414
let minVersion = this.$emulatorSettingsService.minVersion;
415415

416-
let best = _(this.getAvds())
416+
let avdResults = this.getAvds();
417+
if (suggestedImage) {
418+
avdResults = avdResults.filter(avd => avd === suggestedImage);
419+
}
420+
421+
let best = _(avdResults)
417422
.map(avd => this.getInfoFromAvd(avd))
418423
.filter(avd => !!avd)
419424
.maxBy(avd => avd.targetNum);

mobile/ios/simulator/ios-emulator-services.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class IosEmulatorServices implements Mobile.IiOSSimulatorService {
1212
return "";
1313
}
1414

15+
public async getRunningEmulatorId(image: string): Promise<string> {
16+
//todo: plamen5kov: fix later if necessary
17+
return "";
18+
}
19+
1520
public async checkDependencies(): Promise<void> {
1621
return;
1722
}
@@ -29,8 +34,11 @@ class IosEmulatorServices implements Mobile.IiOSSimulatorService {
2934
}
3035
}
3136

32-
public async startEmulator(): Promise<string> {
33-
return this.$iOSSimResolver.iOSSim.startSimulator();
37+
public async startEmulator(emulatorImage?: string): Promise<string> {
38+
return this.$iOSSimResolver.iOSSim.startSimulator({
39+
id: emulatorImage,
40+
state: "None"
41+
});
3442
}
3543

3644
public runApplicationOnEmulator(app: string, emulatorOptions?: Mobile.IEmulatorOptions): Promise<any> {

0 commit comments

Comments
 (0)