Skip to content

Commit 697e1e2

Browse files
Merge pull request #4088 from PowerShell/andschwa/session-path
Use `context.storageUri` for session file (and refactor)
2 parents 84fa978 + 5f80a6e commit 697e1e2

File tree

8 files changed

+122
-165
lines changed

8 files changed

+122
-165
lines changed

src/features/DebugSession.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import { NotificationType, RequestType } from "vscode-languageclient";
88
import { LanguageClient } from "vscode-languageclient/node";
99
import { getPlatformDetails, OperatingSystem } from "../platform";
1010
import { PowerShellProcess} from "../process";
11-
import { SessionManager, SessionStatus } from "../session";
11+
import { IEditorServicesSessionDetails, SessionManager, SessionStatus } from "../session";
1212
import Settings = require("../settings");
13-
import utils = require("../utils");
1413
import { Logger } from "../logging";
1514
import { LanguageClientConsumer } from "../languageClientConsumer";
1615

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

2625
private sessionCount: number = 1;
2726
private tempDebugProcess: PowerShellProcess;
28-
private tempDebugEventHandler: vscode.Disposable;
29-
private tempSessionDetails: utils.IEditorServicesSessionDetails;
27+
private tempSessionDetails: IEditorServicesSessionDetails;
3028

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

314-
const sessionFilePath = utils.getDebugSessionFilePath();
315-
316312
if (config.createTemporaryIntegratedConsole) {
317-
// TODO: This should be cleaned up to support multiple temporary consoles.
318-
this.tempDebugProcess = this.sessionManager.createDebugSessionProcess(sessionFilePath, settings);
313+
this.tempDebugProcess = this.sessionManager.createDebugSessionProcess(settings);
319314
this.tempSessionDetails = await this.tempDebugProcess.start(`DebugSession-${this.sessionCount++}`);
320-
utils.writeSessionFile(sessionFilePath, this.tempSessionDetails);
321-
} else {
322-
utils.writeSessionFile(sessionFilePath, this.sessionManager.getSessionDetails());
323315
}
324316

325317
return config;

src/features/PesterTests.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,7 @@ export class PesterTestsFeature implements vscode.Disposable {
128128
private async launch(launchConfig): Promise<boolean> {
129129
// Create or show the interactive console
130130
// TODO: #367 Check if "newSession" mode is configured
131-
vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);
132-
133-
// Write out temporary debug session file
134-
utils.writeSessionFile(
135-
utils.getDebugSessionFilePath(),
136-
this.sessionManager.getSessionDetails());
131+
await vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);
137132

138133
// TODO: Update to handle multiple root workspaces.
139134
//

src/features/RunCode.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,16 @@ export class RunCodeFeature implements vscode.Disposable {
3535

3636
const launchType = runInDebugger ? LaunchType.Debug : LaunchType.Run;
3737
const launchConfig = createLaunchConfig(launchType, scriptToRun, args);
38-
this.launch(launchConfig);
38+
await this.launch(launchConfig);
3939
}
4040

41-
private launch(launchConfig) {
41+
private async launch(launchConfig: string | vscode.DebugConfiguration) {
4242
// Create or show the interactive console
43-
// TODO #367: Check if "newSession" mode is configured
44-
vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);
45-
46-
// Write out temporary debug session file
47-
utils.writeSessionFile(
48-
utils.getDebugSessionFilePath(),
49-
this.sessionManager.getSessionDetails());
43+
// TODO: #367: Check if "newSession" mode is configured
44+
await vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);
5045

5146
// TODO: Update to handle multiple root workspaces.
52-
vscode.debug.startDebugging(vscode.workspace.workspaceFolders?.[0], launchConfig);
47+
await vscode.debug.startDebugging(vscode.workspace.workspaceFolders?.[0], launchConfig);
5348
}
5449
}
5550

src/logging.ts

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import fs = require("fs");
55
import os = require("os");
66
import path = require("path");
77
import vscode = require("vscode");
8-
import utils = require("./utils");
98

109
export enum LogLevel {
1110
Diagnostic,
@@ -44,7 +43,6 @@ export class Logger implements ILogger {
4443
if (logBasePath === undefined) {
4544
// No workspace, we have to use another folder.
4645
this.logBasePath = vscode.Uri.file(path.resolve(__dirname, "../logs"));
47-
utils.ensurePathExists(this.logBasePath.fsPath);
4846
} else {
4947
this.logBasePath = vscode.Uri.joinPath(logBasePath, "logs");
5048
}

src/process.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
import fs = require("fs");
45
import cp = require("child_process");
56
import * as semver from "semver";
67
import path = require("path");
78
import vscode = require("vscode");
89
import { Logger } from "./logging";
910
import Settings = require("./settings");
1011
import utils = require("./utils");
12+
import { IEditorServicesSessionDetails, SessionManager } from "./session";
1113

1214
export class PowerShellProcess {
13-
public static escapeSingleQuotes(pspath: string): string {
14-
return pspath.replace(new RegExp("'", "g"), "''");
15+
public static escapeSingleQuotes(psPath: string): string {
16+
return psPath.replace(new RegExp("'", "g"), "''");
1517
}
1618

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

3638
this.onExited = this.onExitedEmitter.event;
3739
}
3840

39-
public async start(logFileName: string): Promise<utils.IEditorServicesSessionDetails> {
41+
public async start(logFileName: string): Promise<IEditorServicesSessionDetails> {
4042
const editorServicesLogPath = this.log.getLogFilePath(logFileName);
4143

4244
const psesModulePath =
@@ -52,7 +54,7 @@ export class PowerShellProcess {
5254

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

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

101103
// Make sure no old session file exists
102-
utils.deleteSessionFile(this.sessionFilePath);
104+
await PowerShellProcess.deleteSessionFile(this.sessionFilePath);
103105

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

150152
public dispose() {
151153
// Clean up the session file
152-
utils.deleteSessionFile(this.sessionFilePath);
154+
PowerShellProcess.deleteSessionFile(this.sessionFilePath);
153155

154156
if (this.consoleCloseSubscription) {
155157
this.consoleCloseSubscription.dispose();
@@ -189,17 +191,31 @@ export class PowerShellProcess {
189191
return true;
190192
}
191193

192-
private async waitForSessionFile(): Promise<utils.IEditorServicesSessionDetails> {
194+
private static readSessionFile(sessionFilePath: vscode.Uri): IEditorServicesSessionDetails {
195+
// TODO: Use vscode.workspace.fs.readFile instead of fs.readFileSync.
196+
const fileContents = fs.readFileSync(sessionFilePath.fsPath, "utf-8");
197+
return JSON.parse(fileContents);
198+
}
199+
200+
private static async deleteSessionFile(sessionFilePath: vscode.Uri) {
201+
try {
202+
await vscode.workspace.fs.delete(sessionFilePath);
203+
} catch (e) {
204+
// TODO: Be more specific about what we're catching
205+
}
206+
}
207+
208+
private async waitForSessionFile(): Promise<IEditorServicesSessionDetails> {
193209
// Determine how many tries by dividing by 2000 thus checking every 2 seconds.
194210
const numOfTries = this.sessionSettings.developer.waitForSessionFileTimeoutSeconds / 2;
195211
const warnAt = numOfTries - PowerShellProcess.warnUserThreshold;
196212

197213
// Check every 2 seconds
198214
for (let i = numOfTries; i > 0; i--) {
199-
if (utils.checkIfFileExists(this.sessionFilePath)) {
215+
if (utils.checkIfFileExists(this.sessionFilePath.fsPath)) {
200216
this.log.write("Session file found");
201-
const sessionDetails = utils.readSessionFile(this.sessionFilePath);
202-
utils.deleteSessionFile(this.sessionFilePath);
217+
const sessionDetails = PowerShellProcess.readSessionFile(this.sessionFilePath);
218+
PowerShellProcess.deleteSessionFile(this.sessionFilePath);
203219
return sessionDetails;
204220
}
205221

0 commit comments

Comments
 (0)