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

Add MakeRelative alias to DirectoryPath and FilePath #2838

Closed
augustoproiete opened this issue Aug 9, 2020 · 4 comments · Fixed by #2923
Closed

Add MakeRelative alias to DirectoryPath and FilePath #2838

augustoproiete opened this issue Aug 9, 2020 · 4 comments · Fixed by #2923
Milestone

Comments

@augustoproiete
Copy link
Member

What You Are Seeing?

MakeAbsolute is a useful alias to transform a relative path into an absolute path based on the current WorkingDirectory, but there's not an equivalent alias for doing the opposite, that is, transform an absolute path into a relative path.

What is Expected?

I'd like Cake to provide a MakeRelative alias that transforms an absolute path into a relative path, which is useful when printing messages to the console log, for ease of reading.

e.g.

// Environment.WorkingDirectory => "C:/Working/"

var relativePath = MakeRelative("C:/Working/build/MyApp.csproj");
// relativePath should be "./build/MyApp.csproj"

What version of Cake are you using?

0.38.4


ps: I'm happy to send a PR that adds this feature.

@devlead
Copy link
Member

devlead commented Aug 9, 2020

Might be a good edition
Example of code making path relative

private string GetRelativePath(DirectoryPath root, Path path)
{
if (path != null && !path.FullPath.StartsWith(root.FullPath, _comparison))
{
const string format = "Path '{0}' is not relative to root path '{1}'.";
throw new CakeException(string.Format(CultureInfo.InvariantCulture, format, path.FullPath, root.FullPath));
}
return path?.FullPath.Substring(root.FullPath.Length + (root.FullPath.Length > 1 && path.FullPath.Length > root.FullPath.Length ? 1 : 0));
}

A MakeRelative alias should take rootPath as the last parameter and be optional, that way it could default to working directory but you could also specify a path.

@patriksvensson
Copy link
Member

@augustoproiete @devlead Better to use the RelativePathResolver which was made for this: https://github.com/cake-build/cake/blob/develop/src/Cake.Core/IO/RelativePathResolver.cs

The method above should probably be rewritten to use that as well...

@devlead
Copy link
Member

devlead commented Dec 9, 2020

Fixed by #2923

@cake-build-bot
Copy link

🎉 This issue has been resolved in version v1.0.0 🎉

The release is available on:

Your GitReleaseManager bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants