Skip to content

Commit c45df20

Browse files
Debug on emulator by default when multiple devices/emulators attached
When there are multiple devices/emulators, calling `tns debug android` will fail that multiple devices are attached. This break VS Code as previous code of CLI has been using emulators by default. Fix this by using the most recent version (highest API Level) of running emulators, i.e. in case you have three Android emulators with version 5.1, 6.0 and 7.0, the one with 7.0 will be used for debugging.
1 parent 0166da3 commit c45df20

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

Gruntfile.js

-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ module.exports = function (grunt) {
133133
grunt.loadNpmTasks("grunt-contrib-watch");
134134
grunt.loadNpmTasks("grunt-shell");
135135
grunt.loadNpmTasks("grunt-ts");
136-
grunt.loadNpmTasks("grunt-tslint");
137136

138137
grunt.registerTask("set_package_version", function (version) {
139138
var buildVersion = version !== undefined ? version : buildNumber;

lib/commands/debug.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,21 @@
3232
await this.$devicesService.detectCurrentlyAttachedDevices();
3333

3434
// Now let's take data for each device:
35-
const devices = this.$devicesService.getDeviceInstances();
36-
const deviceDescriptors: ILiveSyncDeviceInfo[] = devices.filter(d => !this.platform || d.deviceInfo.platform === this.platform)
35+
let devices = this.$devicesService.getDeviceInstances().filter(d => !this.platform || d.deviceInfo.platform === this.platform);
36+
37+
if (this.$options.emulator && !this.$options.device) {
38+
const emulators = devices.filter(d => d.isEmulator);
39+
if (emulators.length > 1) {
40+
const sortedEmulators = _.sortBy(emulators, e => e.deviceInfo.version);
41+
const selectedEmulator = _.last(sortedEmulators);
42+
this.$logger.warn(`Multiple emulators found. Starting debugger on emulator ${selectedEmulator.deviceInfo.identifier}. If you want to debug on specific device/emulator, you can specify it with --device option.`);
43+
devices = [selectedEmulator];
44+
} else {
45+
devices = emulators;
46+
}
47+
}
48+
49+
const deviceDescriptors: ILiveSyncDeviceInfo[] = devices
3750
.map(d => {
3851
const info: ILiveSyncDeviceInfo = {
3952
identifier: d.deviceInfo.identifier,
@@ -91,7 +104,9 @@
91104
}
92105

93106
if (this.$devicesService.deviceCount > 1) {
94-
this.$errors.failWithoutHelp("Multiple devices found! To debug on specific device please select device with --device option.");
107+
this.$options.emulator = true;
108+
109+
this.$logger.warn("Multiple devices found! Starting debugger on emulator. If you want to debug on specific device please select device with --device option.".yellow.bold);
95110
}
96111

97112
return true;

lib/definitions/debug.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ interface IDebugDataService {
8383
* @param {IOptions} options The options based on which debugData will be created
8484
* @returns {IDebugData} Data describing the required information for starting debug process.
8585
*/
86-
createDebugData(projectData: IProjectData, options: IOptions): IDebugData;
86+
createDebugData(projectData: IProjectData, options: IDeviceIdentifier): IDebugData;
8787
}
8888

8989
/**

lib/services/debug-data-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export class DebugDataService implements IDebugDataService {
2-
public createDebugData(projectData: IProjectData, options: IOptions): IDebugData {
2+
public createDebugData(projectData: IProjectData, options: IDeviceIdentifier): IDebugData {
33
return {
44
applicationIdentifier: projectData.projectId,
55
projectDir: projectData.projectDir,

lib/services/debug-service-base.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ export abstract class DebugServiceBase extends EventEmitter implements IPlatform
1414
protected getCanExecuteAction(deviceIdentifier: string): (device: Mobile.IDevice) => boolean {
1515
return (device: Mobile.IDevice): boolean => {
1616
if (deviceIdentifier) {
17-
return device.deviceInfo.identifier === deviceIdentifier
18-
|| device.deviceInfo.identifier === this.$devicesService.getDeviceByDeviceOption().deviceInfo.identifier;
17+
let isSearchedDevice = device.deviceInfo.identifier === deviceIdentifier;
18+
if (!isSearchedDevice) {
19+
const deviceByDeviceOption = this.$devicesService.getDeviceByDeviceOption();
20+
isSearchedDevice = deviceByDeviceOption && device.deviceInfo.identifier === deviceByDeviceOption.deviceInfo.identifier;
21+
}
22+
23+
return isSearchedDevice;
1924
} else {
2025
return true;
2126
}

lib/services/livesync/debug-livesync-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class DebugLiveSyncService extends LiveSyncService implements IDebugLiveS
4646
teamId: this.$options.teamId
4747
};
4848

49-
let debugData = this.$debugDataService.createDebugData(this.$projectData, this.$options);
49+
let debugData = this.$debugDataService.createDebugData(this.$projectData, { device: liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier });
5050
const debugService = this.$debugService.getDebugService(liveSyncResultInfo.deviceAppData.device);
5151

5252
await this.$platformService.trackProjectType(this.$projectData);

0 commit comments

Comments
 (0)