mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Some code cleanup work (#10698)
* Move Utils.GetUserConfigurationDirectory to Platform.ConfigDirectory * Unify the use of Platform.ConfigDirectory and Platform.CacheDirectory
This commit is contained in:
@@ -198,7 +198,10 @@ namespace Microsoft.PowerShell
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@@ -12,8 +12,7 @@ namespace Microsoft.PowerShell
|
||||
/// to transfer control to Msh console host implementation.
|
||||
/// </summary>
|
||||
|
||||
public static
|
||||
class ConsoleShell
|
||||
public static class ConsoleShell
|
||||
{
|
||||
/// <summary>Entry point in to ConsoleShell. This method is called by main of minishell.</summary>
|
||||
/// <param name="bannerText">Banner text to be displayed by ConsoleHost.</param>
|
||||
|
||||
@@ -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 @@ namespace Microsoft.PowerShell
|
||||
/// <param name="args">
|
||||
/// Command line arguments to the managed MSH
|
||||
/// </param>
|
||||
/// <param name="argc" />
|
||||
/// <param name="argc">
|
||||
/// Length of the passed in argument array.
|
||||
/// </param>
|
||||
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 @@ namespace Microsoft.PowerShell
|
||||
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
|
||||
|
||||
@@ -140,22 +140,15 @@ namespace System.Management.Automation
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the location for the various caches.
|
||||
/// </summary>
|
||||
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 @@ namespace System.Management.Automation
|
||||
|
||||
return xdgCacheDefault;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
if (!Directory.Exists(Path.Combine(xdgcachehome, "powershell")))
|
||||
|
||||
@@ -2711,14 +2711,7 @@ namespace System.Management.Automation.Runspaces
|
||||
// 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))
|
||||
|
||||
@@ -1121,11 +1121,7 @@ namespace System.Management.Automation
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ namespace System.Management.Automation.Configuration
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -458,20 +458,6 @@ namespace System.Management.Automation
|
||||
|
||||
private static string[] s_productFolderDirectories;
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the per-user configuration settings directory in a platform agnostic manner.
|
||||
/// </summary>
|
||||
/// <returns>The current user's configuration settings directory.</returns>
|
||||
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)
|
||||
|
||||
@@ -228,7 +228,7 @@ namespace System.Management.Automation
|
||||
|
||||
if (forCurrentUser)
|
||||
{
|
||||
basePath = Utils.GetUserConfigurationDirectory();
|
||||
basePath = Platform.ConfigDirectory;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -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 ) {
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace PSTests.Sequential
|
||||
public PowerShellPolicyFixture()
|
||||
{
|
||||
systemWideConfigDirectory = Utils.DefaultPowerShellAppBase;
|
||||
currentUserConfigDirectory = Utils.GetUserConfigurationDirectory();
|
||||
currentUserConfigDirectory = Platform.ConfigDirectory;
|
||||
|
||||
if (!Directory.Exists(currentUserConfigDirectory))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user