Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Future-proof conditional compilation #563

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/Spectre.Console/Cli/Internal/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#if NET5_0
using System;
#endif

namespace Spectre.Console.Cli
{
internal static class StringExtensions
{
internal static int OrdinalIndexOf(this string text, char token)
{
#if NET5_0
return text.IndexOf(token, StringComparison.Ordinal);
#else
#if NETSTANDARD2_0
return text.IndexOf(token);
#else
return text.IndexOf(token, System.StringComparison.Ordinal);
#endif
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Spectre.Console/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,19 @@ internal static string Repeat(this string text, int count)

internal static string ReplaceExact(this string text, string oldValue, string? newValue)
{
#if NET5_0
return text.Replace(oldValue, newValue, StringComparison.Ordinal);
#else
#if NETSTANDARD2_0
return text.Replace(oldValue, newValue);
#else
return text.Replace(oldValue, newValue, StringComparison.Ordinal);
#endif
}

internal static bool ContainsExact(this string text, string value)
{
#if NET5_0
return text.Contains(value, StringComparison.Ordinal);
#else
#if NETSTANDARD2_0
return text.Contains(value);
#else
return text.Contains(value, StringComparison.Ordinal);
#endif
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Spectre.Console/Internal/Backends/Ansi/AnsiLinkHasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public int GenerateId(string link, string text)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetLinkHashCode(string link)
{
#if NET5_0
return link.GetHashCode(StringComparison.Ordinal);
#else
#if NETSTANDARD2_0
return link.GetHashCode();
#else
return link.GetHashCode(StringComparison.Ordinal);
#endif
}
}
Expand Down
13 changes: 4 additions & 9 deletions src/Spectre.Console/Internal/Colors/ColorSystemDetector.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Runtime.InteropServices;

#if !NET5_0
using System.Text.RegularExpressions;
#endif

namespace Spectre.Console
{
internal static class ColorSystemDetector
Expand Down Expand Up @@ -61,7 +57,6 @@ public static ColorSystem Detect(bool supportsAnsi)
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)
{
major = 0;
Expand All @@ -72,15 +67,15 @@ private static bool GetWindowsVersionInformation(out int major, out int build)
return false;
}

#if NET5_0
// The reason we're not always using this, is because it
// will return wrong values on other runtimes than net5.0
#if NET5_0_OR_GREATER
// The reason we're not always using this, is because it will return wrong values on other runtimes than .NET 5+
// See https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/5.0/environment-osversion-returns-correct-version
var version = Environment.OSVersion.Version;
major = version.Major;
build = version.Build;
return true;
#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);
if (match.Success && int.TryParse(match.Groups["major"].Value, out major))
{
Expand Down
6 changes: 3 additions & 3 deletions src/Spectre.Console/Style.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ public override int GetHashCode()
{
int? GetLinkHashCode()
{
#if NET5_0
return Link?.GetHashCode(StringComparison.Ordinal);
#else
#if NETSTANDARD2_0
return Link?.GetHashCode();
#else
return Link?.GetHashCode(StringComparison.Ordinal);
#endif
}

Expand Down