This is an automated email from the ASF dual-hosted git repository. nightowl888 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 529aa4d02287cf8322c0ed24a9f0d05a9c9601c7 Author: Shad Storhaug <[email protected]> AuthorDate: Fri Sep 11 08:50:26 2020 +0700 Lucene.Net.Util.Constants: Refactored to use RuntimeInformation on .NET Framework --- src/Lucene.Net.Tests/Util/TestConstants.cs | 4 +- src/Lucene.Net.Tests/Util/TestVersion.cs | 2 +- src/Lucene.Net/Util/Constants.cs | 127 +++++------------------------ 3 files changed, 25 insertions(+), 108 deletions(-) diff --git a/src/Lucene.Net.Tests/Util/TestConstants.cs b/src/Lucene.Net.Tests/Util/TestConstants.cs index 8a98dbc..7372d7c 100644 --- a/src/Lucene.Net.Tests/Util/TestConstants.cs +++ b/src/Lucene.Net.Tests/Util/TestConstants.cs @@ -26,13 +26,13 @@ namespace Lucene.Net.Util [TestFixture] public class TestConstants : LuceneTestCase { - private string VersionDetails => " (LUCENE_MAIN_VERSION=" + Constants.LUCENE_MAIN_VERSION + ", LUCENE_MAIN_VERSION(without alpha/beta)=" + Constants.MainVersionWithoutAlphaBeta() + ", LUCENE_VERSION=" + Constants.LUCENE_VERSION + ")"; + private string VersionDetails => " (LUCENE_MAIN_VERSION=" + Constants.LUCENE_MAIN_VERSION + ", LUCENE_MAIN_VERSION(without alpha/beta)=" + Constants.MainVersionWithoutAlphaBeta + ", LUCENE_VERSION=" + Constants.LUCENE_VERSION + ")"; [Test] public virtual void TestLuceneMainVersionConstant() { Assert.IsTrue(Regex.IsMatch(Constants.LUCENE_MAIN_VERSION, "\\d+\\.\\d+(|\\.0\\.\\d+)", RegexOptions.IgnoreCase), "LUCENE_MAIN_VERSION does not follow pattern: 'x.y' (stable release) or 'x.y.0.z' (alpha/beta version)" + VersionDetails); - Assert.IsTrue(Constants.LUCENE_VERSION.StartsWith(Constants.MainVersionWithoutAlphaBeta(), StringComparison.Ordinal), "LUCENE_VERSION does not start with LUCENE_MAIN_VERSION (without alpha/beta marker)" + VersionDetails); + Assert.IsTrue(Constants.LUCENE_VERSION.StartsWith(Constants.MainVersionWithoutAlphaBeta, StringComparison.Ordinal), "LUCENE_VERSION does not start with LUCENE_MAIN_VERSION (without alpha/beta marker)" + VersionDetails); } [Test] diff --git a/src/Lucene.Net.Tests/Util/TestVersion.cs b/src/Lucene.Net.Tests/Util/TestVersion.cs index 00642ad..4283d2e 100644 --- a/src/Lucene.Net.Tests/Util/TestVersion.cs +++ b/src/Lucene.Net.Tests/Util/TestVersion.cs @@ -74,7 +74,7 @@ namespace Lucene.Net.Util { LuceneVersion[] values = Enum.GetValues(typeof(LuceneVersion)).Cast<LuceneVersion>().ToArray(); Assert.IsTrue(values.Length >= 2); - string mainVersionWithoutAlphaBeta = Constants.MainVersionWithoutAlphaBeta(); + string mainVersionWithoutAlphaBeta = Constants.MainVersionWithoutAlphaBeta; LuceneVersion mainVersionParsed = LuceneVersionExtensions.ParseLeniently(mainVersionWithoutAlphaBeta); Assert.AreEqual(mainVersionParsed, values[values.Length - 2], "Constant one before last must be the same as the parsed LUCENE_MAIN_VERSION (without alpha/beta) constant: " + mainVersionWithoutAlphaBeta); } diff --git a/src/Lucene.Net/Util/Constants.cs b/src/Lucene.Net/Util/Constants.cs index 2464c93..a4c4db4 100644 --- a/src/Lucene.Net/Util/Constants.cs +++ b/src/Lucene.Net/Util/Constants.cs @@ -1,7 +1,6 @@ using System; -#if NETSTANDARD using System.Runtime.InteropServices; -#else +#if !NETSTANDARD using Microsoft.Win32; #endif using System.Text.RegularExpressions; @@ -31,7 +30,7 @@ namespace Lucene.Net.Util public static class Constants // LUCENENET specific - made static because all members are static and constructor in Lucene was private { // LUCENENET NOTE: IMPORTANT - this line must be placed before RUNTIME_VERSION so it can be parsed. - private static Regex VERSION = new Regex(@"(\d+\.\d+(?:\.\d+)?(?:\.\d+)?)", RegexOptions.Compiled); + private static readonly Regex VERSION = new Regex(@"(\d+\.\d+(?:\.\d+)?(?:\.\d+)?)", RegexOptions.Compiled); // LUCENENET specific - renamed JAVA_VERSION to RUNTIME_VERSION and moved below OS constants because loading is dependent upon OS @@ -44,112 +43,36 @@ namespace Lucene.Net.Util //public static readonly string JVM_VERSION = GetEnvironmentVariable("java.vm.version", ""); //public static readonly string JVM_NAME = GetEnvironmentVariable("java.vm.name", ""); -#if NETSTANDARD /// <summary> /// The value of <see cref="RuntimeInformation.OSDescription"/>, excluding the version number.</summary> -#else - /// <summary> - /// The value of System.Environment.OSVersion.VersionString, excluding the version number.</summary> -#endif - public static readonly string OS_NAME = LoadOSName(); - - private static string LoadOSName() // LUCENENET: Avoid static constructors (see https://github.com/apache/lucenenet/pull/224#issuecomment-469284006) - { -#if NETSTANDARD - return VERSION.Replace(RuntimeInformation.OSDescription, string.Empty).Trim(); -#else - return VERSION.Replace(Environment.OSVersion.VersionString, string.Empty).Trim(); -#endif - } + public static readonly string OS_NAME = VERSION.Replace(RuntimeInformation.OSDescription, string.Empty).Trim(); /// <summary> /// True iff running on Linux. </summary> - public static readonly bool LINUX = LoadLinux(); - private static bool LoadLinux() - { -#if NETSTANDARD - return RuntimeInformation.IsOSPlatform(OSPlatform.Linux); -#else - // 128 is mono's old platform tag for Unix. - // Reference: https://stackoverflow.com/a/5117005 - int id = (int)Environment.OSVersion.Platform; - return id == 4 || id == 128; -#endif - } + public static readonly bool LINUX = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); /// <summary> /// True iff running on Windows. </summary> - public static readonly bool WINDOWS = LoadWindows(); - private static bool LoadWindows() - { -#if NETSTANDARD - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); -#else - PlatformID pid = Environment.OSVersion.Platform; - return pid == PlatformID.Win32NT || pid == PlatformID.Win32Windows; -#endif - } + public static readonly bool WINDOWS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); /// <summary> /// True iff running on SunOS. </summary> - public static readonly bool SUN_OS = LoadSunOS(); - private static bool LoadSunOS() - { -#if NETSTANDARD - return RuntimeInformation.IsOSPlatform(OSPlatform.Create("SunOS")); -#else - return false; // Not possible -#endif - } + public static readonly bool SUN_OS = RuntimeInformation.IsOSPlatform(OSPlatform.Create("SunOS")); /// <summary> /// True iff running on Mac OS X </summary> - public static readonly bool MAC_OS_X = LoadMacOSX(); - private static bool LoadMacOSX() // LUCENENET: Avoid static constructors (see https://github.com/apache/lucenenet/pull/224#issuecomment-469284006) - { -#if NETSTANDARD - return RuntimeInformation.IsOSPlatform(OSPlatform.OSX); -#else - // Reference: https://stackoverflow.com/a/5117005 - return Environment.OSVersion.Platform == PlatformID.MacOSX; -#endif - } + public static readonly bool MAC_OS_X = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); /// <summary> /// True iff running on FreeBSD </summary> - public static readonly bool FREE_BSD = LoadFreeBSD(); - private static bool LoadFreeBSD() // LUCENENET: Avoid static constructors (see https://github.com/apache/lucenenet/pull/224#issuecomment-469284006) - { -#if NETSTANDARD - return RuntimeInformation.IsOSPlatform(OSPlatform.Create("FreeBSD")); -#else - return false; // Not possible -#endif - } + public static readonly bool FREE_BSD = RuntimeInformation.IsOSPlatform(OSPlatform.Create("FreeBSD")); + // Possible Values: X86, X64, Arm, Arm64 + public static readonly string OS_ARCH = RuntimeInformation.OSArchitecture.ToString(); - public static readonly string OS_ARCH = LoadOSArch(); - private static string LoadOSArch() // LUCENENET: Avoid static constructors (see https://github.com/apache/lucenenet/pull/224#issuecomment-469284006) - { -#if NETSTANDARD - // Possible Values: X86, X64, Arm, Arm64 - return RuntimeInformation.OSArchitecture.ToString(); -#else - return Environment.Is64BitOperatingSystem ? "X64" : "X86"; -#endif - } - - public static readonly string OS_VERSION = LoadOSVersion(); - private static string LoadOSVersion() // LUCENENET: Avoid static constructors (see https://github.com/apache/lucenenet/pull/224#issuecomment-469284006) - { -#if NETSTANDARD - return ExtractString(RuntimeInformation.OSDescription, VERSION); -#else - return Environment.OSVersion.Version.ToString(); -#endif - } + public static readonly string OS_VERSION = ExtractString(RuntimeInformation.OSDescription, VERSION); #if NETSTANDARD /// <summary> @@ -175,15 +98,7 @@ namespace Lucene.Net.Util #endif } - - //[Obsolete("We are not running on Java for heavens sake")] - //public static readonly bool JRE_IS_MINIMUM_JAVA6 = (bool)new bool?(true); // prevent inlining in foreign class files - - //[Obsolete("We are not running on Java for heavens sake")] - //public static readonly bool JRE_IS_MINIMUM_JAVA7 = (bool)new bool?(true); // prevent inlining in foreign class files - - //[Obsolete("We are not running on Java for heavens sake")] - //public static readonly bool JRE_IS_MINIMUM_JAVA8; + // LUCENENET: Removed JRE fields /// <summary> /// NOTE: This was JRE_IS_64BIT in Lucene @@ -232,17 +147,20 @@ namespace Lucene.Net.Util /// Returns a LUCENE_MAIN_VERSION without any ALPHA/BETA qualifier /// Used by test only! /// </summary> - public static string MainVersionWithoutAlphaBeta() + internal static string MainVersionWithoutAlphaBeta { - string[] parts = MAIN_VERSION_WITHOUT_ALPHA_BETA.Split(LUCENE_MAIN_VERSION); - if (parts.Length == 4 && "0".Equals(parts[2], StringComparison.Ordinal)) + get { - return parts[0] + "." + parts[1]; + string[] parts = MAIN_VERSION_WITHOUT_ALPHA_BETA.Split(LUCENE_MAIN_VERSION); + if (parts.Length == 4 && "0".Equals(parts[2], StringComparison.Ordinal)) + { + return parts[0] + "." + parts[1]; + } + return LUCENE_MAIN_VERSION; } - return LUCENE_MAIN_VERSION; } - private static Regex MAIN_VERSION_WITHOUT_ALPHA_BETA = new Regex("\\.", RegexOptions.Compiled); + private static readonly Regex MAIN_VERSION_WITHOUT_ALPHA_BETA = new Regex("\\.", RegexOptions.Compiled); #if !NETSTANDARD @@ -273,7 +191,7 @@ namespace Lucene.Net.Util private static string CheckFor45PlusVersion(int releaseKey) { if (releaseKey >= 460799) - return "4.8 or later"; + return "4.8"; if (releaseKey >= 460798) return "4.7"; if (releaseKey >= 394802) @@ -305,7 +223,6 @@ namespace Lucene.Net.Util #endif - // LUCENENET TODO: Move to Support ? /// <summary> /// Extracts the first group matched with the regex as a new string. /// </summary>
