internal static bool TryGetPSReadLineProxy( ILogger logger, Runspace runspace, out PSReadLineProxy readLineProxy) { readLineProxy = null; using (var pwsh = PowerShell.Create()) { pwsh.Runspace = runspace; var psReadLineType = pwsh .AddScript(ReadLineInitScript) .Invoke <Type>() .FirstOrDefault(); if (psReadLineType == null) { return(false); } try { readLineProxy = new PSReadLineProxy(psReadLineType, logger); } catch (InvalidOperationException) { // The Type we got back from PowerShell doesn't have the members we expected. // Could be an older version, a custom build, or something a newer version with // breaking changes. return(false); } } return(true); }
internal static bool TryGetPSReadLineProxy( ILogger logger, Runspace runspace, string bundledModulePath, out PSReadLineProxy readLineProxy) { readLineProxy = null; logger.LogTrace("Attempting to load PSReadLine"); using (var pwsh = PowerShell.Create()) { pwsh.Runspace = runspace; pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module") .AddParameter("Name", Path.Combine(bundledModulePath, "PSReadLine")) .Invoke(); if (pwsh.HadErrors) { logger.LogWarning("PSConsoleReadline type not found: {Reason}", pwsh.Streams.Error[0].ToString()); return(false); } var psReadLineType = Type.GetType("Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2"); if (psReadLineType == null) { // NOTE: For some reason `Type.GetType(...)` can fail to find the type, // and in that case, this search through the `AppDomain` for some reason will succeed. // It's slower, but only happens when needed. logger.LogTrace("PSConsoleReadline type not found using Type.GetType(), searching all loaded assemblies..."); psReadLineType = AppDomain.CurrentDomain .GetAssemblies() .FirstOrDefault(asm => asm.GetName().Name.Equals("Microsoft.PowerShell.PSReadLine2")) ?.ExportedTypes ?.FirstOrDefault(type => type.FullName.Equals("Microsoft.PowerShell.PSConsoleReadLine")); if (psReadLineType == null) { logger.LogWarning("PSConsoleReadLine type not found anywhere!"); return(false); } } try { readLineProxy = new PSReadLineProxy(psReadLineType, logger); } catch (InvalidOperationException e) { // The Type we got back from PowerShell doesn't have the members we expected. // Could be an older version, a custom build, or something a newer version with // breaking changes. logger.LogWarning("PSReadLineProxy unable to be initialized: {Reason}", e); return(false); } } return(true); }
internal PSReadLinePromptContext( PowerShellContextService powerShellContext, PromptNest promptNest, InvocationEventQueue invocationEventQueue, PSReadLineProxy readLineProxy) { _promptNest = promptNest; _powerShellContext = powerShellContext; _invocationEventQueue = invocationEventQueue; _consoleReadLine = new ConsoleReadLine(powerShellContext); _readLineProxy = readLineProxy; _readLineProxy.OverrideReadKey( intercept => ConsoleProxy.SafeReadKey( intercept, _readLineCancellationSource.Token)); }
internal static bool TryGetPSReadLineProxy( ILogger logger, Runspace runspace, out PSReadLineProxy readLineProxy) { readLineProxy = null; logger.LogTrace("Attempting to load PSReadLine"); using (var pwsh = PowerShell.Create()) { pwsh.Runspace = runspace; var psReadLineType = pwsh .AddScript(ReadLineInitScript, useLocalScope: true) .Invoke <Type>() .FirstOrDefault(); if (psReadLineType == null) { logger.LogWarning("PSReadLine unable to be loaded: {Reason}", pwsh.HadErrors ? pwsh.Streams.Error[0].ToString() : "<Unknown reason>"); return(false); } try { readLineProxy = new PSReadLineProxy(psReadLineType, logger); } catch (InvalidOperationException e) { // The Type we got back from PowerShell doesn't have the members we expected. // Could be an older version, a custom build, or something a newer version with // breaking changes. logger.LogWarning("PSReadLine unable to be loaded: {Reason}", e); return(false); } } return(true); }