From 752f1c31828e5587c0de011c1e50780d14ee44b6 Mon Sep 17 00:00:00 2001 From: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com> Date: Thu, 26 May 2022 20:11:27 +0300 Subject: [PATCH] Rework relative path stuff --- GitHubActionsTestLogger/GitHubWorkflow.cs | 2 +- .../Utils/Extensions/StringExtensions.cs | 6 ---- GitHubActionsTestLogger/Utils/PathEx.cs | 27 +++++++++++++----- GitHubActionsTestLogger/Utils/Polyfills.cs | 28 +++++++++++++++---- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/GitHubActionsTestLogger/GitHubWorkflow.cs b/GitHubActionsTestLogger/GitHubWorkflow.cs index e17176e..3dd4fdc 100644 --- a/GitHubActionsTestLogger/GitHubWorkflow.cs +++ b/GitHubActionsTestLogger/GitHubWorkflow.cs @@ -108,7 +108,7 @@ public partial class GitHubWorkflow return null; var filePathNormalized = PathEx - .GetRelativePath(workspacePath.EnsureEndsWith("/"), filePath) + .GetRelativePath(workspacePath, filePath) .Replace("\\", "/") .Trim('/'); diff --git a/GitHubActionsTestLogger/Utils/Extensions/StringExtensions.cs b/GitHubActionsTestLogger/Utils/Extensions/StringExtensions.cs index 6c5c67d..d0a0660 100644 --- a/GitHubActionsTestLogger/Utils/Extensions/StringExtensions.cs +++ b/GitHubActionsTestLogger/Utils/Extensions/StringExtensions.cs @@ -39,10 +39,4 @@ public static StringBuilder Trim(this StringBuilder builder) return builder; } - - public static string EnsureEndsWith( - this string str, - string end, - StringComparison comparison = StringComparison.Ordinal) => - !str.EndsWith(end, comparison) ? str + end : str; } \ No newline at end of file diff --git a/GitHubActionsTestLogger/Utils/PathEx.cs b/GitHubActionsTestLogger/Utils/PathEx.cs index 1306ff8..4cb3d07 100644 --- a/GitHubActionsTestLogger/Utils/PathEx.cs +++ b/GitHubActionsTestLogger/Utils/PathEx.cs @@ -1,20 +1,33 @@ using System; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; namespace GitHubActionsTestLogger.Utils; internal static class PathEx { + private static readonly StringComparison PathStringComparison = + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + ? StringComparison.OrdinalIgnoreCase + : StringComparison.Ordinal; + // This method exists on .NET5+ but it's impossible to polyfill static // members, so we'll just use this one on all targets. public static string GetRelativePath(string basePath, string path) { - var basePathUri = new Uri(basePath, UriKind.Absolute); - var pathUri = new Uri(path, UriKind.Absolute); + var basePathSegments = basePath.Split(Path.PathSeparator, Path.AltDirectorySeparatorChar); + var pathSegments = path.Split(Path.PathSeparator, Path.AltDirectorySeparatorChar); + + var commonSegments = 0; + for (var i = 0; i < basePathSegments.Length && i < pathSegments.Length; i++) + { + if (!string.Equals(basePathSegments[i], pathSegments[i], PathStringComparison)) + break; + + commonSegments++; + } - return Uri.UnescapeDataString( - basePathUri - .MakeRelativeUri(pathUri) - .ToString() - ); + return string.Join("/", pathSegments.Skip(commonSegments)); } } \ No newline at end of file diff --git a/GitHubActionsTestLogger/Utils/Polyfills.cs b/GitHubActionsTestLogger/Utils/Polyfills.cs index c6aa345..15ab163 100644 --- a/GitHubActionsTestLogger/Utils/Polyfills.cs +++ b/GitHubActionsTestLogger/Utils/Polyfills.cs @@ -1,4 +1,5 @@ // ReSharper disable CheckNamespace +// ReSharper disable InconsistentNaming #if NETSTANDARD2_0 || NET451 using System; @@ -12,12 +13,6 @@ public static bool Contains(this string str, string sub, str.IndexOf(sub, comparison) >= 0; } -internal static class StringBuilderPolyfills -{ - public static StringBuilder AppendJoin(this StringBuilder builder, string separator, IEnumerable source) => - builder.Append(string.Join(separator, source)); -} - namespace System.Collections.Generic { internal static class CollectionPolyfills @@ -26,4 +21,25 @@ public static TValue GetValueOrDefault(this IReadOnlyDictionary osPlatform == OSPlatform.Windows; + } +} #endif \ No newline at end of file