Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
filip131311 committed Feb 28, 2025
1 parent d97f8ce commit aae573a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 50 deletions.
50 changes: 8 additions & 42 deletions packages/vscode-extension/src/project/applicationContext.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,26 @@
import { Disposable, window } from "vscode";
import { BuildCache, migrateOldBuildCachesToNewStorage } from "../builders/BuildCache";
import { Disposable } from "vscode";
import { BuildCache } from "../builders/BuildCache";
import { DependencyManager } from "../dependency/DependencyManager";
import { Logger } from "../Logger";
import { LaunchConfigController } from "../panels/LaunchConfigController";
import { findAppRootFolder } from "../utilities/extensionContext";
import { Platform } from "../utilities/platform";
import { setupPathEnv } from "../utilities/subprocess";
import { disposeAll } from "../utilities/disposables";

export class ApplicationContext implements Disposable {
public appRootFolder: string;
public dependencyManager: DependencyManager;
public launchConfig: LaunchConfigController;
public buildCache: BuildCache;
private disposables: Disposable[] = [];

constructor() {
const newAppRoot = findAppRootFolder();
if (!newAppRoot) {
window.showErrorMessage(
"Failed to determine any application root candidates, you can set it up manually in launch configuration",
"Dismiss"
);
Logger.error("[Project] The application root could not be found.");
throw Error(
"Couldn't find app root folder. The extension should not be activated without reachable app root."
);
}
constructor(public appRootFolder: string) {
this.dependencyManager = new DependencyManager(appRootFolder);

Logger.info(`Found app root folder: ${newAppRoot}`);
migrateOldBuildCachesToNewStorage(newAppRoot);
this.launchConfig = new LaunchConfigController(appRootFolder);

this.appRootFolder = newAppRoot;

if (Platform.OS === "macos") {
try {
setupPathEnv(newAppRoot);
} catch (error) {
window.showWarningMessage(
"Error when setting up PATH environment variable, RN IDE may not work correctly.",
"Dismiss"
);
}
}

this.dependencyManager = new DependencyManager(newAppRoot);

this.launchConfig = new LaunchConfigController(newAppRoot);

this.buildCache = new BuildCache(newAppRoot);
this.buildCache = new BuildCache(appRootFolder);

this.disposables.push(this.launchConfig, this.dependencyManager);
}

public dispose() {
this.disposables.forEach((disposable) => {
disposable.dispose();
});
disposeAll(this.disposables);
}
}
14 changes: 8 additions & 6 deletions packages/vscode-extension/src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ import {
import { getTelemetryReporter } from "../utilities/telemetry";
import { ToolKey, ToolsManager } from "./tools";
import { UtilsInterface } from "../common/utils";
import { ApplicationContext } from "./applicationContext";
import { ApplicationContext } from "./ApplicationContext";
import { disposeAll } from "../utilities/disposables";
import { findAndSetupNewAppRootFolder } from "../utilities/findAndSetupNewAppRootFolder";

const DEVICE_SETTINGS_KEY = "device_settings_v4";

Expand Down Expand Up @@ -92,7 +94,8 @@ export class Project
private readonly deviceManager: DeviceManager,
private readonly utils: UtilsInterface
) {
this.applicationContext = new ApplicationContext();
const appRoot = findAndSetupNewAppRootFolder();
this.applicationContext = new ApplicationContext(appRoot);

this.deviceSettings = extensionContext.workspaceState.get(DEVICE_SETTINGS_KEY) ?? {
appearance: "dark",
Expand Down Expand Up @@ -169,8 +172,9 @@ export class Project
}

private setupAppRoot() {
const newAppRoot = findAndSetupNewAppRootFolder();
const oldApplicationContext = this.applicationContext;
this.applicationContext = new ApplicationContext();
this.applicationContext = new ApplicationContext(newAppRoot);
oldApplicationContext.dispose();

this.reload("reboot");
Expand Down Expand Up @@ -416,9 +420,7 @@ export class Project
this.devtools?.dispose();
this.deviceManager.removeListener("deviceRemoved", this.removeDeviceListener);
this.applicationContext.dispose();
this.disposables.forEach((disposable) => {
disposable.dispose();
});
disposeAll(this.disposables);
}

private async reloadMetro() {
Expand Down
4 changes: 2 additions & 2 deletions packages/vscode-extension/src/utilities/extensionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ function searchForFilesDirectory(
})
.forEach((dir) => {
searchQueue.push({
path: currentDir!.path + "/" + dir.name,
searchDepth: currentDir!.searchDepth + 1,
path: currentDir.path + "/" + dir.name,
searchDepth: currentDir.searchDepth + 1,
});
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { window } from "vscode";
import { migrateOldBuildCachesToNewStorage } from "../builders/BuildCache";
import { Logger } from "../Logger";
import { findAppRootFolder } from "./extensionContext";
import { Platform } from "./platform";
import { setupPathEnv } from "./subprocess";

export function findAndSetupNewAppRootFolder() {
const newAppRoot = findAppRootFolder();
if (!newAppRoot) {
window.showErrorMessage(
"Failed to determine any application root candidates, you can set it up manually in launch configuration",
"Dismiss"
);
Logger.error("[Project] The application root could not be found.");
throw Error(
"Couldn't find app root folder. The extension should not be activated without reachable app root."
);
}

Logger.info(`Found app root folder: ${newAppRoot}`);
migrateOldBuildCachesToNewStorage(newAppRoot);

if (Platform.OS === "macos") {
try {
setupPathEnv(newAppRoot);
} catch (error) {
window.showWarningMessage(
"Error when setting up PATH environment variable, RN IDE may not work correctly.",
"Dismiss"
);
}
}
return newAppRoot;
}

0 comments on commit aae573a

Please sign in to comment.