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