Exclusively use idb or xcrun for querying devices

Summary:
Simplifies the logic by only using idb when available to query *both* simulators and physical devices.

Changelog: Prefer idb for device listing when available.

Reviewed By: mweststrate

Differential Revision: D31271539

fbshipit-source-id: ac5b73ca5b00b7707bf87cd4d3a6af6e0c0ae214
This commit is contained in:
Pascal Hartig
2021-10-01 08:05:07 -07:00
committed by Facebook GitHub Bot
parent 111177b44e
commit 7706b26d7a
2 changed files with 56 additions and 35 deletions

View File

@@ -61,13 +61,39 @@ test('test parseXcodeFromCoreSimPath from standard locations', () => {
test('test getAllPromisesForQueryingDevices when xcode detected', () => { test('test getAllPromisesForQueryingDevices when xcode detected', () => {
const flipperServer = new FlipperServerImpl({}, mockStore, getInstance()); const flipperServer = new FlipperServerImpl({}, mockStore, getInstance());
flipperServer.ios.iosBridge = {} as IOSBridge; flipperServer.ios.iosBridge = {} as IOSBridge;
const promises = flipperServer.ios.getAllPromisesForQueryingDevices(true); const promises = flipperServer.ios.getAllPromisesForQueryingDevices(
expect(promises.length).toEqual(3); true,
false,
);
expect(promises.length).toEqual(2);
}); });
test('test getAllPromisesForQueryingDevices when xcode is not detected', () => { test('test getAllPromisesForQueryingDevices when xcode is not detected', () => {
const flipperServer = new FlipperServerImpl({}, mockStore, getInstance()); const flipperServer = new FlipperServerImpl({}, mockStore, getInstance());
flipperServer.ios.iosBridge = {} as IOSBridge; flipperServer.ios.iosBridge = {} as IOSBridge;
const promises = flipperServer.ios.getAllPromisesForQueryingDevices(false); const promises = flipperServer.ios.getAllPromisesForQueryingDevices(
false,
true,
);
expect(promises.length).toEqual(1); expect(promises.length).toEqual(1);
}); });
test('test getAllPromisesForQueryingDevices when xcode and idb are both unavailable', () => {
const flipperServer = new FlipperServerImpl({}, mockStore, getInstance());
flipperServer.ios.iosBridge = {} as IOSBridge;
const promises = flipperServer.ios.getAllPromisesForQueryingDevices(
false,
false,
);
expect(promises.length).toEqual(0);
});
test('test getAllPromisesForQueryingDevices when both idb and xcode are available', () => {
const flipperServer = new FlipperServerImpl({}, mockStore, getInstance());
flipperServer.ios.iosBridge = {} as IOSBridge;
const promises = flipperServer.ios.getAllPromisesForQueryingDevices(
true,
true,
);
expect(promises.length).toEqual(2);
});

View File

@@ -22,6 +22,7 @@ import {
makeIOSBridge, makeIOSBridge,
} from './IOSBridge'; } from './IOSBridge';
import {FlipperServerImpl} from '../../FlipperServerImpl'; import {FlipperServerImpl} from '../../FlipperServerImpl';
import {notNull} from '../../utils/typeUtils';
type iOSSimulatorDevice = { type iOSSimulatorDevice = {
state: 'Booted' | 'Shutdown' | 'Shutting Down'; state: 'Booted' | 'Shutdown' | 'Shutting Down';
@@ -114,55 +115,51 @@ export class IOSDeviceManager {
getAllPromisesForQueryingDevices( getAllPromisesForQueryingDevices(
isXcodeDetected: boolean, isXcodeDetected: boolean,
isIdbAvailable: boolean,
): Array<Promise<any>> { ): Array<Promise<any>> {
const {config} = this.flipperServer; const {config} = this.flipperServer;
const promArray = [ return [
getActiveDevices(config.idbPath, config.enablePhysicalIOS).then( isIdbAvailable
? getActiveDevices(config.idbPath, config.enablePhysicalIOS).then(
(devices: IOSDeviceParams[]) => { (devices: IOSDeviceParams[]) => {
this.processDevices(devices, 'physical'); this.processDevices(devices);
}, },
), )
]; : null,
if (isXcodeDetected) { !isIdbAvailable && isXcodeDetected
promArray.push( ? this.getSimulators(true).then((devices) =>
...[ this.processDevices(devices),
this.checkXcodeVersionMismatch(), )
this.getSimulators(true).then((devices) => { : null,
this.processDevices(devices, 'emulator'); isXcodeDetected ? this.checkXcodeVersionMismatch() : null,
}), ].filter(notNull);
],
);
}
return promArray;
} }
private async queryDevices(): Promise<any> { private async queryDevices(): Promise<any> {
const {config} = this.flipperServer;
const isXcodeInstalled = await iosUtil.isXcodeDetected(); const isXcodeInstalled = await iosUtil.isXcodeDetected();
return Promise.all(this.getAllPromisesForQueryingDevices(isXcodeInstalled)); const isIdbAvailable = await iosUtil.isAvailable(config.idbPath);
return Promise.all(
this.getAllPromisesForQueryingDevices(isXcodeInstalled, isIdbAvailable),
);
} }
private processDevices( private processDevices(activeDevices: IOSDeviceParams[]) {
activeDevices: IOSDeviceParams[],
targetType: 'physical' | 'emulator',
) {
if (!this.iosBridge) { if (!this.iosBridge) {
throw new Error('iOS bridge not yet initialized'); throw new Error('iOS bridge not yet initialized');
} }
const currentDeviceIDs = new Set( const currentDeviceIDs = new Set(
this.flipperServer this.flipperServer
.getDevices() .getDevices()
.filter( .filter((device) => device.info.os === 'iOS')
(device) =>
device.info.os === 'iOS' && device.info.deviceType === targetType,
)
.map((device) => device.serial), .map((device) => device.serial),
); );
for (const {udid, type, name} of activeDevices) { for (const {udid, type, name} of activeDevices) {
if (currentDeviceIDs.has(udid)) { if (currentDeviceIDs.has(udid)) {
currentDeviceIDs.delete(udid); currentDeviceIDs.delete(udid);
} else if (targetType === type) { } else {
console.log(`[conn] detected new iOS device ${targetType} ${udid}`); console.log(`[conn] detected new iOS device ${udid}`);
const iOSDevice = new IOSDevice( const iOSDevice = new IOSDevice(
this.flipperServer, this.flipperServer,
this.iosBridge, this.iosBridge,
@@ -175,9 +172,7 @@ export class IOSDeviceManager {
} }
currentDeviceIDs.forEach((id) => { currentDeviceIDs.forEach((id) => {
console.log( console.log(`[conn] Could no longer find ${id}, removing...`);
`[conn] Could no longer find ${targetType} ${id}, removing...`,
);
this.flipperServer.unregisterDevice(id); this.flipperServer.unregisterDevice(id);
}); });
} }