Improve xcode version mismatch messaging

Summary:
This runs in a very hot loop (which I will change shortly)

However, there's also some simplification that we can do here:
- Only look for `Simulator.app` processes instead of all the processes. This limits the amount of string comparison to do. If there's a `Simulator.app` running, then we know a sim is running for that Xcode.
- Use piping to extract the launch path of the process, instead of then regex'ing this out.
- Use a simpler substring match to determine if paths have a different start.

Reviewed By: passy

Differential Revision: D33891296

fbshipit-source-id: e37d5f260fc6c6113be9c5268b7c8cffb4057b9a
This commit is contained in:
Lawrence Lomax
2022-02-01 00:43:24 -08:00
committed by Facebook GitHub Bot
parent 1e309ddd5c
commit e16662a28f
2 changed files with 56 additions and 55 deletions

View File

@@ -231,21 +231,27 @@ export class IOSDeviceManager {
try {
let {stdout: xcodeCLIVersion} = await exec('xcode-select -p');
xcodeCLIVersion = xcodeCLIVersion!.toString().trim();
const {stdout} = await exec('ps aux | grep CoreSimulator');
for (const line of stdout!.toString().split('\n')) {
const match = parseXcodeFromCoreSimPath(line);
const runningVersion =
match && match.length > 0 ? match[0].trim() : null;
if (runningVersion && runningVersion !== xcodeCLIVersion) {
const errorMessage = `Xcode version mismatch: Simulator is running from "${runningVersion}" while Xcode CLI is "${xcodeCLIVersion}". Running "xcode-select --switch ${runningVersion}" can fix this. For example: "sudo xcode-select -s /Applications/Xcode.app/Contents/Developer"`;
this.flipperServer.emit('notification', {
type: 'error',
title: 'Xcode version mismatch',
description: '' + errorMessage,
});
this.xcodeVersionMismatchFound = true;
break;
const {stdout} = await exec(
"pgrep Simulator | xargs ps | grep Simulator.app | awk '{print $NF}'",
);
for (const runningSimulatorApp of stdout!.toString().split('\n')) {
if (!runningSimulatorApp) {
continue;
}
if (runningSimulatorApp.startsWith(xcodeCLIVersion)) {
continue;
}
const runningVersion =
runningSimulatorApp.split('/Contents/Developer')[0] +
'/Contents/Developer';
const errorMessage = `Xcode version mismatch: Simulator is running from "${runningVersion}" while Xcode CLI is "${xcodeCLIVersion}". Running "xcode-select --switch ${runningVersion}" can fix this. For example: "sudo xcode-select -s /Applications/Xcode.app/Contents/Developer"`;
this.flipperServer.emit('notification', {
type: 'error',
title: 'Xcode version mismatch',
description: '' + errorMessage,
});
this.xcodeVersionMismatchFound = true;
break;
}
} catch (e) {
console.error('Failed to determine Xcode version:', e);
@@ -253,15 +259,28 @@ export class IOSDeviceManager {
}
}
export function checkXcodeVersionMismatch(
runningSimulatorApps: Array<string>,
xcodeCLIVersion: string,
): string | undefined {
for (const runningSimulatorApp of runningSimulatorApps) {
if (!runningSimulatorApp) {
continue;
}
if (runningSimulatorApp.startsWith(xcodeCLIVersion)) {
continue;
}
return (
runningSimulatorApp.split('/Contents/Developer')[0] +
'/Contents/Developer'
);
}
return undefined;
}
function getActiveDevices(
idbPath: string,
isPhysicalDeviceEnabled: boolean,
): Promise<Array<IOSDeviceParams>> {
return new IDBBridge(idbPath, isPhysicalDeviceEnabled).getActiveDevices(true);
}
export function parseXcodeFromCoreSimPath(
line: string,
): RegExpMatchArray | null {
return line.match(/\/[\/\w@)(\-\+]*\/Xcode[^/]*\.app\/Contents\/Developer/);
}