diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index 6335230d1f6..6f44ae14a01 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -198,7 +198,10 @@ internal class CommandLineParameterParser internal CommandLineParameterParser(PSHostUserInterface hostUI, string bannerText, string helpText) { - if (hostUI == null) { throw new PSArgumentNullException("hostUI"); } + if (hostUI == null) + { + throw new PSArgumentNullException(nameof(hostUI)); + } _hostUI = hostUI; diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleShell.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleShell.cs index 5f34692cd02..f8467e634cb 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleShell.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleShell.cs @@ -12,8 +12,7 @@ namespace Microsoft.PowerShell /// to transfer control to Msh console host implementation. /// - public static - class ConsoleShell + public static class ConsoleShell { /// Entry point in to ConsoleShell. This method is called by main of minishell. /// Banner text to be displayed by ConsoleHost. diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs index eb9b588d414..5c271197af2 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs @@ -2,12 +2,12 @@ // Licensed under the MIT License. using System; +using System.ComponentModel; using System.Globalization; using System.Management.Automation; -using System.Management.Automation.Internal; +using System.Management.Automation.Host; using System.Management.Automation.Runspaces; using System.Management.Automation.Tracing; -using System.Reflection; using System.Runtime.InteropServices; using System.Threading; @@ -27,14 +27,16 @@ public sealed class UnmanagedPSEntry /// /// Command line arguments to the managed MSH /// - /// + /// + /// Length of the passed in argument array. + /// public static int Start(string consoleFilePath, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 2)]string[] args, int argc) { // Warm up some components concurrently on background threads. - System.Management.Automation.Runspaces.EarlyStartup.Init(); + EarlyStartup.Init(); // We need to read the settings file before we create the console host - Microsoft.PowerShell.CommandLineParameterParser.EarlyParse(args); + CommandLineParameterParser.EarlyParse(args); #if !UNIX // NOTE: On Unix, logging has to be deferred until after command-line parsing @@ -67,18 +69,18 @@ public static int Start(string consoleFilePath, [MarshalAs(UnmanagedType.LPArray int exitCode = 0; try { - var banner = ManagedEntranceStrings.ShellBannerNonWindowsPowerShell; - var formattedBanner = string.Format(CultureInfo.InvariantCulture, banner, PSVersionInfo.GitCommitId); - exitCode = Microsoft.PowerShell.ConsoleShell.Start( - formattedBanner, - ManagedEntranceStrings.UsageHelp, - args); + var banner = string.Format( + CultureInfo.InvariantCulture, + ManagedEntranceStrings.ShellBannerNonWindowsPowerShell, + PSVersionInfo.GitCommitId); + + exitCode = ConsoleShell.Start(banner, ManagedEntranceStrings.UsageHelp, args); } - catch (System.Management.Automation.Host.HostException e) + catch (HostException e) { - if (e.InnerException != null && e.InnerException.GetType() == typeof(System.ComponentModel.Win32Exception)) + if (e.InnerException != null && e.InnerException.GetType() == typeof(Win32Exception)) { - System.ComponentModel.Win32Exception win32e = e.InnerException as System.ComponentModel.Win32Exception; + Win32Exception win32e = e.InnerException as Win32Exception; // These exceptions are caused by killing conhost.exe // 1236, network connection aborted by local system diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index f26b3cd7e69..85154cffae4 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -140,22 +140,15 @@ public static bool IsWindowsDesktop } } - /// - /// Gets the location for the various caches. - /// - internal static string CacheDirectory - { - get - { #if UNIX - return Platform.SelectProductNameForDirectory(Platform.XDG_Type.CACHE); + // Gets the location for cache and config folders. + internal static readonly string CacheDirectory = Platform.SelectProductNameForDirectory(Platform.XDG_Type.CACHE); + internal static readonly string ConfigDirectory = Platform.SelectProductNameForDirectory(Platform.XDG_Type.CONFIG); #else - return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Microsoft\PowerShell"; -#endif - } - } + // Gets the location for cache and config folders. + internal static readonly string CacheDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Microsoft\PowerShell"; + internal static readonly string ConfigDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\PowerShell"; -#if !UNIX private static bool? _isNanoServer = null; private static bool? _isIoT = null; private static bool? _isWindowsDesktop = null; @@ -355,7 +348,6 @@ public static string SelectProductNameForDirectory(Platform.XDG_Type dirpath) return xdgCacheDefault; } - else { if (!Directory.Exists(Path.Combine(xdgcachehome, "powershell"))) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index ebf1f768185..f7bcdb29210 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -2711,14 +2711,7 @@ private Exception ProcessUserDrive(Runspace initializedRunspace) // Or for virtual accounts // WinDir\System32\Microsoft\PowerShell\DriveRoots\[UserName] string directoryName = MakeUserNamePath(); -#if UNIX - string userDrivePath = Path.Combine(Platform.SelectProductNameForDirectory(Platform.XDG_Type.CACHE), "DriveRoots", directoryName); -#else - string userDrivePath = Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), - @"Microsoft\PowerShell\DriveRoots", - directoryName); -#endif + string userDrivePath = Path.Combine(Platform.CacheDirectory, "DriveRoots", directoryName); // Create directory if it doesn't exist. if (!System.IO.Directory.Exists(userDrivePath)) diff --git a/src/System.Management.Automation/engine/Modules/AnalysisCache.cs b/src/System.Management.Automation/engine/Modules/AnalysisCache.cs index dec838a37f3..99943d8faa6 100644 --- a/src/System.Management.Automation/engine/Modules/AnalysisCache.cs +++ b/src/System.Management.Automation/engine/Modules/AnalysisCache.cs @@ -1121,11 +1121,7 @@ static AnalysisCacheData() cacheFileName = string.Format(CultureInfo.InvariantCulture, "{0}-{1}", cacheFileName, hashString); } -#if UNIX - s_cacheStoreLocation = Path.Combine(Platform.SelectProductNameForDirectory(Platform.XDG_Type.CACHE), cacheFileName); -#else - s_cacheStoreLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\PowerShell", cacheFileName); -#endif + s_cacheStoreLocation = Path.Combine(Platform.CacheDirectory, cacheFileName); } } diff --git a/src/System.Management.Automation/engine/PSConfiguration.cs b/src/System.Management.Automation/engine/PSConfiguration.cs index 94033e4d04a..d204924f633 100644 --- a/src/System.Management.Automation/engine/PSConfiguration.cs +++ b/src/System.Management.Automation/engine/PSConfiguration.cs @@ -68,7 +68,7 @@ private PowerShellConfig() // Note: This directory may or may not exist depending upon the // execution scenario. Writes will attempt to create the directory // if it does not already exist. - perUserConfigDirectory = Utils.GetUserConfigurationDirectory(); + perUserConfigDirectory = Platform.ConfigDirectory; perUserConfigFile = Path.Combine(perUserConfigDirectory, configFileName); } diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index c8557d91d7b..af9fce89e9f 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -458,20 +458,6 @@ internal static string GetApplicationBase(string shellId) private static string[] s_productFolderDirectories; - /// - /// Specifies the per-user configuration settings directory in a platform agnostic manner. - /// - /// The current user's configuration settings directory. - internal static string GetUserConfigurationDirectory() - { -#if UNIX - return Platform.SelectProductNameForDirectory(Platform.XDG_Type.CONFIG); -#else - string basePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); - return IO.Path.Combine(basePath, Utils.ProductNameForDirectory); -#endif - } - private static string[] GetProductFolderDirectories() { if (s_productFolderDirectories == null) diff --git a/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs b/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs index 63eaa9b8b98..c7388833c74 100644 --- a/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs +++ b/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs @@ -228,7 +228,7 @@ internal static string GetFullProfileFileName(string shellId, bool forCurrentUse if (forCurrentUser) { - basePath = Utils.GetUserConfigurationDirectory(); + basePath = Platform.ConfigDirectory; } else { diff --git a/test/powershell/engine/Basic/Telemetry.Tests.ps1 b/test/powershell/engine/Basic/Telemetry.Tests.ps1 index 0bc233bb746..6b5a68dd03b 100644 --- a/test/powershell/engine/Basic/Telemetry.Tests.ps1 +++ b/test/powershell/engine/Basic/Telemetry.Tests.ps1 @@ -9,7 +9,7 @@ Describe "Telemetry for shell startup" -Tag CI { BeforeAll { # if the telemetry file exists, move it out of the way # the member is internal, but we can retrieve it via reflection - $cacheDir = [System.Management.Automation.Platform].GetMember("CacheDirectory","NonPublic,Static").GetMethod.Invoke($null, $null) + $cacheDir = [System.Management.Automation.Platform].GetField("CacheDirectory","NonPublic,Static").GetValue($null) $uuidPath = Join-Path -Path $cacheDir -ChildPath telemetry.uuid $uuidFileExists = Test-Path -Path $uuidPath if ( $uuidFileExists ) { diff --git a/test/xUnit/csharp/test_PSConfiguration.cs b/test/xUnit/csharp/test_PSConfiguration.cs index 06e1b3b05de..400c0c97b95 100644 --- a/test/xUnit/csharp/test_PSConfiguration.cs +++ b/test/xUnit/csharp/test_PSConfiguration.cs @@ -37,7 +37,7 @@ public class PowerShellPolicyFixture : IDisposable public PowerShellPolicyFixture() { systemWideConfigDirectory = Utils.DefaultPowerShellAppBase; - currentUserConfigDirectory = Utils.GetUserConfigurationDirectory(); + currentUserConfigDirectory = Platform.ConfigDirectory; if (!Directory.Exists(currentUserConfigDirectory)) {