@@ -22,134 +22,111 @@ export class PowerShellProcess {
2222
2323 private consoleTerminal : vscode . Terminal = undefined ;
2424 private consoleCloseSubscription : vscode . Disposable ;
25- private sessionDetails : utils . IEditorServicesSessionDetails ;
2625
2726 constructor (
2827 public exePath : string ,
2928 private bundledModulesPath : string ,
3029 private title : string ,
3130 private log : Logger ,
32- private startArgs : string ,
31+ private startPsesArgs : string ,
3332 private sessionFilePath : string ,
3433 private sessionSettings : Settings . ISettings ) {
3534
3635 this . onExited = this . onExitedEmitter . event ;
3736 }
3837
39- public start ( logFileName : string ) : Thenable < utils . IEditorServicesSessionDetails > {
40-
41- return new Promise < utils . IEditorServicesSessionDetails > (
42- ( resolve , reject ) => {
43- try {
44- const psesModulePath =
45- path . resolve (
46- __dirname ,
47- this . bundledModulesPath ,
48- "PowerShellEditorServices/PowerShellEditorServices.psd1" ) ;
49-
50- const editorServicesLogPath = this . log . getLogFilePath ( logFileName ) ;
51-
52- const featureFlags =
53- this . sessionSettings . developer . featureFlags !== undefined
54- ? this . sessionSettings . developer . featureFlags . map ( ( f ) => `'${ f } '` ) . join ( ", " )
55- : "" ;
56-
57- this . startArgs +=
58- `-LogPath '${ PowerShellProcess . escapeSingleQuotes ( editorServicesLogPath ) } ' ` +
59- `-SessionDetailsPath '${ PowerShellProcess . escapeSingleQuotes ( this . sessionFilePath ) } ' ` +
60- `-FeatureFlags @(${ featureFlags } ) ` ;
61-
62- if ( this . sessionSettings . integratedConsole . useLegacyReadLine ) {
63- this . startArgs += "-UseLegacyReadLine" ;
64- }
65-
66- const powerShellArgs = [ ] ;
67-
68- const useLoginShell : boolean =
69- ( utils . isMacOS && this . sessionSettings . startAsLoginShell . osx )
70- || ( utils . isLinux && this . sessionSettings . startAsLoginShell . linux ) ;
71-
72- if ( useLoginShell && this . isLoginShell ( this . exePath ) ) {
73- // This MUST be the first argument.
74- powerShellArgs . push ( "-Login" ) ;
75- }
76-
77- powerShellArgs . push ( "-NoProfile" ) ;
78- powerShellArgs . push ( "-NonInteractive" ) ;
79-
80- // Only add ExecutionPolicy param on Windows
81- if ( utils . isWindows ) {
82- powerShellArgs . push ( "-ExecutionPolicy" , "Bypass" ) ;
83- }
84-
85- const startEditorServices = "Import-Module '" +
86- PowerShellProcess . escapeSingleQuotes ( psesModulePath ) +
87- "'; Start-EditorServices " + this . startArgs ;
88-
89- if ( utils . isWindows ) {
90- powerShellArgs . push (
91- "-Command" ,
92- startEditorServices ) ;
93- } else {
94- // Use -EncodedCommand for better quote support on non-Windows
95- powerShellArgs . push (
96- "-EncodedCommand" ,
97- Buffer . from ( startEditorServices , "utf16le" ) . toString ( "base64" ) ) ;
98- }
99-
100- this . log . write (
101- "Language server starting --" ,
102- " PowerShell executable: " + this . exePath ,
103- " PowerShell args: " + powerShellArgs . join ( " " ) ,
104- " PowerShell Editor Services args: " + startEditorServices ) ;
105-
106- // Make sure no old session file exists
107- utils . deleteSessionFile ( this . sessionFilePath ) ;
108-
109- // Launch PowerShell in the integrated terminal
110- this . consoleTerminal =
111- vscode . window . createTerminal ( {
112- name : this . title ,
113- shellPath : this . exePath ,
114- shellArgs : powerShellArgs ,
115- hideFromUser : ! this . sessionSettings . integratedConsole . showOnStartup ,
116- } ) ;
117-
118- if ( this . sessionSettings . integratedConsole . showOnStartup ) {
119- // We still need to run this to set the active terminal to the Integrated Console.
120- this . consoleTerminal . show ( true ) ;
121- }
122-
123- // Start the language client
124- utils . waitForSessionFile (
125- this . sessionFilePath ,
126- ( sessionDetails , error ) => {
127- // Clean up the session file
128- utils . deleteSessionFile ( this . sessionFilePath ) ;
129-
130- if ( error ) {
131- reject ( error ) ;
132- } else {
133- this . sessionDetails = sessionDetails ;
134- resolve ( this . sessionDetails ) ;
135- }
136- } ) ;
137-
138- this . consoleCloseSubscription =
139- vscode . window . onDidCloseTerminal (
140- ( terminal ) => {
141- if ( terminal === this . consoleTerminal ) {
142- this . log . write ( "powershell.exe terminated or terminal UI was closed" ) ;
143- this . onExitedEmitter . fire ( ) ;
144- }
145- } ) ;
146-
147- this . consoleTerminal . processId . then (
148- ( pid ) => { this . log . write ( `powershell.exe started, pid: ${ pid } ` ) ; } ) ;
149- } catch ( e ) {
150- reject ( e ) ;
151- }
38+ public async start ( logFileName : string ) : Promise < utils . IEditorServicesSessionDetails > {
39+ const editorServicesLogPath = this . log . getLogFilePath ( logFileName ) ;
40+
41+ const psesModulePath =
42+ path . resolve (
43+ __dirname ,
44+ this . bundledModulesPath ,
45+ "PowerShellEditorServices/PowerShellEditorServices.psd1" ) ;
46+
47+ const featureFlags =
48+ this . sessionSettings . developer . featureFlags !== undefined
49+ ? this . sessionSettings . developer . featureFlags . map ( ( f ) => `'${ f } '` ) . join ( ", " )
50+ : "" ;
51+
52+ this . startPsesArgs +=
53+ `-LogPath '${ PowerShellProcess . escapeSingleQuotes ( editorServicesLogPath ) } ' ` +
54+ `-SessionDetailsPath '${ PowerShellProcess . escapeSingleQuotes ( this . sessionFilePath ) } ' ` +
55+ `-FeatureFlags @(${ featureFlags } ) ` ;
56+
57+ if ( this . sessionSettings . integratedConsole . useLegacyReadLine ) {
58+ this . startPsesArgs += "-UseLegacyReadLine" ;
59+ }
60+
61+ const powerShellArgs = [ ] ;
62+
63+ const useLoginShell : boolean =
64+ ( utils . isMacOS && this . sessionSettings . startAsLoginShell . osx )
65+ || ( utils . isLinux && this . sessionSettings . startAsLoginShell . linux ) ;
66+
67+ if ( useLoginShell && this . isLoginShell ( this . exePath ) ) {
68+ // This MUST be the first argument.
69+ powerShellArgs . push ( "-Login" ) ;
70+ }
71+
72+ powerShellArgs . push ( "-NoProfile" ) ;
73+ powerShellArgs . push ( "-NonInteractive" ) ;
74+
75+ // Only add ExecutionPolicy param on Windows
76+ if ( utils . isWindows ) {
77+ powerShellArgs . push ( "-ExecutionPolicy" , "Bypass" ) ;
78+ }
79+
80+ const startEditorServices = "Import-Module '" +
81+ PowerShellProcess . escapeSingleQuotes ( psesModulePath ) +
82+ "'; Start-EditorServices " + this . startPsesArgs ;
83+
84+ if ( utils . isWindows ) {
85+ powerShellArgs . push (
86+ "-Command" ,
87+ startEditorServices ) ;
88+ } else {
89+ // Use -EncodedCommand for better quote support on non-Windows
90+ powerShellArgs . push (
91+ "-EncodedCommand" ,
92+ Buffer . from ( startEditorServices , "utf16le" ) . toString ( "base64" ) ) ;
93+ }
94+
95+ this . log . write (
96+ "Language server starting --" ,
97+ " PowerShell executable: " + this . exePath ,
98+ " PowerShell args: " + powerShellArgs . join ( " " ) ,
99+ " PowerShell Editor Services args: " + startEditorServices ) ;
100+
101+ // Make sure no old session file exists
102+ utils . deleteSessionFile ( this . sessionFilePath ) ;
103+
104+ // Launch PowerShell in the integrated terminal
105+ this . consoleTerminal =
106+ vscode . window . createTerminal ( {
107+ name : this . title ,
108+ shellPath : this . exePath ,
109+ shellArgs : powerShellArgs ,
110+ hideFromUser : ! this . sessionSettings . integratedConsole . showOnStartup ,
152111 } ) ;
112+
113+ if ( this . sessionSettings . integratedConsole . showOnStartup ) {
114+ // We still need to run this to set the active terminal to the Integrated Console.
115+ this . consoleTerminal . show ( true ) ;
116+ }
117+
118+ // Start the language client
119+ const sessionDetails = await this . waitForSessionFile ( ) ;
120+
121+ // Subscribe a log event for when the terminal closes
122+ this . consoleCloseSubscription = vscode . window . onDidCloseTerminal ( ( terminal ) => this . onTerminalClose ( terminal ) ) ;
123+
124+ // Log that the PowerShell terminal process has been started
125+ const terminalPid = await this . consoleTerminal . processId ;
126+ const pwshName = path . basename ( this . exePath ) ;
127+ this . log . write ( `${ pwshName } started, pid: ${ terminalPid } ` ) ;
128+
129+ return sessionDetails ;
153130 }
154131
155132 public showConsole ( preserveFocus : boolean ) {
@@ -188,4 +165,27 @@ export class PowerShellProcess {
188165
189166 return true ;
190167 }
168+
169+ private waitForSessionFile ( ) : Promise < utils . IEditorServicesSessionDetails > {
170+ return new Promise ( ( resolve , reject ) => {
171+ utils . waitForSessionFile ( this . sessionFilePath , ( sessionDetails , error ) => {
172+ utils . deleteSessionFile ( this . sessionFilePath ) ;
173+
174+ if ( error ) {
175+ return reject ( error ) ;
176+ }
177+
178+ resolve ( sessionDetails ) ;
179+ } ) ;
180+ } ) ;
181+ }
182+
183+ private onTerminalClose ( terminal : vscode . Terminal ) {
184+ if ( terminal !== this . consoleTerminal ) {
185+ return ;
186+ }
187+
188+ this . log . write ( "powershell.exe terminated or terminal UI was closed" ) ;
189+ this . onExitedEmitter . fire ( ) ;
190+ }
191191}
0 commit comments