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

Allow retrieval of the "relative file path" from the source link map #699

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions src/SourceLink.Tools.UnitTests/SourceLinkMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,24 @@ public void Entries()
Assert.True(map.TryGetUri(@"C:\a", out var url));
Assert.Equal("http://server/[]", url);

Assert.True(map.TryGetUri(@"C:\a", out url, out var relativeFilePath));
Assert.Equal("http://server/[]", url);
Assert.Equal("", relativeFilePath);

Assert.True(map.TryGetUri(@"C:\a\b\c\d\e", out url));
Assert.Equal("http://server/[/b/c/d/e]", url);

Assert.True(map.TryGetUri(@"C:\a\b\c\d\e", out url, out relativeFilePath));
Assert.Equal("http://server/[/b/c/d/e]", url);
Assert.Equal(@"/b/c/d/e", relativeFilePath);

Assert.True(map.TryGetUri(@"C:\b", out url));
Assert.Equal("http://b", url);

Assert.True(map.TryGetUri(@"C:\b", out url, out relativeFilePath));
Assert.Equal("http://b", url);
Assert.Equal("b", relativeFilePath);

Assert.False(map.TryGetUri(@"C:\b\c", out _));
}

Expand Down
25 changes: 24 additions & 1 deletion src/SourceLink.Tools/SourceLinkMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,24 @@ private static bool TryParseEntry(string key, string value, out Entry entry)
[NotNullWhen(true)]
#endif
out string? uri)
{
return TryGetUri(path, out uri, out _);
}

/// <summary>
/// Maps specified <paramref name="path"/> to the corresponding URL.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
public bool TryGetUri(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add doc comment for relativeFilePath

string path,
#if NETCOREAPP
[NotNullWhen(true)]
#endif
out string? uri,
#if NETCOREAPP
[NotNullWhen(true)]
#endif
out string? relativeFilePath)
{
if (path == null)
{
Expand All @@ -198,6 +216,7 @@ private static bool TryParseEntry(string key, string value, out Entry entry)
if (path.IndexOf('*') >= 0)
{
uri = null;
relativeFilePath = null;
return false;
}

Expand All @@ -209,20 +228,24 @@ private static bool TryParseEntry(string key, string value, out Entry entry)
{
if (path.StartsWith(file.Path, StringComparison.OrdinalIgnoreCase))
{
var escapedPath = string.Join("/", path.Substring(file.Path.Length).Split(new[] { '/', '\\' }).Select(Uri.EscapeDataString));
var relativePath = path.Substring(file.Path.Length).Replace('\\', '/');
var escapedPath = string.Join("/", relativePath.Split('/').Select(Uri.EscapeDataString));
uri = mappedUri.Prefix + escapedPath + mappedUri.Suffix;
relativeFilePath = relativePath;
return true;
}
}
else if (string.Equals(path, file.Path, StringComparison.OrdinalIgnoreCase))
{
Debug.Assert(mappedUri.Suffix.Length == 0);
relativeFilePath = Path.GetFileName(file.Path);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tmat Mac tests don't like this because the test input is windows style paths and I'm assuming Path.GetFileName(...) looks for forward slashes on that OS.

should this just be something like

                    int lastSeparatorIndex = file.Path.LastIndexOfAny(new[] { '\\', '/' });
                    relativeFilePath = lastSeparatorIndex > 0 ? file.Path.Substring(lastSeparatorIndex + 1) : file.Path;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, can't use GetFileName.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set relativeFilePath = null here? Since there is no relative path... the caller can use the file name, if they want to.

uri = mappedUri.Prefix;
return true;
}
}

uri = null;
relativeFilePath = null;
return false;
}
}
Expand Down