Skip to content

Commit f73ec8e

Browse files
author
Fatme
authored
Merge pull request #3733 from NativeScript/fatme/emulator-api
Expose emulator api
2 parents f84897f + 27b4c22 commit f73ec8e

17 files changed

+61
-238
lines changed

PublicAPI.md

+42
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const tns = require("nativescript");
5353
* [sysInfo](#sysinfo)
5454
* [getSupportedNodeVersionRange](#getsupportednodeversionrange)
5555
* [getSystemWarnings](#getsystemwarnings)
56+
* [devicesService](#devicesService)
57+
* [getAvailableEmulators](#getAvailableEmulators)
58+
* [startEmulator](#startEmulator)
5659

5760
## Module projectService
5861

@@ -1198,6 +1201,45 @@ tns.sysInfo.getSystemWarnings()
11981201
.catch(err => console.error(`Error while trying to get system warnings: ${err}`));
11991202
```
12001203
1204+
## devicesService
1205+
The `devicesService` module allows interaction with devices and emulators. You can get a list of the available emulators or start a specific emulator.
1206+
1207+
### getAvailableEmulators
1208+
The `getAvailableEmulators` method returns object of all running and available emulators. The result is in the following format:
1209+
```JavaScript
1210+
{
1211+
android: {
1212+
devices: Mobile.IDeviceInfo[],
1213+
errors: string[]
1214+
},
1215+
ios: {
1216+
devices: Mobile.IDeviceInfo[],
1217+
errors: string[]
1218+
}
1219+
}
1220+
```
1221+
1222+
This method accepts platform parameter. If provided only devices by specified platform will be returned.
1223+
1224+
* Usage
1225+
```TypeScript
1226+
tns.devicesService.getAvailableEmulators()
1227+
.then(availableEmulatorsOutput => {
1228+
Object.keys(availableEmulatorsOutput)
1229+
.forEach(platform => {
1230+
availableEmulatorsOutput[platform].devices.forEach(device => console.log(device));
1231+
})
1232+
})
1233+
```
1234+
1235+
### startEmulator
1236+
The `startEmulator` method starts the emulator specified by provided options. Returns an array of errors if something unexpected happens or null otherwise.
1237+
* Usage
1238+
```TypeScript
1239+
tns.devicesService.startEmulator({imageIdentifier: "my emulator imageIdentifier"})
1240+
.then(errors => { });
1241+
```
1242+
12011243
## How to add a new method to Public API
12021244
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.
12031245
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`.

lib/bootstrap.ts

-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ $injector.requireCommand("update", "./commands/update");
140140
$injector.require("iOSLogFilter", "./services/ios-log-filter");
141141
$injector.require("projectChangesService", "./services/project-changes-service");
142142

143-
$injector.require("emulatorPlatformService", "./services/emulator-platform-service");
144-
145143
$injector.require("pbxprojDomXcode", "./node/pbxproj-dom-xcode");
146144
$injector.require("xcode", "./node/xcode");
147145

lib/commands/run.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,9 @@ export class RunAndroidCommand implements ICommand {
9494
private $injector: IInjector,
9595
private $platformService: IPlatformService,
9696
private $projectData: IProjectData,
97-
private $options: IOptions) {
98-
}
97+
private $options: IOptions) { }
9998

100-
public async execute(args: string[]): Promise<void> {
99+
public execute(args: string[]): Promise<void> {
101100
return this.runCommand.execute(args);
102101
}
103102

lib/definitions/debug.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ interface IDebugOptions {
9191
* If not provided, defaults to 10 seconds.
9292
*/
9393
timeout?: string;
94+
/**
95+
* The sdk version of the emulator.
96+
*/
97+
sdk?: string;
9498
}
9599

96100
/**

lib/definitions/emulator-platform-service.d.ts

-15
This file was deleted.

lib/definitions/platform.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ interface ITrackPlatformAction extends IPlatform {
264264
interface IPlatformData {
265265
frameworkPackageName: string;
266266
platformProjectService: IPlatformProjectService;
267-
emulatorServices: Mobile.IEmulatorPlatformServices;
268267
projectRoot: string;
269268
normalizedPlatformName: string;
270269
appDestinationDirectoryPath: string;

lib/helpers/livesync-command-helper.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export class LiveSyncCommandHelper implements ILiveSyncCommandHelper {
2727
platform,
2828
emulator,
2929
skipDeviceDetectionInterval: true,
30-
skipInferPlatform: !platform
30+
skipInferPlatform: !platform,
31+
sdk: this.$options.sdk
3132
});
3233

3334
const devices = this.$devicesService.getDeviceInstances()

lib/services/android-project-service.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
1717

1818
private isAndroidStudioTemplate: boolean;
1919

20-
constructor(private $androidEmulatorServices: Mobile.IEmulatorPlatformServices,
21-
private $androidToolsInfo: IAndroidToolsInfo,
20+
constructor(private $androidToolsInfo: IAndroidToolsInfo,
2221
private $childProcess: IChildProcess,
2322
private $errors: IErrors,
2423
$fs: IFileSystem,
@@ -72,7 +71,6 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
7271
normalizedPlatformName: "Android",
7372
appDestinationDirectoryPath: path.join(...appDestinationDirectoryArr),
7473
platformProjectService: this,
75-
emulatorServices: this.$androidEmulatorServices,
7674
projectRoot: projectRoot,
7775
deviceBuildOutputPath: path.join(...deviceBuildOutputArr),
7876
getValidBuildOutputData: (buildOptions: IBuildOutputOptions): IValidBuildOutputData => {

lib/services/emulator-platform-service.ts

-179
This file was deleted.

lib/services/ios-debug-service.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
129129
args: args,
130130
appId: debugData.applicationIdentifier,
131131
skipInstall: true,
132-
device: debugData.deviceIdentifier
132+
device: debugData.deviceIdentifier,
133+
justlaunch: debugOptions.justlaunch,
134+
timeout: debugOptions.timeout,
135+
sdk: debugOptions.sdk
133136
});
134137

135138
const pid = getPidFromiOSSimulatorLogs(debugData.applicationIdentifier, launchResult);

lib/services/ios-project-service.ts

-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
3939
private $cocoapodsService: ICocoaPodsService,
4040
private $errors: IErrors,
4141
private $logger: ILogger,
42-
private $iOSEmulatorServices: Mobile.IEmulatorPlatformServices,
4342
private $injector: IInjector,
4443
$projectDataService: IProjectDataService,
4544
private $prompter: IPrompter,
@@ -76,7 +75,6 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
7675
normalizedPlatformName: "iOS",
7776
appDestinationDirectoryPath: path.join(projectRoot, projectData.projectName),
7877
platformProjectService: this,
79-
emulatorServices: this.$iOSEmulatorServices,
8078
projectRoot: projectRoot,
8179
deviceBuildOutputPath: path.join(projectRoot, constants.BUILD_DIR, "device"),
8280
emulatorBuildOutputPath: path.join(projectRoot, constants.BUILD_DIR, "emulator"),

test/ios-project-service.ts

+5
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ function createTestInjector(projectPath: string, projectName: string, xcode?: IX
121121
testInjector.register("httpClient", {});
122122
testInjector.register("platformEnvironmentRequirements", {});
123123
testInjector.register("plistParser", {});
124+
testInjector.register("androidEmulatorServices", {});
125+
testInjector.register("androidEmulatorDiscovery", {
126+
on: () => ({})
127+
});
128+
testInjector.register("emulatorHelper", {});
124129

125130
return testInjector;
126131
}

test/npm-support.ts

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ function createTestInjector(): IInjector {
8383
testInjector.register("xmlValidator", XmlValidator);
8484
testInjector.register("config", StaticConfigLib.Configuration);
8585
testInjector.register("projectChangesService", ProjectChangesLib.ProjectChangesService);
86-
testInjector.register("emulatorPlatformService", stubs.EmulatorPlatformService);
8786
testInjector.register("analyticsService", {
8887
track: async (): Promise<any> => undefined
8988
});

test/platform-commands.ts

-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class PlatformData implements IPlatformData {
3333
// intentionally left blank
3434
}
3535
};
36-
emulatorServices: Mobile.IEmulatorPlatformServices = null;
3736
projectRoot = "";
3837
deviceBuildOutputPath = "";
3938
getValidBuildOutputData = (buildOptions: IBuildOutputOptions) => ({ packageNames: [""] });
@@ -143,7 +142,6 @@ function createTestInjector() {
143142
testInjector.register("preparePlatformJSService", {});
144143
testInjector.register("childProcess", ChildProcessLib.ChildProcess);
145144
testInjector.register("projectChangesService", ProjectChangesLib.ProjectChangesService);
146-
testInjector.register("emulatorPlatformService", stubs.EmulatorPlatformService);
147145
testInjector.register("analyticsService", {
148146
track: async () => async (): Promise<any[]> => undefined
149147
});

0 commit comments

Comments
 (0)