Skip to content

Expose emulator api #3733

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 2 commits into from
Jul 19, 2018
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
42 changes: 42 additions & 0 deletions PublicAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const tns = require("nativescript");
* [sysInfo](#sysinfo)
* [getSupportedNodeVersionRange](#getsupportednodeversionrange)
* [getSystemWarnings](#getsystemwarnings)
* [devicesService](#devicesService)
* [getAvailableEmulators](#getAvailableEmulators)
* [startEmulator](#startEmulator)

## Module projectService

Expand Down Expand Up @@ -1198,6 +1201,45 @@ tns.sysInfo.getSystemWarnings()
.catch(err => console.error(`Error while trying to get system warnings: ${err}`));
```

## devicesService
The `devicesService` module allows interaction with devices and emulators. You can get a list of the available emulators or start a specific emulator.

### getAvailableEmulators
The `getAvailableEmulators` method returns object of all running and available emulators. The result is in the following format:
```JavaScript
{
android: {
devices: Mobile.IDeviceInfo[],
errors: string[]
},
ios: {
devices: Mobile.IDeviceInfo[],
errors: string[]
}
}
```

This method accepts platform parameter. If provided only devices by specified platform will be returned.

* Usage
```TypeScript
tns.devicesService.getAvailableEmulators()
.then(availableEmulatorsOutput => {
Object.keys(availableEmulatorsOutput)
.forEach(platform => {
availableEmulatorsOutput[platform].devices.forEach(device => console.log(device));
})
})
```

### startEmulator
The `startEmulator` method starts the emulator specified by provided options. Returns an array of errors if something unexpected happens or null otherwise.
* Usage
```TypeScript
tns.devicesService.startEmulator({imageIdentifier: "my emulator imageIdentifier"})
.then(errors => { });
```

## How to add a new method to Public API
CLI is designed as command line tool and when it is used as a library, it does not give you access to all of the methods. This is mainly implementation detail. Most of the CLI's code is created to work in command line, not as a library, so before adding method to public API, most probably it will require some modification.
For example the `$options` injected module contains information about all `--` options passed on the terminal. When the CLI is used as a library, the options are not populated. Before adding method to public API, make sure its implementation does not rely on `$options`.
Expand Down
2 changes: 0 additions & 2 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ $injector.requireCommand("update", "./commands/update");
$injector.require("iOSLogFilter", "./services/ios-log-filter");
$injector.require("projectChangesService", "./services/project-changes-service");

$injector.require("emulatorPlatformService", "./services/emulator-platform-service");

$injector.require("pbxprojDomXcode", "./node/pbxproj-dom-xcode");
$injector.require("xcode", "./node/xcode");

Expand Down
5 changes: 2 additions & 3 deletions lib/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ export class RunAndroidCommand implements ICommand {
private $injector: IInjector,
private $platformService: IPlatformService,
private $projectData: IProjectData,
private $options: IOptions) {
}
private $options: IOptions) { }

public async execute(args: string[]): Promise<void> {
public execute(args: string[]): Promise<void> {
return this.runCommand.execute(args);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/common
Submodule common updated 33 files
+9 −2 appbuilder/device-emitter.ts
+6 −1 bootstrap.ts
+10 −0 child-process.ts
+21 −3 commands/device/list-devices.ts
+39 −0 constants.ts
+2 −1 declarations.d.ts
+327 −40 definitions/mobile.d.ts
+17 −0 helpers.ts
+22 −0 mobile/android/android-debug-bridge.ts
+10 −4 mobile/android/android-device.ts
+110 −472 mobile/android/android-emulator-services.ts
+82 −0 mobile/android/android-ini-file-parser.ts
+315 −0 mobile/android/android-virtual-device-service.ts
+179 −0 mobile/android/genymotion/genymotion-service.ts
+107 −0 mobile/android/genymotion/virtualbox-service.ts
+77 −0 mobile/emulator-helper.ts
+3 −2 mobile/ios/device/ios-device.ts
+85 −34 mobile/ios/simulator/ios-emulator-services.ts
+1 −1 mobile/mobile-core/android-device-discovery.ts
+52 −0 mobile/mobile-core/android-emulator-discovery.ts
+0 −4 mobile/mobile-core/device-discovery.ts
+102 −35 mobile/mobile-core/devices-service.ts
+44 −2 mobile/mobile-core/ios-simulator-discovery.ts
+4 −0 mobile/mobile-helper.ts
+23 −28 mobile/wp8/wp8-emulator-services.ts
+0 −136 services/emulator-image-service.ts
+16 −16 test/unit-tests/mobile/android-device-discovery.ts
+208 −0 test/unit-tests/mobile/android-emulator-service.ts
+339 −0 test/unit-tests/mobile/android-virtual-device-service.ts
+19 −4 test/unit-tests/mobile/devices-service.ts
+339 −0 test/unit-tests/mobile/genymotion/genymotion-service.ts
+73 −0 test/unit-tests/mobile/genymotion/virtualbox-service.ts
+2 −1 test/unit-tests/mobile/ios-simulator-discovery.ts
4 changes: 4 additions & 0 deletions lib/definitions/debug.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ interface IDebugOptions {
* If not provided, defaults to 10 seconds.
*/
timeout?: string;
/**
* The sdk version of the emulator.
*/
sdk?: string;
}

/**
Expand Down
15 changes: 0 additions & 15 deletions lib/definitions/emulator-platform-service.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion lib/definitions/platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ interface ITrackPlatformAction extends IPlatform {
interface IPlatformData {
frameworkPackageName: string;
platformProjectService: IPlatformProjectService;
emulatorServices: Mobile.IEmulatorPlatformServices;
projectRoot: string;
normalizedPlatformName: string;
appDestinationDirectoryPath: string;
Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/livesync-command-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export class LiveSyncCommandHelper implements ILiveSyncCommandHelper {
platform,
emulator,
skipDeviceDetectionInterval: true,
skipInferPlatform: !platform
skipInferPlatform: !platform,
sdk: this.$options.sdk
});

const devices = this.$devicesService.getDeviceInstances()
Expand Down
4 changes: 1 addition & 3 deletions lib/services/android-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject

private isAndroidStudioTemplate: boolean;

constructor(private $androidEmulatorServices: Mobile.IEmulatorPlatformServices,
private $androidToolsInfo: IAndroidToolsInfo,
constructor(private $androidToolsInfo: IAndroidToolsInfo,
private $childProcess: IChildProcess,
private $errors: IErrors,
$fs: IFileSystem,
Expand Down Expand Up @@ -72,7 +71,6 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
normalizedPlatformName: "Android",
appDestinationDirectoryPath: path.join(...appDestinationDirectoryArr),
platformProjectService: this,
emulatorServices: this.$androidEmulatorServices,
projectRoot: projectRoot,
deviceBuildOutputPath: path.join(...deviceBuildOutputArr),
getValidBuildOutputData: (buildOptions: IBuildOutputOptions): IValidBuildOutputData => {
Expand Down
179 changes: 0 additions & 179 deletions lib/services/emulator-platform-service.ts

This file was deleted.

5 changes: 4 additions & 1 deletion lib/services/ios-debug-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
args: args,
appId: debugData.applicationIdentifier,
skipInstall: true,
device: debugData.deviceIdentifier
device: debugData.deviceIdentifier,
justlaunch: debugOptions.justlaunch,
timeout: debugOptions.timeout,
sdk: debugOptions.sdk
});

const pid = getPidFromiOSSimulatorLogs(debugData.applicationIdentifier, launchResult);
Expand Down
2 changes: 0 additions & 2 deletions lib/services/ios-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
private $cocoapodsService: ICocoaPodsService,
private $errors: IErrors,
private $logger: ILogger,
private $iOSEmulatorServices: Mobile.IEmulatorPlatformServices,
private $injector: IInjector,
$projectDataService: IProjectDataService,
private $prompter: IPrompter,
Expand Down Expand Up @@ -76,7 +75,6 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
normalizedPlatformName: "iOS",
appDestinationDirectoryPath: path.join(projectRoot, projectData.projectName),
platformProjectService: this,
emulatorServices: this.$iOSEmulatorServices,
projectRoot: projectRoot,
deviceBuildOutputPath: path.join(projectRoot, constants.BUILD_DIR, "device"),
emulatorBuildOutputPath: path.join(projectRoot, constants.BUILD_DIR, "emulator"),
Expand Down
5 changes: 5 additions & 0 deletions test/ios-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ function createTestInjector(projectPath: string, projectName: string, xcode?: IX
testInjector.register("httpClient", {});
testInjector.register("platformEnvironmentRequirements", {});
testInjector.register("plistParser", {});
testInjector.register("androidEmulatorServices", {});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we register Android specific services in iOS project service?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this tests relies on real DevicesService -

testInjector.register("devicesService", DevicesService);

On the other hand devicesService injects androidEmulatorServices so we need it here.

testInjector.register("androidEmulatorDiscovery", {
on: () => ({})
});
testInjector.register("emulatorHelper", {});

return testInjector;
}
Expand Down
1 change: 0 additions & 1 deletion test/npm-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ function createTestInjector(): IInjector {
testInjector.register("xmlValidator", XmlValidator);
testInjector.register("config", StaticConfigLib.Configuration);
testInjector.register("projectChangesService", ProjectChangesLib.ProjectChangesService);
testInjector.register("emulatorPlatformService", stubs.EmulatorPlatformService);
testInjector.register("analyticsService", {
track: async (): Promise<any> => undefined
});
Expand Down
2 changes: 0 additions & 2 deletions test/platform-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class PlatformData implements IPlatformData {
// intentionally left blank
}
};
emulatorServices: Mobile.IEmulatorPlatformServices = null;
projectRoot = "";
deviceBuildOutputPath = "";
getValidBuildOutputData = (buildOptions: IBuildOutputOptions) => ({ packageNames: [""] });
Expand Down Expand Up @@ -143,7 +142,6 @@ function createTestInjector() {
testInjector.register("preparePlatformJSService", {});
testInjector.register("childProcess", ChildProcessLib.ChildProcess);
testInjector.register("projectChangesService", ProjectChangesLib.ProjectChangesService);
testInjector.register("emulatorPlatformService", stubs.EmulatorPlatformService);
testInjector.register("analyticsService", {
track: async () => async (): Promise<any[]> => undefined
});
Expand Down
Loading