| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Runtime.CompilerServices; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Amusoft.DotnetNew.Tests.CLI; |
| | 8 | | using Amusoft.DotnetNew.Tests.Interfaces; |
| | 9 | | using Amusoft.DotnetNew.Tests.Templating; |
| | 10 | |
|
| | 11 | | namespace Amusoft.DotnetNew.Tests.Scaffolding; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// |
| | 15 | | /// </summary> |
| | 16 | | public class Scaffold : IDisposable |
| | 17 | | { |
| | 18 | | private readonly ITempDirectory _tempDirectory; |
| | 19 | | private readonly IDotnetCli _cli; |
| | 20 | |
|
| 9 | 21 | | internal Scaffold(ITempDirectory tempDirectory, IDotnetCli cli) |
| | 22 | | { |
| 9 | 23 | | _tempDirectory = tempDirectory; |
| 9 | 24 | | _cli = cli; |
| 9 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Builds the project at a given path |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="relativePath">relative path of the project/solution you want to build within the scaffold. e.g. src/S |
| | 31 | | /// <param name="arguments">build arguments</param> |
| | 32 | | /// <param name="cancellationToken">cancellation token</param> |
| | 33 | | /// <param name="verbosity"></param> |
| | 34 | | /// <param name="restore"></param> |
| | 35 | | public async Task BuildAsync(string relativePath, string? arguments, CancellationToken cancellationToken, Verbosity ve |
| | 36 | | { |
| 1 | 37 | | await _cli.BuildAsync(_tempDirectory.Path.PathTranslator.GetAbsolutePath(relativePath).OriginalPath, arguments, verb |
| 1 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Builds the project at a given path |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="relativePath">relative path of the project/solution you want to build within the scaffold. e.g. src/S |
| | 44 | | /// <param name="arguments">build arguments</param> |
| | 45 | | /// <param name="cancellationToken">cancellation token</param> |
| | 46 | | /// <param name="verbosity"></param> |
| | 47 | | public async Task RestoreAsync(string relativePath, string? arguments, CancellationToken cancellationToken, Verbosity |
| | 48 | | { |
| 1 | 49 | | await _cli.RestoreAsync(_tempDirectory.Path.PathTranslator.GetAbsolutePath(relativePath).OriginalPath, arguments, ve |
| 1 | 50 | | } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Builds the project at a given path |
| | 54 | | /// </summary> |
| | 55 | | /// <param name="relativePath">relative path of the project/solution you want to build within the scaffold. e.g. src/S |
| | 56 | | /// <param name="arguments">build arguments</param> |
| | 57 | | /// <param name="cancellationToken">cancellation token</param> |
| | 58 | | /// <param name="verbosity"></param> |
| | 59 | | public async Task TestAsync(string relativePath, string? arguments, CancellationToken cancellationToken, Verbosity ver |
| | 60 | | { |
| 1 | 61 | | await _cli.TestAsync(_tempDirectory.Path.PathTranslator.GetAbsolutePath(relativePath).OriginalPath, arguments, verbo |
| 1 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Returns the file contents for a given relative path |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="relativePath">path relative to scaffold folder</param> |
| | 68 | | /// <param name="cancellationToken">cancellation token</param> |
| | 69 | | /// <returns></returns> |
| | 70 | | public async Task<string> GetFileContentAsync(string relativePath, CancellationToken cancellationToken = default) |
| | 71 | | { |
| 5 | 72 | | return await _tempDirectory.GetFileContentAsync(relativePath, cancellationToken).ConfigureAwait(false); |
| 5 | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Returns the content of all scaffolded files |
| | 77 | | /// </summary> |
| | 78 | | /// <param name="filter">true removes a file from the results, false keeps it</param> |
| | 79 | | /// <param name="cancellationToken"></param> |
| | 80 | | /// <returns></returns> |
| | 81 | | public async IAsyncEnumerable<FileContent> GetAllFileContentsAsync(RelativeFileFilter? filter = default, [EnumeratorCa |
| | 82 | | { |
| 3 | 83 | | var filterInstance = filter ?? TemplatingDefaults.Instance.GetAllFileContentsFilter; |
| 24 | 84 | | foreach (var relativePath in GetRelativeDirectoryPaths()) |
| | 85 | | { |
| 9 | 86 | | if(filterInstance(relativePath)) |
| | 87 | | continue; |
| | 88 | |
|
| 5 | 89 | | cancellationToken.ThrowIfCancellationRequested(); |
| 5 | 90 | | var content = await GetFileContentAsync(relativePath, cancellationToken); |
| 5 | 91 | | yield return new FileContent(relativePath, content); |
| 5 | 92 | | } |
| 3 | 93 | | } |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// Returns the directory path of the scaffold |
| | 97 | | /// </summary> |
| | 98 | | /// <returns></returns> |
| 1 | 99 | | public string GetAbsoluteRootPath() => _tempDirectory.Path.File.OriginalPath; |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Gets all paths within the temp directory with their relative paths |
| | 103 | | /// </summary> |
| | 104 | | /// <returns></returns> |
| | 105 | | public IEnumerable<string> GetRelativeDirectoryPaths() |
| | 106 | | { |
| 12 | 107 | | return _tempDirectory.GetRelativePaths().OrderBy(d => d); |
| | 108 | | } |
| | 109 | |
|
| | 110 | | private bool _disposed; |
| | 111 | | /// <summary> |
| | 112 | | /// |
| | 113 | | /// </summary> |
| | 114 | | public void Dispose() |
| | 115 | | { |
| 2 | 116 | | if (_disposed) |
| 1 | 117 | | return; |
| 1 | 118 | | _disposed = true; |
| | 119 | |
|
| 1 | 120 | | _tempDirectory.Dispose(); |
| 1 | 121 | | GC.SuppressFinalize(this); |
| 1 | 122 | | } |
| | 123 | | } |