Skip to content

Commit

Permalink
fix: use devicectl for iOS 17 if Xcode 15 is available (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Sep 21, 2023
1 parent 8492b24 commit 5c56d71
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions src/ios/utils/device.ts
@@ -1,3 +1,4 @@
import { spawn } from 'child_process';
import * as Debug from 'debug';
import { readFileSync } from 'fs';
import * as path from 'path';
Expand All @@ -13,7 +14,7 @@ import {
UsbmuxdClient,
} from '../lib';

import { getDeveloperDiskImagePath } from './xcode';
import { getDeveloperDiskImagePath, getXcodeVersionInfo } from './xcode';

const debug = Debug('native-run:ios:utils:device');

Expand Down Expand Up @@ -56,21 +57,55 @@ export async function runOnDevice(
const { [bundleId]: appInfo } = await installer.lookupApp([bundleId]);
// launch fails with EBusy or ENotFound if you try to launch immediately after install
await wait(200);
const debugServerClient = await launchApp(clientManager, appInfo);
if (waitForApp) {
onBeforeExit(async () => {
// causes continue() to return
debugServerClient.halt();
// give continue() time to return response
await wait(64);
});

debug(`Waiting for app to close...\n`);
const result = await debugServerClient.continue();
// TODO: I have no idea what this packet means yet (successful close?)
// if not a close (ie, most likely due to halt from onBeforeExit), then kill the app
if (result !== 'W00') {
await debugServerClient.kill();
try {
const debugServerClient = await launchApp(clientManager, appInfo);
if (waitForApp) {
onBeforeExit(async () => {
// causes continue() to return
debugServerClient.halt();
// give continue() time to return response
await wait(64);
});

debug(`Waiting for app to close...\n`);
const result = await debugServerClient.continue();
// TODO: I have no idea what this packet means yet (successful close?)
// if not a close (ie, most likely due to halt from onBeforeExit), then kill the app
if (result !== 'W00') {
await debugServerClient.kill();
}
}
} catch {
// if launching app throws, try with devicectl, but requires Xcode 15
const [xcodeVersion] = getXcodeVersionInfo();
if (Number(xcodeVersion) >= 15) {
const launchResult = spawn('xcrun', [
'devicectl',
'device',
'process',
'launch',
'--device',
udid,
bundleId,
]);
return new Promise<void>((resolve, reject) => {
launchResult.on('close', code => {
if (code === 0) {
resolve();
} else {
reject(
new Exception(`There was an error launching app on device`),
);
}
});
launchResult.on('error', err => {
reject(err);
});
});
} else {
throw new Exception(
`running on iOS 17 devices requires Xcode 15 and later`,
);
}
}
} finally {
Expand Down

0 comments on commit 5c56d71

Please sign in to comment.