Skip to content

Use context.storageUri for session file (and refactor) #4088

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

Merged
merged 8 commits into from
Jul 21, 2022
14 changes: 3 additions & 11 deletions src/features/DebugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import { NotificationType, RequestType } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";
import { getPlatformDetails, OperatingSystem } from "../platform";
import { PowerShellProcess} from "../process";
import { SessionManager, SessionStatus } from "../session";
import { IEditorServicesSessionDetails, SessionManager, SessionStatus } from "../session";
import Settings = require("../settings");
import utils = require("../utils");
import { Logger } from "../logging";
import { LanguageClientConsumer } from "../languageClientConsumer";

Expand All @@ -25,8 +24,7 @@ export class DebugSessionFeature extends LanguageClientConsumer

private sessionCount: number = 1;
private tempDebugProcess: PowerShellProcess;
private tempDebugEventHandler: vscode.Disposable;
private tempSessionDetails: utils.IEditorServicesSessionDetails;
private tempSessionDetails: IEditorServicesSessionDetails;

constructor(context: ExtensionContext, private sessionManager: SessionManager, private logger: Logger) {
super();
Expand Down Expand Up @@ -311,15 +309,9 @@ export class DebugSessionFeature extends LanguageClientConsumer
// Create or show the interactive console
vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);

const sessionFilePath = utils.getDebugSessionFilePath();

if (config.createTemporaryIntegratedConsole) {
// TODO: This should be cleaned up to support multiple temporary consoles.
this.tempDebugProcess = this.sessionManager.createDebugSessionProcess(sessionFilePath, settings);
this.tempDebugProcess = this.sessionManager.createDebugSessionProcess(settings);
this.tempSessionDetails = await this.tempDebugProcess.start(`DebugSession-${this.sessionCount++}`);
utils.writeSessionFile(sessionFilePath, this.tempSessionDetails);
} else {
utils.writeSessionFile(sessionFilePath, this.sessionManager.getSessionDetails());
}

return config;
Expand Down
7 changes: 1 addition & 6 deletions src/features/PesterTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,7 @@ export class PesterTestsFeature implements vscode.Disposable {
private async launch(launchConfig): Promise<boolean> {
// Create or show the interactive console
// TODO: #367 Check if "newSession" mode is configured
vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);

// Write out temporary debug session file
utils.writeSessionFile(
utils.getDebugSessionFilePath(),
this.sessionManager.getSessionDetails());
await vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);

// TODO: Update to handle multiple root workspaces.
//
Expand Down
15 changes: 5 additions & 10 deletions src/features/RunCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,16 @@ export class RunCodeFeature implements vscode.Disposable {

const launchType = runInDebugger ? LaunchType.Debug : LaunchType.Run;
const launchConfig = createLaunchConfig(launchType, scriptToRun, args);
this.launch(launchConfig);
await this.launch(launchConfig);
}

private launch(launchConfig) {
private async launch(launchConfig: string | vscode.DebugConfiguration) {
// Create or show the interactive console
// TODO #367: Check if "newSession" mode is configured
vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);

// Write out temporary debug session file
utils.writeSessionFile(
utils.getDebugSessionFilePath(),
this.sessionManager.getSessionDetails());
// TODO: #367: Check if "newSession" mode is configured
await vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);

// TODO: Update to handle multiple root workspaces.
vscode.debug.startDebugging(vscode.workspace.workspaceFolders?.[0], launchConfig);
await vscode.debug.startDebugging(vscode.workspace.workspaceFolders?.[0], launchConfig);
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import fs = require("fs");
import os = require("os");
import path = require("path");
import vscode = require("vscode");
import utils = require("./utils");

export enum LogLevel {
Diagnostic,
Expand Down Expand Up @@ -44,7 +43,6 @@ export class Logger implements ILogger {
if (logBasePath === undefined) {
// No workspace, we have to use another folder.
this.logBasePath = vscode.Uri.file(path.resolve(__dirname, "../logs"));
utils.ensurePathExists(this.logBasePath.fsPath);
} else {
this.logBasePath = vscode.Uri.joinPath(logBasePath, "logs");
}
Expand Down
38 changes: 27 additions & 11 deletions src/process.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import fs = require("fs");
import cp = require("child_process");
import * as semver from "semver";
import path = require("path");
import vscode = require("vscode");
import { Logger } from "./logging";
import Settings = require("./settings");
import utils = require("./utils");
import { IEditorServicesSessionDetails, SessionManager } from "./session";

export class PowerShellProcess {
public static escapeSingleQuotes(pspath: string): string {
return pspath.replace(new RegExp("'", "g"), "''");
public static escapeSingleQuotes(psPath: string): string {
return psPath.replace(new RegExp("'", "g"), "''");
}

// This is used to warn the user that the extension is taking longer than expected to startup.
Expand All @@ -30,13 +32,13 @@ export class PowerShellProcess {
private title: string,
private log: Logger,
private startPsesArgs: string,
private sessionFilePath: string,
private sessionFilePath: vscode.Uri,
private sessionSettings: Settings.ISettings) {

this.onExited = this.onExitedEmitter.event;
}

public async start(logFileName: string): Promise<utils.IEditorServicesSessionDetails> {
public async start(logFileName: string): Promise<IEditorServicesSessionDetails> {
const editorServicesLogPath = this.log.getLogFilePath(logFileName);

const psesModulePath =
Expand All @@ -52,7 +54,7 @@ export class PowerShellProcess {

this.startPsesArgs +=
`-LogPath '${PowerShellProcess.escapeSingleQuotes(editorServicesLogPath.fsPath)}' ` +
`-SessionDetailsPath '${PowerShellProcess.escapeSingleQuotes(this.sessionFilePath)}' ` +
`-SessionDetailsPath '${PowerShellProcess.escapeSingleQuotes(this.sessionFilePath.fsPath)}' ` +
`-FeatureFlags @(${featureFlags}) `;

if (this.sessionSettings.integratedConsole.useLegacyReadLine) {
Expand Down Expand Up @@ -99,7 +101,7 @@ export class PowerShellProcess {
" PowerShell Editor Services args: " + startEditorServices);

// Make sure no old session file exists
utils.deleteSessionFile(this.sessionFilePath);
await PowerShellProcess.deleteSessionFile(this.sessionFilePath);

// Launch PowerShell in the integrated terminal
const terminalOptions: vscode.TerminalOptions = {
Expand Down Expand Up @@ -149,7 +151,7 @@ export class PowerShellProcess {

public dispose() {
// Clean up the session file
utils.deleteSessionFile(this.sessionFilePath);
PowerShellProcess.deleteSessionFile(this.sessionFilePath);

if (this.consoleCloseSubscription) {
this.consoleCloseSubscription.dispose();
Expand Down Expand Up @@ -189,17 +191,31 @@ export class PowerShellProcess {
return true;
}

private async waitForSessionFile(): Promise<utils.IEditorServicesSessionDetails> {
private static readSessionFile(sessionFilePath: vscode.Uri): IEditorServicesSessionDetails {
// TODO: Use vscode.workspace.fs.readFile instead of fs.readFileSync.
const fileContents = fs.readFileSync(sessionFilePath.fsPath, "utf-8");
return JSON.parse(fileContents);
}

private static async deleteSessionFile(sessionFilePath: vscode.Uri) {
try {
await vscode.workspace.fs.delete(sessionFilePath);
} catch (e) {
// TODO: Be more specific about what we're catching
}
}

private async waitForSessionFile(): Promise<IEditorServicesSessionDetails> {
// Determine how many tries by dividing by 2000 thus checking every 2 seconds.
const numOfTries = this.sessionSettings.developer.waitForSessionFileTimeoutSeconds / 2;
const warnAt = numOfTries - PowerShellProcess.warnUserThreshold;

// Check every 2 seconds
for (let i = numOfTries; i > 0; i--) {
if (utils.checkIfFileExists(this.sessionFilePath)) {
if (utils.checkIfFileExists(this.sessionFilePath.fsPath)) {
this.log.write("Session file found");
const sessionDetails = utils.readSessionFile(this.sessionFilePath);
utils.deleteSessionFile(this.sessionFilePath);
const sessionDetails = PowerShellProcess.readSessionFile(this.sessionFilePath);
PowerShellProcess.deleteSessionFile(this.sessionFilePath);
return sessionDetails;
}

Expand Down
Loading