12
12
namespace Microsoft . PowerShell . EditorServices . Services . PowerShell . Execution
13
13
{
14
14
[ Flags ]
15
- internal enum PromptFrameType
15
+ internal enum PowerShellFrameType
16
16
{
17
17
Normal = 0 ,
18
18
NestedPrompt = 1 ,
19
19
Debug = 2 ,
20
20
Remote = 4 ,
21
+ NonInteractive = 8 ,
21
22
}
22
23
23
24
internal class PowerShellPushedArgs
24
25
{
25
- public PowerShellPushedArgs ( PromptFrameType frameType )
26
+ public PowerShellPushedArgs ( PowerShellFrameType frameType )
26
27
{
27
28
FrameType = frameType ;
28
29
}
29
30
30
- public PromptFrameType FrameType { get ; }
31
+ public PowerShellFrameType FrameType { get ; }
31
32
}
32
33
33
34
internal class PowerShellPoppedArgs
34
35
{
35
- public PowerShellPoppedArgs ( PromptFrameType frameType )
36
+ public PowerShellPoppedArgs ( PowerShellFrameType frameType )
36
37
{
37
38
FrameType = frameType ;
38
39
}
39
40
40
- public PromptFrameType FrameType { get ; }
41
+ public PowerShellFrameType FrameType { get ; }
41
42
}
42
43
43
44
internal class DebuggerResumingArgs
@@ -54,7 +55,7 @@ internal class PromptCancellationRequestedArgs
54
55
{
55
56
}
56
57
57
- internal class NestedPromptExitingArgs
58
+ internal class FrameExitingArgs
58
59
{
59
60
}
60
61
@@ -108,48 +109,59 @@ public CancellationTokenSource CurrentCancellationSource
108
109
109
110
public event Action < object , PowerShellPoppedArgs > PowerShellPopped ;
110
111
111
- public event Action < object , NestedPromptExitingArgs > NestedPromptExiting ;
112
+ public event Action < object , FrameExitingArgs > FrameExiting ;
112
113
113
114
public event Action < object , DebuggerStopEventArgs > DebuggerStopped ;
114
115
115
116
public event Action < object , DebuggerResumingArgs > DebuggerResumed ;
116
117
117
118
public event Action < object , BreakpointUpdatedEventArgs > BreakpointUpdated ;
118
119
119
- public void BeginExiting ( )
120
+ public void SetShouldExit ( )
120
121
{
122
+ if ( _frameStack . Count <= 1 )
123
+ {
124
+ return ;
125
+ }
126
+
121
127
IsExiting = true ;
122
- NestedPromptExiting ? . Invoke ( this , new NestedPromptExitingArgs ( ) ) ;
128
+ FrameExiting ? . Invoke ( this , new FrameExitingArgs ( ) ) ;
123
129
}
124
130
125
131
public void ProcessDebuggerResult ( DebuggerCommandResults result )
126
132
{
127
133
if ( result . ResumeAction != null )
128
134
{
129
135
DebuggerResumed ? . Invoke ( this , new DebuggerResumingArgs ( result . ResumeAction ) ) ;
136
+ FrameExiting ? . Invoke ( this , new FrameExitingArgs ( ) ) ;
130
137
}
131
138
}
132
139
140
+ public void PushNonInteractivePowerShell ( )
141
+ {
142
+ PushNestedPowerShell ( PowerShellFrameType . NonInteractive ) ;
143
+ }
144
+
133
145
public void PushNestedPowerShell ( )
134
146
{
135
- PushNestedPowerShell ( PromptFrameType . Normal ) ;
147
+ PushNestedPowerShell ( PowerShellFrameType . Normal ) ;
136
148
}
137
149
138
150
public void PushDebugPowerShell ( )
139
151
{
140
- PushNestedPowerShell ( PromptFrameType . Debug ) ;
152
+ PushNestedPowerShell ( PowerShellFrameType . Debug ) ;
141
153
}
142
154
143
155
public void PushPowerShell ( Runspace runspace )
144
156
{
145
157
var pwsh = SMA . PowerShell . Create ( ) ;
146
158
pwsh . Runspace = runspace ;
147
159
148
- PromptFrameType frameType = PromptFrameType . Normal ;
160
+ PowerShellFrameType frameType = PowerShellFrameType . Normal ;
149
161
150
162
if ( runspace . RunspaceIsRemote )
151
163
{
152
- frameType |= PromptFrameType . Remote ;
164
+ frameType |= PowerShellFrameType . Remote ;
153
165
}
154
166
155
167
PushFrame ( new ContextFrame ( pwsh , frameType , new CancellationTokenSource ( ) ) ) ;
@@ -184,19 +196,28 @@ private void PushInitialPowerShell(Runspace runspace)
184
196
var pwsh = SMA . PowerShell . Create ( ) ;
185
197
pwsh . Runspace = runspace ;
186
198
187
- PushFrame ( new ContextFrame ( pwsh , PromptFrameType . Normal , new CancellationTokenSource ( ) ) ) ;
199
+ PushFrame ( new ContextFrame ( pwsh , PowerShellFrameType . Normal , new CancellationTokenSource ( ) ) ) ;
188
200
}
189
201
190
- private void PushNestedPowerShell ( PromptFrameType frameType )
202
+ private void PushNestedPowerShell ( PowerShellFrameType frameType )
191
203
{
192
204
SMA . PowerShell pwsh = CreateNestedPowerShell ( ) ;
193
- PromptFrameType newFrameType = _frameStack . Peek ( ) . FrameType | PromptFrameType . NestedPrompt | frameType ;
205
+ PowerShellFrameType newFrameType = _frameStack . Peek ( ) . FrameType | PowerShellFrameType . NestedPrompt | frameType ;
194
206
PushFrame ( new ContextFrame ( pwsh , newFrameType , new CancellationTokenSource ( ) ) ) ;
195
207
}
196
208
197
209
private SMA . PowerShell CreateNestedPowerShell ( )
198
210
{
211
+ ContextFrame currentFrame = _frameStack . Peek ( ) ;
212
+ if ( ( currentFrame . FrameType & PowerShellFrameType . Remote ) != 0 )
213
+ {
214
+ var remotePwsh = SMA . PowerShell . Create ( ) ;
215
+ remotePwsh . Runspace = currentFrame . PowerShell . Runspace ;
216
+ return remotePwsh ;
217
+ }
218
+
199
219
// PowerShell.CreateNestedPowerShell() sets IsNested but not IsChild
220
+ // This means it throws due to the parent pipeline not running...
200
221
// So we must use the RunspaceMode.CurrentRunspace option on PowerShell.Create() instead
201
222
var pwsh = SMA . PowerShell . Create ( RunspaceMode . CurrentRunspace ) ;
202
223
pwsh . Runspace . ThreadOptions = PSThreadOptions . UseCurrentThread ;
@@ -205,9 +226,12 @@ private SMA.PowerShell CreateNestedPowerShell()
205
226
206
227
private void PushFrame ( ContextFrame frame )
207
228
{
229
+ if ( _frameStack . Count > 0 )
230
+ {
231
+ RemoveRunspaceEventHandlers ( CurrentPowerShell . Runspace ) ;
232
+ }
233
+ AddRunspaceEventHandlers ( frame . PowerShell . Runspace ) ;
208
234
_frameStack . Push ( frame ) ;
209
- frame . PowerShell . Runspace . Debugger . DebuggerStop += OnDebuggerStopped ;
210
- frame . PowerShell . Runspace . Debugger . BreakpointUpdated += OnBreakpointUpdated ;
211
235
PowerShellPushed ? . Invoke ( this , new PowerShellPushedArgs ( frame . FrameType ) ) ;
212
236
}
213
237
@@ -217,8 +241,11 @@ private void PopFrame()
217
241
ContextFrame frame = _frameStack . Pop ( ) ;
218
242
try
219
243
{
220
- frame . PowerShell . Runspace . Debugger . DebuggerStop -= OnDebuggerStopped ;
221
- frame . PowerShell . Runspace . Debugger . BreakpointUpdated -= OnBreakpointUpdated ;
244
+ RemoveRunspaceEventHandlers ( frame . PowerShell . Runspace ) ;
245
+ if ( _frameStack . Count > 0 )
246
+ {
247
+ AddRunspaceEventHandlers ( CurrentPowerShell . Runspace ) ;
248
+ }
222
249
PowerShellPopped ? . Invoke ( this , new PowerShellPoppedArgs ( frame . FrameType ) ) ;
223
250
}
224
251
finally
@@ -227,6 +254,18 @@ private void PopFrame()
227
254
}
228
255
}
229
256
257
+ private void AddRunspaceEventHandlers ( Runspace runspace )
258
+ {
259
+ runspace . Debugger . DebuggerStop += OnDebuggerStopped ;
260
+ runspace . Debugger . BreakpointUpdated += OnBreakpointUpdated ;
261
+ }
262
+
263
+ private void RemoveRunspaceEventHandlers ( Runspace runspace )
264
+ {
265
+ runspace . Debugger . DebuggerStop -= OnDebuggerStopped ;
266
+ runspace . Debugger . BreakpointUpdated -= OnBreakpointUpdated ;
267
+ }
268
+
230
269
private void OnDebuggerStopped ( object sender , DebuggerStopEventArgs args )
231
270
{
232
271
DebuggerStopped ? . Invoke ( this , args ) ;
@@ -241,7 +280,7 @@ private class ContextFrame : IDisposable
241
280
{
242
281
private bool disposedValue ;
243
282
244
- public ContextFrame ( SMA . PowerShell powerShell , PromptFrameType frameType , CancellationTokenSource cancellationTokenSource )
283
+ public ContextFrame ( SMA . PowerShell powerShell , PowerShellFrameType frameType , CancellationTokenSource cancellationTokenSource )
245
284
{
246
285
PowerShell = powerShell ;
247
286
FrameType = frameType ;
@@ -250,7 +289,7 @@ public ContextFrame(SMA.PowerShell powerShell, PromptFrameType frameType, Cancel
250
289
251
290
public SMA . PowerShell PowerShell { get ; }
252
291
253
- public PromptFrameType FrameType { get ; }
292
+ public PowerShellFrameType FrameType { get ; }
254
293
255
294
public CancellationTokenSource CancellationTokenSource { get ; }
256
295
0 commit comments