1
1
// Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
3
4
+ import fs = require( "fs" ) ;
4
5
import cp = require( "child_process" ) ;
5
6
import * as semver from "semver" ;
6
7
import path = require( "path" ) ;
7
8
import vscode = require( "vscode" ) ;
8
9
import { Logger } from "./logging" ;
9
10
import Settings = require( "./settings" ) ;
10
11
import utils = require( "./utils" ) ;
12
+ import { IEditorServicesSessionDetails , SessionManager } from "./session" ;
11
13
12
14
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" ) , "''" ) ;
15
17
}
16
18
17
19
// This is used to warn the user that the extension is taking longer than expected to startup.
@@ -30,13 +32,13 @@ export class PowerShellProcess {
30
32
private title : string ,
31
33
private log : Logger ,
32
34
private startPsesArgs : string ,
33
- private sessionFilePath : string ,
35
+ private sessionFilePath : vscode . Uri ,
34
36
private sessionSettings : Settings . ISettings ) {
35
37
36
38
this . onExited = this . onExitedEmitter . event ;
37
39
}
38
40
39
- public async start ( logFileName : string ) : Promise < utils . IEditorServicesSessionDetails > {
41
+ public async start ( logFileName : string ) : Promise < IEditorServicesSessionDetails > {
40
42
const editorServicesLogPath = this . log . getLogFilePath ( logFileName ) ;
41
43
42
44
const psesModulePath =
@@ -52,7 +54,7 @@ export class PowerShellProcess {
52
54
53
55
this . startPsesArgs +=
54
56
`-LogPath '${ PowerShellProcess . escapeSingleQuotes ( editorServicesLogPath . fsPath ) } ' ` +
55
- `-SessionDetailsPath '${ PowerShellProcess . escapeSingleQuotes ( this . sessionFilePath ) } ' ` +
57
+ `-SessionDetailsPath '${ PowerShellProcess . escapeSingleQuotes ( this . sessionFilePath . fsPath ) } ' ` +
56
58
`-FeatureFlags @(${ featureFlags } ) ` ;
57
59
58
60
if ( this . sessionSettings . integratedConsole . useLegacyReadLine ) {
@@ -99,7 +101,7 @@ export class PowerShellProcess {
99
101
" PowerShell Editor Services args: " + startEditorServices ) ;
100
102
101
103
// Make sure no old session file exists
102
- utils . deleteSessionFile ( this . sessionFilePath ) ;
104
+ await PowerShellProcess . deleteSessionFile ( this . sessionFilePath ) ;
103
105
104
106
// Launch PowerShell in the integrated terminal
105
107
const terminalOptions : vscode . TerminalOptions = {
@@ -149,7 +151,7 @@ export class PowerShellProcess {
149
151
150
152
public dispose ( ) {
151
153
// Clean up the session file
152
- utils . deleteSessionFile ( this . sessionFilePath ) ;
154
+ PowerShellProcess . deleteSessionFile ( this . sessionFilePath ) ;
153
155
154
156
if ( this . consoleCloseSubscription ) {
155
157
this . consoleCloseSubscription . dispose ( ) ;
@@ -189,17 +191,31 @@ export class PowerShellProcess {
189
191
return true ;
190
192
}
191
193
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 > {
193
209
// Determine how many tries by dividing by 2000 thus checking every 2 seconds.
194
210
const numOfTries = this . sessionSettings . developer . waitForSessionFileTimeoutSeconds / 2 ;
195
211
const warnAt = numOfTries - PowerShellProcess . warnUserThreshold ;
196
212
197
213
// Check every 2 seconds
198
214
for ( let i = numOfTries ; i > 0 ; i -- ) {
199
- if ( utils . checkIfFileExists ( this . sessionFilePath ) ) {
215
+ if ( utils . checkIfFileExists ( this . sessionFilePath . fsPath ) ) {
200
216
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 ) ;
203
219
return sessionDetails ;
204
220
}
205
221
0 commit comments