Skip to content

Commit

Permalink
Rework relative path stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed May 26, 2022
1 parent ef58724 commit 752f1c3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion GitHubActionsTestLogger/GitHubWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public partial class GitHubWorkflow
return null;

var filePathNormalized = PathEx
.GetRelativePath(workspacePath.EnsureEndsWith("/"), filePath)
.GetRelativePath(workspacePath, filePath)
.Replace("\\", "/")
.Trim('/');

Expand Down
6 changes: 0 additions & 6 deletions GitHubActionsTestLogger/Utils/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
27 changes: 20 additions & 7 deletions GitHubActionsTestLogger/Utils/PathEx.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
28 changes: 22 additions & 6 deletions GitHubActionsTestLogger/Utils/Polyfills.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming

#if NETSTANDARD2_0 || NET451
using System;
Expand All @@ -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<T>(this StringBuilder builder, string separator, IEnumerable<T> source) =>
builder.Append(string.Join(separator, source));
}

namespace System.Collections.Generic
{
internal static class CollectionPolyfills
Expand All @@ -26,4 +21,25 @@ public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TK
dic.TryGetValue(key!, out var result) ? result! : default!;
}
}
#endif

#if NET451
namespace System.Runtime.InteropServices
{
internal enum OSPlatform
{
Windows,
Linux,
OSX,
FreeBSD,
NetBSD,
Solaris,
Unknown
}

internal static class RuntimeInformation
{
public static bool IsOSPlatform(OSPlatform osPlatform) => osPlatform == OSPlatform.Windows;
}
}
#endif

0 comments on commit 752f1c3

Please sign in to comment.