| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | |
|
| | 4 | | namespace Amusoft.DotnetNew.Tests.Utility; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Utility class to provide pathing details |
| | 8 | | /// </summary> |
| | 9 | | public class PathTranslator |
| | 10 | | { |
| | 11 | | private readonly CrossPlatformPath _referenceDirectory; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Constructor |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="referenceDirectory">absolute path to directory</param> |
| 37 | 17 | | internal PathTranslator(string referenceDirectory) |
| | 18 | | { |
| 37 | 19 | | _referenceDirectory = new CrossPlatformPath(referenceDirectory.TrimEnd(Path.DirectorySeparatorChar)); |
| 37 | 20 | | } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Retrieves the absolute path based on the relative path in relation to the reference path |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="relativePath">e.g. ../a/b/c</param> |
| | 26 | | /// <returns></returns> |
| | 27 | | /// <exception cref="ArgumentException"></exception> |
| | 28 | | public CrossPlatformPath GetAbsolutePath(string relativePath) |
| | 29 | | { |
| 20 | 30 | | if (relativePath.StartsWith("./") || relativePath.StartsWith(".\\")) |
| 1 | 31 | | throw new ArgumentException("Relative paths should not start with the current location pattern or resolution would |
| | 32 | |
|
| 19 | 33 | | var absoluteUri = new Uri(new Uri(_referenceDirectory.VirtualPath + Path.DirectorySeparatorChar, UriKind.Absolute), |
| | 34 | |
|
| 19 | 35 | | return new CrossPlatformPath(absoluteUri.AbsolutePath); |
| | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Retrieves the relative path based on the absolute path in relation to the reference path |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="absolutePath"></param> |
| | 42 | | /// <returns></returns> |
| | 43 | | public CrossPlatformPath GetRelativePath(string absolutePath) |
| | 44 | | { |
| 13 | 45 | | var baseUri = new Uri(_referenceDirectory.VirtualPath.TrimEnd('/') + '/', UriKind.Absolute); |
| 13 | 46 | | var refUri = new Uri(new CrossPlatformPath(absolutePath).VirtualPath.TrimEnd('/') + '/', UriKind.Absolute); |
| 13 | 47 | | var relUri = baseUri.MakeRelativeUri(refUri); |
| 13 | 48 | | return new CrossPlatformPath(relUri.OriginalString.TrimEnd('/')); |
| | 49 | | } |
| | 50 | | } |