| | 1 | | using System.Diagnostics.CodeAnalysis; |
| | 2 | | using System.IO; |
| | 3 | | using Amusoft.DotnetNew.Tests.Interfaces; |
| | 4 | | using Amusoft.DotnetNew.Tests.Utility; |
| | 5 | |
|
| | 6 | | namespace Amusoft.DotnetNew.Tests.Templating; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Solution path details |
| | 10 | | /// </summary> |
| | 11 | | public class PathSource : IPathSource |
| | 12 | | { |
| | 13 | | [ExcludeFromCodeCoverage] |
| | 14 | | internal PathSource(string fullPath) |
| | 15 | | { |
| | 16 | | var paths = GetPathDetails(fullPath); |
| | 17 | | File = new CrossPlatformPath(paths.File); |
| | 18 | | Directory = new CrossPlatformPath(paths.Directory); |
| | 19 | | PathTranslator = new PathTranslator(paths.Directory); |
| | 20 | | } |
| | 21 | |
|
| | 22 | | [ExcludeFromCodeCoverage] |
| | 23 | | private (string File, string Directory) GetPathDetails(string fullPath) |
| | 24 | | { |
| | 25 | | if (System.IO.File.Exists(fullPath) || System.IO.File.Exists(fullPath)) |
| | 26 | | { |
| | 27 | | return System.IO.File.GetAttributes(fullPath).HasFlag(FileAttributes.Directory) switch |
| | 28 | | { |
| | 29 | | true => (File: fullPath, Directory: fullPath), |
| | 30 | | false => (File: fullPath, Directory: Path.GetDirectoryName(fullPath)!), |
| | 31 | | }; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | // Path does not really exist, so we can only guess by extension |
| | 35 | | return Path.GetExtension(fullPath) is { Length: > 0 } |
| | 36 | | ? (fullPath, Path.GetDirectoryName(fullPath)!) |
| | 37 | | : (fullPath, fullPath); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Path translator for navigating the file system |
| | 42 | | /// </summary> |
| 24 | 43 | | public PathTranslator PathTranslator { get; } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Path to solution directory |
| | 47 | | /// </summary> |
| 54 | 48 | | public CrossPlatformPath Directory { get; } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Path to solution file |
| | 52 | | /// </summary> |
| 10 | 53 | | public CrossPlatformPath File { get; } |
| | 54 | | } |