protected async Task HandleLaunchRequest( LaunchRequestArguments launchParams, RequestContext<object> requestContext) { // Execute the given PowerShell script and send the response. // Note that we aren't waiting for execution to complete here // because the debugger could stop while the script executes. Task executeTask = editorSession.PowerShellContext .ExecuteScriptAtPath(launchParams.Program) .ContinueWith( async (t) => { Logger.Write(LogLevel.Verbose, "Execution completed, terminating..."); await requestContext.SendEvent( TerminatedEvent.Type, null); // Stop the server this.Stop(); }); await requestContext.SendResult(null); }
protected async Task HandleLaunchRequest( LaunchRequestArguments launchParams, RequestContext<object> requestContext) { // Set the working directory for the PowerShell runspace to the cwd passed in via launch.json. // In case that is null, use the the folder of the script to be executed. If the resulting // working dir path is a file path then extract the directory and use that. string workingDir = launchParams.Cwd ?? launchParams.Program; try { if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory) { workingDir = Path.GetDirectoryName(workingDir); } } catch (Exception ex) { Logger.Write(LogLevel.Error, "cwd path is bad: " + ex.Message); workingDir = Environment.CurrentDirectory; } var setWorkingDirCommand = new PSCommand(); setWorkingDirCommand.AddCommand(@"Microsoft.PowerShell.Management\Set-Location") .AddParameter("LiteralPath", workingDir); await editorSession.PowerShellContext.ExecuteCommand(setWorkingDirCommand); Logger.Write(LogLevel.Verbose, "Working dir set to: " + workingDir); // Prepare arguments to the script - if specified string arguments = null; if ((launchParams.Args != null) && (launchParams.Args.Length > 0)) { arguments = string.Join(" ", launchParams.Args); Logger.Write(LogLevel.Verbose, "Script arguments are: " + arguments); } // Execute the given PowerShell script and send the response. // Note that we aren't waiting for execution to complete here // because the debugger could stop while the script executes. Task executeTask = editorSession.PowerShellContext .ExecuteScriptAtPath(launchParams.Program, arguments) .ContinueWith( async (t) => { Logger.Write(LogLevel.Verbose, "Execution completed, terminating..."); await requestContext.SendEvent( TerminatedEvent.Type, null); // Stop the server this.Stop(); }); await requestContext.SendResult(null); }