|
| 1 | +#if NETFRAMEWORK |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace JD.Efcpt.Build.Tasks.Compatibility; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Provides polyfills for APIs not available in .NET Framework 4.7.2. |
| 10 | +/// </summary> |
| 11 | +internal static class NetFrameworkPolyfills |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Throws ArgumentNullException if argument is null. |
| 15 | + /// Polyfill for ArgumentNullException.ThrowIfNull (introduced in .NET 6). |
| 16 | + /// </summary> |
| 17 | + public static void ThrowIfNull(object argument, string paramName = null) |
| 18 | + { |
| 19 | + if (argument is null) |
| 20 | + throw new ArgumentNullException(paramName); |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Throws ArgumentException if argument is null or whitespace. |
| 25 | + /// Polyfill for ArgumentException.ThrowIfNullOrWhiteSpace (introduced in .NET 7). |
| 26 | + /// </summary> |
| 27 | + public static void ThrowIfNullOrWhiteSpace(string argument, string paramName = null) |
| 28 | + { |
| 29 | + if (string.IsNullOrWhiteSpace(argument)) |
| 30 | + throw new ArgumentException("Value cannot be null or whitespace.", paramName); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Gets a relative path from one path to another. |
| 35 | + /// Polyfill for Path.GetRelativePath (introduced in .NET Standard 2.1). |
| 36 | + /// </summary> |
| 37 | + public static string GetRelativePath(string relativeTo, string path) |
| 38 | + { |
| 39 | + if (string.IsNullOrEmpty(relativeTo)) |
| 40 | + throw new ArgumentNullException(nameof(relativeTo)); |
| 41 | + if (string.IsNullOrEmpty(path)) |
| 42 | + throw new ArgumentNullException(nameof(path)); |
| 43 | + |
| 44 | + relativeTo = Path.GetFullPath(relativeTo); |
| 45 | + path = Path.GetFullPath(path); |
| 46 | + |
| 47 | + // Ensure relativeTo ends with directory separator |
| 48 | + if (!relativeTo.EndsWith(Path.DirectorySeparatorChar.ToString()) && |
| 49 | + !relativeTo.EndsWith(Path.AltDirectorySeparatorChar.ToString())) |
| 50 | + { |
| 51 | + relativeTo += Path.DirectorySeparatorChar; |
| 52 | + } |
| 53 | + |
| 54 | + var relativeToUri = new Uri(relativeTo); |
| 55 | + var pathUri = new Uri(path); |
| 56 | + |
| 57 | + if (relativeToUri.Scheme != pathUri.Scheme) |
| 58 | + return path; |
| 59 | + |
| 60 | + var relativeUri = relativeToUri.MakeRelativeUri(pathUri); |
| 61 | + var relativePath = Uri.UnescapeDataString(relativeUri.ToString()); |
| 62 | + |
| 63 | + if (string.Equals(pathUri.Scheme, Uri.UriSchemeFile, StringComparison.OrdinalIgnoreCase)) |
| 64 | + { |
| 65 | + relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); |
| 66 | + } |
| 67 | + |
| 68 | + return relativePath; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Converts byte array to hex string. |
| 73 | + /// Polyfill for Convert.ToHexString (introduced in .NET 5). |
| 74 | + /// </summary> |
| 75 | + public static string ToHexString(byte[] bytes) |
| 76 | + { |
| 77 | + var sb = new StringBuilder(bytes.Length * 2); |
| 78 | + foreach (var b in bytes) |
| 79 | + sb.Append(b.ToString("X2")); |
| 80 | + return sb.ToString(); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// <summary> |
| 85 | +/// Polyfill for OperatingSystem static methods (introduced in .NET 5). |
| 86 | +/// </summary> |
| 87 | +internal static class OperatingSystemPolyfill |
| 88 | +{ |
| 89 | + public static bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); |
| 90 | + public static bool IsLinux() => RuntimeInformation.IsOSPlatform(OSPlatform.Linux); |
| 91 | + public static bool IsMacOS() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX); |
| 92 | +} |
| 93 | + |
| 94 | +/// <summary> |
| 95 | +/// Extension methods for KeyValuePair deconstruction (not available in .NET Framework). |
| 96 | +/// </summary> |
| 97 | +internal static class KeyValuePairExtensions |
| 98 | +{ |
| 99 | + public static void Deconstruct<TKey, TValue>( |
| 100 | + this KeyValuePair<TKey, TValue> kvp, |
| 101 | + out TKey key, |
| 102 | + out TValue value) |
| 103 | + { |
| 104 | + key = kvp.Key; |
| 105 | + value = kvp.Value; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/// <summary> |
| 110 | +/// Extension methods for string operations not available in .NET Framework. |
| 111 | +/// </summary> |
| 112 | +internal static class StringPolyfillExtensions |
| 113 | +{ |
| 114 | + /// <summary> |
| 115 | + /// Splits a string using StringSplitOptions. |
| 116 | + /// Polyfill for string.Split(char, StringSplitOptions) overload. |
| 117 | + /// </summary> |
| 118 | + public static string[] Split(this string str, char separator, StringSplitOptions options) |
| 119 | + { |
| 120 | + return str.Split(new[] { separator }, options); |
| 121 | + } |
| 122 | +} |
| 123 | +#endif |
0 commit comments