Skip to content

Commit 0d6d745

Browse files
author
Vladimir Enchev
committed
deployOnEmulator refactored
1 parent b29fa09 commit 0d6d745

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

docs/man_pages/project/testing/emulate-android.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ emulate android
33

44
Usage | Synopsis
55
---|---
6-
Run in the native emulator | `$ tns emulate android [--avd <Name>] [--path <Directory>] [--timeout <Seconds>] [--key-store-path <File Path> --key-store-password <Password> --key-store-alias <Name> --key-store-alias-password <Password>] [--release] [--justlaunch]`
6+
Run in the native emulator | `$ tns emulate android [--device <Name>] [--path <Directory>] [--timeout <Seconds>] [--key-store-path <File Path> --key-store-password <Password> --key-store-alias <Name> --key-store-alias-password <Password>] [--release] [--justlaunch]`
77
Run in Genymotion | `$ tns emulate android --geny <GenyName> [--path <Directory>] [--timeout <Seconds>] [--key-store-path <File Path> --key-store-password <Password> --key-store-alias <Name> --key-store-alias-password <Password>] [--release] [--justlaunch]`
88
Run in the default Android virtual device or in a currently running emulator | `$ tns emulate android [--path <Directory>] [--timeout <Seconds>] [--key-store-path <File Path> --key-store-password <Password> --key-store-alias <Name> --key-store-alias-password <Password>] [--release] [--justlaunch]`
99

10-
Builds the specified project and runs it in the native emulator from the Android SDK or Genymotion. While your app is running, prints the output from the application in the console.<% if(isHtml) { %>If you do not select an Android virtual device (AVD) with the `--avd` option or a Genymotion virtual device with the `--geny` option, your app runs in the default AVD or a currently running emulator, if any. <% } %>
10+
Builds the specified project and runs it in the native emulator from the Android SDK or Genymotion. While your app is running, prints the output from the application in the console.<% if(isHtml) { %>If you do not select an Android virtual device (AVD) with the `--device` option or a Genymotion virtual device with the `--geny` option, your app runs in the default AVD or a currently running emulator, if any. <% } %>
1111

1212
### Options
1313
* `--path` - Specifies the directory that contains the project. If not specified, the project is searched for in the current directory and all directories above it.
14-
* `--avd` - Sets the Android virtual device on which you want to run your app. You can set only one device at a time. You cannot use `--avd` and `--geny` simultaneously.
15-
* `--geny` - Sets the Genymotion virtual device on which you want to run your app. You can set only one device at a time. You cannot use `--avd` and `--geny` simultaneously.
14+
* `--device` - Sets the Android virtual device on which you want to run your app. You can set only one device at a time. You cannot use `--device` and `--geny` simultaneously.
15+
* `--geny` - Sets the Genymotion virtual device on which you want to run your app. You can set only one device at a time. You cannot use `--device` and `--geny` simultaneously.
1616
* `--timeout` - Sets the number of seconds that the NativeScript CLI will wait for the virtual device to boot before quitting the operation and releasing the console. If not set, the default timeout is 120 seconds. To wait indefinitely, set 0.
1717
* `--release` - If set, produces a release build. Otherwise, produces a debug build. When set, you must also specify the `--key-store-*` options.
1818
* `--key-store-path` - Specifies the file path to the keystore file (P12) which you want to use to code sign your APK. You can use the `--key-store-*` options along with `--release` to produce a signed release build. You need to specify all `--key-store-*` options.
@@ -45,7 +45,7 @@ Before running your app in the Android emulator from the Android SDK, verify tha
4545

4646
### Command Limitations
4747

48-
* You can run this command for one virtual device at a time. To test your app on multiple Android virtual devices, run `$ tns emulate android --avd <Name>` or `$ tns emulate android --geny <GenyName>` for each virtual device.
48+
* You can run this command for one virtual device at a time. To test your app on multiple Android virtual devices, run `$ tns emulate android --device <Name>` or `$ tns emulate android --geny <GenyName>` for each virtual device.
4949
* When the `--release` flag is set, you must also specify all `--key-store-*` options.
5050

5151
### Related Commands

lib/services/platform-service.ts

+31-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
///<reference path="../.d.ts"/>
22
"use strict";
33

4-
import * as child_process from "child_process";
54
import * as path from "path";
65
import * as shell from "shelljs";
76
import * as constants from "../constants";
@@ -35,7 +34,8 @@ export class PlatformService implements IPlatformService {
3534
private $xmlValidator: IXmlValidator,
3635
private $npm: INodePackageManager,
3736
private $sysInfo: ISysInfo,
38-
private $staticConfig: Config.IStaticConfig) { }
37+
private $staticConfig: Config.IStaticConfig,
38+
private $childProcess: IChildProcess) { }
3939

4040
public addPlatforms(platforms: string[]): IFuture<void> {
4141
return (() => {
@@ -485,20 +485,39 @@ export class PlatformService implements IPlatformService {
485485
public deployOnEmulator(platform: string, buildConfig?: IBuildConfig): IFuture<void> {
486486
platform = platform.toLowerCase();
487487

488-
if (this.$options.availableDevices) {
488+
if (this.$options.avd) {
489+
this.$logger.warn(`Option --avd is no longer supported. Please use --device isntead!`);
490+
}
491+
492+
if (this.$options.availableDevices || this.$options.device || this.$options.avd) {
489493
return (() => {
490-
let callback = (error: Error, stdout: Buffer, stderr: Buffer) => {
491-
if (error !== null) {
492-
this.$errors.fail(error);
493-
} else {
494-
this.$logger.info(stdout);
495-
}
496-
};
494+
let devices: string;
497495

498496
if (this.$mobileHelper.isiOSPlatform(platform)) {
499-
child_process.exec("instruments -s devices", callback);
497+
devices = this.$childProcess.exec("instruments -s devices").wait();
500498
} else if (this.$mobileHelper.isAndroidPlatform(platform)) {
501-
child_process.exec("android list avd", callback);
499+
let androidPath = path.join(process.env.ANDROID_HOME, "tools", "android");
500+
devices = this.$childProcess.exec(`${androidPath} list avd`).wait();
501+
}
502+
503+
if(this.$options.availableDevices) {
504+
this.$logger.info(devices);
505+
}
506+
507+
if(this.$options.device || this.$options.avd) {
508+
let deviceName = this.$options.device || this.$options.avd;
509+
510+
if(devices.indexOf(deviceName) !== -1) {
511+
let packageFile: string, logFilePath: string;
512+
let platformData = this.$platformsData.getPlatformData(platform);
513+
let emulatorServices = platformData.emulatorServices;
514+
515+
emulatorServices.checkAvailability().wait();
516+
emulatorServices.checkDependencies().wait();
517+
emulatorServices.runApplicationOnEmulator(packageFile, { stderrFilePath: logFilePath, stdoutFilePath: logFilePath, appId: this.$projectData.projectId }).wait();
518+
} else {
519+
this.$errors.fail(`Cannot find device with name: ${this.$options.device}.`);
520+
}
502521
}
503522
}).future<void>()();
504523
} else {

test/platform-commands.ts

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {DeviceAppDataProvider} from "../lib/providers/device-app-data-provider";
2121
import {MobilePlatformsCapabilities} from "../lib/mobile-platforms-capabilities";
2222
import {DevicePlatformsConstants} from "../lib/common/mobile/device-platforms-constants";
2323
import { XmlValidator } from "../lib/xml-validator";
24+
import * as ChildProcessLib from "../lib/common/child-process";
2425

2526
let isCommandExecuted = true;
2627

@@ -139,6 +140,7 @@ function createTestInjector() {
139140
testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);
140141
testInjector.register("xmlValidator", XmlValidator);
141142
testInjector.register("npm", {});
143+
testInjector.register("childProcess", ChildProcessLib.ChildProcess);
142144

143145
return testInjector;
144146
}

test/platform-service.ts

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {DeviceAppDataProvider} from "../lib/providers/device-app-data-provider";
2020
import {MobilePlatformsCapabilities} from "../lib/mobile-platforms-capabilities";
2121
import {DevicePlatformsConstants} from "../lib/common/mobile/device-platforms-constants";
2222
import { XmlValidator } from "../lib/xml-validator";
23+
import * as ChildProcessLib from "../lib/common/child-process";
2324

2425
require("should");
2526
let temp = require("temp");
@@ -73,6 +74,7 @@ function createTestInjector() {
7374
testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);
7475
testInjector.register("xmlValidator", XmlValidator);
7576
testInjector.register("npm", {});
77+
testInjector.register("childProcess", ChildProcessLib.ChildProcess);
7678

7779
return testInjector;
7880
}

0 commit comments

Comments
 (0)