From f2cc74e9448d57f277547208bc6e7e2c67a41ddc Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 28 Jun 2019 10:29:56 -0700 Subject: [PATCH 1/6] minor change --- .../engine/PSVersionInfo.cs | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index 7140c3e243c..618dded4d8b 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -4,10 +4,9 @@ using System.Collections; using System.Diagnostics; using System.Globalization; -using System.Management.Automation.Internal; -using System.Reflection; using System.Text; using System.Text.RegularExpressions; +using System.Threading; using Microsoft.Win32; @@ -41,7 +40,7 @@ public class PSVersionInfo internal const string PSOSName = "OS"; internal const string SerializationVersionName = "SerializationVersion"; internal const string WSManStackVersionName = "WSManStackVersion"; - private static PSVersionHashTable s_psVersionTable = null; + private static readonly PSVersionHashTable s_psVersionTable; /// /// A constant to track current PowerShell Version. @@ -54,14 +53,15 @@ public class PSVersionInfo /// For each later release of PowerShell, this constant needs to /// be updated to reflect the right version. /// - private static Version s_psV1Version = new Version(1, 0); - private static Version s_psV2Version = new Version(2, 0); - private static Version s_psV3Version = new Version(3, 0); - private static Version s_psV4Version = new Version(4, 0); - private static Version s_psV5Version = new Version(5, 0); - private static Version s_psV51Version = new Version(5, 1, NTVerpVars.PRODUCTBUILD, NTVerpVars.PRODUCTBUILD_QFE); - private static SemanticVersion s_psV6Version = new SemanticVersion(6, 0, 0, null, null); - private static SemanticVersion s_psV7Version; + private static readonly Version s_psV1Version = new Version(1, 0); + private static readonly Version s_psV2Version = new Version(2, 0); + private static readonly Version s_psV3Version = new Version(3, 0); + private static readonly Version s_psV4Version = new Version(4, 0); + private static readonly Version s_psV5Version = new Version(5, 0); + private static readonly Version s_psV51Version = new Version(5, 1, NTVerpVars.PRODUCTBUILD, NTVerpVars.PRODUCTBUILD_QFE); + private static readonly SemanticVersion s_psV6Version = new SemanticVersion(6, 0, 0, null, null); + private static readonly SemanticVersion s_psV7Version; + private static Version s_psVersionCache; /// /// A constant to track current PowerShell Edition. @@ -122,7 +122,7 @@ internal static Hashtable GetPSVersionTableForDownLevel() { var result = (Hashtable)s_psVersionTable.Clone(); // Downlevel systems don't support SemanticVersion, but Version is most likely good enough anyway. - result[PSVersionInfo.PSVersionName] = (Version)(SemanticVersion)s_psVersionTable[PSVersionInfo.PSVersionName]; + result[PSVersionInfo.PSVersionName] = PSVersion; return result; } @@ -173,7 +173,14 @@ public static Version PSVersion { get { - return (SemanticVersion)GetPSVersionTable()[PSVersionInfo.PSVersionName]; + if (s_psVersionCache == null) + { + // Ideally, we only do one conversion for 'PSVersion' and use the cached value afterwards. + var version = (Version)(SemanticVersion)s_psVersionTable[PSVersionInfo.PSVersionName]; + Interlocked.CompareExchange(ref s_psVersionCache, version, null); + } + + return s_psVersionCache; } } @@ -181,7 +188,7 @@ internal static string GitCommitId { get { - return (string)GetPSVersionTable()[PSGitCommitIdName]; + return (string)s_psVersionTable[PSGitCommitIdName]; } } @@ -189,7 +196,7 @@ internal static Version[] PSCompatibleVersions { get { - return (Version[])GetPSVersionTable()[PSCompatibleVersionsName]; + return (Version[])s_psVersionTable[PSCompatibleVersionsName]; } } @@ -200,7 +207,7 @@ public static string PSEdition { get { - return (string)GetPSVersionTable()[PSVersionInfo.PSEditionName]; + return (string)s_psVersionTable[PSVersionInfo.PSEditionName]; } } @@ -208,7 +215,7 @@ internal static Version SerializationVersion { get { - return (Version)GetPSVersionTable()[SerializationVersionName]; + return (Version)s_psVersionTable[SerializationVersionName]; } } From cbd432ba6c403459a1ef3f605f2d1d24ece6f772 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 28 Jun 2019 13:20:30 -0700 Subject: [PATCH 2/6] Move s_psVersionCache to static constructor --- .../engine/PSVersionInfo.cs | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index 618dded4d8b..1a11a7abbba 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -60,8 +60,8 @@ public class PSVersionInfo private static readonly Version s_psV5Version = new Version(5, 0); private static readonly Version s_psV51Version = new Version(5, 1, NTVerpVars.PRODUCTBUILD, NTVerpVars.PRODUCTBUILD_QFE); private static readonly SemanticVersion s_psV6Version = new SemanticVersion(6, 0, 0, null, null); - private static readonly SemanticVersion s_psV7Version; - private static Version s_psVersionCache; + private static readonly SemanticVersion s_psCurrentVersion; + private static readonly Version s_psVersionCache; /// /// A constant to track current PowerShell Edition. @@ -100,12 +100,13 @@ static PSVersionInfo() rawGitCommitId = mainVersion; } - s_psV7Version = new SemanticVersion(mainVersion); + s_psCurrentVersion = new SemanticVersion(mainVersion); + s_psVersionCache = (Version)s_psCurrentVersion; - s_psVersionTable[PSVersionInfo.PSVersionName] = s_psV7Version; + s_psVersionTable[PSVersionInfo.PSVersionName] = s_psCurrentVersion; s_psVersionTable[PSVersionInfo.PSEditionName] = PSEditionValue; s_psVersionTable[PSGitCommitIdName] = rawGitCommitId; - s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version, s_psV7Version }; + s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version, s_psVersionCache }; s_psVersionTable[PSVersionInfo.SerializationVersionName] = new Version(InternalSerializer.DefaultVersion); s_psVersionTable[PSVersionInfo.PSRemotingProtocolVersionName] = RemotingConstants.ProtocolVersion; s_psVersionTable[PSVersionInfo.WSManStackVersionName] = GetWSManStackVersion(); @@ -173,13 +174,6 @@ public static Version PSVersion { get { - if (s_psVersionCache == null) - { - // Ideally, we only do one conversion for 'PSVersion' and use the cached value afterwards. - var version = (Version)(SemanticVersion)s_psVersionTable[PSVersionInfo.PSVersionName]; - Interlocked.CompareExchange(ref s_psVersionCache, version, null); - } - return s_psVersionCache; } } @@ -279,9 +273,9 @@ internal static string FeatureVersionString internal static bool IsValidPSVersion(Version version) { - if (version.Major == s_psV7Version.Major) + if (version.Major == s_psCurrentVersion.Major) { - return version.Minor == s_psV7Version.Minor; + return version.Minor == s_psCurrentVersion.Minor; } if (version.Major == s_psV6Version.Major) @@ -334,9 +328,9 @@ internal static SemanticVersion PSV6Version get { return s_psV6Version; } } - internal static SemanticVersion PSV7Version + internal static SemanticVersion PSCurrentVersion { - get { return s_psV7Version; } + get { return s_psCurrentVersion; } } #endregion From 6b7b43d5a6c88284273b6b127b3b3177bd2f3b30 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 28 Jun 2019 13:22:04 -0700 Subject: [PATCH 3/6] Remove unneeded using --- src/System.Management.Automation/engine/PSVersionInfo.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index 1a11a7abbba..90339aa9950 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -6,7 +6,6 @@ using System.Globalization; using System.Text; using System.Text.RegularExpressions; -using System.Threading; using Microsoft.Win32; From 8532a5ce5a2ce8b88ef2020702415a54fb346ee8 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 28 Jun 2019 13:23:54 -0700 Subject: [PATCH 4/6] Minor change --- src/System.Management.Automation/engine/PSVersionInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index 90339aa9950..55142a56f92 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -122,7 +122,7 @@ internal static Hashtable GetPSVersionTableForDownLevel() { var result = (Hashtable)s_psVersionTable.Clone(); // Downlevel systems don't support SemanticVersion, but Version is most likely good enough anyway. - result[PSVersionInfo.PSVersionName] = PSVersion; + result[PSVersionInfo.PSVersionName] = s_psVersionCache; return result; } From 25d88b48eb0f95e4e8be1db2018d3be454e47e1c Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 1 Jul 2019 08:45:45 -0700 Subject: [PATCH 5/6] Rename members --- .../engine/PSVersionInfo.cs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index 55142a56f92..edc1e727e7e 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -59,8 +59,8 @@ public class PSVersionInfo private static readonly Version s_psV5Version = new Version(5, 0); private static readonly Version s_psV51Version = new Version(5, 1, NTVerpVars.PRODUCTBUILD, NTVerpVars.PRODUCTBUILD_QFE); private static readonly SemanticVersion s_psV6Version = new SemanticVersion(6, 0, 0, null, null); - private static readonly SemanticVersion s_psCurrentVersion; - private static readonly Version s_psVersionCache; + private static readonly SemanticVersion s_psSemVersion; + private static readonly Version s_psVersion; /// /// A constant to track current PowerShell Edition. @@ -99,13 +99,13 @@ static PSVersionInfo() rawGitCommitId = mainVersion; } - s_psCurrentVersion = new SemanticVersion(mainVersion); - s_psVersionCache = (Version)s_psCurrentVersion; + s_psSemVersion = new SemanticVersion(mainVersion); + s_psVersion = (Version)s_psSemVersion; - s_psVersionTable[PSVersionInfo.PSVersionName] = s_psCurrentVersion; + s_psVersionTable[PSVersionInfo.PSVersionName] = s_psSemVersion; s_psVersionTable[PSVersionInfo.PSEditionName] = PSEditionValue; s_psVersionTable[PSGitCommitIdName] = rawGitCommitId; - s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version, s_psVersionCache }; + s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version, s_psVersion }; s_psVersionTable[PSVersionInfo.SerializationVersionName] = new Version(InternalSerializer.DefaultVersion); s_psVersionTable[PSVersionInfo.PSRemotingProtocolVersionName] = RemotingConstants.ProtocolVersion; s_psVersionTable[PSVersionInfo.WSManStackVersionName] = GetWSManStackVersion(); @@ -122,7 +122,7 @@ internal static Hashtable GetPSVersionTableForDownLevel() { var result = (Hashtable)s_psVersionTable.Clone(); // Downlevel systems don't support SemanticVersion, but Version is most likely good enough anyway. - result[PSVersionInfo.PSVersionName] = s_psVersionCache; + result[PSVersionInfo.PSVersionName] = s_psVersion; return result; } @@ -173,7 +173,7 @@ public static Version PSVersion { get { - return s_psVersionCache; + return s_psVersion; } } @@ -272,9 +272,9 @@ internal static string FeatureVersionString internal static bool IsValidPSVersion(Version version) { - if (version.Major == s_psCurrentVersion.Major) + if (version.Major == s_psSemVersion.Major) { - return version.Minor == s_psCurrentVersion.Minor; + return version.Minor == s_psSemVersion.Minor; } if (version.Major == s_psV6Version.Major) @@ -329,7 +329,7 @@ internal static SemanticVersion PSV6Version internal static SemanticVersion PSCurrentVersion { - get { return s_psCurrentVersion; } + get { return s_psSemVersion; } } #endregion From 4a7db9b09b62def0873f90622c71a6160842a5da Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Wed, 3 Jul 2019 09:44:32 -0700 Subject: [PATCH 6/6] Address Aditya's comment --- src/System.Management.Automation/engine/PSVersionInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index edc1e727e7e..4f49ac8646c 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -58,7 +58,7 @@ public class PSVersionInfo private static readonly Version s_psV4Version = new Version(4, 0); private static readonly Version s_psV5Version = new Version(5, 0); private static readonly Version s_psV51Version = new Version(5, 1, NTVerpVars.PRODUCTBUILD, NTVerpVars.PRODUCTBUILD_QFE); - private static readonly SemanticVersion s_psV6Version = new SemanticVersion(6, 0, 0, null, null); + private static readonly SemanticVersion s_psV6Version = new SemanticVersion(6, 0, 0, preReleaseLabel: null, buildLabel: null); private static readonly SemanticVersion s_psSemVersion; private static readonly Version s_psVersion;