Skip to content

Commit 65ff98d

Browse files
committed
Squash
1 parent d7ce815 commit 65ff98d

File tree

6 files changed

+128
-120
lines changed

6 files changed

+128
-120
lines changed

.vsts-ci/templates/release-general.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ steps:
2121
certificateId: CP-230012 # Authenticode certificate
2222
shouldSign: true # We always want to sign
2323
useMinimatch: true # This enables the use of globbing
24+
shouldSign: true # We always want to sign
2425
pattern: |
2526
# PowerShellEditorServices Script
2627
PowerShellEditorServices/*.{ps1,psd1,psm1,ps1xml}
@@ -40,6 +41,7 @@ steps:
4041
certificateId: CP-231522 # Third-party certificate
4142
shouldSign: true # We always want to sign
4243
useMinimatch: true # This enables the use of globbing
44+
shouldSign: true # We always want to sign
4345
pattern: |
4446
**/MediatR.dll
4547
**/Nerdbank.Streams.dll

src/PowerShellEditorServices.Hosting/Commands/InvokeReadLineForEditorServicesCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Linq;
56
using System.Management.Automation;
67
using System.Management.Automation.Runspaces;
78
using System.Reflection;
@@ -24,7 +25,7 @@ private delegate string ReadLineInvoker(
2425
Type type = Type.GetType("Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2");
2526
MethodInfo method = type?.GetMethod(
2627
"ReadLine",
27-
new[] { typeof(Runspace), typeof(EngineIntrinsics), typeof(CancellationToken) });
28+
new [] { typeof(Runspace), typeof(EngineIntrinsics), typeof(CancellationToken) });
2829

2930
// TODO: Handle method being null here. This shouldn't ever happen.
3031

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Management.Automation.Remoting;
1212
using System.Management.Automation.Runspaces;
1313
using System.Reflection;
14+
using System.Runtime.InteropServices;
1415
using System.Text;
1516
using System.Threading;
1617
using System.Threading.Tasks;
@@ -30,7 +31,7 @@ namespace Microsoft.PowerShell.EditorServices.Services
3031
/// Handles nested PowerShell prompts and also manages execution of
3132
/// commands whether inside or outside of the debugger.
3233
/// </summary>
33-
internal class PowerShellContextService : IHostSupportsInteractiveSession
34+
internal class PowerShellContextService: IHostSupportsInteractiveSession
3435
{
3536
// This is a default that can be overriden at runtime by the user or tests.
3637
private static string s_bundledModulePath = Path.GetFullPath(Path.Combine(
@@ -45,6 +46,10 @@ internal class PowerShellContextService : IHostSupportsInteractiveSession
4546
"Commands",
4647
"PowerShellEditorServices.Commands.psd1"));
4748

49+
private static string s_psReadLineModulePath => Path.GetFullPath(Path.Combine(
50+
s_bundledModulePath,
51+
"PSReadLine"));
52+
4853
private static readonly Action<Runspace, ApartmentState> s_runspaceApartmentStateSetter;
4954
private static readonly PropertyInfo s_writeStreamProperty;
5055
private static readonly object s_errorStreamValue;
@@ -180,17 +185,17 @@ public RunspaceDetails CurrentRunspace
180185
public PowerShellContextService(
181186
ILogger logger,
182187
OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServerFacade languageServer,
183-
bool isPSReadLineEnabled)
188+
HostStartupInfo hostStartupInfo)
184189
{
185190
logger.LogTrace("Instantiating PowerShellContextService and adding event handlers");
186191
_languageServer = languageServer;
187192
this.logger = logger;
188-
this.isPSReadLineEnabled = isPSReadLineEnabled;
193+
this.isPSReadLineEnabled = hostStartupInfo.ConsoleReplEnabled
194+
&& !hostStartupInfo.UsesLegacyReadLine;
189195

190196
RunspaceChanged += PowerShellContext_RunspaceChangedAsync;
191197
ExecutionStatusChanged += PowerShellContext_ExecutionStatusChangedAsync;
192198
}
193-
194199
[SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Checked by Validate call")]
195200
public static PowerShellContextService Create(
196201
ILoggerFactory factory,
@@ -201,24 +206,14 @@ public static PowerShellContextService Create(
201206

202207
Validate.IsNotNull(nameof(hostStartupInfo), hostStartupInfo);
203208

204-
// Respect a user provided bundled module path.
205-
if (Directory.Exists(hostStartupInfo.BundledModulePath))
206-
{
207-
logger.LogTrace($"Using new bundled module path: {hostStartupInfo.BundledModulePath}");
208-
s_bundledModulePath = hostStartupInfo.BundledModulePath;
209-
}
210-
211-
bool shouldUsePSReadLine = hostStartupInfo.ConsoleReplEnabled
212-
&& !hostStartupInfo.UsesLegacyReadLine;
213-
214209
var powerShellContext = new PowerShellContextService(
215210
logger,
216211
languageServer,
217-
shouldUsePSReadLine);
212+
hostStartupInfo);
218213

219214
EditorServicesPSHostUserInterface hostUserInterface =
220215
hostStartupInfo.ConsoleReplEnabled
221-
? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, hostStartupInfo.PSHost, logger)
216+
? (EditorServicesPSHostUserInterface)new TerminalPSHostUserInterface(powerShellContext, hostStartupInfo.PSHost, logger)
222217
: new ProtocolPSHostUserInterface(languageServer, powerShellContext, logger);
223218

224219
EditorServicesPSHost psHost =
@@ -230,26 +225,7 @@ public static PowerShellContextService Create(
230225

231226
logger.LogTrace("Creating initial PowerShell runspace");
232227
Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost, hostStartupInfo.InitialSessionState);
233-
powerShellContext.Initialize(hostStartupInfo.ProfilePaths, initialRunspace, true, hostUserInterface);
234-
powerShellContext.ImportCommandsModuleAsync();
235-
236-
// TODO: This can be moved to the point after the $psEditor object
237-
// gets initialized when that is done earlier than LanguageServer.Initialize
238-
foreach (string module in hostStartupInfo.AdditionalModules)
239-
{
240-
var command =
241-
new PSCommand()
242-
.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
243-
.AddParameter("Name", module);
244-
245-
#pragma warning disable CS4014
246-
// This call queues the loading on the pipeline thread, so no need to await
247-
powerShellContext.ExecuteCommandAsync<PSObject>(
248-
command,
249-
sendOutputToHost: false,
250-
sendErrorToHost: true);
251-
#pragma warning restore CS4014
252-
}
228+
powerShellContext.Initialize(hostStartupInfo, initialRunspace, true, hostUserInterface);
253229

254230
return powerShellContext;
255231
}
@@ -311,7 +287,7 @@ public static Runspace CreateRunspace(PSHost psHost, InitialSessionState initial
311287
/// <param name="ownsInitialRunspace">If true, the PowerShellContext owns this runspace.</param>
312288
/// <param name="consoleHost">An IHostOutput implementation. Optional.</param>
313289
public void Initialize(
314-
ProfilePathInfo profilePaths,
290+
HostStartupInfo hostStartupInfo,
315291
Runspace initialRunspace,
316292
bool ownsInitialRunspace,
317293
IHostOutput consoleHost)
@@ -342,7 +318,12 @@ public void Initialize(
342318
RunspaceContext.Original,
343319
connectionString: null);
344320
this.CurrentRunspace = this.initialRunspace;
345-
321+
// Respect a user provided bundled module path.
322+
if (Directory.Exists(hostStartupInfo.BundledModulePath))
323+
{
324+
logger.LogTrace($"Using new bundled module path: {hostStartupInfo.BundledModulePath}");
325+
s_bundledModulePath = hostStartupInfo.BundledModulePath;
326+
}
346327
// Write out the PowerShell version for tracking purposes
347328
this.logger.LogInformation($"PowerShell Version: {this.LocalPowerShellVersion.Version}, Edition: {this.LocalPowerShellVersion.Edition}");
348329

@@ -365,7 +346,7 @@ public void Initialize(
365346
this.ConfigureRunspaceCapabilities(this.CurrentRunspace);
366347

367348
// Set the $profile variable in the runspace
368-
this.profilePaths = profilePaths;
349+
this.profilePaths = hostStartupInfo.ProfilePaths;
369350
if (profilePaths != null)
370351
{
371352
this.SetProfileVariableInCurrentRunspace(profilePaths);
@@ -399,10 +380,47 @@ public void Initialize(
399380
this.ConsoleReader,
400381
this.versionSpecificOperations);
401382
this.InvocationEventQueue = InvocationEventQueue.Create(this, this.PromptNest);
383+
this.ImportCommandsModuleAsync().GetAwaiter().GetResult();
384+
if (isPSReadLineEnabled)
385+
{
386+
this.ImportPSReadLine2ModuleAsync().GetAwaiter().GetResult();
387+
}
388+
// TODO: This can be moved to the point after the $psEditor object
389+
// gets initialized when that is done earlier than LanguageServer.Initialize
390+
foreach (string module in hostStartupInfo.AdditionalModules)
391+
{
392+
var command =
393+
new PSCommand()
394+
.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
395+
.AddParameter("Name", module);
396+
397+
#pragma warning disable CS4014
398+
// This call queues the loading on the pipeline thread, so no need to await
399+
this.ExecuteCommandAsync<PSObject>(
400+
command,
401+
sendOutputToHost: false,
402+
sendErrorToHost: true).GetAwaiter().GetResult();
403+
#pragma warning restore CS4014
404+
}
405+
406+
407+
// Import the PowerShellEditorServices.Commands module into the runspace.
408+
this.ImportModuleAsync(s_commandsModulePath).GetAwaiter().GetResult();
409+
if (isPSReadLineEnabled)
410+
{
411+
// Imports the PSReadLine2 module into the runspace.
412+
ImportModuleAsync(s_psReadLineModulePath).GetAwaiter().GetResult();
413+
}
414+
// TODO: This can be moved to the point after the $psEditor object
415+
// gets initialized when that is done earlier than LanguageServer.Initialize
416+
foreach (string module in hostStartupInfo.AdditionalModules)
417+
{
418+
this.ImportModuleAsync(module).GetAwaiter().GetResult();
419+
}
402420

403421
if (powerShellVersion.Major >= 5 &&
404422
this.isPSReadLineEnabled &&
405-
PSReadLinePromptContext.TryGetPSReadLineProxy(logger, initialRunspace, s_bundledModulePath, out PSReadLineProxy proxy))
423+
PSReadLinePromptContext.TryGetPSReadLineProxy(logger, out PSReadLineProxy proxy))
406424
{
407425
this.PromptContext = new PSReadLinePromptContext(
408426
this,
@@ -414,24 +432,18 @@ public void Initialize(
414432
{
415433
this.PromptContext = new LegacyReadLineContext(this);
416434
}
417-
418435
// Finally, restore the runspace's execution policy to the user's policy instead of
419436
// Bypass.
420437
this.RestoreExecutionPolicy();
421438
}
422439

423-
/// <summary>
424-
/// Imports the PowerShellEditorServices.Commands module into
425-
/// the runspace. This method will be moved somewhere else soon.
426-
/// </summary>
427-
/// <returns></returns>
428-
public Task ImportCommandsModuleAsync()
440+
public Task ImportModuleAsync(string path)
429441
{
430-
this.logger.LogTrace($"Importing PowershellEditorServices commands from {s_commandsModulePath}");
442+
this.logger.LogTrace($"Importing PowershellEditorServices commands from {path}");
431443

432444
PSCommand importCommand = new PSCommand()
433445
.AddCommand("Import-Module")
434-
.AddArgument(s_commandsModulePath);
446+
.AddArgument(path);
435447

436448
return this.ExecuteCommandAsync<PSObject>(importCommand, sendOutputToHost: false, sendErrorToHost: false);
437449
}
@@ -485,7 +497,7 @@ private void CleanupRunspace(RunspaceDetails runspaceDetails)
485497
/// <returns>A RunspaceHandle instance that gives access to the session's runspace.</returns>
486498
public Task<RunspaceHandle> GetRunspaceHandleAsync()
487499
{
488-
return this.GetRunspaceHandleImplAsync(isReadLine: false, CancellationToken.None);
500+
return this.GetRunspaceHandleImplAsync(CancellationToken.None, isReadLine: false);
489501
}
490502

491503
/// <summary>
@@ -497,7 +509,7 @@ public Task<RunspaceHandle> GetRunspaceHandleAsync()
497509
/// <returns>A RunspaceHandle instance that gives access to the session's runspace.</returns>
498510
public Task<RunspaceHandle> GetRunspaceHandleAsync(CancellationToken cancellationToken)
499511
{
500-
return this.GetRunspaceHandleImplAsync(isReadLine: false, cancellationToken);
512+
return this.GetRunspaceHandleImplAsync(cancellationToken, isReadLine: false);
501513
}
502514

503515
/// <summary>
@@ -994,7 +1006,7 @@ public Task<IEnumerable<object>> ExecuteScriptStringAsync(
9941006
Validate.IsNotNull(nameof(scriptString), scriptString);
9951007

9961008
PSCommand command = null;
997-
if(CurrentRunspace.Runspace.SessionStateProxy.LanguageMode != PSLanguageMode.FullLanguage)
1009+
if (CurrentRunspace.Runspace.SessionStateProxy.LanguageMode != PSLanguageMode.FullLanguage)
9981010
{
9991011
try
10001012
{
@@ -1009,7 +1021,7 @@ public Task<IEnumerable<object>> ExecuteScriptStringAsync(
10091021
}
10101022

10111023
// fall back to old behavior
1012-
if(command == null)
1024+
if (command == null)
10131025
{
10141026
command = new PSCommand().AddScript(scriptString.Trim());
10151027
}
@@ -1415,12 +1427,12 @@ public void Close()
14151427

14161428
private Task<RunspaceHandle> GetRunspaceHandleAsync(bool isReadLine)
14171429
{
1418-
return this.GetRunspaceHandleImplAsync(isReadLine, CancellationToken.None);
1430+
return this.GetRunspaceHandleImplAsync(CancellationToken.None, isReadLine);
14191431
}
14201432

1421-
private Task<RunspaceHandle> GetRunspaceHandleImplAsync(bool isReadLine, CancellationToken cancellationToken)
1433+
private Task<RunspaceHandle> GetRunspaceHandleImplAsync(CancellationToken cancellationToken, bool isReadLine)
14221434
{
1423-
return this.PromptNest.GetRunspaceHandleAsync(isReadLine, cancellationToken);
1435+
return this.PromptNest.GetRunspaceHandleAsync(cancellationToken, isReadLine);
14241436
}
14251437

14261438
private ExecutionTarget GetExecutionTarget(ExecutionOptions options = null)
@@ -1707,7 +1719,7 @@ internal static string QuoteEscapeString(string escapedPath)
17071719
internal static string WildcardEscapePath(string path, bool escapeSpaces = false)
17081720
{
17091721
var sb = new StringBuilder();
1710-
for (int i = 0; i < path.Length; i++)
1722+
for (int i = 0;i < path.Length;i++)
17111723
{
17121724
char curr = path[i];
17131725
switch (curr)
@@ -1760,7 +1772,7 @@ internal static string UnescapeWildcardEscapedPath(string wildcardEscapedPath)
17601772
}
17611773

17621774
var sb = new StringBuilder(wildcardEscapedPath.Length);
1763-
for (int i = 0; i < wildcardEscapedPath.Length; i++)
1775+
for (int i = 0;i < wildcardEscapedPath.Length;i++)
17641776
{
17651777
// If we see a backtick perform a lookahead
17661778
char curr = wildcardEscapedPath[i];
@@ -2360,7 +2372,7 @@ private static IEnumerable<string> GetLoadableProfilePaths(ProfilePathInfo profi
23602372
yield break;
23612373
}
23622374

2363-
foreach (string path in new [] { profilePaths.AllUsersAllHosts, profilePaths.AllUsersCurrentHost, profilePaths.CurrentUserAllHosts, profilePaths.CurrentUserCurrentHost })
2375+
foreach (string path in new[] { profilePaths.AllUsersAllHosts, profilePaths.AllUsersCurrentHost, profilePaths.CurrentUserAllHosts, profilePaths.CurrentUserCurrentHost })
23642376
{
23652377
if (path != null && File.Exists(path))
23662378
{

0 commit comments

Comments
 (0)