Move screenshot to iOSBridge

Summary:
In order to support IOS cloud devices, we need to abstract
over the direct uses of idb/xcrun so we can switch them
out based on more than the device type.

Note that there's a bit of a type weirdness in there. I'll
clean this up with the next diff.

Reviewed By: mweststrate

Differential Revision: D30248036

fbshipit-source-id: ec8571429e04abe059850ef334a6645ae4a5e034
This commit is contained in:
Pascal Hartig
2021-08-11 11:02:10 -07:00
committed by Facebook GitHub Bot
parent f515df1c01
commit 52b3edc5ad
3 changed files with 66 additions and 19 deletions

View File

@@ -7,9 +7,13 @@
* @format
*/
import fs from 'fs';
import fs from 'fs-extra';
import child_process from 'child_process';
import {DeviceType} from 'flipper-plugin-lib';
import {v1 as uuid} from 'uuid';
import path from 'path';
import {exec} from 'promisify-child-process';
import {getAppTempPath} from '../utils/pathUtils';
export interface IOSBridge {
idbAvailable: boolean;
@@ -17,6 +21,7 @@ export interface IOSBridge {
udid: string,
deviceType: DeviceType,
) => child_process.ChildProcessWithoutNullStreams;
screenshot?: (serial: string) => Promise<Buffer>;
}
async function isAvailable(idbPath: string): Promise<boolean> {
@@ -32,7 +37,8 @@ async function isAvailable(idbPath: string): Promise<boolean> {
function getLogExtraArgs(deviceType: DeviceType) {
if (deviceType === 'physical') {
return [
// idb has a --json option, but that doesn't actually work!
// idb has a --json option, but that doesn't actually work for physical
// devices!
];
} else {
return [
@@ -77,6 +83,36 @@ export function xcrunStartLogListener(udid: string, deviceType: DeviceType) {
);
}
function makeTempScreenshotFilePath() {
const imageName = uuid() + '.png';
const directory = getAppTempPath();
return path.join(directory, imageName);
}
function runScreenshotCommand(
command: string,
imagePath: string,
): Promise<Buffer> {
return exec(command)
.then(() => fs.readFile(imagePath))
.then(async (buffer) => {
await fs.unlink(imagePath);
return buffer;
});
}
export async function xcrunScreenshot(serial: string): Promise<Buffer> {
const imagePath = makeTempScreenshotFilePath();
const command = `xcrun simctl io ${serial} screenshot ${imagePath}`;
return runScreenshotCommand(command, imagePath);
}
export async function idbScreenshot(serial: string): Promise<Buffer> {
const imagePath = makeTempScreenshotFilePath();
const command = `idb screenshot --udid ${serial} ${imagePath}`;
return runScreenshotCommand(command, imagePath);
}
export async function makeIOSBridge(
idbPath: string,
isXcodeDetected: boolean,
@@ -87,6 +123,7 @@ export async function makeIOSBridge(
return {
idbAvailable: true,
startLogListener: idbStartLogListener.bind(null, idbPath),
screenshot: idbScreenshot,
};
}
@@ -95,6 +132,7 @@ export async function makeIOSBridge(
return {
idbAvailable: false,
startLogListener: xcrunStartLogListener,
screenshot: xcrunScreenshot,
};
}
// no idb, and not a simulator, we can't log this device