Future-proof conditional compilation

* Invert `#if NET5_0` conditions so that when adding net6.0 target framework, the _new_ APIs are used.
* Use `NET5_0_OR_GREATER` instead of `NET5_0` to ensure consistent behaviour on future target frameworks.
This commit is contained in:
Cédric Luthi 2021-09-29 09:40:58 +02:00 committed by Patrik Svensson
parent 644fb76d61
commit a5716a35e2
5 changed files with 19 additions and 28 deletions

View File

@ -1,17 +1,13 @@
#if NET5_0
using System;
#endif
namespace Spectre.Console.Cli namespace Spectre.Console.Cli
{ {
internal static class StringExtensions internal static class StringExtensions
{ {
internal static int OrdinalIndexOf(this string text, char token) internal static int OrdinalIndexOf(this string text, char token)
{ {
#if NET5_0 #if NETSTANDARD2_0
return text.IndexOf(token, StringComparison.Ordinal);
#else
return text.IndexOf(token); return text.IndexOf(token);
#else
return text.IndexOf(token, System.StringComparison.Ordinal);
#endif #endif
} }
} }

View File

@ -177,19 +177,19 @@ namespace Spectre.Console
internal static string ReplaceExact(this string text, string oldValue, string? newValue) internal static string ReplaceExact(this string text, string oldValue, string? newValue)
{ {
#if NET5_0 #if NETSTANDARD2_0
return text.Replace(oldValue, newValue, StringComparison.Ordinal);
#else
return text.Replace(oldValue, newValue); return text.Replace(oldValue, newValue);
#else
return text.Replace(oldValue, newValue, StringComparison.Ordinal);
#endif #endif
} }
internal static bool ContainsExact(this string text, string value) internal static bool ContainsExact(this string text, string value)
{ {
#if NET5_0 #if NETSTANDARD2_0
return text.Contains(value, StringComparison.Ordinal);
#else
return text.Contains(value); return text.Contains(value);
#else
return text.Contains(value, StringComparison.Ordinal);
#endif #endif
} }
} }

View File

@ -32,10 +32,10 @@ namespace Spectre.Console
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetLinkHashCode(string link) private static int GetLinkHashCode(string link)
{ {
#if NET5_0 #if NETSTANDARD2_0
return link.GetHashCode(StringComparison.Ordinal);
#else
return link.GetHashCode(); return link.GetHashCode();
#else
return link.GetHashCode(StringComparison.Ordinal);
#endif #endif
} }
} }

View File

@ -1,10 +1,6 @@
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
#if !NET5_0
using System.Text.RegularExpressions;
#endif
namespace Spectre.Console namespace Spectre.Console
{ {
internal static class ColorSystemDetector internal static class ColorSystemDetector
@ -61,7 +57,6 @@ namespace Spectre.Console
return ColorSystem.EightBit; return ColorSystem.EightBit;
} }
// See https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/5.0/environment-osversion-returns-correct-version
private static bool GetWindowsVersionInformation(out int major, out int build) private static bool GetWindowsVersionInformation(out int major, out int build)
{ {
major = 0; major = 0;
@ -72,15 +67,15 @@ namespace Spectre.Console
return false; return false;
} }
#if NET5_0 #if NET5_0_OR_GREATER
// The reason we're not always using this, is because it // The reason we're not always using this, is because it will return wrong values on other runtimes than .NET 5+
// will return wrong values on other runtimes than net5.0 // See https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/5.0/environment-osversion-returns-correct-version
var version = Environment.OSVersion.Version; var version = Environment.OSVersion.Version;
major = version.Major; major = version.Major;
build = version.Build; build = version.Build;
return true; return true;
#else #else
var regex = new Regex("Microsoft Windows (?'major'[0-9]*).(?'minor'[0-9]*).(?'build'[0-9]*)\\s*$"); var regex = new System.Text.RegularExpressions.Regex("Microsoft Windows (?'major'[0-9]*).(?'minor'[0-9]*).(?'build'[0-9]*)\\s*$");
var match = regex.Match(RuntimeInformation.OSDescription); var match = regex.Match(RuntimeInformation.OSDescription);
if (match.Success && int.TryParse(match.Groups["major"].Value, out major)) if (match.Success && int.TryParse(match.Groups["major"].Value, out major))
{ {

View File

@ -165,10 +165,10 @@ namespace Spectre.Console
{ {
int? GetLinkHashCode() int? GetLinkHashCode()
{ {
#if NET5_0 #if NETSTANDARD2_0
return Link?.GetHashCode(StringComparison.Ordinal);
#else
return Link?.GetHashCode(); return Link?.GetHashCode();
#else
return Link?.GetHashCode(StringComparison.Ordinal);
#endif #endif
} }