1
1
using Microsoft . Extensions . Logging ;
2
- using Microsoft . PowerShell . EditorServices . Services . PowerShell . Console ;
3
2
using Microsoft . PowerShell . EditorServices . Services . PowerShell . Execution ;
4
3
using Microsoft . PowerShell . EditorServices . Services . PowerShell . Host ;
5
4
using Microsoft . PowerShell . EditorServices . Services . PowerShell . Utility ;
6
- using Microsoft . PowerShell . EditorServices . Utility ;
5
+ using PowerShellEditorServices . Services . PowerShell . Utility ;
7
6
using System ;
8
7
using System . Collections . Concurrent ;
9
8
using System . Collections . Generic ;
10
9
using System . Linq ;
11
10
using System . Management . Automation ;
12
- using System . Management . Automation . Runspaces ;
13
- using System . Reflection ;
14
11
using System . Text ;
15
12
using System . Threading ;
16
13
using System . Threading . Tasks ;
17
14
18
- namespace Microsoft . PowerShell . EditorServices . Services . PowerShell
15
+ namespace Microsoft . PowerShell . EditorServices . Services . PowerShell . Console
19
16
{
20
- internal class PowerShellConsoleService : IDisposable
17
+ internal class ConsoleReplRunner : IDisposable
21
18
{
22
- public static PowerShellConsoleService CreateAndStart (
23
- ILoggerFactory loggerFactory ,
24
- PowerShellExecutionService executionService )
25
- {
26
- return new PowerShellConsoleService (
27
- loggerFactory ,
28
- executionService ,
29
- executionService . EditorServicesHost ,
30
- executionService . ReadLine ,
31
- executionService . PSReadLineProxy ) ;
32
- }
33
-
34
19
private readonly ILogger _logger ;
35
20
36
21
private readonly PowerShellExecutionService _executionService ;
37
22
38
- private readonly EditorServicesConsolePSHost _editorServicesHost ;
39
-
40
- private readonly ConsoleReadLine _readLine ;
41
-
42
- private readonly PSReadLineProxy _psrlProxy ;
43
-
44
23
private readonly ConcurrentStack < ReplTask > _replLoopTaskStack ;
45
24
46
25
// This is required because PSRL will keep prompting for keys as we push a new REPL task
@@ -52,35 +31,23 @@ public static PowerShellConsoleService CreateAndStart(
52
31
53
32
private bool _exiting ;
54
33
55
- private bool _debugging ;
56
-
57
- private PowerShellConsoleService (
34
+ public ConsoleReplRunner (
58
35
ILoggerFactory loggerFactory ,
59
- PowerShellExecutionService executionService ,
60
- EditorServicesConsolePSHost editorServicesHost ,
61
- ConsoleReadLine readLine ,
62
- PSReadLineProxy psrlProxy )
36
+ PowerShellExecutionService executionService )
63
37
{
64
- _logger = loggerFactory . CreateLogger < PowerShellConsoleService > ( ) ;
38
+ _logger = loggerFactory . CreateLogger < ConsoleReplRunner > ( ) ;
65
39
_replLoopTaskStack = new ConcurrentStack < ReplTask > ( ) ;
66
40
_commandCancellationStack = new ConcurrentStack < CommandCancellation > ( ) ;
67
41
_executionService = executionService ;
68
- _editorServicesHost = editorServicesHost ;
69
- _readLine = readLine ;
70
- _psrlProxy = psrlProxy ;
71
42
_exiting = false ;
72
- _debugging = false ;
73
43
}
74
44
75
45
public void StartRepl ( )
76
46
{
77
47
System . Console . CancelKeyPress += OnCancelKeyPress ;
78
48
System . Console . InputEncoding = Encoding . UTF8 ;
79
49
System . Console . OutputEncoding = Encoding . UTF8 ;
80
- _psrlProxy . OverrideReadKey ( ReadKey ) ;
81
- _executionService . PowerShellPushed += OnPowerShellPushed ;
82
- _executionService . PromptCancellationRequested += OnPromptCancellationRequested ;
83
- _executionService . FrameExiting += OnFrameExiting ;
50
+ _executionService . PSReadLineProxy . OverrideReadKey ( ReadKey ) ;
84
51
PushNewReplTask ( ) ;
85
52
}
86
53
@@ -92,9 +59,6 @@ public void Dispose()
92
59
}
93
60
94
61
System . Console . CancelKeyPress -= OnCancelKeyPress ;
95
- _executionService . PowerShellPushed -= OnPowerShellPushed ;
96
- _executionService . PromptCancellationRequested -= OnPromptCancellationRequested ;
97
- _executionService . FrameExiting -= OnFrameExiting ;
98
62
}
99
63
100
64
public void CancelCurrentPrompt ( )
@@ -149,7 +113,7 @@ private async Task RunReplLoopAsync()
149
113
if ( currentCommandCancellation . CancellationSource . IsCancellationRequested
150
114
|| LastKeyWasCtrlC ( ) )
151
115
{
152
- _editorServicesHost . UI . WriteLine ( ) ;
116
+ _executionService . EditorServicesHost . UI . WriteLine ( ) ;
153
117
}
154
118
continue ;
155
119
}
@@ -190,9 +154,9 @@ private async Task<string> GetPromptStringAsync(CancellationToken cancellationTo
190
154
{
191
155
string prompt = ( await GetPromptOutputAsync ( cancellationToken ) . ConfigureAwait ( false ) ) . FirstOrDefault ( ) ?? "PS> " ;
192
156
193
- if ( _editorServicesHost . Runspace . RunspaceIsRemote )
157
+ if ( _executionService . EditorServicesHost . Runspace . RunspaceIsRemote )
194
158
{
195
- prompt = _editorServicesHost . Runspace . GetRemotePrompt ( prompt ) ;
159
+ prompt = _executionService . EditorServicesHost . Runspace . GetRemotePrompt ( prompt ) ;
196
160
}
197
161
198
162
return prompt ;
@@ -210,12 +174,12 @@ private Task<IReadOnlyList<string>> GetPromptOutputAsync(CancellationToken cance
210
174
211
175
private void WritePrompt ( string promptString )
212
176
{
213
- _editorServicesHost . UI . Write ( promptString ) ;
177
+ _executionService . EditorServicesHost . UI . Write ( promptString ) ;
214
178
}
215
179
216
180
private Task < string > InvokeReadLineAsync ( CancellationToken cancellationToken )
217
181
{
218
- return _readLine . ReadCommandLineAsync ( cancellationToken ) ;
182
+ return _executionService . ReadLine . ReadCommandLineAsync ( cancellationToken ) ;
219
183
}
220
184
221
185
private Task InvokeInputAsync ( string input , CancellationToken cancellationToken )
@@ -230,30 +194,17 @@ private Task InvokeInputAsync(string input, CancellationToken cancellationToken)
230
194
return _executionService . ExecutePSCommandAsync ( command , executionOptions , cancellationToken ) ;
231
195
}
232
196
233
- private void OnCancelKeyPress ( object sender , ConsoleCancelEventArgs args )
234
- {
235
- CancelCurrentPrompt ( ) ;
236
- }
237
-
238
- private void OnPowerShellPushed ( object sender , PowerShellPushedArgs args )
197
+ public void SetReplPop ( )
239
198
{
240
- if ( ( args . FrameType & PowerShellFrameType . NonInteractive ) == 0 )
241
- {
242
- PushNewReplTask ( ) ;
243
- }
199
+ _exiting = true ;
200
+ StopCurrentRepl ( ) ;
244
201
}
245
202
246
- private void OnPromptCancellationRequested ( object sender , PromptCancellationRequestedArgs args )
203
+ private void OnCancelKeyPress ( object sender , ConsoleCancelEventArgs args )
247
204
{
248
205
CancelCurrentPrompt ( ) ;
249
206
}
250
207
251
- private void OnFrameExiting ( object sender , FrameExitingArgs args )
252
- {
253
- _exiting = true ;
254
- StopCurrentRepl ( ) ;
255
- }
256
-
257
208
private void OnReplCanceled ( )
258
209
{
259
210
// Ordinarily, when the REPL is canceled
@@ -293,12 +244,11 @@ private bool LastKeyWasCtrlC()
293
244
&& ( _lastKey . Value . Modifiers & ConsoleModifiers . Alt ) == 0 ;
294
245
}
295
246
296
- private void PushNewReplTask ( )
247
+ public void PushNewReplTask ( )
297
248
{
298
249
ReplTask . PushAndStart ( _replLoopTaskStack , RunReplLoopAsync , OnReplCanceled ) ;
299
250
}
300
251
301
-
302
252
private class ReplTask
303
253
{
304
254
public static void PushAndStart (
0 commit comments