From cd225a0e3a4da3f32fe22f928efa088f97dea2af Mon Sep 17 00:00:00 2001 From: Rob Holt Date: Wed, 19 Feb 2020 11:43:50 -0800 Subject: [PATCH 1/3] Clean up start code --- src/process.ts | 227 +++++++++++++++++++++++++------------------------ 1 file changed, 114 insertions(+), 113 deletions(-) diff --git a/src/process.ts b/src/process.ts index 0734a571da..2d10140faa 100644 --- a/src/process.ts +++ b/src/process.ts @@ -36,120 +36,96 @@ export class PowerShellProcess { this.onExited = this.onExitedEmitter.event; } - public start(logFileName: string): Thenable { - - return new Promise( - (resolve, reject) => { - try { - const psesModulePath = - path.resolve( - __dirname, - this.bundledModulesPath, - "PowerShellEditorServices/PowerShellEditorServices.psd1"); - - const editorServicesLogPath = this.log.getLogFilePath(logFileName); - - const featureFlags = - this.sessionSettings.developer.featureFlags !== undefined - ? this.sessionSettings.developer.featureFlags.map((f) => `'${f}'`).join(", ") - : ""; - - this.startArgs += - `-LogPath '${PowerShellProcess.escapeSingleQuotes(editorServicesLogPath)}' ` + - `-SessionDetailsPath '${PowerShellProcess.escapeSingleQuotes(this.sessionFilePath)}' ` + - `-FeatureFlags @(${featureFlags}) `; - - if (this.sessionSettings.integratedConsole.useLegacyReadLine) { - this.startArgs += "-UseLegacyReadLine"; - } - - const powerShellArgs = []; - - const useLoginShell: boolean = - (utils.isMacOS && this.sessionSettings.startAsLoginShell.osx) - || (utils.isLinux && this.sessionSettings.startAsLoginShell.linux); - - if (useLoginShell && this.isLoginShell(this.exePath)) { - // This MUST be the first argument. - powerShellArgs.push("-Login"); - } - - powerShellArgs.push("-NoProfile"); - powerShellArgs.push("-NonInteractive"); - - // Only add ExecutionPolicy param on Windows - if (utils.isWindows) { - powerShellArgs.push("-ExecutionPolicy", "Bypass"); - } - - const startEditorServices = "Import-Module '" + - PowerShellProcess.escapeSingleQuotes(psesModulePath) + - "'; Start-EditorServices " + this.startArgs; - - if (utils.isWindows) { - powerShellArgs.push( - "-Command", - startEditorServices); - } else { - // Use -EncodedCommand for better quote support on non-Windows - powerShellArgs.push( - "-EncodedCommand", - Buffer.from(startEditorServices, "utf16le").toString("base64")); - } - - this.log.write( - "Language server starting --", - " PowerShell executable: " + this.exePath, - " PowerShell args: " + powerShellArgs.join(" "), - " PowerShell Editor Services args: " + startEditorServices); - - // Make sure no old session file exists - utils.deleteSessionFile(this.sessionFilePath); - - // Launch PowerShell in the integrated terminal - this.consoleTerminal = - vscode.window.createTerminal({ - name: this.title, - shellPath: this.exePath, - shellArgs: powerShellArgs, - hideFromUser: !this.sessionSettings.integratedConsole.showOnStartup, - }); - - if (this.sessionSettings.integratedConsole.showOnStartup) { - // We still need to run this to set the active terminal to the Integrated Console. - this.consoleTerminal.show(true); - } - - // Start the language client - utils.waitForSessionFile( - this.sessionFilePath, - (sessionDetails, error) => { - // Clean up the session file - utils.deleteSessionFile(this.sessionFilePath); - - if (error) { - reject(error); - } else { - this.sessionDetails = sessionDetails; - resolve(this.sessionDetails); - } - }); - - this.consoleCloseSubscription = - vscode.window.onDidCloseTerminal( - (terminal) => { - if (terminal === this.consoleTerminal) { - this.log.write("powershell.exe terminated or terminal UI was closed"); - this.onExitedEmitter.fire(); - } - }); - - this.consoleTerminal.processId.then( - (pid) => { this.log.write(`powershell.exe started, pid: ${pid}`); }); - } catch (e) { - reject(e); - } + public async start(logFileName: string): Promise { + const editorServicesLogPath = this.log.getLogFilePath(logFileName); + + const psesModulePath = + path.resolve( + __dirname, + this.bundledModulesPath, + "PowerShellEditorServices/PowerShellEditorServices.psd1"); + + const featureFlags = + this.sessionSettings.developer.featureFlags !== undefined + ? this.sessionSettings.developer.featureFlags.map((f) => `'${f}'`).join(", ") + : ""; + + this.startArgs += + `-LogPath '${PowerShellProcess.escapeSingleQuotes(editorServicesLogPath)}' ` + + `-SessionDetailsPath '${PowerShellProcess.escapeSingleQuotes(this.sessionFilePath)}' ` + + `-FeatureFlags @(${featureFlags}) `; + + if (this.sessionSettings.integratedConsole.useLegacyReadLine) { + this.startArgs += "-UseLegacyReadLine"; + } + + const powerShellArgs = []; + + const useLoginShell: boolean = + (utils.isMacOS && this.sessionSettings.startAsLoginShell.osx) + || (utils.isLinux && this.sessionSettings.startAsLoginShell.linux); + + if (useLoginShell && this.isLoginShell(this.exePath)) { + // This MUST be the first argument. + powerShellArgs.push("-Login"); + } + + powerShellArgs.push("-NoProfile"); + powerShellArgs.push("-NonInteractive"); + + // Only add ExecutionPolicy param on Windows + if (utils.isWindows) { + powerShellArgs.push("-ExecutionPolicy", "Bypass"); + } + + const startEditorServices = "Import-Module '" + + PowerShellProcess.escapeSingleQuotes(psesModulePath) + + "'; Start-EditorServices " + this.startArgs; + + if (utils.isWindows) { + powerShellArgs.push( + "-Command", + startEditorServices); + } else { + // Use -EncodedCommand for better quote support on non-Windows + powerShellArgs.push( + "-EncodedCommand", + Buffer.from(startEditorServices, "utf16le").toString("base64")); + } + + this.log.write( + "Language server starting --", + " PowerShell executable: " + this.exePath, + " PowerShell args: " + powerShellArgs.join(" "), + " PowerShell Editor Services args: " + startEditorServices); + + // Make sure no old session file exists + utils.deleteSessionFile(this.sessionFilePath); + + // Launch PowerShell in the integrated terminal + this.consoleTerminal = + vscode.window.createTerminal({ + name: this.title, + shellPath: this.exePath, + shellArgs: powerShellArgs, + hideFromUser: !this.sessionSettings.integratedConsole.showOnStartup, }); + + if (this.sessionSettings.integratedConsole.showOnStartup) { + // We still need to run this to set the active terminal to the Integrated Console. + this.consoleTerminal.show(true); + } + + // Start the language client + this.sessionDetails = await this.waitForSessionFile(); + + // Subscribe a log event for when the terminal closes + this.consoleCloseSubscription = vscode.window.onDidCloseTerminal((terminal) => this.onTerminalClose(terminal)); + + // Log that the PowerShell terminal process has been started + const terminalPid = await this.consoleTerminal.processId; + const pwshName = path.basename(this.exePath); + this.log.write(`${pwshName} started, pid: ${terminalPid}`); } public showConsole(preserveFocus: boolean) { @@ -188,4 +164,29 @@ export class PowerShellProcess { return true; } + + private waitForSessionFile(): Promise { + return new Promise((resolve, reject) => { + utils.waitForSessionFile(this.sessionFilePath, (sessionDetails, error) => { + utils.deleteSessionFile(this.sessionFilePath); + + if (error) { + return reject(error); + } + + resolve(sessionDetails); + }); + }); + } + + private onTerminalClose(terminal: vscode.Terminal) { + if (terminal !== this.consoleTerminal) { + return; + } + + // It would be very + + this.log.write("powershell.exe terminated or terminal UI was closed"); + this.onExitedEmitter.fire(); + } } From dcdfa8859b4430142e0f5b3a4fdda39a0cbbac77 Mon Sep 17 00:00:00 2001 From: Rob Holt Date: Wed, 19 Feb 2020 14:14:22 -0800 Subject: [PATCH 2/3] Fix promise usage --- src/process.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/process.ts b/src/process.ts index 2d10140faa..f2fc337b94 100644 --- a/src/process.ts +++ b/src/process.ts @@ -22,14 +22,13 @@ export class PowerShellProcess { private consoleTerminal: vscode.Terminal = undefined; private consoleCloseSubscription: vscode.Disposable; - private sessionDetails: utils.IEditorServicesSessionDetails; constructor( public exePath: string, private bundledModulesPath: string, private title: string, private log: Logger, - private startArgs: string, + private startPsesArgs: string, private sessionFilePath: string, private sessionSettings: Settings.ISettings) { @@ -50,13 +49,13 @@ export class PowerShellProcess { ? this.sessionSettings.developer.featureFlags.map((f) => `'${f}'`).join(", ") : ""; - this.startArgs += + this.startPsesArgs += `-LogPath '${PowerShellProcess.escapeSingleQuotes(editorServicesLogPath)}' ` + `-SessionDetailsPath '${PowerShellProcess.escapeSingleQuotes(this.sessionFilePath)}' ` + `-FeatureFlags @(${featureFlags}) `; if (this.sessionSettings.integratedConsole.useLegacyReadLine) { - this.startArgs += "-UseLegacyReadLine"; + this.startPsesArgs += "-UseLegacyReadLine"; } const powerShellArgs = []; @@ -80,7 +79,7 @@ export class PowerShellProcess { const startEditorServices = "Import-Module '" + PowerShellProcess.escapeSingleQuotes(psesModulePath) + - "'; Start-EditorServices " + this.startArgs; + "'; Start-EditorServices " + this.startPsesArgs; if (utils.isWindows) { powerShellArgs.push( @@ -117,7 +116,7 @@ export class PowerShellProcess { } // Start the language client - this.sessionDetails = await this.waitForSessionFile(); + const sessionDetails = await this.waitForSessionFile(); // Subscribe a log event for when the terminal closes this.consoleCloseSubscription = vscode.window.onDidCloseTerminal((terminal) => this.onTerminalClose(terminal)); @@ -126,6 +125,8 @@ export class PowerShellProcess { const terminalPid = await this.consoleTerminal.processId; const pwshName = path.basename(this.exePath); this.log.write(`${pwshName} started, pid: ${terminalPid}`); + + return sessionDetails; } public showConsole(preserveFocus: boolean) { From 688180e64334ed30b1e6d8b1920b100418e08448 Mon Sep 17 00:00:00 2001 From: Rob Holt Date: Wed, 19 Feb 2020 14:17:15 -0800 Subject: [PATCH 3/3] Remove half comment --- src/process.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/process.ts b/src/process.ts index f2fc337b94..ba945aaa49 100644 --- a/src/process.ts +++ b/src/process.ts @@ -185,8 +185,6 @@ export class PowerShellProcess { return; } - // It would be very - this.log.write("powershell.exe terminated or terminal UI was closed"); this.onExitedEmitter.fire(); }