From 0c62b9a12d5a383a535148e612fe79f0ee35d31a Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 12:44:13 -0700 Subject: [PATCH 01/56] Always build ConsoleHost as top project --- PowerShellGitHubDev.psm1 | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/PowerShellGitHubDev.psm1 b/PowerShellGitHubDev.psm1 index a8b00c5a0..b3af37ea9 100644 --- a/PowerShellGitHubDev.psm1 +++ b/PowerShellGitHubDev.psm1 @@ -221,12 +221,7 @@ function New-PSOptions { [switch]$FullCLR ) - $Top = if ($FullCLR) { - "$PSScriptRoot\src\Microsoft.PowerShell.ConsoleHost" - } else { - "$PSScriptRoot/src/Microsoft.PowerShell.CoreConsoleHost" - } - Write-Verbose "Top project directory is $Top" + $Top = "$PSScriptRoot/src/Microsoft.PowerShell.ConsoleHost" if (-not $Configuration) { $Configuration = if ($IsLinux -or $IsOSX) { From ad9c4084d8aa5b90d50408b0f37950217e0543da Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 12:44:43 -0700 Subject: [PATCH 02/56] Make ConsoleHost emit powershell executable --- .../project.json | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/project.json b/src/Microsoft.PowerShell.ConsoleHost/project.json index 76a1494a5..fa752e34b 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/project.json +++ b/src/Microsoft.PowerShell.ConsoleHost/project.json @@ -1,12 +1,13 @@ { - "name": "Microsoft.PowerShell.ConsoleHost", + "name": "powershell", "version": "1.0.0-*", - "description": "Full Windows PowerShell Host (requires native host)", + "description": "PowerShell Host", "authors": [ "sevoroby", "andschwa" ], "compilationOptions": { "warningsAsErrors": true, - "allowUnsafe": true + "allowUnsafe": true, + "emitEntryPoint": true }, "dependencies": { @@ -16,15 +17,35 @@ "content": [ "Modules", - "../Modules", - "powershell.exe" + "../Modules" ], "frameworks": { + "netcoreapp1.0": { + "imports": [ "dnxcore50", "portable-net45+win8" ], + "compilationOptions": { + "define": [ "CORECLR", "OPEN" ] + }, + "dependencies": { + "Microsoft.NETCore.App": "1.0.0-rc2-3002668", + "Newtonsoft.Json": "8.0.2", + "System.Xml.XDocument": "4.0.11-rc2-24103", + "System.IO.MemoryMappedFiles": "4.0.0-rc2-24103" + } + }, "net451": { } }, + "runtimes": { + "ubuntu.14.04-x64": { }, + "centos.7.1-x64": { }, + "win7-x64": { }, + "win81-x64": { }, + "win10-x64": { }, + "osx.10.11-x64": { } + }, + "compile": [ "../windows-build/gen/Microsoft.PowerShell.ConsoleHost/*.cs" ] From 2d2d58cb230aadf922224450b615ed50a340b38c Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 16:47:18 -0700 Subject: [PATCH 03/56] Make ConsoleHost/AssemblyInfo portable --- src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs b/src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs index dde5dcdf4..8dea28787 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs @@ -2,12 +2,15 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Resources; using System.Runtime.CompilerServices; +#if !CORECLR using System.Runtime.ConstrainedExecution; using System.Security.Permissions; +#endif [assembly:AssemblyCulture("")] [assembly:NeutralResourcesLanguage("en-US")] +#if !CORECLR [assembly:AssemblyConfiguration("")] [assembly:AssemblyInformationalVersionAttribute (@"10.0.10011.16384")] [assembly:ReliabilityContractAttribute(Consistency.MayCorruptAppDomain, Cer.MayFail)] @@ -15,14 +18,15 @@ using System.Security.Permissions; [assembly:AssemblyDescription("Microsoft Windows PowerShell Console Host")] [assembly:System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.5")] +[assembly:System.Reflection.AssemblyFileVersion("10.0.10011.16384")] +[assembly:AssemblyKeyFileAttribute(@"..\signing\visualstudiopublic.snk")] +[assembly:System.Reflection.AssemblyDelaySign(true)] +#endif [assembly:System.Runtime.InteropServices.ComVisible(false)] [assembly:System.Reflection.AssemblyVersion("3.0.0.0")] [assembly:System.Reflection.AssemblyProduct("Microsoft (R) Windows (R) Operating System")] [assembly:System.Reflection.AssemblyCopyright("Copyright (c) Microsoft Corporation. All rights reserved.")] [assembly:System.Reflection.AssemblyCompany("Microsoft Corporation")] -[assembly:System.Reflection.AssemblyFileVersion("10.0.10011.16384")] -[assembly:AssemblyKeyFileAttribute(@"..\signing\visualstudiopublic.snk")] -[assembly:System.Reflection.AssemblyDelaySign(true)] internal static class AssemblyStrings { From 6f425d0ffd9e8470ac132ccbdedcb88af482171b Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 12:47:14 -0700 Subject: [PATCH 04/56] Make ConsoleHost deploy profiles and native libraries --- src/Microsoft.PowerShell.ConsoleHost/project.json | 5 ++++- src/libpsl-native/CMakeLists.txt | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/project.json b/src/Microsoft.PowerShell.ConsoleHost/project.json index fa752e34b..f3065438b 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/project.json +++ b/src/Microsoft.PowerShell.ConsoleHost/project.json @@ -17,7 +17,10 @@ "content": [ "Modules", - "../Modules" + "../Modules", + "*_profile.ps1", + "*.so", + "*.dylib" ], "frameworks": { diff --git a/src/libpsl-native/CMakeLists.txt b/src/libpsl-native/CMakeLists.txt index c1e6c19ef..b4665909b 100644 --- a/src/libpsl-native/CMakeLists.txt +++ b/src/libpsl-native/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8.4) project(PSL-NATIVE) add_compile_options(-std=c++11 -Wall -Werror) -set(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/../Microsoft.PowerShell.CoreConsoleHost") +set(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/../Microsoft.PowerShell.ConsoleHost") # test in BUILD_DIR enable_testing() From 2f73af53df24fce9b704510dbec2a89cf75d877e Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 13:54:26 -0700 Subject: [PATCH 05/56] Remove native host dependency for ConsoleHost When Open PowerShell is built (as .NET Core project), `OPEN` is defined. The `Start` function normally used as the entry point from PowerShell's custom native host is instead the real `Main` entry point of the program, so it can be launched from the `dotnet` host. Because Core PowerShell depends on the native host to setup the custom assembly load context, we do it here in managed code immediately on start-up. --- .../host/msh/ManagedEntrance.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs index 5f516948c..c1b283e68 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs @@ -26,7 +26,9 @@ namespace Microsoft.PowerShell /// /// Command line arguments to the managed MSH /// -#if CORECLR +#if OPEN && CORECLR // Open PowerShell needs an actual Main entry-point + public static int Main(string[] args) +#elif CORECLR #pragma warning disable 1573 public static int Start(string consoleFilePath, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 2)]string[] args, int argc) #pragma warning restore 1573 @@ -34,6 +36,9 @@ namespace Microsoft.PowerShell public int Start(string consoleFilePath, string[] args) #endif { +#if OPEN && CORECLR // Open PowerShell has to set the ALC here, since we don't own the native host + PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(string.Empty); +#endif System.Management.Automation.Runspaces.EarlyStartup.Init(); // Set ETW activity Id @@ -69,7 +74,9 @@ namespace Microsoft.PowerShell // PSSnapInException will cause the control to return back to the native code // and stuff the EXCEPINFO field with the message of the exception. // The native code will print this out and exit the process. +#if !OPEN // consoleFilePath is not available in Open PowerShell if (string.IsNullOrEmpty(consoleFilePath)) +#endif { #if DEBUG // Special switches for debug mode to allow self-hosting on InitialSessionState instead @@ -96,12 +103,14 @@ namespace Microsoft.PowerShell configuration = null; #endif } +#if !OPEN // consoleFilePath is not available in Open PowerShell else { //TODO : Deprecate RunspaceConfiguration and use InitialSessionState configuration = RunspaceConfigForSingleShell.Create(consoleFilePath, out warning); } +#endif int exitCode = 0; try { From 8021247b43853e59ac9dd0628881d126714b9fd8 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 12:49:27 -0700 Subject: [PATCH 06/56] Guard AmsiUtils in SessionState Init --- .../engine/InitialSessionState.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 7923ef0de..37abd6bba 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -60,7 +60,10 @@ namespace System.Management.Automation.Runspaces var unused0 = RunspaceInit.OutputEncodingDescription; // Amsi initialize can also be a little slow - AmsiUtils.Init(); + if (Platform.IsWindows) + { + AmsiUtils.Init(); + } // This will init some tables and could load some assemblies. var unused1 = TypeAccelerators.builtinTypeAccelerators; From 04f551fe91526ba4b41a88b7226449974c9eb29b Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 12:50:12 -0700 Subject: [PATCH 07/56] Don't build MshHostMshSnapin --- .../singleshell/installer/MshHostMshSnapin.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/singleshell/installer/MshHostMshSnapin.cs b/src/Microsoft.PowerShell.ConsoleHost/singleshell/installer/MshHostMshSnapin.cs index 615d5dfc2..07fc481bf 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/singleshell/installer/MshHostMshSnapin.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/singleshell/installer/MshHostMshSnapin.cs @@ -1,3 +1,4 @@ +#if !OPEN /********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ @@ -92,3 +93,4 @@ namespace Microsoft.PowerShell } } } +#endif From 602c7c39fc44f868918c76adad993d945f441af1 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 12:50:32 -0700 Subject: [PATCH 08/56] Don't build EngineInstaller --- .../singleshell/installer/EngineInstaller.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/singleshell/installer/EngineInstaller.cs b/src/Microsoft.PowerShell.ConsoleHost/singleshell/installer/EngineInstaller.cs index bcc5467e6..260c56229 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/singleshell/installer/EngineInstaller.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/singleshell/installer/EngineInstaller.cs @@ -1,3 +1,4 @@ +#if !OPEN /********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ @@ -86,3 +87,4 @@ namespace Microsoft.PowerShell } } } +#endif From 3cd0d75278e4096b33b82d2b595e19890bdf5323 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 12:55:30 -0700 Subject: [PATCH 09/56] Don't build ConsoleControl --- .../host/msh/ConsoleControl.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs index 830a2f03c..71804e87c 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs @@ -1,3 +1,4 @@ +#if !OPEN /********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ @@ -3685,4 +3686,4 @@ namespace Microsoft.PowerShell private static PSTraceSource tracer = PSTraceSource.GetTracer("ConsoleControl", "Console control methods"); } } // namespace - +#endif From 702082f597a5008ea9bb3903b95ca137f0cf1159 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 16:03:46 -0700 Subject: [PATCH 10/56] Guard use of ConsoleControl in ConsoleHost --- .../host/msh/ConsoleHost.cs | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 5eeef3c10..04faa65fc 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -190,9 +190,11 @@ namespace Microsoft.PowerShell System.Threading.Thread.CurrentThread.Name = "ConsoleHost main thread"; theConsoleHost = ConsoleHost.CreateSingletonInstance(configuration); +#if !OPEN theConsoleHost.BindBreakHandler(); PSHost.IsStdOutputRedirected = theConsoleHost.IsStandardOutputRedirected; +#endif if (args == null) { @@ -268,6 +270,7 @@ namespace Microsoft.PowerShell +#if !OPEN /// /// /// The break handler for the program. Dispatches a break event to the current Executor. @@ -307,6 +310,7 @@ namespace Microsoft.PowerShell return false; } } +#endif private static bool BreakIntoDebugger() { @@ -425,8 +429,10 @@ namespace Microsoft.PowerShell // call the console APIs directly, instead of ui.rawui.FlushInputHandle, as ui may be finalized // already if this thread is lagging behind the main thread. +#if !OPEN ConsoleHandle handle = ConsoleControl.GetInputHandle(); ConsoleControl.FlushConsoleInputBuffer(handle); +#endif ConsoleHost.SingletonInstance.breakHandlerThread = null; } @@ -1019,11 +1025,13 @@ namespace Microsoft.PowerShell #endif } +#if !OPEN private void BindBreakHandler() { breakHandlerGcHandle = GCHandle.Alloc(new ConsoleControl.BreakHandler(MyBreakHandler)); ConsoleControl.AddBreakHandler((ConsoleControl.BreakHandler)breakHandlerGcHandle.Target); } +#endif #if !CORECLR // Not used on NanoServer: CurrentDomain.UnhandledException not supported on CoreCLR private void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) @@ -1076,9 +1084,11 @@ namespace Microsoft.PowerShell { if (!isDisposed) { +#if !OPEN Dbg.Assert(breakHandlerGcHandle != null, "break handler should be set"); ConsoleControl.RemoveBreakHandler(); breakHandlerGcHandle.Free(); +#endif if (isDisposingNotFinalizing) { @@ -2053,9 +2063,12 @@ namespace Microsoft.PowerShell +#if !OPEN private delegate void InitializeStandardHandleDelegate(NakedWin32Handle handle); +#endif +#if !OPEN [ArchitectureSensitive] private bool IsStandardHandleRedirected( ConsoleControl.StandardHandleId handleId, @@ -2091,11 +2104,15 @@ namespace Microsoft.PowerShell return isHandleRedirected; } +#endif internal bool IsStandardOutputRedirected { get { +#if OPEN + return Console.IsOutputRedirected; +#else if (initStandardOutDelegate == null) { initStandardOutDelegate = new InitializeStandardHandleDelegate(InitializeStandardOutputWriter); @@ -2107,6 +2124,7 @@ namespace Microsoft.PowerShell ref isStandardOutputRedirectionDetermined, ref isStandardOutputRedirected, initStandardOutDelegate); +#endif } } @@ -2114,6 +2132,9 @@ namespace Microsoft.PowerShell { get { +#if OPEN + return Console.IsInputRedirected; +#else if (initStandardInDelegate == null) { initStandardInDelegate = new InitializeStandardHandleDelegate(InitializeStandardInputReader); @@ -2125,6 +2146,7 @@ namespace Microsoft.PowerShell ref isStandardInputRedirectionDetermined, ref isStandardInputRedirected, initStandardInDelegate); +#endif } } @@ -2132,6 +2154,9 @@ namespace Microsoft.PowerShell { get { +#if OPEN + return Console.IsErrorRedirected; +#else if (initStandardErrorDelegate == null) { initStandardErrorDelegate = new InitializeStandardHandleDelegate(InitializeStandardErrorWriter); @@ -2143,9 +2168,11 @@ namespace Microsoft.PowerShell ref isStandardErrorRedirectionDetermined, ref isStandardErrorRedirected, initStandardErrorDelegate); +#endif } } +#if !OPEN [ArchitectureSensitive] private void InitializeStandardInputReader(NakedWin32Handle stdHandle) { @@ -2200,6 +2227,7 @@ namespace Microsoft.PowerShell Dbg.Assert(standardErrorWriter == null, "standardErrorWriter should not exist at this point"); standardErrorWriter = Console.Error; } +#endif internal TextWriter StandardOutputWriter { @@ -3047,7 +3075,9 @@ namespace Microsoft.PowerShell /// private RunspaceRef runspaceRef; +#if !OPEN private GCHandle breakHandlerGcHandle; +#endif private System.Threading.Thread breakHandlerThread; private bool isDisposed; internal ConsoleHostUserInterface ui; @@ -3076,18 +3106,22 @@ namespace Microsoft.PowerShell private bool shouldEndSession; private int beginApplicationNotifyCount; +#if !OPEN private bool isStandardOutputRedirectionDetermined; private bool isStandardOutputRedirected; - private TextWriter standardOutputWriter; private bool isStandardErrorRedirectionDetermined; private bool isStandardErrorRedirected; - private TextWriter standardErrorWriter; private bool isStandardInputRedirectionDetermined; private bool isStandardInputRedirected; - private TextReader standardInputReader; +#endif + private TextWriter standardOutputWriter = null; + private TextWriter standardErrorWriter = null; + private TextReader standardInputReader = null; +#if !OPEN private InitializeStandardHandleDelegate initStandardOutDelegate; private InitializeStandardHandleDelegate initStandardInDelegate; private InitializeStandardHandleDelegate initStandardErrorDelegate; +#endif private ConsoleTextWriter consoleWriter; private WrappedSerializer outputSerializer; private WrappedSerializer errorSerializer; From 02e9f100d25e8f524b0a4e0be938484c01ce6487 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 16:05:46 -0700 Subject: [PATCH 11/56] Guard use of ConsoleControl in ConsoleHostUserInterface --- .../host/msh/ConsoleHostUserInterface.cs | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 569bceb89..a166b93f3 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -14,7 +14,9 @@ using System.Management.Automation.Internal; using System.Management.Automation.Host; using System.Security; using Dbg = System.Management.Automation.Diagnostics; +#if !OPEN using ConsoleHandle = Microsoft.Win32.SafeHandles.SafeFileHandle; +#endif namespace Microsoft.PowerShell { @@ -58,7 +60,11 @@ namespace Microsoft.PowerShell this.parent = parent; this.rawui = new ConsoleHostRawUserInterface(this); + isInteractiveTestToolListening = false; +#if OPEN + this._supportsVirtualTerminal = false; +#else // Turn on virtual terminal if possible. var handle = ConsoleControl.GetActiveScreenBufferHandle(); var m = ConsoleControl.GetMode(handle); @@ -71,9 +77,8 @@ namespace Microsoft.PowerShell // systems ignore the setting. m = ConsoleControl.GetMode(handle); this._supportsVirtualTerminal = (m & ConsoleControl.ConsoleModes.VirtualTerminal) != 0; - - isInteractiveTestToolListening = false; isTestingShiftTab = false; +#endif } /// @@ -284,6 +289,9 @@ namespace Microsoft.PowerShell private object ReadLineSafe(bool isSecureString, char? printToken) { +#if OPEN + throw new PlatformNotSupportedException("Cannot read secure strings!"); +#else // Don't lock (instanceLock) in here -- the caller needs to do that... PreRead(); @@ -404,6 +412,7 @@ namespace Microsoft.PowerShell { return result; } +#endif } /// @@ -535,7 +544,7 @@ namespace Microsoft.PowerShell /// false otherwise /// /// - +#if !OPEN private static bool shouldUnsetMode( ConsoleControl.ConsoleModes flagToUnset, ref ConsoleControl.ConsoleModes m) @@ -547,11 +556,15 @@ namespace Microsoft.PowerShell } return false; } +#endif #region WriteToConsole internal void WriteToConsole(string value, bool transcribeResult) { +#if OPEN + Console.Write(value); +#else ConsoleHandle handle = ConsoleControl.GetActiveScreenBufferHandle(); // Ensure that we're in the proper line-output mode. We don't lock here as it does not matter if we @@ -574,6 +587,7 @@ namespace Microsoft.PowerShell // This is atomic, so we don't lock here... ConsoleControl.WriteConsole(handle, value); +#endif if (isInteractiveTestToolListening && parent.IsStandardOutputRedirected) { @@ -1480,9 +1494,9 @@ namespace Microsoft.PowerShell - // We don't use System.Environment.NewLine because we are very platform specific with our use of the win32 console APIs + // We use System.Environment.NewLine because we are platform-agnostic - internal const string Crlf = "\x000D\x000A"; + internal static string Crlf = System.Environment.NewLine; private const string Tab = "\x0009"; internal enum ReadLineResult @@ -1550,6 +1564,10 @@ namespace Microsoft.PowerShell internal string ReadLine(bool endOnTab, string initialContent, out ReadLineResult result, bool calledFromPipeline, bool transcribeResult) { +#if OPEN + result = ReadLineResult.endedOnEnter; + return Console.ReadLine(); +#else result = ReadLineResult.endedOnEnter; // If the test hook is set, read from it. @@ -1705,6 +1723,7 @@ namespace Microsoft.PowerShell s += restOfLine; return s; +#endif } /// @@ -1712,6 +1731,7 @@ namespace Microsoft.PowerShell /// /// the cursor position where 'tab' is hit /// +#if !OPEN private char GetCharacterUnderCursor(Coordinates cursorPosition) { Rectangle region = new Rectangle(0, cursorPosition.Y, RawUI.BufferSize.Width - 1, cursorPosition.Y); @@ -1734,6 +1754,7 @@ namespace Microsoft.PowerShell Dbg.Assert(false, "the character at the cursor should be retrieved, never gets to here"); return '\0'; } +#endif /// @@ -1780,8 +1801,6 @@ namespace Microsoft.PowerShell /// internal string ReadLineWithTabCompletion(Executor exec, bool useUserDefinedCustomReadLine) { - ConsoleHandle handle = ConsoleControl.GetActiveScreenBufferHandle(); - string input = null; string lastInput = ""; string lastCompletion = ""; @@ -1912,13 +1931,19 @@ namespace Microsoft.PowerShell if (deltaInput > 0) { - ConsoleControl.FillConsoleOutputCharacter(handle, ' ', deltaInput, endOfCompletionCursorPos); + Console.SetCursorPosition(endOfCompletionCursorPos.X, endOfCompletionCursorPos.Y); + for (int i = 0; i < deltaInput; i++) + { + Console.Write(' '); + } } if (restOfLine != string.Empty) { lastCompletion = completedInput.Remove(completedInput.Length - restOfLine.Length); +#if !OPEN SendLeftArrows(restOfLine.Length); +#endif } else { @@ -1942,6 +1967,7 @@ namespace Microsoft.PowerShell return input; } +#if !OPEN private void SendLeftArrows(int length) { var inputs = new ConsoleControl.INPUT[length * 2]; @@ -1971,6 +1997,7 @@ namespace Microsoft.PowerShell ConsoleControl.MimicKeyPress(inputs); } +#endif private CommandCompletion GetNewCompletionResults(string input) { @@ -2086,7 +2113,9 @@ namespace Microsoft.PowerShell // this is a test hook for the ConsoleInteractiveTestTool, which sets this field to true. private bool isInteractiveTestToolListening; +#if !OPEN private bool isTestingShiftTab; +#endif // This instance data is "read-only" and need not have access serialized. From 8af7d5f344b7c3229647f09b1b1358e55b7539aa Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 16:07:56 -0700 Subject: [PATCH 12/56] Guard use of ConsoleControl in ConsoleHostRawUserInterface This uses the RawUserInterface implementation from the original CoreConsoleHost. --- .../host/msh/ConsoleHostRawUserInterface.cs | 252 ++++++++++++++++++ 1 file changed, 252 insertions(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs index 12bf65b80..9c4d08712 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostRawUserInterface.cs @@ -1,3 +1,4 @@ +#if !OPEN /********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ @@ -1499,3 +1500,254 @@ namespace Microsoft.PowerShell } } // namespace +#else + +// Managed code only implementation for portability + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Management.Automation; +using System.Management.Automation.Runspaces; +using System.Management.Automation.Host; +using System.Globalization; +using System.Reflection; +using System.Runtime.InteropServices; + +namespace Microsoft.PowerShell +{ + // this is all originally from https://msdn.microsoft.com/en-us/library/ee706570%28v=vs.85%29.aspx + + internal sealed class ConsoleHostRawUserInterface : PSHostRawUserInterface + { + private ConsoleColor defaultForeground = ConsoleColor.Gray; + + private ConsoleColor defaultBackground = ConsoleColor.Black; + + private ConsoleHostUserInterface parent = null; + + internal ConsoleHostRawUserInterface(ConsoleHostUserInterface mshConsole) : base() + { + defaultForeground = ForegroundColor; + defaultBackground = BackgroundColor; + parent = mshConsole; + } + + /// + /// Gets or sets the background color of the displayed text. + /// This maps to the corresponding Console.Background property. + /// + public override ConsoleColor BackgroundColor + { + get { return Console.BackgroundColor; } + set { Console.BackgroundColor = value; } + } + + /// + /// Gets or sets the size of the host buffer. In this example the + /// buffer size is adapted from the Console buffer size members. + /// + public override Size BufferSize + { + get { return new Size(Console.BufferWidth, Console.BufferHeight); } + set { Console.SetBufferSize(value.Width, value.Height); } + } + + /// + /// Gets or sets the cursor position. In this example this + /// functionality is not needed so the property throws a + /// NotImplementException exception. + /// + public override Coordinates CursorPosition + { + get { return new Coordinates(Console.CursorLeft, Console.CursorTop); } + set { Console.SetCursorPosition(value.X, value.Y); } + } + + /// + /// Gets or sets the size of the displayed cursor. In this example + /// the cursor size is taken directly from the Console.CursorSize + /// property. + /// + public override int CursorSize + { + get { return Console.CursorSize; } + set { Console.CursorSize = value; } + } + + /// + /// Gets or sets the foreground color of the displayed text. + /// This maps to the corresponding Console.ForgroundColor property. + /// + public override ConsoleColor ForegroundColor + { + get { return Console.ForegroundColor; } + set { Console.ForegroundColor = value; } + } + + /// + /// Gets a value indicating whether the user has pressed a key. This maps + /// to the corresponding Console.KeyAvailable property. + /// + public override bool KeyAvailable + { + get { return Console.KeyAvailable; } + } + + /// + /// Gets the dimensions of the largest window that could be + /// rendered in the current display, if the buffer was at the least + /// that large. This example uses the Console.LargestWindowWidth and + /// Console.LargestWindowHeight properties to determine the returned + /// value of this property. + /// + public override Size MaxPhysicalWindowSize + { + get { return new Size(Console.LargestWindowWidth, Console.LargestWindowHeight); } + } + + /// + /// Gets the dimentions of the largest window size that can be + /// displayed. This example uses the Console.LargestWindowWidth and + /// console.LargestWindowHeight properties to determine the returned + /// value of this property. + /// + public override Size MaxWindowSize + { + get { return new Size(Console.LargestWindowWidth, Console.LargestWindowHeight); } + } + + /// + /// Gets or sets the position of the displayed window. This example + /// uses the Console window position APIs to determine the returned + /// value of this property. + /// + public override Coordinates WindowPosition + { + get { return new Coordinates(Console.WindowLeft, Console.WindowTop); } + set { Console.SetWindowPosition(value.X, value.Y); } + } + + /// + /// Gets or sets the size of the displayed window. This example + /// uses the corresponding Console window size APIs to determine the + /// returned value of this property. + /// + public override Size WindowSize + { + get { return new Size(Console.WindowWidth, Console.WindowHeight); } + set { Console.SetWindowSize(value.Width, value.Height); } + } + + /// + /// Cached Window Title, for systems that needs it + /// + private string title = String.Empty; + + /// + /// Gets or sets the title of the displayed window. The example + /// maps the Console.Title property to the value of this property. + /// + public override string WindowTitle + { + get + { + // In Unix/Linux systems, Console.Title current results in a not-implemented + // exception. In that case, we return a cached copy of title that was set earlier. + // Obviously, this will not work if: 1) Title was never set in PowerShell, or 2) + // one sets the windows's title outside of PowerShell. + string result; + try + { + result = Console.Title; + } + catch (PlatformNotSupportedException) + { + return title; + } + return result; + } + + set + { + Console.Title = value; + title = value; + } + } + + /// + /// This API resets the input buffer. In this example this + /// functionality is not needed so the method returns nothing. + /// + public override void FlushInputBuffer() + { + } + + /// + /// This API returns a rectangular region of the screen buffer. In + /// this example this functionality is not needed so the method throws + /// a NotImplementException exception. + /// + /// Defines the size of the rectangle. + /// Throws a NotImplementedException exception. + public override BufferCell[,] GetBufferContents(Rectangle rectangle) + { + throw new NotImplementedException("The method or operation is not implemented."); + } + + /// + /// This API reads a pressed, released, or pressed and released keystroke + /// from the keyboard device, blocking processing until a keystroke is + /// typed that matches the specified keystroke options. + /// + /// Unused + public override KeyInfo ReadKey(ReadKeyOptions options) + { + ConsoleKeyInfo key = Console.ReadKey(); + return new KeyInfo((int)key.Key, key.KeyChar, new ControlKeyStates(), true); + } + + /// + /// This API crops a region of the screen buffer. In this example + /// this functionality is not needed so the method throws a + /// NotImplementException exception. + /// + /// The region of the screen to be scrolled. + /// The region of the screen to receive the + /// source region contents. + /// The region of the screen to include in the operation. + /// The character and attributes to be used to fill all cell. + public override void ScrollBufferContents(Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill) + { + throw new NotImplementedException("The method or operation is not implemented."); + } + + /// + /// This method copies an array of buffer cells into the screen buffer + /// at a specified location. In this example this functionality is + /// not needed so the method throws a NotImplementedException exception. + /// + /// The parameter is not used. + /// The parameter is not used. + public override void SetBufferContents(Coordinates origin, + BufferCell[,] contents) + { + throw new NotImplementedException("The method or operation is not implemented."); + } + + /// + /// This method copies a given character, foreground color, and background + /// color to a region of the screen buffer. In this example this + /// functionality is not needed so the method throws a + /// NotImplementException exception./// + /// Defines the area to be filled. + /// Defines the fill character. + public override void SetBufferContents(Rectangle rectangle, BufferCell fill) + { + throw new NotImplementedException("The method or operation is not implemented."); + } + } +} +#endif From 2e71a090e7aa5243cdbfb8e6e7ffcf6be04022ca Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 16:46:26 -0700 Subject: [PATCH 13/56] Use ~/.powershell for ConsoleHost profile on Linux --- .../host/msh/ConsoleHost.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 04faa65fc..69898f48a 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -170,8 +170,15 @@ namespace Microsoft.PowerShell { try { - var profileDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + - @"\Microsoft\Windows\PowerShell"; + string profileDir; + if (Platform.IsWindows) + { + profileDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + + @"\Microsoft\Windows\PowerShell"; + } else + { + profileDir = System.IO.Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".powershell"); + } if (!Directory.Exists(profileDir)) { From 78dd93c1acd5e0b48eff6d3a1cebb410896fc55d Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 12:56:31 -0700 Subject: [PATCH 14/56] Build PSReadLine for ConsoleHost --- src/Microsoft.PowerShell.ConsoleHost/project.json | 1 + src/Microsoft.PowerShell.PSReadLine/project.json | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/project.json b/src/Microsoft.PowerShell.ConsoleHost/project.json index f3065438b..db6a61beb 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/project.json +++ b/src/Microsoft.PowerShell.ConsoleHost/project.json @@ -11,6 +11,7 @@ }, "dependencies": { + "Microsoft.PowerShell.PSReadLine": "1.0.0-*", "Microsoft.PowerShell.Commands.Management": "1.0.0-*", "Microsoft.PowerShell.Commands.Utility": "1.0.0-*" }, diff --git a/src/Microsoft.PowerShell.PSReadLine/project.json b/src/Microsoft.PowerShell.PSReadLine/project.json index 809c0b397..7734ad2ed 100644 --- a/src/Microsoft.PowerShell.PSReadLine/project.json +++ b/src/Microsoft.PowerShell.PSReadLine/project.json @@ -17,6 +17,8 @@ "define": [ "CORECLR" ] }, "imports": [ "dnxcore50", "portable-net45+win8" ] + }, + "net451": { } } } From 849ea50ab74210cb9dd1ed7cea72465544528ca1 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 16:46:58 -0700 Subject: [PATCH 15/56] Load PSReadLine by correct name Linux is case-sensitive, so the modules are too. --- src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 69898f48a..04a132d93 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -1545,7 +1545,7 @@ namespace Microsoft.PowerShell { // Create and open Runspace with PSReadline. defaultImportModulesList = DefaultInitialSessionState.Modules; - DefaultInitialSessionState.ImportPSModule(new[] { "PSReadline" }); + DefaultInitialSessionState.ImportPSModule(new[] { "PSReadLine" }); consoleRunspace = RunspaceFactory.CreateRunspace(this, DefaultInitialSessionState); try { From c4bfc52c7643464f77c4ea115b717161ba157fa1 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Wed, 4 May 2016 16:50:10 -0700 Subject: [PATCH 16/56] Migrate ConsoleHost .gitignore --- .../.gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/{Microsoft.PowerShell.CoreConsoleHost => Microsoft.PowerShell.ConsoleHost}/.gitignore (52%) diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/.gitignore b/src/Microsoft.PowerShell.ConsoleHost/.gitignore similarity index 52% rename from src/Microsoft.PowerShell.CoreConsoleHost/.gitignore rename to src/Microsoft.PowerShell.ConsoleHost/.gitignore index e7842a07d..1b1efedeb 100644 --- a/src/Microsoft.PowerShell.CoreConsoleHost/.gitignore +++ b/src/Microsoft.PowerShell.ConsoleHost/.gitignore @@ -1,3 +1,3 @@ *.a *.so -*.dylib \ No newline at end of file +*.dylib From d8b2d1fb0eb9448eda7d5170654028ab06d25621 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Wed, 4 May 2016 17:04:48 -0700 Subject: [PATCH 17/56] Fix command-line parser to support --parameters --- .../host/msh/CommandLineParameterParser.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index 814614f93..d32847a4e 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -365,6 +365,12 @@ namespace Microsoft.PowerShell switchKey = switchKey.Substring(1); + // chop off the second dash so we're agnostic wrt specifying - or -- + if (SpecialCharacters.IsDash(switchKey[0])) + { + switchKey = switchKey.Substring(1); + } + if (MatchSwitch(switchKey, "help", "h") || MatchSwitch(switchKey, "?", "?")) { showHelp = true; From dca17ac0f7597ae0eccc73978cdf0bd529d64ec9 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Wed, 4 May 2016 18:03:28 -0700 Subject: [PATCH 18/56] Migrate ConsoleHost tests --- .../{CoreConsoleHost.Tests.ps1 => ConsoleHost.Tests.ps1} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/powershell/{CoreConsoleHost.Tests.ps1 => ConsoleHost.Tests.ps1} (100%) diff --git a/test/powershell/CoreConsoleHost.Tests.ps1 b/test/powershell/ConsoleHost.Tests.ps1 similarity index 100% rename from test/powershell/CoreConsoleHost.Tests.ps1 rename to test/powershell/ConsoleHost.Tests.ps1 From 5c0d17e3a2973dd7be5cae7b0793af6bdcb4b2ba Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Wed, 4 May 2016 18:05:46 -0700 Subject: [PATCH 19/56] Update ConsoleHost tests --- test/powershell/ConsoleHost.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/ConsoleHost.Tests.ps1 b/test/powershell/ConsoleHost.Tests.ps1 index 4d57be5ed..4013faf25 100644 --- a/test/powershell/ConsoleHost.Tests.ps1 +++ b/test/powershell/ConsoleHost.Tests.ps1 @@ -1,11 +1,11 @@ -Describe "CoreConsoleHost unit tests" { +Describe "ConsoleHost unit tests" { $powershell = Join-Path -Path $PsHome -ChildPath "powershell" Context "Command-line parsing" { foreach ($x in "--help", "-help", "-h", "-?", "--he", "-hel", "--HELP", "-hEl") { It "Accepts '$x' as a parameter for help" { - & $powershell -noprofile $x | ?{ $_ -match "usage: powershell" } | Should Not BeNullOrEmpty + & $powershell -noprofile $x | ?{ $_ -match "PowerShell[.exe] -Help | -? | /?" } | Should Not BeNullOrEmpty } } From 8435ed758c5cff926aef1f9ed434492457d62cbb Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Thu, 5 May 2016 14:28:33 -0700 Subject: [PATCH 20/56] Fix PSReadLine for net451 build Needed System.Windows.Forms --- src/Microsoft.PowerShell.PSReadLine/project.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Microsoft.PowerShell.PSReadLine/project.json b/src/Microsoft.PowerShell.PSReadLine/project.json index 7734ad2ed..172b9ae40 100644 --- a/src/Microsoft.PowerShell.PSReadLine/project.json +++ b/src/Microsoft.PowerShell.PSReadLine/project.json @@ -19,6 +19,11 @@ "imports": [ "dnxcore50", "portable-net45+win8" ] }, "net451": { + "frameworkAssemblies": { + "System.Windows.Forms": { + "type": "build" + } + } } } } From 658f3e0d48a1bca98e4702e41f770e4d8efd622f Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Thu, 5 May 2016 15:53:15 -0700 Subject: [PATCH 21/56] Update xUnit project to use ConsoleHost --- test/csharp/project.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/csharp/project.json b/test/csharp/project.json index 66756d4dc..9ba406513 100644 --- a/test/csharp/project.json +++ b/test/csharp/project.json @@ -5,7 +5,7 @@ "authors": [ "andschwa" ], "dependencies": { - "Microsoft.PowerShell.CoreConsoleHost": "1.0.0-*" + "Microsoft.PowerShell.ConsoleHost": "1.0.0-*" }, "frameworks": { From fc9aa4cd88eacd1f3f40c7029be79d0a9232c2fa Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Thu, 5 May 2016 16:25:49 -0700 Subject: [PATCH 22/56] Make ConsoleHost internals visible to powershell-tests --- src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs b/src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs index 8dea28787..b3af64a44 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs @@ -7,6 +7,8 @@ using System.Runtime.ConstrainedExecution; using System.Security.Permissions; #endif +[assembly:InternalsVisibleTo("powershell-tests")] + [assembly:AssemblyCulture("")] [assembly:NeutralResourcesLanguage("en-US")] From 4234b3db730eda43d8af9f54274d216bb86d915a Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Thu, 5 May 2016 16:31:31 -0700 Subject: [PATCH 23/56] Remove CoreConsoleHost from xUnit tests --- test/csharp/fixture_AssemblyLoadContext.cs | 1 - test/csharp/test_FileSystemProvider.cs | 1 - test/csharp/test_Runspace.cs | 1 - test/csharp/test_SessionState.cs | 1 - 4 files changed, 4 deletions(-) diff --git a/test/csharp/fixture_AssemblyLoadContext.cs b/test/csharp/fixture_AssemblyLoadContext.cs index 8b2040c9e..b24f41fc2 100644 --- a/test/csharp/fixture_AssemblyLoadContext.cs +++ b/test/csharp/fixture_AssemblyLoadContext.cs @@ -1,7 +1,6 @@ using Xunit; using System; using System.Management.Automation; -using Microsoft.PowerShell.CoreConsoleHost; // This collection fixture initializes Core PowerShell's AssemblyLoadContext once and only // once. Attempting to initialize in a class level fixture will cause multiple diff --git a/test/csharp/test_FileSystemProvider.cs b/test/csharp/test_FileSystemProvider.cs index ba83368c9..18a30d66f 100644 --- a/test/csharp/test_FileSystemProvider.cs +++ b/test/csharp/test_FileSystemProvider.cs @@ -13,7 +13,6 @@ using System.Management.Automation.Provider; using System.Management.Automation.Runspaces; using Microsoft.PowerShell; using Microsoft.PowerShell.Commands; -using Microsoft.PowerShell.CoreConsoleHost; namespace PSTests { diff --git a/test/csharp/test_Runspace.cs b/test/csharp/test_Runspace.cs index 6943e826a..fae68418f 100644 --- a/test/csharp/test_Runspace.cs +++ b/test/csharp/test_Runspace.cs @@ -2,7 +2,6 @@ using Xunit; using System; using System.Management.Automation; using System.Management.Automation.Runspaces; -using Microsoft.PowerShell.CoreConsoleHost; namespace PSTests { diff --git a/test/csharp/test_SessionState.cs b/test/csharp/test_SessionState.cs index 973601a5b..7558c1b67 100644 --- a/test/csharp/test_SessionState.cs +++ b/test/csharp/test_SessionState.cs @@ -9,7 +9,6 @@ using System.Management.Automation.Internal; using System.Management.Automation.Internal.Host; using System.Management.Automation.Runspaces; using Microsoft.PowerShell; -using Microsoft.PowerShell.CoreConsoleHost; namespace PSTests { From 0012fa90d6ccc0a8e4aa64db3c50568f97f6b93a Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Thu, 5 May 2016 16:31:44 -0700 Subject: [PATCH 24/56] Remove deprecated Listener tests --- test/csharp/test_Runspace.cs | 49 ------------------------------------ 1 file changed, 49 deletions(-) diff --git a/test/csharp/test_Runspace.cs b/test/csharp/test_Runspace.cs index fae68418f..687a7afc0 100644 --- a/test/csharp/test_Runspace.cs +++ b/test/csharp/test_Runspace.cs @@ -62,55 +62,6 @@ namespace PSTests } } - [Fact] - public void TestRunspaceWithPowerShellAndHost() - { - Listener listener = new Listener("", false, false); - MyHost myHost = new MyHost(listener); - using (var runspace = RunspaceFactory.CreateRunspace(myHost)) - { - runspace.Open(); - - using (PowerShell powerShell = PowerShell.Create()) - { - powerShell.Runspace = runspace; - - powerShell.AddScript(script); - - int objCount = 0; - foreach (var result in powerShell.Invoke()) - { - ++objCount; - Assert.NotNull(result); - } - Assert.Equal(count, objCount); - } - - runspace.Close(); - } - } - - [Fact] - public void TestRunspaceWithFunction() - { - Listener listener = new Listener("", false, false); - MyHost myHost = new MyHost(listener); - using (var runspace = RunspaceFactory.CreateRunspace(myHost)) - { - runspace.Open(); - - using (PowerShell powerShell = PowerShell.Create()) - { - powerShell.Runspace = runspace; - - powerShell.AddScript("{1}.Invoke()"); - powerShell.Invoke(); - } - - runspace.Close(); - } - } - [Fact] public void TestRunspaceWithPowerShellAndInitialSessionState() { From 7c80035d3572c7e332141fef5a4d338a33e83357 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Thu, 5 May 2016 16:36:24 -0700 Subject: [PATCH 25/56] Skip xUnit tests not yet ported to OS X --- test/csharp/test_CorePsPlatform.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/csharp/test_CorePsPlatform.cs b/test/csharp/test_CorePsPlatform.cs index 1284b2583..18e0f8ab5 100644 --- a/test/csharp/test_CorePsPlatform.cs +++ b/test/csharp/test_CorePsPlatform.cs @@ -80,7 +80,7 @@ namespace PSTests } } - [Fact] + [Fact(Skip="Bad arguments for OS X")] public static void TestGetMachineName() { var startInfo = new ProcessStartInfo @@ -103,7 +103,7 @@ namespace PSTests } } - [Fact] + [Fact(Skip="Bad arguments for OS X")] public static void TestGetFQDN() { var startInfo = new ProcessStartInfo @@ -126,7 +126,7 @@ namespace PSTests } } - [Fact] + [Fact(Skip="Bad arguments for OS X")] public static void TestGetDomainName() { var startInfo = new ProcessStartInfo From 7f9052c1c62f6762875933e293f3484f44c506a2 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Thu, 5 May 2016 16:36:47 -0700 Subject: [PATCH 26/56] Remove platform tests This is pointless to test as the .NET Code better be correct, or we have worse problems. --- test/csharp/test_CorePsPlatform.cs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/test/csharp/test_CorePsPlatform.cs b/test/csharp/test_CorePsPlatform.cs index 18e0f8ab5..2a3898e82 100644 --- a/test/csharp/test_CorePsPlatform.cs +++ b/test/csharp/test_CorePsPlatform.cs @@ -9,18 +9,6 @@ namespace PSTests [Collection("AssemblyLoadContext")] public static class PlatformTests { - [Fact] - public static void TestIsLinux() - { - Assert.True(Platform.IsLinux); - } - - [Fact] - public static void TestIsWindows() - { - Assert.False(Platform.IsWindows); - } - [Fact] public static void TestIsCore() { From 72b763a7e1c0d1a59083ebbb9c4c19bb67d5102d Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 9 May 2016 12:39:24 -0700 Subject: [PATCH 27/56] Fix ConsoleHost on Windows CoreCLR build --- src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs b/src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs index b3af64a44..fad3142bb 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/AssemblyInfo.cs @@ -7,12 +7,11 @@ using System.Runtime.ConstrainedExecution; using System.Security.Permissions; #endif -[assembly:InternalsVisibleTo("powershell-tests")] - +#if CORECLR [assembly:AssemblyCulture("")] [assembly:NeutralResourcesLanguage("en-US")] - -#if !CORECLR +[assembly:InternalsVisibleTo("powershell-tests")] +#else [assembly:AssemblyConfiguration("")] [assembly:AssemblyInformationalVersionAttribute (@"10.0.10011.16384")] [assembly:ReliabilityContractAttribute(Consistency.MayCorruptAppDomain, Cer.MayFail)] @@ -24,6 +23,7 @@ using System.Security.Permissions; [assembly:AssemblyKeyFileAttribute(@"..\signing\visualstudiopublic.snk")] [assembly:System.Reflection.AssemblyDelaySign(true)] #endif + [assembly:System.Runtime.InteropServices.ComVisible(false)] [assembly:System.Reflection.AssemblyVersion("3.0.0.0")] [assembly:System.Reflection.AssemblyProduct("Microsoft (R) Windows (R) Operating System")] From a1a722c4a133be6e6909ccd0607af110e3531ba8 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 9 May 2016 14:40:52 -0700 Subject: [PATCH 28/56] Add powershell project shim The `src/powershell` project builds Core PowerShell as a .NET CLI app, with a dependency on `Microsoft.PowerShell.ConsoleHost`, so that the latter can remain a library. --- PowerShellGitHubDev.psm1 | 7 ++- .../project.json | 21 ++------- .../AssemblyInfo.cs | 2 +- src/libpsl-native/CMakeLists.txt | 2 +- .../.gitignore | 0 src/powershell/project.json | 45 +++++++++++++++++++ 6 files changed, 57 insertions(+), 20 deletions(-) rename src/{Microsoft.PowerShell.ConsoleHost => powershell}/.gitignore (100%) create mode 100644 src/powershell/project.json diff --git a/PowerShellGitHubDev.psm1 b/PowerShellGitHubDev.psm1 index b3af37ea9..5d4cab947 100644 --- a/PowerShellGitHubDev.psm1 +++ b/PowerShellGitHubDev.psm1 @@ -221,7 +221,12 @@ function New-PSOptions { [switch]$FullCLR ) - $Top = "$PSScriptRoot/src/Microsoft.PowerShell.ConsoleHost" + if ($FullCLR) { + $Top = "$PSScriptRoot/src/Microsoft.PowerShell.ConsoleHost" + } else { + $Top = "$PSScriptRoot/src/powershell" + } + Write-Verbose "Top project directory is $Top" if (-not $Configuration) { $Configuration = if ($IsLinux -or $IsOSX) { diff --git a/src/Microsoft.PowerShell.ConsoleHost/project.json b/src/Microsoft.PowerShell.ConsoleHost/project.json index db6a61beb..babf345d0 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/project.json +++ b/src/Microsoft.PowerShell.ConsoleHost/project.json @@ -1,13 +1,12 @@ { - "name": "powershell", + "name": "Microsoft.PowerShell.ConsoleHost", "version": "1.0.0-*", "description": "PowerShell Host", "authors": [ "sevoroby", "andschwa" ], "compilationOptions": { "warningsAsErrors": true, - "allowUnsafe": true, - "emitEntryPoint": true + "allowUnsafe": true }, "dependencies": { @@ -19,19 +18,16 @@ "content": [ "Modules", "../Modules", - "*_profile.ps1", - "*.so", - "*.dylib" + "powershell.exe" ], "frameworks": { - "netcoreapp1.0": { + "netstandard1.5": { "imports": [ "dnxcore50", "portable-net45+win8" ], "compilationOptions": { "define": [ "CORECLR", "OPEN" ] }, "dependencies": { - "Microsoft.NETCore.App": "1.0.0-rc2-3002668", "Newtonsoft.Json": "8.0.2", "System.Xml.XDocument": "4.0.11-rc2-24103", "System.IO.MemoryMappedFiles": "4.0.0-rc2-24103" @@ -41,15 +37,6 @@ } }, - "runtimes": { - "ubuntu.14.04-x64": { }, - "centos.7.1-x64": { }, - "win7-x64": { }, - "win81-x64": { }, - "win10-x64": { }, - "osx.10.11-x64": { } - }, - "compile": [ "../windows-build/gen/Microsoft.PowerShell.ConsoleHost/*.cs" ] diff --git a/src/System.Management.Automation/AssemblyInfo.cs b/src/System.Management.Automation/AssemblyInfo.cs index 609774f13..cfe4a9105 100644 --- a/src/System.Management.Automation/AssemblyInfo.cs +++ b/src/System.Management.Automation/AssemblyInfo.cs @@ -14,7 +14,7 @@ using System.Resources; [assembly:InternalsVisibleTo("Microsoft.PowerShell.Commands.Utility")] [assembly:InternalsVisibleTo("Microsoft.PowerShell.Security")] [assembly:InternalsVisibleTo("Microsoft.PowerShell.CoreCLR.AssemblyLoadContext")] -[assembly:InternalsVisibleTo("powershell")] +[assembly:InternalsVisibleTo("Microsoft.PowerShell.ConsoleHost")] [assembly:InternalsVisibleTo("powershell-tests")] #else diff --git a/src/libpsl-native/CMakeLists.txt b/src/libpsl-native/CMakeLists.txt index b4665909b..3e77b46ee 100644 --- a/src/libpsl-native/CMakeLists.txt +++ b/src/libpsl-native/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8.4) project(PSL-NATIVE) add_compile_options(-std=c++11 -Wall -Werror) -set(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/../Microsoft.PowerShell.ConsoleHost") +set(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/../powershell") # test in BUILD_DIR enable_testing() diff --git a/src/Microsoft.PowerShell.ConsoleHost/.gitignore b/src/powershell/.gitignore similarity index 100% rename from src/Microsoft.PowerShell.ConsoleHost/.gitignore rename to src/powershell/.gitignore diff --git a/src/powershell/project.json b/src/powershell/project.json new file mode 100644 index 000000000..08e061986 --- /dev/null +++ b/src/powershell/project.json @@ -0,0 +1,45 @@ +{ + "name": "powershell", + "version": "1.0.0-*", + "description": ".NET CLI PowerShell app", + "authors": [ "andschwa" ], + + "compilationOptions": { + "warningsAsErrors": true, + "allowUnsafe": true, + "emitEntryPoint": true + }, + + "dependencies": { + "Microsoft.PowerShell.ConsoleHost": "1.0.0-*" + }, + + "content": [ + "Modules", + "../Modules", + "*_profile.ps1", + "*.so", + "*.dylib" + ], + + "frameworks": { + "netcoreapp1.0": { + "imports": [ "dnxcore50", "portable-net45+win8" ], + "compilationOptions": { + "define": [ "CORECLR" ] + }, + "dependencies": { + "Microsoft.NETCore.App": "1.0.0-rc2-3002668" + } + } + }, + + "runtimes": { + "ubuntu.14.04-x64": { }, + "centos.7.1-x64": { }, + "win7-x64": { }, + "win81-x64": { }, + "win10-x64": { }, + "osx.10.11-x64": { } + } +} From 630eb69c7cd53d2e18274e9e26c05d52eee75e56 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 9 May 2016 14:41:44 -0700 Subject: [PATCH 29/56] Add managed entrance for powershell project The powershell project's `Main` function initializes the custom `AssemblyLoadContext` and then delegates to `UnmanagedPSEntry.Start`. This lets us undo several changes to the `ConsoleHost` managed entry (notably, the `Start` function signature does not have to change, thus the `consoleFilePath` variable still exists). --- .../host/msh/ManagedEntrance.cs | 11 +------ src/powershell/Program.cs | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 src/powershell/Program.cs diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs index c1b283e68..5f516948c 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs @@ -26,9 +26,7 @@ namespace Microsoft.PowerShell /// /// Command line arguments to the managed MSH /// -#if OPEN && CORECLR // Open PowerShell needs an actual Main entry-point - public static int Main(string[] args) -#elif CORECLR +#if CORECLR #pragma warning disable 1573 public static int Start(string consoleFilePath, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 2)]string[] args, int argc) #pragma warning restore 1573 @@ -36,9 +34,6 @@ namespace Microsoft.PowerShell public int Start(string consoleFilePath, string[] args) #endif { -#if OPEN && CORECLR // Open PowerShell has to set the ALC here, since we don't own the native host - PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(string.Empty); -#endif System.Management.Automation.Runspaces.EarlyStartup.Init(); // Set ETW activity Id @@ -74,9 +69,7 @@ namespace Microsoft.PowerShell // PSSnapInException will cause the control to return back to the native code // and stuff the EXCEPINFO field with the message of the exception. // The native code will print this out and exit the process. -#if !OPEN // consoleFilePath is not available in Open PowerShell if (string.IsNullOrEmpty(consoleFilePath)) -#endif { #if DEBUG // Special switches for debug mode to allow self-hosting on InitialSessionState instead @@ -103,14 +96,12 @@ namespace Microsoft.PowerShell configuration = null; #endif } -#if !OPEN // consoleFilePath is not available in Open PowerShell else { //TODO : Deprecate RunspaceConfiguration and use InitialSessionState configuration = RunspaceConfigForSingleShell.Create(consoleFilePath, out warning); } -#endif int exitCode = 0; try { diff --git a/src/powershell/Program.cs b/src/powershell/Program.cs new file mode 100644 index 000000000..eea047769 --- /dev/null +++ b/src/powershell/Program.cs @@ -0,0 +1,29 @@ +/********************************************************************++ +Copyright (c) Microsoft Corporation. All rights reserved. +--********************************************************************/ + +using System.Management.Automation; + +namespace Microsoft.PowerShell +{ + /// + /// Defines an entry point for the .NET CLI "powershell" app + /// + public sealed class ManagedPSEntry + { + /// + /// Starts the managed MSH + /// + /// + /// Command line arguments to the managed MSH + /// + public static int Main(string[] args) + { +#if CORECLR + // Open PowerShell has to set the ALC here, since we don't own the native host + PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(string.Empty); +#endif + return UnmanagedPSEntry.Start(string.Empty, args, args.Length); + } + } +} From 1a61837c0783dae6d1412e2b797585987388a302 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 9 May 2016 14:44:56 -0700 Subject: [PATCH 30/56] Migrate CoreConsoleHost/Modules And update mapping.json --- mapping.json | 4 ++-- .../Microsoft.PowerShell.Diagnostics.psd1 | 0 .../Microsoft.PowerShell.Management.psd1 | 0 .../Microsoft.PowerShell.Security.psd1 | 0 .../Microsoft.PowerShell.Utility.psd1 | 0 .../Microsoft.PowerShell.Utility.psm1 | 0 .../Modules/PSDiagnostics/PSDiagnostics.psd1 | Bin .../Modules/PSDiagnostics/PSDiagnostics.psm1 | Bin 8 files changed, 2 insertions(+), 2 deletions(-) rename src/{Microsoft.PowerShell.CoreConsoleHost => powershell}/Modules/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1 (100%) rename src/{Microsoft.PowerShell.CoreConsoleHost => powershell}/Modules/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 (100%) rename src/{Microsoft.PowerShell.CoreConsoleHost => powershell}/Modules/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.psd1 (100%) rename src/{Microsoft.PowerShell.CoreConsoleHost => powershell}/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 (100%) rename src/{Microsoft.PowerShell.CoreConsoleHost => powershell}/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1 (100%) rename src/{Microsoft.PowerShell.CoreConsoleHost => powershell}/Modules/PSDiagnostics/PSDiagnostics.psd1 (100%) rename src/{Microsoft.PowerShell.CoreConsoleHost => powershell}/Modules/PSDiagnostics/PSDiagnostics.psm1 (100%) diff --git a/mapping.json b/mapping.json index 63b3f08d8..ebedb25cc 100644 --- a/mapping.json +++ b/mapping.json @@ -1187,12 +1187,12 @@ "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Archive/ArchiveResources.psd1": "src/Modules/Microsoft.PowerShell.Archive/ArchiveResources.psd1", "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psd1": "src/Modules/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psd1", "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1": "src/Modules/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1", - "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Diagnostics/CoreClr/Microsoft.PowerShell.Diagnostics.psd1": "src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1", + "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Diagnostics/CoreClr/Microsoft.PowerShell.Diagnostics.psd1": "src/powershell/Modules/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1", "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1": "src/Microsoft.PowerShell.ConsoleHost/Modules/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1", "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Host/Microsoft.PowerShell.Host.psd1": "src/Modules/Microsoft.PowerShell.Host/Microsoft.PowerShell.Host.psd1", "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1": "src/Modules/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1", "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.psd1": "src/Modules/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.psd1", - "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Utility/CoreClr/Microsoft.PowerShell.Utility.psd1": "src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1", + "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Utility/CoreClr/Microsoft.PowerShell.Utility.psd1": "src/powershell/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1", "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1": "src/Microsoft.PowerShell.ConsoleHost/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1", "src/monad/monad/miscfiles/modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1": "src/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1", "src/monad/monad/miscfiles/modules/Microsoft.WSMan.Management/Microsoft.WSMan.Management.psd1": "src/Modules/Microsoft.WSMan.Management/Microsoft.WSMan.Management.psd1", diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1 b/src/powershell/Modules/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1 similarity index 100% rename from src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1 rename to src/powershell/Modules/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1 diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 b/src/powershell/Modules/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 similarity index 100% rename from src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 rename to src/powershell/Modules/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.psd1 b/src/powershell/Modules/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.psd1 similarity index 100% rename from src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.psd1 rename to src/powershell/Modules/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.psd1 diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 b/src/powershell/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 similarity index 100% rename from src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 rename to src/powershell/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1 b/src/powershell/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1 similarity index 100% rename from src/Microsoft.PowerShell.CoreConsoleHost/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1 rename to src/powershell/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1 diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/Modules/PSDiagnostics/PSDiagnostics.psd1 b/src/powershell/Modules/PSDiagnostics/PSDiagnostics.psd1 similarity index 100% rename from src/Microsoft.PowerShell.CoreConsoleHost/Modules/PSDiagnostics/PSDiagnostics.psd1 rename to src/powershell/Modules/PSDiagnostics/PSDiagnostics.psd1 diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/Modules/PSDiagnostics/PSDiagnostics.psm1 b/src/powershell/Modules/PSDiagnostics/PSDiagnostics.psm1 similarity index 100% rename from src/Microsoft.PowerShell.CoreConsoleHost/Modules/PSDiagnostics/PSDiagnostics.psm1 rename to src/powershell/Modules/PSDiagnostics/PSDiagnostics.psm1 From 6d3c42a0b01ec30583e2d28886a8be2f6fcd9b1b Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 9 May 2016 16:32:54 -0700 Subject: [PATCH 31/56] Migrate profile --- .../Microsoft.PowerShell_profile.ps1} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{Microsoft.PowerShell.CoreConsoleHost/Microsoft.PowerShellCore_profile.ps1 => powershell/Microsoft.PowerShell_profile.ps1} (100%) diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/Microsoft.PowerShellCore_profile.ps1 b/src/powershell/Microsoft.PowerShell_profile.ps1 similarity index 100% rename from src/Microsoft.PowerShell.CoreConsoleHost/Microsoft.PowerShellCore_profile.ps1 rename to src/powershell/Microsoft.PowerShell_profile.ps1 From 97ea9308fbe81e5f38f7b785a25ffcf2866ac831 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 9 May 2016 16:33:36 -0700 Subject: [PATCH 32/56] Add readme to src/powershell --- src/powershell/README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/powershell/README.md diff --git a/src/powershell/README.md b/src/powershell/README.md new file mode 100644 index 000000000..7745dd8c1 --- /dev/null +++ b/src/powershell/README.md @@ -0,0 +1,20 @@ +PowerShell +========== + +The `powershell[.exe]` executable for Open PowerShell is built by this project, +as it is the top dependency of the graph, and has `emitEntryPoint: true`, +meaning a native executable is produced automatically by CLI (no need to own a +separate native host). + +This project is a very simple shim that provides a `Main` function for .NET CLI +to produce an app. It initializes PowerShell's custom `AssemblyLoadContext` and +then delegates to the same `Start` function in +`Microsoft.PowerShell.ConsoleHost` that the original native PowerShell host +executes; thus we share the same entry point and the same PowerShell host, but +use a different native host. This lets us take full advantage of .NET CLI's +native host. + +We use this shim so that the `ConsoleHost` project and the original native host +do not have to be changed. Additionally, until .NET CLI bugs surrounding content +file deployment are solved, this shim allows us to continue with our split +`Modules` folders work-around to deploy the correct versions. From eee564e46509160f0e2d888659f0eb6b744f4af3 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 9 May 2016 16:34:16 -0700 Subject: [PATCH 33/56] Delete deprecated CoreConsoleHost project --- .../AssemblyInfo.cs | 8 - .../README.md | 25 - .../host.cs | 218 ---- .../main.cs | 857 ------------- .../project.json | 46 - .../rawui.cs | 237 ---- .../readline.cs | 1133 ----------------- .../ui.cs | 566 -------- 8 files changed, 3090 deletions(-) delete mode 100644 src/Microsoft.PowerShell.CoreConsoleHost/AssemblyInfo.cs delete mode 100644 src/Microsoft.PowerShell.CoreConsoleHost/README.md delete mode 100644 src/Microsoft.PowerShell.CoreConsoleHost/host.cs delete mode 100644 src/Microsoft.PowerShell.CoreConsoleHost/main.cs delete mode 100644 src/Microsoft.PowerShell.CoreConsoleHost/project.json delete mode 100644 src/Microsoft.PowerShell.CoreConsoleHost/rawui.cs delete mode 100644 src/Microsoft.PowerShell.CoreConsoleHost/readline.cs delete mode 100644 src/Microsoft.PowerShell.CoreConsoleHost/ui.cs diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/AssemblyInfo.cs b/src/Microsoft.PowerShell.CoreConsoleHost/AssemblyInfo.cs deleted file mode 100644 index 3e4b06af9..000000000 --- a/src/Microsoft.PowerShell.CoreConsoleHost/AssemblyInfo.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System.Runtime.CompilerServices; -using System.Reflection; -using System.Resources; - -[assembly:InternalsVisibleTo("powershell-tests")] - -[assembly:AssemblyCulture("")] -[assembly:NeutralResourcesLanguage("en-US")] diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/README.md b/src/Microsoft.PowerShell.CoreConsoleHost/README.md deleted file mode 100644 index 9c8e1e197..000000000 --- a/src/Microsoft.PowerShell.CoreConsoleHost/README.md +++ /dev/null @@ -1,25 +0,0 @@ -# Core PowerShell Host - -This host is based off the infamous `host06` example. It relies only on managed -code, and is cross-platform. It needs a fair bit of work, but it loads -profiles, has tab-completion, and can be used to debug PowerShell scripts. - -## Executable - -The `bin/powershell[.exe]` executable for Core PowerShell is built by this -project, as it is the top dependency of the graph, and has `emitEntryPoint: -true`, meaning a native executable is produced automatically by CLI (no need to -own a separate native host). It is also the project that deploys the `ps1xml` -types and formatting files, as well as the default profile and the included -PowerShell modules. - -Note that many of these should probably live with System.Management.Automation, -but we're waiting on a bug fix from CLI to move them. - -## update-content.sh - -This script is used to update our current tree's files with those that live in -`src/monad`. We only need to update them when new changes are merged from -Source Depot, and the build scripts are much simpler without this logic. With -the files living here, the `content` key in the `project.json` handles the -deployment for us in a cross-platform manner. diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/host.cs b/src/Microsoft.PowerShell.CoreConsoleHost/host.cs deleted file mode 100644 index c987abac7..000000000 --- a/src/Microsoft.PowerShell.CoreConsoleHost/host.cs +++ /dev/null @@ -1,218 +0,0 @@ -namespace Microsoft.PowerShell.CoreConsoleHost -{ - using System; - using System.Globalization; - using System.Management.Automation.Host; - using System.Management.Automation.Runspaces; - - /// - /// This is a sample implementation of the PSHost abstract class for - /// console applications. Not all members are implemented. Those that - /// are not implemented throw a NotImplementedException exception or - /// return nothing. - /// - internal class MyHost : PSHost, IHostSupportsInteractiveSession - { - public MyHost(Listener Listener) - { - this.Listener = Listener; - this.myHostUserInterface = new MyHostUserInterface(Listener.HasUI, Listener.Interactive); - } - - /// - /// A reference to the PSHost implementation. - /// - private Listener Listener; - - /// - /// The culture information of the thread that created - /// this object. - /// - private CultureInfo originalCultureInfo = CultureInfo.CurrentCulture; - - /// - /// The UI culture information of the thread that created - /// this object. - /// - private CultureInfo originalUICultureInfo = CultureInfo.CurrentCulture; - - /// - /// The identifier of this PSHost implementation. - /// - private static Guid instanceId = Guid.NewGuid(); - - /// - /// A reference to the implementation of the PSHostUserInterface - /// class for this application. - /// - private MyHostUserInterface myHostUserInterface; - - /// - /// A reference to the runspace used to start an interactive session. - /// - public Runspace pushedRunspace = null; - - - /// - /// Gets the culture information to use. This implementation - /// returns a snapshot of the culture information of the thread - /// that created this object. - /// - public override CultureInfo CurrentCulture - { - get { return this.originalCultureInfo; } - } - - /// - /// Gets the UI culture information to use. This implementation - /// returns a snapshot of the UI culture information of the thread - /// that created this object. - /// - public override CultureInfo CurrentUICulture - { - get { return this.originalUICultureInfo; } - } - - /// - /// Gets an identifier for this host. This implementation always - /// returns the GUID allocated at instantiation time. - /// - public override Guid InstanceId - { - get { return instanceId; } - } - - /// - /// Gets a string that contains the name of this host implementation. - /// Keep in mind that this string may be used by script writers to - /// identify when your host is being used. - /// - public override string Name - { - get { return "CoreConsoleHost"; } - } - - /// - /// Gets an instance of the implementation of the PSHostUserInterface - /// class for this application. This instance is allocated once at startup time - /// and returned every time thereafter. - /// - public override PSHostUserInterface UI - { - get { return this.myHostUserInterface; } - } - - /// - /// Gets the version object for this application. Typically this - /// should match the version resource in the application. - /// - public override Version Version - { - get { return new Version(1, 0, 0, 0); } - } - -#region IHostSupportsInteractiveSession Properties - - /// - /// Gets a value indicating whether a request - /// to open a PSSession has been made. - /// - public bool IsRunspacePushed - { - get { return this.pushedRunspace != null; } - } - - /// - /// Gets or sets the runspace used by the PSSession. - /// - public Runspace Runspace - { - get { return this.Listener.myRunSpace; } - internal set { this.Listener.myRunSpace = value; } - } -#endregion IHostSupportsInteractiveSession Properties - - /// - /// This API Instructs the host to interrupt the currently running - /// pipeline and start a new nested input loop. In this example this - /// functionality is not needed so the method throws a - /// NotImplementedException exception. - /// - public override void EnterNestedPrompt() - { - throw new NotImplementedException( - "The method or operation is not implemented."); - } - - /// - /// This API instructs the host to exit the currently running input loop. - /// In this example this functionality is not needed so the method - /// throws a NotImplementedException exception. - /// - public override void ExitNestedPrompt() - { - throw new NotImplementedException( - "The method or operation is not implemented."); - } - - /// - /// This API is called before an external application process is - /// started. Typically it is used to save state so that the parent - /// can restore state that has been modified by a child process (after - /// the child exits). In this example this functionality is not - /// needed so the method returns nothing. - /// - public override void NotifyBeginApplication() - { - return; - } - - /// - /// This API is called after an external application process finishes. - /// Typically it is used to restore state that a child process has - /// altered. In this example, this functionality is not needed so - /// the method returns nothing. - /// - public override void NotifyEndApplication() - { - return; - } - - /// - /// Indicate to the host application that exit has - /// been requested. Pass the exit code that the host - /// application should use when exiting the process. - /// - /// The exit code that the - /// host application should use. - public override void SetShouldExit(int exitCode) - { - this.Listener.ShouldExit = true; - this.Listener.ExitCode = exitCode; - } - -#region IHostSupportsInteractiveSession Methods - - /// - /// Requests to close a PSSession. - /// - public void PopRunspace() - { - Runspace = this.pushedRunspace; - this.pushedRunspace = null; - } - - /// - /// Requests to open a PSSession. - /// - /// Runspace to use. - public void PushRunspace(Runspace runspace) - { - this.pushedRunspace = Runspace; - Runspace = runspace; - } - -#endregion IHostSupportsInteractiveSession Methods - } -} - diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/main.cs b/src/Microsoft.PowerShell.CoreConsoleHost/main.cs deleted file mode 100644 index 94c0df6c1..000000000 --- a/src/Microsoft.PowerShell.CoreConsoleHost/main.cs +++ /dev/null @@ -1,857 +0,0 @@ -namespace Microsoft.PowerShell.CoreConsoleHost -{ - using System; - using System.Collections.Generic; - using System.Collections.ObjectModel; - using System.Management.Automation; - using System.Management.Automation.Host; - using System.Management.Automation.Runspaces; - using System.Text; - using System.IO; - using System.Runtime.InteropServices; - using System.Linq; - using PowerShell = System.Management.Automation.PowerShell; - - public static class Program - { - private enum Options {Help, File, Command, NoProfile, NonInteractive, EncodedCommand, Invalid}; - private class CommandOptions - { - public Options opt; - public string optString; - - public CommandOptions(Options o, string s) - { - opt = o; - optString = s; - } - } - - private static CommandOptions[] commandOptions = new CommandOptions[] - { - new CommandOptions(Options.Help, "help"), - new CommandOptions(Options.Help, "?"), - new CommandOptions(Options.File, "file"), - new CommandOptions(Options.Command, "command"), - new CommandOptions(Options.NoProfile, "noprofile"), - new CommandOptions(Options.NonInteractive, "noninteractive"), - new CommandOptions(Options.EncodedCommand, "encodedcommand") - }; - - /// - /// Creates and initiates the listener instance. - /// - public static void Main(string[] args) - { - // Setup the Assembly Load Context, which Core PowerShell uses to - // analyze the libraries for types, functions, cmdlets, etc. and - // provide the ability to load assemblies by file path. Doing this - // here eliminates the need for a custom native host. - PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(string.Empty); - - - // Argument parsing - string initialScript = null; - bool loadProfiles = true; - bool interactive = true; - - if (args.Length > 0) - { - for (int i = 0; i < args.Length; ++i) - { - string arg = args[i]; - bool hasNext = (i+1) < args.Length; - string nextArg = hasNext ? args[i+1] : string.Empty; - - // options start with "-" or "--" - - string option; - if (arg.StartsWith("-")) - { - if (arg.StartsWith("--")) - { - option = arg.Remove(0, 2); - } - else - { - option = arg.Remove(0, 1); - } - - Options bestOption = FindOption(option); - - switch (bestOption) - { - case Options.Help: - Console.WriteLine(@" -usage: powershell[.exe] [ (--help | -h | -?) ] - [ (--file | -f) ] - [