Skip to content

Commit bef1740

Browse files
committed
Get to console repl, remove PSContext dependency
1 parent 95558cc commit bef1740

37 files changed

+870
-409
lines changed

src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private async Task CreateEditorServicesAndRunUntilShutdown()
130130
_logger.Log(PsesLogLevel.Diagnostic, "Creating/running editor services");
131131

132132
bool creatingLanguageServer = _config.LanguageServiceTransport != null;
133-
bool creatingDebugServer = _config.DebugServiceTransport != null;
133+
bool creatingDebugServer = false;// _config.DebugServiceTransport != null;
134134
bool isTempDebugSession = creatingDebugServer && !creatingLanguageServer;
135135

136136
// Set up information required to instantiate servers

src/PowerShellEditorServices/Server/PsesDebugServer.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.PowerShell.EditorServices.Handlers;
1212
using Microsoft.PowerShell.EditorServices.Services;
13+
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
1314
using Microsoft.PowerShell.EditorServices.Utility;
1415
using OmniSharp.Extensions.DebugAdapter.Protocol;
1516
using OmniSharp.Extensions.DebugAdapter.Protocol.Serialization;
@@ -42,7 +43,8 @@ internal class PsesDebugServer : IDisposable
4243
private readonly TaskCompletionSource<bool> _serverStopped;
4344

4445
private DebugAdapterServer _debugAdapterServer;
45-
private PowerShellContextService _powerShellContextService;
46+
47+
private PowerShellExecutionService _executionService;
4648

4749
protected readonly ILoggerFactory _loggerFactory;
4850

@@ -75,9 +77,9 @@ public async Task StartAsync()
7577
{
7678
// We need to let the PowerShell Context Service know that we are in a debug session
7779
// so that it doesn't send the powerShell/startDebugger message.
78-
_powerShellContextService = ServiceProvider.GetService<PowerShellContextService>();
79-
_powerShellContextService.IsDebugServerActive = true;
80+
_executionService = ServiceProvider.GetService<PowerShellExecutionService>();
8081

82+
/*
8183
// Needed to make sure PSReadLine's static properties are initialized in the pipeline thread.
8284
// This is only needed for Temp sessions who only have a debug server.
8385
if (_usePSReadLine && _useTempSession && Interlocked.Exchange(ref s_hasRunPsrlStaticCtor, 1) == 0)
@@ -91,6 +93,7 @@ public async Task StartAsync()
9193
.GetAwaiter()
9294
.GetResult();
9395
}
96+
*/
9497

9598
options
9699
.WithInput(_inputStream)
@@ -136,7 +139,6 @@ public async Task StartAsync()
136139

137140
public void Dispose()
138141
{
139-
_powerShellContextService.IsDebugServerActive = false;
140142
// TODO: If the debugger has stopped, should we clear the breakpoints?
141143
_debugAdapterServer.Dispose();
142144
_inputStream.Dispose();

src/PowerShellEditorServices/Server/PsesLanguageServer.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using System;
45
using System.IO;
6+
using System.Management.Automation;
57
using System.Threading.Tasks;
68
using Microsoft.Extensions.DependencyInjection;
79
using Microsoft.Extensions.Logging;
810
using Microsoft.PowerShell.EditorServices.Handlers;
911
using Microsoft.PowerShell.EditorServices.Hosting;
1012
using Microsoft.PowerShell.EditorServices.Services;
13+
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
14+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
1115
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
1216
using OmniSharp.Extensions.LanguageServer.Server;
1317
using Serilog;
@@ -112,7 +116,10 @@ public async Task StartAsync()
112116
{
113117
Log.Logger.Debug("Initializing OmniSharp Language Server");
114118

115-
var serviceProvider = languageServer.Services;
119+
IServiceProvider serviceProvider = languageServer.Services;
120+
121+
serviceProvider.GetService<PowerShellConsoleService>().StartRepl();
122+
116123
var workspaceService = serviceProvider.GetService<WorkspaceService>();
117124

118125
// Grab the workspace path from the parameters
@@ -130,6 +137,18 @@ public async Task StartAsync()
130137
break;
131138
}
132139
}
140+
141+
// Set the working directory of the PowerShell session to the workspace path
142+
if (workspaceService.WorkspacePath != null
143+
&& Directory.Exists(workspaceService.WorkspacePath))
144+
{
145+
await serviceProvider.GetService<PowerShellExecutionService>()
146+
.ExecutePSCommandAsync(
147+
new PSCommand().AddCommand("Set-Location").AddParameter("-Path", workspaceService.WorkspacePath),
148+
new PowerShellExecutionOptions(),
149+
cancellationToken)
150+
.ConfigureAwait(false);
151+
}
133152
});
134153
}).ConfigureAwait(false);
135154

src/PowerShellEditorServices/Server/PsesServiceCollectionExtensions.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Extensions.Logging;
99
using Microsoft.PowerShell.EditorServices.Hosting;
1010
using Microsoft.PowerShell.EditorServices.Services;
11+
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
1112

1213
namespace Microsoft.PowerShell.EditorServices.Server
1314
{
@@ -20,25 +21,20 @@ public static IServiceCollection AddPsesLanguageServices(
2021
return collection.AddSingleton<WorkspaceService>()
2122
.AddSingleton<SymbolsService>()
2223
.AddSingleton<ConfigurationService>()
23-
.AddSingleton<PowerShellContextService>(
24-
(provider) =>
25-
PowerShellContextService.Create(
26-
provider.GetService<ILoggerFactory>(),
27-
// NOTE: Giving the context service access to the language server this
28-
// early is dangerous because it allows it to start sending
29-
// notifications etc. before it has initialized, potentially resulting
30-
// in deadlocks. We're working on a solution to this.
31-
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServerFacade>(),
32-
hostStartupInfo))
33-
.AddSingleton<TemplateService>() // TODO: What's the difference between this and the TemplateHandler?
24+
.AddSingleton<PowerShellStartupService>(
25+
(provider) => PowerShellStartupService.Create(provider.GetService<ILogger>(), hostStartupInfo))
26+
.AddSingleton<PowerShellExecutionService>(
27+
(provider) => PowerShellExecutionService.CreateAndStart(provider.GetService<ILogger>(), hostStartupInfo, provider.GetService<PowerShellStartupService>()))
28+
.AddSingleton<PowerShellConsoleService>(
29+
(provider) => PowerShellConsoleService.CreateAndStart(provider.GetService<ILogger>(), provider.GetService<PowerShellStartupService>(), provider.GetService<PowerShellExecutionService>()))
30+
.AddSingleton<TemplateService>()
3431
.AddSingleton<EditorOperationsService>()
3532
.AddSingleton<RemoteFileManagerService>()
3633
.AddSingleton<ExtensionService>(
3734
(provider) =>
3835
{
3936
var extensionService = new ExtensionService(
40-
provider.GetService<PowerShellContextService>(),
41-
// NOTE: See above warning.
37+
provider.GetService<PowerShellExecutionService>(),
4238
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServerFacade>());
4339
extensionService.InitializeAsync(
4440
serviceProvider: provider,
@@ -55,7 +51,7 @@ public static IServiceCollection AddPsesDebugServices(
5551
PsesDebugServer psesDebugServer,
5652
bool useTempSession)
5753
{
58-
return collection.AddSingleton(languageServiceProvider.GetService<PowerShellContextService>())
54+
return collection.AddSingleton(languageServiceProvider.GetService<PowerShellExecutionService>())
5955
.AddSingleton(languageServiceProvider.GetService<WorkspaceService>())
6056
.AddSingleton(languageServiceProvider.GetService<RemoteFileManagerService>())
6157
.AddSingleton<PsesDebugServer>(psesDebugServer)

src/PowerShellEditorServices/Services/DebugAdapter/BreakpointService.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Management.Automation;
8+
using System.Threading;
89
using System.Threading.Tasks;
910
using Microsoft.Extensions.Logging;
1011
using Microsoft.PowerShell.EditorServices.Logging;
1112
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter;
13+
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
14+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
15+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
1216

1317
namespace Microsoft.PowerShell.EditorServices.Services
1418
{
1519
internal class BreakpointService
1620
{
1721
private readonly ILogger<BreakpointService> _logger;
18-
private readonly PowerShellContextService _powerShellContextService;
22+
private readonly PowerShellExecutionService _executionService;
23+
private readonly EditorServicesConsolePSHost _editorServicesHost;
1924
private readonly DebugStateService _debugStateService;
2025

2126
// TODO: This needs to be managed per nested session
@@ -27,11 +32,13 @@ internal class BreakpointService
2732

2833
public BreakpointService(
2934
ILoggerFactory factory,
30-
PowerShellContextService powerShellContextService,
35+
PowerShellExecutionService executionService,
36+
EditorServicesConsolePSHost editorServicesHost,
3137
DebugStateService debugStateService)
3238
{
3339
_logger = factory.CreateLogger<BreakpointService>();
34-
_powerShellContextService = powerShellContextService;
40+
_executionService = executionService;
41+
_editorServicesHost = editorServicesHost;
3542
_debugStateService = debugStateService;
3643
}
3744

@@ -40,14 +47,14 @@ public async Task<List<Breakpoint>> GetBreakpointsAsync()
4047
if (BreakpointApiUtils.SupportsBreakpointApis)
4148
{
4249
return BreakpointApiUtils.GetBreakpoints(
43-
_powerShellContextService.CurrentRunspace.Runspace.Debugger,
50+
_editorServicesHost.Runspace.Debugger,
4451
_debugStateService.RunspaceId);
4552
}
4653

4754
// Legacy behavior
4855
PSCommand psCommand = new PSCommand();
4956
psCommand.AddCommand(@"Microsoft.PowerShell.Utility\Get-PSBreakpoint");
50-
IEnumerable<Breakpoint> breakpoints = await _powerShellContextService.ExecuteCommandAsync<Breakpoint>(psCommand).ConfigureAwait(false);
57+
IEnumerable<Breakpoint> breakpoints = await _executionService.ExecutePSCommandAsync<Breakpoint>(psCommand, new PowerShellExecutionOptions(), CancellationToken.None);
5158
return breakpoints.ToList();
5259
}
5360

@@ -59,7 +66,7 @@ public async Task<IEnumerable<BreakpointDetails>> SetBreakpointsAsync(string esc
5966
{
6067
try
6168
{
62-
BreakpointApiUtils.SetBreakpoint(_powerShellContextService.CurrentRunspace.Runspace.Debugger, breakpointDetails, _debugStateService.RunspaceId);
69+
BreakpointApiUtils.SetBreakpoint(_editorServicesHost.Runspace.Debugger, breakpointDetails, _debugStateService.RunspaceId);
6370
}
6471
catch(InvalidOperationException e)
6572
{
@@ -133,7 +140,7 @@ public async Task<IEnumerable<BreakpointDetails>> SetBreakpointsAsync(string esc
133140
if (psCommand != null)
134141
{
135142
IEnumerable<Breakpoint> setBreakpoints =
136-
await _powerShellContextService.ExecuteCommandAsync<Breakpoint>(psCommand).ConfigureAwait(false);
143+
await _executionService.ExecutePSCommandAsync<Breakpoint>(psCommand, new PowerShellExecutionOptions(), CancellationToken.None);
137144
configuredBreakpoints.AddRange(
138145
setBreakpoints.Select((breakpoint) => BreakpointDetails.Create(breakpoint))
139146
);
@@ -150,7 +157,7 @@ public async Task<IEnumerable<CommandBreakpointDetails>> SetCommandBreakpoints(I
150157
{
151158
try
152159
{
153-
BreakpointApiUtils.SetBreakpoint(_powerShellContextService.CurrentRunspace.Runspace.Debugger, commandBreakpointDetails, _debugStateService.RunspaceId);
160+
BreakpointApiUtils.SetBreakpoint(_editorServicesHost.Runspace.Debugger, commandBreakpointDetails, _debugStateService.RunspaceId);
154161
}
155162
catch(InvalidOperationException e)
156163
{
@@ -211,7 +218,7 @@ public async Task<IEnumerable<CommandBreakpointDetails>> SetCommandBreakpoints(I
211218
if (psCommand != null)
212219
{
213220
IEnumerable<Breakpoint> setBreakpoints =
214-
await _powerShellContextService.ExecuteCommandAsync<Breakpoint>(psCommand).ConfigureAwait(false);
221+
await _executionService.ExecutePSCommandAsync<Breakpoint>(psCommand, new PowerShellExecutionOptions(), CancellationToken.None);
215222
configuredBreakpoints.AddRange(
216223
setBreakpoints.Select(CommandBreakpointDetails.Create));
217224
}
@@ -229,13 +236,13 @@ public async Task RemoveAllBreakpointsAsync(string scriptPath = null)
229236
if (BreakpointApiUtils.SupportsBreakpointApis)
230237
{
231238
foreach (Breakpoint breakpoint in BreakpointApiUtils.GetBreakpoints(
232-
_powerShellContextService.CurrentRunspace.Runspace.Debugger,
239+
_editorServicesHost.Runspace.Debugger,
233240
_debugStateService.RunspaceId))
234241
{
235242
if (scriptPath == null || scriptPath == breakpoint.Script)
236243
{
237244
BreakpointApiUtils.RemoveBreakpoint(
238-
_powerShellContextService.CurrentRunspace.Runspace.Debugger,
245+
_editorServicesHost.Runspace.Debugger,
239246
breakpoint,
240247
_debugStateService.RunspaceId);
241248
}
@@ -256,7 +263,7 @@ public async Task RemoveAllBreakpointsAsync(string scriptPath = null)
256263

257264
psCommand.AddCommand(@"Microsoft.PowerShell.Utility\Remove-PSBreakpoint");
258265

259-
await _powerShellContextService.ExecuteCommandAsync<object>(psCommand).ConfigureAwait(false);
266+
await _executionService.ExecutePSCommandAsync<object>(psCommand, new PowerShellExecutionOptions(), CancellationToken.None).ConfigureAwait(false);
260267
}
261268
catch (Exception e)
262269
{
@@ -271,7 +278,7 @@ public async Task RemoveBreakpointsAsync(IEnumerable<Breakpoint> breakpoints)
271278
foreach (Breakpoint breakpoint in breakpoints)
272279
{
273280
BreakpointApiUtils.RemoveBreakpoint(
274-
_powerShellContextService.CurrentRunspace.Runspace.Debugger,
281+
_editorServicesHost.Runspace.Debugger,
275282
breakpoint,
276283
_debugStateService.RunspaceId);
277284

@@ -302,7 +309,7 @@ public async Task RemoveBreakpointsAsync(IEnumerable<Breakpoint> breakpoints)
302309
psCommand.AddCommand(@"Microsoft.PowerShell.Utility\Remove-PSBreakpoint");
303310
psCommand.AddParameter("Id", breakpoints.Select(b => b.Id).ToArray());
304311

305-
await _powerShellContextService.ExecuteCommandAsync<object>(psCommand).ConfigureAwait(false);
312+
await _executionService.ExecutePSCommandAsync<object>(psCommand, new PowerShellExecutionOptions(), CancellationToken.None);
306313
}
307314
}
308315

src/PowerShellEditorServices/Services/DebugAdapter/DebugEventHandlerService.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Management.Automation;
55
using Microsoft.Extensions.Logging;
66
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter;
7+
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
78
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
89
using Microsoft.PowerShell.EditorServices.Utility;
910
using OmniSharp.Extensions.DebugAdapter.Protocol.Events;
@@ -14,39 +15,39 @@ namespace Microsoft.PowerShell.EditorServices.Services
1415
internal class DebugEventHandlerService
1516
{
1617
private readonly ILogger<DebugEventHandlerService> _logger;
17-
private readonly PowerShellContextService _powerShellContextService;
18+
private readonly PowerShellExecutionService _executionService;
1819
private readonly DebugService _debugService;
1920
private readonly DebugStateService _debugStateService;
2021
private readonly IDebugAdapterServerFacade _debugAdapterServer;
2122

2223
public DebugEventHandlerService(
2324
ILoggerFactory factory,
24-
PowerShellContextService powerShellContextService,
25+
PowerShellExecutionService executionService,
2526
DebugService debugService,
2627
DebugStateService debugStateService,
2728
IDebugAdapterServerFacade debugAdapterServer)
2829
{
2930
_logger = factory.CreateLogger<DebugEventHandlerService>();
30-
_powerShellContextService = powerShellContextService;
31+
_executionService = executionService;
3132
_debugService = debugService;
3233
_debugStateService = debugStateService;
3334
_debugAdapterServer = debugAdapterServer;
3435
}
3536

3637
internal void RegisterEventHandlers()
3738
{
38-
_powerShellContextService.RunspaceChanged += PowerShellContext_RunspaceChanged;
39+
//_powerShellContextService.RunspaceChanged += PowerShellContext_RunspaceChanged;
3940
_debugService.BreakpointUpdated += DebugService_BreakpointUpdated;
4041
_debugService.DebuggerStopped += DebugService_DebuggerStopped;
41-
_powerShellContextService.DebuggerResumed += PowerShellContext_DebuggerResumed;
42+
//_powerShellContextService.DebuggerResumed += PowerShellContext_DebuggerResumed;
4243
}
4344

4445
internal void UnregisterEventHandlers()
4546
{
46-
_powerShellContextService.RunspaceChanged -= PowerShellContext_RunspaceChanged;
47+
//_powerShellContextService.RunspaceChanged -= PowerShellContext_RunspaceChanged;
4748
_debugService.BreakpointUpdated -= DebugService_BreakpointUpdated;
4849
_debugService.DebuggerStopped -= DebugService_DebuggerStopped;
49-
_powerShellContextService.DebuggerResumed -= PowerShellContext_DebuggerResumed;
50+
//_powerShellContextService.DebuggerResumed -= PowerShellContext_DebuggerResumed;
5051
}
5152

5253
#region Public methods
@@ -98,8 +99,8 @@ private void PowerShellContext_RunspaceChanged(object sender, RunspaceChangedEve
9899
_debugStateService.ServerStarted.SetResult(true);
99100
}
100101
else if (
101-
e.ChangeAction == RunspaceChangeAction.Exit &&
102-
_powerShellContextService.IsDebuggerStopped)
102+
e.ChangeAction == RunspaceChangeAction.Exit && false)
103+
// _powerShellContextService.IsDebuggerStopped)
103104
{
104105
// Exited the session while the debugger is stopped,
105106
// send a ContinuedEvent so that the client changes the

0 commit comments

Comments
 (0)