diff --git a/docs/man_pages/project/testing/emulate-android.md b/docs/man_pages/project/testing/emulate-android.md index 8a29790535..d2c6c55bcc 100644 --- a/docs/man_pages/project/testing/emulate-android.md +++ b/docs/man_pages/project/testing/emulate-android.md @@ -3,16 +3,16 @@ emulate android Usage | Synopsis ---|--- -Run in the native emulator | `$ tns emulate android [--avd ] [--path ] [--timeout ] [--key-store-path --key-store-password --key-store-alias --key-store-alias-password ] [--release] [--justlaunch]` +Run in the native emulator | `$ tns emulate android [--device ] [--path ] [--timeout ] [--key-store-path --key-store-password --key-store-alias --key-store-alias-password ] [--release] [--justlaunch]` Run in Genymotion | `$ tns emulate android --geny [--path ] [--timeout ] [--key-store-path --key-store-password --key-store-alias --key-store-alias-password ] [--release] [--justlaunch]` Run in the default Android virtual device or in a currently running emulator | `$ tns emulate android [--path ] [--timeout ] [--key-store-path --key-store-password --key-store-alias --key-store-alias-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. @@ -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 ` or `$ tns emulate android --geny ` 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 ` or `$ tns emulate android --geny ` for each virtual device. * When the `--release` flag is set, you must also specify all `--key-store-*` options. ### Related Commands diff --git a/lib/services/platform-service.ts b/lib/services/platform-service.ts index 5b61922ebe..70d7d16502 100644 --- a/lib/services/platform-service.ts +++ b/lib/services/platform-service.ts @@ -1,7 +1,6 @@ /// "use strict"; -import * as child_process from "child_process"; import * as path from "path"; import * as shell from "shelljs"; import * as constants from "../constants"; @@ -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 { return (() => { @@ -485,20 +485,47 @@ export class PlatformService implements IPlatformService { public deployOnEmulator(platform: string, buildConfig?: IBuildConfig): IFuture { 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()(); } else { diff --git a/test/platform-commands.ts b/test/platform-commands.ts index 36ce0841b9..740118250b 100644 --- a/test/platform-commands.ts +++ b/test/platform-commands.ts @@ -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; @@ -139,6 +140,7 @@ function createTestInjector() { testInjector.register("devicePlatformsConstants", DevicePlatformsConstants); testInjector.register("xmlValidator", XmlValidator); testInjector.register("npm", {}); + testInjector.register("childProcess", ChildProcessLib.ChildProcess); return testInjector; } diff --git a/test/platform-service.ts b/test/platform-service.ts index 0856b6974d..b5c965355e 100644 --- a/test/platform-service.ts +++ b/test/platform-service.ts @@ -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"); @@ -73,6 +74,7 @@ function createTestInjector() { testInjector.register("devicePlatformsConstants", DevicePlatformsConstants); testInjector.register("xmlValidator", XmlValidator); testInjector.register("npm", {}); + testInjector.register("childProcess", ChildProcessLib.ChildProcess); return testInjector; }