| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Amusoft.DotnetNew.Tests.Diagnostics; |
| | 8 | | using Amusoft.DotnetNew.Tests.Interfaces; |
| | 9 | | using Amusoft.DotnetNew.Tests.Scopes; |
| | 10 | |
|
| | 11 | | namespace Amusoft.DotnetNew.Tests.Templating; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Temporary directory |
| | 15 | | /// </summary> |
| | 16 | | internal class TempDirectory : ITempDirectory |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Constructor |
| | 20 | | /// </summary> |
| 7 | 21 | | internal TempDirectory() |
| | 22 | | { |
| 7 | 23 | | var directory = System.IO.Path.Combine( |
| 7 | 24 | | System.IO.Path.GetTempPath(), |
| 7 | 25 | | Guid.NewGuid().ToString("N") |
| 7 | 26 | | ).TrimEnd(System.IO.Path.DirectorySeparatorChar); |
| | 27 | |
|
| 7 | 28 | | Path = new PathSource(directory); |
| 7 | 29 | | Directory.CreateDirectory(Path.Directory.OriginalPath); |
| 7 | 30 | | LoggingScope.TryAddResult(new TextResult($"Created temp directory at {Path.Directory.OriginalPath}")); |
| 7 | 31 | | } |
| | 32 | |
|
| 36 | 33 | | public IPathSource Path { get; } |
| | 34 | |
|
| | 35 | | private bool _disposed; |
| | 36 | | /// <summary> |
| | 37 | | /// Dispose |
| | 38 | | /// </summary> |
| | 39 | | public void Dispose() |
| | 40 | | { |
| 7 | 41 | | if (_disposed) |
| 1 | 42 | | return; |
| 6 | 43 | | _disposed = true; |
| | 44 | |
|
| 6 | 45 | | Directory.Delete(Path.Directory.OriginalPath, true); |
| 6 | 46 | | LoggingScope.TryAddResult(new TextResult($"Deleting temp directory {Path.Directory.OriginalPath}")); |
| 6 | 47 | | GC.SuppressFinalize(this); |
| 6 | 48 | | } |
| | 49 | |
|
| | 50 | | public async Task<string> GetFileContentAsync(string relativePath, CancellationToken cancellationToken) |
| | 51 | | { |
| 1 | 52 | | return await File.ReadAllTextAsync(Path.PathTranslator.GetAbsolutePath(relativePath).OriginalPath, cancellationToken |
| 1 | 53 | | } |
| | 54 | |
|
| | 55 | | public IEnumerable<string> GetRelativePaths() |
| | 56 | | { |
| 1 | 57 | | return Directory.EnumerateFiles(Path.Directory.OriginalPath, "*", SearchOption.AllDirectories) |
| 2 | 58 | | .Select(d => Path.PathTranslator.GetRelativePath(d).VirtualPath); |
| | 59 | | } |
| | 60 | | } |