Host code to enable PSReadLine, and remove checking of .NET Framework version if CORECLR

This commit is contained in:
George Fleming 2016-04-13 11:42:59 -07:00
parent f38a11fc02
commit eb9daf0238
2 changed files with 71 additions and 17 deletions

View file

@ -9,6 +9,7 @@ namespace Microsoft.PowerShell.CoreConsoleHost
using System.Text; using System.Text;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Linq;
using PowerShell = System.Management.Automation.PowerShell; using PowerShell = System.Management.Automation.PowerShell;
public static class Program public static class Program
@ -562,27 +563,78 @@ OPTIONS
this.myHost.UI.Write(prompt); this.myHost.UI.Write(prompt);
ConsoleReadLine.ReadResult result = consoleReadLine.Read(false, initialCommand); string input;
if (TryInvokeUserDefinedReadLine(out input, true))
switch(result.state)
{ {
case ConsoleReadLine.ReadResult.State.Abort: this.Execute(input);
incompleteLine = false; }
partialLine = string.Empty; else
initialCommand = String.Empty; {
break; ConsoleReadLine.ReadResult result = consoleReadLine.Read(false, initialCommand);
case ConsoleReadLine.ReadResult.State.Redraw:
initialCommand = result.command; switch(result.state)
break; {
case ConsoleReadLine.ReadResult.State.Complete: case ConsoleReadLine.ReadResult.State.Abort:
default: incompleteLine = false;
this.Execute(result.command); partialLine = string.Empty;
initialCommand = String.Empty; initialCommand = String.Empty;
break; break;
case ConsoleReadLine.ReadResult.State.Redraw:
initialCommand = result.command;
break;
case ConsoleReadLine.ReadResult.State.Complete:
default:
this.Execute(result.command);
initialCommand = String.Empty;
break;
}
} }
} }
} }
/// <summary>
/// Helper function to test if PSReadLine or another alternate ReadLine has been loaded
/// </summary>
const string CustomReadlineCommand = "PSConsoleHostReadLine";
private bool TryInvokeUserDefinedReadLine(out string input, bool useUserDefinedCustomReadLine)
{
if (useUserDefinedCustomReadLine)
{
var runspace = this.myHost.Runspace;
if (runspace != null
&& runspace.ExecutionContext.EngineIntrinsics.InvokeCommand.GetCommands(CustomReadlineCommand, CommandTypes.Function | CommandTypes.Cmdlet, nameIsPattern: false).Any())
{
try
{
PowerShell ps;
if ((runspace.ExecutionContext.EngineHostInterface.NestedPromptCount > 0) && (Runspace.DefaultRunspace != null))
{
ps = PowerShell.Create(RunspaceMode.CurrentRunspace);
}
else
{
ps = PowerShell.Create();
ps.Runspace = runspace;
}
var result = ps.AddCommand(CustomReadlineCommand).Invoke();
if (result.Count == 1)
{
input = PSObject.Base(result[0]) as string;
return true;
}
}
catch (Exception e)
{
CommandProcessorBase.CheckForSevereException(e);
}
}
}
input = null;
return false;
}
/// <summary> /// <summary>
/// Method to handle the Debugger DebuggerStop event. /// Method to handle the Debugger DebuggerStop event.
/// </summary> /// </summary>

View file

@ -2010,7 +2010,8 @@ namespace Microsoft.PowerShell.Commands
} }
// Test the required .NET Framework version // Test the required .NET Framework version
Version requestedDotNetFrameworkVersion; Version requestedDotNetFrameworkVersion = new Version (0, 0, 0, 0);
#if !CORECLR
if ( if (
!GetScalarFromData(data, moduleManifestPath, "DotNetFrameworkVersion", manifestProcessingFlags, !GetScalarFromData(data, moduleManifestPath, "DotNetFrameworkVersion", manifestProcessingFlags,
out requestedDotNetFrameworkVersion)) out requestedDotNetFrameworkVersion))
@ -2044,6 +2045,7 @@ namespace Microsoft.PowerShell.Commands
WriteVerbose(cannotDetectNetFrameworkVersionMessage); WriteVerbose(cannotDetectNetFrameworkVersionMessage);
} }
} }
#endif
// HelpInfo URI // HelpInfo URI
string helpInfoUri = null; string helpInfoUri = null;