Some more code cleanup related to the use of PSVersionInfo (#18231)

This commit is contained in:
Dongbo Wang
2022-10-11 10:24:31 -07:00
committed by GitHub
parent ed1f757728
commit 80d2fb7e47
10 changed files with 29 additions and 143 deletions
@@ -14,15 +14,3 @@ using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo(@"System.Management.Automation.Remoting" + @",PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: InternalsVisibleTo(@"Microsoft.PowerShell.ConsoleHost" + @",PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: InternalsVisibleTo(@"Microsoft.PowerShell.DscSubsystem" + @",PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
namespace System.Management.Automation
{
internal static class NTVerpVars
{
internal const int PRODUCTMAJORVERSION = 10;
internal const int PRODUCTMINORVERSION = 0;
internal const int PRODUCTBUILD = 10032;
internal const int PRODUCTBUILD_QFE = 0;
internal const int PACKAGEBUILD_QFE = 814;
}
}
@@ -421,7 +421,7 @@ namespace System.Management.Automation
// in single shell mode
if (requiresPSVersion != null)
{
if (!Utils.IsPSVersionSupported(requiresPSVersion))
if (!PSVersionInfo.IsValidPSVersion(requiresPSVersion))
{
ScriptRequiresException scriptRequiresException =
new ScriptRequiresException(
@@ -458,7 +458,7 @@ namespace System.Management.Automation
var message = StringUtil.Format(DiscoveryExceptions.RequiresPSEditionNotCompatible,
scriptInfo.Name,
specifiedEditionsString,
PSVersionInfo.PSEdition);
PSVersionInfo.PSEditionValue);
var ex = new RuntimeException(message);
ex.SetErrorId("ScriptRequiresUnmatchedPSEdition");
ex.SetTargetObject(scriptInfo.Name);
@@ -3732,7 +3732,7 @@ namespace System.Management.Automation.Runspaces
// implementation and should be refactored.
PSSnapInInfo newPSSnapIn = PSSnapInReader.Read("2", name);
if (!Utils.IsPSVersionSupported(newPSSnapIn.PSVersion.ToString()))
if (!PSVersionInfo.IsValidPSVersion(newPSSnapIn.PSVersion))
{
s_PSSnapInTracer.TraceError("MshSnapin {0} and current monad engine's versions don't match.", name);
@@ -2654,7 +2654,7 @@ namespace Microsoft.PowerShell.Commands
protected override void Validate(object arguments, EngineIntrinsics engineIntrinsics)
{
Version version = arguments as Version;
if (version == null || !PSVersionInfo.IsValidPSVersion(version))
if (!PSVersionInfo.IsValidPSVersion(version))
{
// No conversion succeeded so throw and exception...
throw new ValidationMetadataException(
@@ -58,7 +58,7 @@ namespace System.Management.Automation
private static readonly Version s_psV3Version = new(3, 0);
private static readonly Version s_psV4Version = new(4, 0);
private static readonly Version s_psV5Version = new(5, 0);
private static readonly Version s_psV51Version = new(5, 1, NTVerpVars.PRODUCTBUILD, NTVerpVars.PRODUCTBUILD_QFE);
private static readonly Version s_psV51Version = new(5, 1);
private static readonly Version s_psV6Version = new(6, 0, 0);
private static readonly Version s_psV61Version = new(6, 1, 0);
private static readonly Version s_psV62Version = new(6, 2, 0);
@@ -193,14 +193,6 @@ namespace System.Management.Automation
}
}
internal static Version[] PSCompatibleVersions
{
get
{
return (Version[])s_psVersionTable[PSCompatibleVersionsName];
}
}
/// <summary>
/// Gets the edition of PowerShell.
/// </summary>
@@ -208,7 +200,7 @@ namespace System.Management.Automation
{
get
{
return (string)s_psVersionTable[PSVersionInfo.PSEditionName];
return PSEditionValue;
}
}
@@ -220,21 +212,6 @@ namespace System.Management.Automation
}
}
/// <summary>
/// </summary>
/// <remarks>
/// For 2.0 PowerShell, we still use "1" as the registry version key.
/// For >=3.0 PowerShell, we still use "1" as the registry version key for
/// Snapin and Custom shell lookup/discovery.
/// </remarks>
internal static string RegistryVersion1Key
{
get
{
return "1";
}
}
/// <summary>
/// </summary>
/// <remarks>
@@ -270,76 +247,32 @@ namespace System.Management.Automation
return null;
}
internal static string FeatureVersionString
{
get
{
return string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}.{1}", PSVersionInfo.PSVersion.Major, PSVersionInfo.PSVersion.Minor);
}
}
internal static bool IsValidPSVersion(Version version)
{
if (version.Major == s_psSemVersion.Major)
if (version is null)
{
return version.Minor == s_psSemVersion.Minor;
return false;
}
if (version.Major == s_psV6Version.Major)
int minor = version.Minor;
switch (version.Major)
{
return version.Minor == s_psV6Version.Minor;
}
if (version.Major == s_psV5Version.Major)
{
return (version.Minor == s_psV5Version.Minor || version.Minor == s_psV51Version.Minor);
}
if (version.Major == s_psV4Version.Major)
{
return (version.Minor == s_psV4Version.Minor);
}
else if (version.Major == s_psV3Version.Major)
{
return version.Minor == s_psV3Version.Minor;
}
else if (version.Major == s_psV2Version.Major)
{
return version.Minor == s_psV2Version.Minor;
}
else if (version.Major == s_psV1Version.Major)
{
return version.Minor == s_psV1Version.Minor;
case 1:
case 2:
case 3:
case 4:
return minor == 0;
case 5:
return minor == 0 || minor == 1;
case 6:
return minor >= 0 && minor <= 2;
case 7:
return minor >= 0 && minor <= s_psVersion.Minor;
}
return false;
}
internal static Version PSV4Version
{
get { return s_psV4Version; }
}
internal static Version PSV5Version
{
get { return s_psV5Version; }
}
internal static Version PSV51Version
{
get { return s_psV51Version; }
}
internal static Version PSV6Version
{
get { return s_psV6Version; }
}
internal static Version PSV7Version
{
get { return s_psV7Version; }
}
internal static SemanticVersion PSCurrentVersion
{
get { return s_psSemVersion; }
@@ -637,41 +637,6 @@ namespace System.Management.Automation
return null;
}
/// <summary>
/// Checks whether current monad session supports version specified
/// by ver.
/// </summary>
/// <param name="ver">Version to check.</param>
/// <returns>True if supported, false otherwise.</returns>
internal static bool IsPSVersionSupported(string ver)
{
// Convert version to supported format ie., x.x
Version inputVersion = StringToVersion(ver);
return IsPSVersionSupported(inputVersion);
}
/// <summary>
/// Checks whether current monad session supports version specified
/// by checkVersion.
/// </summary>
/// <param name="checkVersion">Version to check.</param>
/// <returns>True if supported, false otherwise.</returns>
internal static bool IsPSVersionSupported(Version checkVersion)
{
if (checkVersion == null)
{
return false;
}
foreach (Version compatibleVersion in PSVersionInfo.PSCompatibleVersions)
{
if (checkVersion.Major == compatibleVersion.Major && checkVersion.Minor <= compatibleVersion.Minor)
return true;
}
return false;
}
/// <summary>
/// Checks whether current PowerShell session supports edition specified
/// by checkEdition.
@@ -680,7 +645,7 @@ namespace System.Management.Automation
/// <returns>True if supported, false otherwise.</returns>
internal static bool IsPSEditionSupported(string checkEdition)
{
return PSVersionInfo.PSEdition.Equals(checkEdition, StringComparison.OrdinalIgnoreCase);
return PSVersionInfo.PSEditionValue.Equals(checkEdition, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
@@ -690,7 +655,7 @@ namespace System.Management.Automation
/// <returns>True if the edition is supported by this runtime, false otherwise.</returns>
internal static bool IsPSEditionSupported(IEnumerable<string> editions)
{
string currentPSEdition = PSVersionInfo.PSEdition;
string currentPSEdition = PSVersionInfo.PSEditionValue;
foreach (string edition in editions)
{
if (currentPSEdition.Equals(edition, StringComparison.OrdinalIgnoreCase))
@@ -2780,8 +2780,8 @@ namespace System.Management.Automation
{
throw new PSInvalidOperationException(
// The remote session to which you are connected does not support remote debugging.
// You must connect to a remote computer that is running PowerShell {0} or greater.
StringUtil.Format(RemotingErrorIdStrings.RemoteDebuggingEndpointVersionError, PSVersionInfo.PSV4Version),
// You must connect to a remote computer that is running PowerShell 4.0 or greater.
RemotingErrorIdStrings.RemoteDebuggingEndpointVersionError,
null,
"RemoteDebugger:RemoteDebuggingNotSupported",
ErrorCategory.NotImplemented,
@@ -43,14 +43,14 @@ namespace System.Management.Automation.Remoting
}
// Commands added in v5
if (PSVersion.Major >= PSVersionInfo.PSV5Version.Major)
if (PSVersion.Major >= 5)
{
_supportedCommands.Add(RemoteDebuggingCommands.SetDebuggerStepMode);
_supportedCommands.Add(RemoteDebuggingCommands.SetUnhandledBreakpointMode);
}
// Commands added in v7
if (PSVersion.Major >= PSVersionInfo.PSV7Version.Major)
if (PSVersion.Major >= 7)
{
_supportedCommands.Add(RemoteDebuggingCommands.GetBreakpoint);
_supportedCommands.Add(RemoteDebuggingCommands.SetBreakpoint);
@@ -472,7 +472,7 @@ namespace System.Management.Automation
// Remote debugger is created only when client version is PSVersion (4.0)
// or greater, and remote session supports debugging.
if ((_driverNestedInvoker != null) &&
(_clientPSVersion != null && _clientPSVersion >= PSVersionInfo.PSV4Version) &&
(_clientPSVersion != null && _clientPSVersion.Major >= 4) &&
(runspace != null && runspace.Debugger != null))
{
_serverRemoteDebugger = new ServerRemoteDebugger(this, runspace, runspace.Debugger);
@@ -1360,7 +1360,7 @@ All WinRM sessions connected to PowerShell session configurations, such as Micro
<value>The remote session command is currently stopped in the debugger. Use the Enter-PSSession cmdlet to connect interactively to the remote session and automatically enter into the console debugger.</value>
</data>
<data name="RemoteDebuggingEndpointVersionError" xml:space="preserve">
<value>The remote session to which you are connected does not support remote debugging. You must connect to a remote computer that is running PowerShell {0} or greater.</value>
<value>The remote session to which you are connected does not support remote debugging. You must connect to a remote computer that is running PowerShell 4.0 or greater.</value>
</data>
<data name="ICMInvalidSessionState" xml:space="preserve">
<value>Because the session state for session {0}, {1}, {2} is not equal to Open, you cannot run a command in the session. The session state is {3}.</value>