Skip to content

deployOnEmulator refactored #1817

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
Jun 8, 2016
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
10 changes: 5 additions & 5 deletions docs/man_pages/project/testing/emulate-android.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ emulate android

Usage | Synopsis
---|---
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]`
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]`
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]`
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]`

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. <% } %>
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. <% } %>

### Options
* `--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.
* `--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.
* `--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.
* `--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.
* `--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.
* `--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.
* `--release` - If set, produces a release build. Otherwise, produces a debug build. When set, you must also specify the `--key-store-*` options.
* `--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.
Expand Down Expand Up @@ -45,7 +45,7 @@ Before running your app in the Android emulator from the Android SDK, verify tha

### Command Limitations

* 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.
* 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.
* When the `--release` flag is set, you must also specify all `--key-store-*` options.

### Related Commands
Expand Down
51 changes: 39 additions & 12 deletions lib/services/platform-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
///<reference path="../.d.ts"/>
"use strict";

import * as child_process from "child_process";
import * as path from "path";
import * as shell from "shelljs";
import * as constants from "../constants";
Expand Down Expand Up @@ -35,7 +34,8 @@ export class PlatformService implements IPlatformService {
private $xmlValidator: IXmlValidator,
private $npm: INodePackageManager,
private $sysInfo: ISysInfo,
private $staticConfig: Config.IStaticConfig) { }
private $staticConfig: Config.IStaticConfig,
private $childProcess: IChildProcess) { }

public addPlatforms(platforms: string[]): IFuture<void> {
return (() => {
Expand Down Expand Up @@ -485,20 +485,47 @@ export class PlatformService implements IPlatformService {
public deployOnEmulator(platform: string, buildConfig?: IBuildConfig): IFuture<void> {
platform = platform.toLowerCase();

if (this.$options.availableDevices) {
if (this.$options.avd) {
this.$logger.warn(`Option --avd is no longer supported. Please use --device isntead!`);
}

if (this.$options.availableDevices || this.$options.device || this.$options.avd) {
return (() => {
let callback = (error: Error, stdout: Buffer, stderr: Buffer) => {
if (error !== null) {
this.$errors.fail(error);
} else {
this.$logger.info(stdout);
}
};
let devices: string;

if (this.$mobileHelper.isiOSPlatform(platform)) {
child_process.exec("instruments -s devices", callback);
devices = this.$childProcess.exec("instruments -s devices").wait();
} else if (this.$mobileHelper.isAndroidPlatform(platform)) {
child_process.exec("android list avd", callback);
let androidPath = path.join(process.env.ANDROID_HOME, "tools", "android");
devices = this.$childProcess.exec(`${androidPath} list avd`).wait();
}

if(this.$options.availableDevices) {
this.$logger.info(devices);
}

if(this.$options.device || this.$options.avd) {
let deviceName = this.$options.device || this.$options.avd;

if(devices.indexOf(deviceName) !== -1) {
let packageFile: string, logFilePath: string;
let platformData = this.$platformsData.getPlatformData(platform);
let emulatorServices = platformData.emulatorServices;

emulatorServices.checkAvailability().wait();
emulatorServices.checkDependencies().wait();

this.buildPlatform(platform, buildConfig).wait();

packageFile = this.getLatestApplicationPackageForEmulator(platformData).wait().packageName;
this.$logger.out("Using ", packageFile);

logFilePath = path.join(platformData.projectRoot, this.$projectData.projectName, "emulator.log");

emulatorServices.runApplicationOnEmulator(packageFile, { stderrFilePath: logFilePath, stdoutFilePath: logFilePath, appId: this.$projectData.projectId }).wait();
} else {
this.$errors.fail(`Cannot find device with name: ${this.$options.device}.`);
}
}
}).future<void>()();
} else {
Expand Down
2 changes: 2 additions & 0 deletions test/platform-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {DeviceAppDataProvider} from "../lib/providers/device-app-data-provider";
import {MobilePlatformsCapabilities} from "../lib/mobile-platforms-capabilities";
import {DevicePlatformsConstants} from "../lib/common/mobile/device-platforms-constants";
import { XmlValidator } from "../lib/xml-validator";
import * as ChildProcessLib from "../lib/common/child-process";

let isCommandExecuted = true;

Expand Down Expand Up @@ -139,6 +140,7 @@ function createTestInjector() {
testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);
testInjector.register("xmlValidator", XmlValidator);
testInjector.register("npm", {});
testInjector.register("childProcess", ChildProcessLib.ChildProcess);

return testInjector;
}
Expand Down
2 changes: 2 additions & 0 deletions test/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {DeviceAppDataProvider} from "../lib/providers/device-app-data-provider";
import {MobilePlatformsCapabilities} from "../lib/mobile-platforms-capabilities";
import {DevicePlatformsConstants} from "../lib/common/mobile/device-platforms-constants";
import { XmlValidator } from "../lib/xml-validator";
import * as ChildProcessLib from "../lib/common/child-process";

require("should");
let temp = require("temp");
Expand Down Expand Up @@ -73,6 +74,7 @@ function createTestInjector() {
testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);
testInjector.register("xmlValidator", XmlValidator);
testInjector.register("npm", {});
testInjector.register("childProcess", ChildProcessLib.ChildProcess);

return testInjector;
}
Expand Down