Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,14 @@ internal class PSReadLineProxy

private static readonly Type[] s_addToHistoryTypes = { typeof(string) };

private static readonly string _psReadLineModulePath = Path.GetFullPath(
Path.Combine(
Path.GetDirectoryName(typeof(PSReadLineProxy).Assembly.Location),
"..",
"..",
"..",
"PSReadLine"));

private static readonly string ReadLineInitScript = $@"
[System.Diagnostics.DebuggerHidden()]
[System.Diagnostics.DebuggerStepThrough()]
param()
end {{
$module = Get-Module -ListAvailable PSReadLine |
Where-Object {{ $_.Version -ge '2.2.1' }} |
Sort-Object -Descending Version |
Select-Object -First 1
if (-not $module) {{
Import-Module '{_psReadLineModulePath.Replace("'", "''")}'
return [Microsoft.PowerShell.PSConsoleReadLine]
}}

Import-Module -ModuleInfo $module
return [Microsoft.PowerShell.PSConsoleReadLine]
}}";

public static PSReadLineProxy LoadAndCreate(
ILoggerFactory loggerFactory,
string bundledModulePath,
SMA.PowerShell pwsh)
{
Type psConsoleReadLineType = pwsh.AddScript(ReadLineInitScript).InvokeAndClear<Type>().FirstOrDefault();
pwsh.ImportModule(Path.Combine(bundledModulePath, "PSReadLine"));
Type psConsoleReadLineType = pwsh.AddScript("return [Microsoft.PowerShell.PSConsoleReadLine]")
.InvokeAndClear<Type>().FirstOrDefault();

RuntimeHelpers.RunClassConstructor(psConsoleReadLineType.TypeHandle);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Hosting;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Console;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Context;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Runspace;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Management.Automation.Host;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Hosting;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Console;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Context;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Debugging;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Runspace;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;
using Microsoft.PowerShell.EditorServices.Utility;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;

namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host
{
Expand All @@ -29,11 +28,12 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host
internal class PsesInternalHost : PSHost, IHostSupportsInteractiveSession, IRunspaceContext, IInternalPowerShellExecutionService
{
private const string DefaultPrompt = "PSIC> ";
// This is a default that can be overriden at runtime by the user or tests.
private static string s_bundledModulePath = Path.GetFullPath(Path.Combine(
Path.GetDirectoryName(typeof(PsesInternalHost).Assembly.Location), "..", "..", ".."));

private static readonly string s_commandsModulePath = Path.GetFullPath(
Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"../../Commands/PowerShellEditorServices.Commands.psd1"));
private static string s_commandsModulePath => Path.GetFullPath(Path.Combine(
s_bundledModulePath, "PowerShellEditorServices", "Commands", "PowerShellEditorServices.Commands.psd1"));

private readonly ILoggerFactory _loggerFactory;

Expand Down Expand Up @@ -85,6 +85,13 @@ public PsesInternalHost(
_languageServer = languageServer;
_hostInfo = hostInfo;

// Respect a user provided bundled module path.
if (Directory.Exists(hostInfo.BundledModulePath))
{
_logger.LogTrace("Using new bundled module path: {}", hostInfo.BundledModulePath);
s_bundledModulePath = hostInfo.BundledModulePath;
}

_readLineProvider = new ReadLineProvider(loggerFactory);
_taskQueue = new BlockingConcurrentDeque<ISynchronousTask>();
_psFrameStack = new Stack<PowerShellContextFrame>();
Expand Down Expand Up @@ -212,7 +219,7 @@ public async Task<bool> TryStartAsync(HostStartOptions startOptions, Cancellatio
await ExecuteDelegateAsync(
"LoadProfiles",
new PowerShellExecutionOptions { MustRunInForeground = true, ThrowOnError = false },
(pwsh, delegateCancellation) => pwsh.LoadProfiles(_hostInfo.ProfilePaths),
(pwsh, _) => pwsh.LoadProfiles(_hostInfo.ProfilePaths),
cancellationToken).ConfigureAwait(false);

_logger.LogInformation("Profiles loaded");
Expand Down Expand Up @@ -747,7 +754,7 @@ private static PowerShell CreatePowerShellForRunspace(Runspace runspace)

pwsh.ImportModule(s_commandsModulePath);

if (hostStartupInfo.AdditionalModules != null && hostStartupInfo.AdditionalModules.Count > 0)
if (hostStartupInfo.AdditionalModules?.Count > 0)
{
foreach (string module in hostStartupInfo.AdditionalModules)
{
Expand Down Expand Up @@ -931,7 +938,7 @@ private bool TryLoadPSReadLine(PowerShell pwsh, EngineIntrinsics engineIntrinsic
psrlReadLine = null;
try
{
var psrlProxy = PSReadLineProxy.LoadAndCreate(_loggerFactory, pwsh);
var psrlProxy = PSReadLineProxy.LoadAndCreate(_loggerFactory, s_bundledModulePath, pwsh);
psrlReadLine = new PsrlReadLine(psrlProxy, this, engineIntrinsics, ReadKey, OnPowerShellIdle);
return true;
}
Expand Down