-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
43 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters