Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate cashes directories #562

Merged
merged 6 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions packages/vscode-extension/src/devices/AndroidEmulatorDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fs from "fs";
import { EOL } from "node:os";
import xml2js from "xml2js";
import { retry } from "../utilities/retry";
import { getAppCachesDir, getNativeABI } from "../utilities/common";
import { getAppCachesDir, getNativeABI, getOldAppCachesDir } from "../utilities/common";
import { ANDROID_HOME } from "../utilities/android";
import { ChildProcess, exec, lineReader } from "../utilities/subprocess";
import { v4 as uuidv4 } from "uuid";
Expand Down Expand Up @@ -123,7 +123,7 @@ export class AndroidEmulatorDevice extends DeviceBase {
// this prevents booting device with the same AVD twice
await ensureOldEmulatorProcessExited(this.avdId);

const avdDirectory = getOrCreateAvdDirectory();
const avdDirectory = getOrCreateAvdDirectory(this.avdId);
const subprocess = exec(
EMULATOR_BINARY,
[
Expand Down Expand Up @@ -454,6 +454,16 @@ async function getAvdIds(avdDirectory: string) {

export async function listEmulators() {
const avdDirectory = getOrCreateAvdDirectory();
const emulators = listEmulatorsForDirectory(avdDirectory);
const oldAvdDirectory = getOldAvdDirectoryLocation();
const oldEmulators = listEmulatorsForDirectory(oldAvdDirectory);

const combinedEmulators = await Promise.all([emulators, oldEmulators]);

return combinedEmulators[0].concat(combinedEmulators[1]);
}

async function listEmulatorsForDirectory(avdDirectory: string) {
const avdIds = await getAvdIds(avdDirectory);
const systemImages = await getAndroidSystemImages();
return Promise.all(
Expand Down Expand Up @@ -504,7 +514,7 @@ export async function removeEmulator(avdId: string) {
await ensureOldEmulatorProcessExited(avdId);
}

const avdDirectory = getOrCreateAvdDirectory();
const avdDirectory = getOrCreateAvdDirectory(avdId);
const removeAvd = fs.promises.rm(path.join(avdDirectory, `${avdId}.avd`), {
recursive: true,
});
Expand Down Expand Up @@ -584,18 +594,35 @@ async function waitForEmulatorOnline(serial: string, timeoutMs: number) {
]);
}

function getOrCreateAvdDirectory() {
const avdDirectory = getAvdDirectoryLocation();
function getOrCreateAvdDirectory(avd?: string) {
const avdDirectory = getAvdDirectoryLocation(avd);
if (!fs.existsSync(avdDirectory)) {
fs.mkdirSync(avdDirectory, { recursive: true });
}

return avdDirectory;
}

function getAvdDirectoryLocation() {
function getOldAvdDirectoryLocation() {
const oldAppCachesDir = getOldAppCachesDir();
return path.join(oldAppCachesDir, "Devices", "Android", "avd");
}

function getAvdDirectoryLocation(avd?: string) {
const appCachesDir = getAppCachesDir();
const avdDirectory = path.join(appCachesDir, "Devices", "Android", "avd");
if (!avd) {
return avdDirectory;
}

const oldAvdDirectory = getOldAvdDirectoryLocation();
if (!fs.existsSync(oldAvdDirectory)) {
return avdDirectory;
}
const devices = fs.readdirSync(oldAvdDirectory);
if (devices.includes(`${avd}.avd`)) {
return oldAvdDirectory;
}
return avdDirectory;
}

Expand Down
96 changes: 73 additions & 23 deletions packages/vscode-extension/src/devices/IosSimulatorDevice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAppCachesDir } from "../utilities/common";
import { getAppCachesDir, getOldAppCachesDir } from "../utilities/common";
import { DeviceBase } from "./DeviceBase";
import { Preview } from "./preview";
import { Logger } from "../Logger";
Expand Down Expand Up @@ -60,18 +60,24 @@ export class IosSimulatorDevice extends DeviceBase {
}

get lockFilePath(): string {
const deviceSetLocation = getDeviceSetLocation();
const deviceSetLocation = getDeviceSetLocation(this.deviceUDID);
const pidFile = path.join(deviceSetLocation, this.deviceUDID, "lock.pid");
return pidFile;
}

public dispose() {
super.dispose();
return exec("xcrun", ["simctl", "--set", getOrCreateDeviceSet(), "shutdown", this.deviceUDID]);
return exec("xcrun", [
"simctl",
"--set",
getOrCreateDeviceSet(this.deviceUDID),
"shutdown",
this.deviceUDID,
]);
}

async bootDevice() {
const deviceSetLocation = getOrCreateDeviceSet();
const deviceSetLocation = getOrCreateDeviceSet(this.deviceUDID);
try {
await exec("xcrun", ["simctl", "--set", deviceSetLocation, "boot", this.deviceUDID], {
allowNonZeroExit: true,
Expand All @@ -87,7 +93,7 @@ export class IosSimulatorDevice extends DeviceBase {
}

async changeSettings(settings: DeviceSettings) {
const deviceSetLocation = getOrCreateDeviceSet();
const deviceSetLocation = getOrCreateDeviceSet(this.deviceUDID);
await exec("xcrun", [
"simctl",
"--set",
Expand Down Expand Up @@ -149,7 +155,7 @@ export class IosSimulatorDevice extends DeviceBase {
]);
}
async sendBiometricAuthorization(isMatch: boolean) {
const deviceSetLocation = getOrCreateDeviceSet();
const deviceSetLocation = getOrCreateDeviceSet(this.deviceUDID);
await exec("xcrun", [
"simctl",
"--set",
Expand All @@ -165,7 +171,7 @@ export class IosSimulatorDevice extends DeviceBase {
}

async configureMetroPort(bundleID: string, metroPort: number) {
const deviceSetLocation = getOrCreateDeviceSet();
const deviceSetLocation = getOrCreateDeviceSet(this.deviceUDID);
const { stdout: appDataLocation } = await exec("xcrun", [
"simctl",
"--set",
Expand Down Expand Up @@ -201,7 +207,7 @@ export class IosSimulatorDevice extends DeviceBase {
}

async launchWithBuild(build: IOSBuildResult) {
const deviceSetLocation = getOrCreateDeviceSet();
const deviceSetLocation = getOrCreateDeviceSet(this.deviceUDID);
await exec("xcrun", [
"simctl",
"--set",
Expand All @@ -218,7 +224,7 @@ export class IosSimulatorDevice extends DeviceBase {
// 1. Add the deeplink to the scheme approval list
// 2. Terminate the app if it's running
// 3. Open the deeplink
const deviceSetLocation = getOrCreateDeviceSet();
const deviceSetLocation = getOrCreateDeviceSet(this.deviceUDID);

// Add the deeplink to the scheme approval list:
const schema = new URL(expoDeeplink).protocol.slice(0, -1);
Expand Down Expand Up @@ -279,7 +285,7 @@ export class IosSimulatorDevice extends DeviceBase {
if (build.platform !== DevicePlatform.IOS) {
throw new Error("Invalid platform");
}
const deviceSetLocation = getOrCreateDeviceSet();
const deviceSetLocation = getOrCreateDeviceSet(this.deviceUDID);
if (forceReinstall) {
try {
await exec(
Expand Down Expand Up @@ -309,7 +315,7 @@ export class IosSimulatorDevice extends DeviceBase {
await exec("xcrun", [
"simctl",
"--set",
getOrCreateDeviceSet(),
getOrCreateDeviceSet(this.deviceUDID),
"privacy",
this.deviceUDID,
"reset",
Expand All @@ -320,7 +326,7 @@ export class IosSimulatorDevice extends DeviceBase {
}

makePreview(): Preview {
return new Preview(["ios", "--id", this.deviceUDID, "--device-set", getOrCreateDeviceSet()]);
return new Preview(["ios", "--id", this.deviceUDID, "--device-set", getOrCreateDeviceSet(this.deviceUDID)]);
}
}

Expand Down Expand Up @@ -348,28 +354,55 @@ export async function removeIosSimulator(udid: string | undefined, location: Sim

let deviceSetArgs: string[] = [];
if (location === SimulatorDeviceSet.RN_IDE) {
const setDirectory = getOrCreateDeviceSet();
const setDirectory = getOrCreateDeviceSet(udid);
deviceSetArgs = ["--set", setDirectory];
}

return exec("xcrun", ["simctl", ...deviceSetArgs, "delete", udid]);
}

async function listSimulatorsForLocation(location?: string) {
let deviceSetArgs: string[] = [];
if (location) {
deviceSetArgs = ["--set", location];
}
try {
const { stdout } = await exec(
"xcrun",
["simctl", ...deviceSetArgs, "list", "devices", "--json"],
{ allowNonZeroExit: true }
);
const parsedData: SimulatorData = JSON.parse(stdout);

const { devices: devicesPerRuntime } = parsedData;

return Object.entries(devicesPerRuntime);
} catch (e) {
// ignore errors because some locations might not exist
}
return [];
}

export async function listSimulators(
location: SimulatorDeviceSet = SimulatorDeviceSet.RN_IDE
): Promise<IOSDeviceInfo[]> {
let deviceSetArgs: string[] = [];
let devicesPerRuntime;
if (location === SimulatorDeviceSet.RN_IDE) {
const deviceSetLocation = getOrCreateDeviceSet();
deviceSetArgs = ["--set", deviceSetLocation];

devicesPerRuntime = await listSimulatorsForLocation(deviceSetLocation);

const oldDeviceSetLocation = getOldDeviceSetLocation();
const oldDevicesPerRuntime = await listSimulatorsForLocation(oldDeviceSetLocation);

devicesPerRuntime = devicesPerRuntime.concat(oldDevicesPerRuntime);
} else {
devicesPerRuntime = await listSimulatorsForLocation();
}
const { stdout } = await exec("xcrun", ["simctl", ...deviceSetArgs, "list", "devices", "--json"]);
const parsedData: SimulatorData = JSON.parse(stdout);

const { devices: devicesPerRuntime } = parsedData;
const runtimes = await getAvailableIosRuntimes();

const simulators = Object.entries(devicesPerRuntime)
const simulators = devicesPerRuntime
.map(([runtimeID, devices]) => {
const runtime = runtimes.find((item) => item.identifier === runtimeID);

Expand Down Expand Up @@ -432,13 +465,30 @@ export async function createSimulator(
} as IOSDeviceInfo;
}

function getDeviceSetLocation() {
function getDeviceSetLocation(deviceUDID?: string) {
const appCachesDir = getAppCachesDir();
return path.join(appCachesDir, "Devices", "iOS");
const deviceSetLocation = path.join(appCachesDir, "Devices", "iOS");
if (!deviceUDID) {
return deviceSetLocation;
}
const oldDeviceSetLocation = getOldDeviceSetLocation();
if (!fs.existsSync(oldDeviceSetLocation)) {
return deviceSetLocation;
}
const devices = fs.readdirSync(oldDeviceSetLocation);
if (devices.includes(deviceUDID)) {
return oldDeviceSetLocation;
}
return deviceSetLocation;
}

function getOldDeviceSetLocation() {
const oldAppCachesDir = getOldAppCachesDir();
return path.join(oldAppCachesDir, "Devices", "iOS");
}

function getOrCreateDeviceSet() {
const deviceSetLocation = getDeviceSetLocation();
function getOrCreateDeviceSet(deviceUDID?: string) {
let deviceSetLocation = getDeviceSetLocation(deviceUDID);
if (!fs.existsSync(deviceSetLocation)) {
fs.mkdirSync(deviceSetLocation, { recursive: true });
}
Expand Down
6 changes: 5 additions & 1 deletion packages/vscode-extension/src/utilities/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ export function getNativeABI() {
}
}

export function getOldAppCachesDir() {
return join(os.homedir(), "Library", "Caches", "com.swmansion.react-native-ide");
}

export function getAppCachesDir() {
// this one is tricky to rename as Android emulators keep absolute path in config files
return join(os.homedir(), "Library", "Caches", "com.swmansion.react-native-ide");
return join(os.homedir(), "Library", "Caches", "com.swmansion.radon-ide");
}

export function getLogsDir() {
Expand Down
Loading