-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathConfigurationDoneHandler.cs
108 lines (98 loc) · 5.11 KB
/
ConfigurationDoneHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Debugging;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Utility;
using OmniSharp.Extensions.DebugAdapter.Protocol.Events;
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
using OmniSharp.Extensions.DebugAdapter.Protocol.Server;
namespace Microsoft.PowerShell.EditorServices.Handlers
{
internal class ConfigurationDoneHandler : IConfigurationDoneHandler
{
private static readonly PowerShellExecutionOptions s_debuggerExecutionOptions = new()
{
MustRunInForeground = true,
WriteInputToHost = true,
WriteOutputToHost = true,
ThrowOnError = false,
AddToHistory = true,
};
private readonly ILogger _logger;
private readonly IDebugAdapterServerFacade _debugAdapterServer;
private readonly DebugService _debugService;
private readonly DebugStateService _debugStateService;
private readonly DebugEventHandlerService _debugEventHandlerService;
private readonly IInternalPowerShellExecutionService _executionService;
private readonly WorkspaceService _workspaceService;
private readonly IPowerShellDebugContext _debugContext;
public ConfigurationDoneHandler(
ILoggerFactory loggerFactory,
IDebugAdapterServerFacade debugAdapterServer,
DebugService debugService,
DebugStateService debugStateService,
DebugEventHandlerService debugEventHandlerService,
IInternalPowerShellExecutionService executionService,
WorkspaceService workspaceService,
IPowerShellDebugContext debugContext)
{
_logger = loggerFactory.CreateLogger<ConfigurationDoneHandler>();
_debugAdapterServer = debugAdapterServer;
_debugService = debugService;
_debugStateService = debugStateService;
_debugEventHandlerService = debugEventHandlerService;
_executionService = executionService;
_workspaceService = workspaceService;
_debugContext = debugContext;
}
public Task<ConfigurationDoneResponse> Handle(ConfigurationDoneArguments request, CancellationToken cancellationToken)
{
_debugService.IsClientAttached = true;
if (!string.IsNullOrEmpty(_debugStateService.ScriptToLaunch))
{
// NOTE: This is an unawaited task because responding to "configuration done" means
// setting up the debugger, and in our case that means starting the script but not
// waiting for it to finish.
Task _ = LaunchScriptAsync(_debugStateService.ScriptToLaunch).HandleErrorsAsync(_logger);
}
if (_debugStateService.IsInteractiveDebugSession && _debugService.IsDebuggerStopped)
{
if (_debugService.CurrentDebuggerStoppedEventArgs is not null)
{
// If this is an interactive session and there's a pending breakpoint, send that
// information along to the debugger client.
_debugEventHandlerService.TriggerDebuggerStopped(_debugService.CurrentDebuggerStoppedEventArgs);
}
else
{
// If this is an interactive session and there's a pending breakpoint that has
// not been propagated through the debug service, fire the debug service's
// OnDebuggerStop event.
_debugService.OnDebuggerStopAsync(null, _debugContext.LastStopEventArgs);
}
}
return Task.FromResult(new ConfigurationDoneResponse());
}
private async Task LaunchScriptAsync(string scriptToLaunch)
{
// TODO: Theoretically we can make PowerShell respect line breakpoints in untitled
// files, but the previous method was a hack that conflicted with correct passing of
// arguments to the debugged script. We are prioritizing the latter over the former, as
// command breakpoints and `Wait-Debugger` work fine.
string command = ScriptFile.IsUntitledPath(scriptToLaunch)
? string.Concat("{ ", _workspaceService.GetFile(scriptToLaunch).Contents, " }")
: string.Concat('"', scriptToLaunch, '"');
await _executionService.ExecutePSCommandAsync(
PSCommandHelpers.BuildCommandFromArguments(command, _debugStateService.Arguments),
CancellationToken.None,
s_debuggerExecutionOptions).ConfigureAwait(false);
_debugAdapterServer.SendNotification(EventNames.Terminated);
}
}
}