| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Threading; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Amusoft.DotnetNew.Tests.Internals; |
| | 7 | | using Amusoft.DotnetNew.Tests.Rewriters; |
| | 8 | | using Amusoft.DotnetNew.Tests.Scopes; |
| | 9 | | using Amusoft.DotnetNew.Tests.Utility; |
| | 10 | |
|
| | 11 | | namespace Amusoft.DotnetNew.Tests.Templating; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Template solution |
| | 15 | | /// </summary> |
| | 16 | | public class TemplateSolution |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// |
| | 20 | | /// </summary> |
| 34 | 21 | | public PathSource Solution { get; } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Constructor that uses the path to the solution file |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="solutionPath"></param> |
| 12 | 27 | | public TemplateSolution(string solutionPath) |
| | 28 | | { |
| 12 | 29 | | if (!solutionPath.EndsWith(".sln") && !solutionPath.EndsWith(".slnx")) |
| 1 | 30 | | throw new ArgumentException("Solution files are expected to end with the extension sln or slnx"); |
| 11 | 31 | | if (!File.Exists(solutionPath)) |
| 1 | 32 | | throw new FileNotFoundException(solutionPath); |
| | 33 | |
|
| 10 | 34 | | Solution = new PathSource(solutionPath); |
| | 35 | |
|
| 10 | 36 | | LoggingScope.TryAddRewriter(BackslashRewriter.Instance); |
| 10 | 37 | | LoggingScope.TryAddRewriter(new SolutionDirectoryRewriter(Solution.Directory)); |
| 10 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="searchDirectoryStart">directory indicating where to start looking for the solution file</param> |
| | 44 | | /// <param name="maxParentJumps"></param> |
| | 45 | | /// <param name="solutionName">filename of the solution</param> |
| 12 | 46 | | public TemplateSolution(string searchDirectoryStart, int maxParentJumps, string solutionName) : this(GetSolutionPathFr |
| | 47 | | { |
| 10 | 48 | | } |
| | 49 | |
|
| | 50 | | private static string GetSolutionPathFromAssembly(string searchDirectoryStart, int maxParentJumps, string solutionName |
| | 51 | | { |
| 12 | 52 | | if (Path.GetExtension(searchDirectoryStart) is { Length: > 0}) |
| 10 | 53 | | searchDirectoryStart = Path.GetDirectoryName(searchDirectoryStart)!; |
| 12 | 54 | | if (!Directory.Exists(searchDirectoryStart)) |
| 1 | 55 | | throw new DirectoryNotFoundException($"Directory {searchDirectoryStart} not found"); |
| | 56 | |
|
| 11 | 57 | | var iterations = maxParentJumps; |
| 11 | 58 | | var searchPath = searchDirectoryStart; |
| 61 | 59 | | while (--iterations >= 0 && searchPath != null) |
| | 60 | | { |
| 60 | 61 | | var slnFiles = Directory.EnumerateFiles(searchPath, "*.sln", SearchOption.AllDirectories) |
| 60 | 62 | | .Concat(Directory.EnumerateFiles(searchPath, "*.slnx", SearchOption.AllDirectories)); |
| 80 | 63 | | var match = slnFiles.FirstOrDefault(d => Path.GetFileName(d).Equals(solutionName, StringComparison.OrdinalIgnoreCa |
| 60 | 64 | | if (match is not null) |
| 10 | 65 | | return match; |
| | 66 | |
|
| 50 | 67 | | searchPath = new DirectoryInfo(searchPath).Parent?.FullName; |
| | 68 | | } |
| | 69 | |
|
| 1 | 70 | | throw new FileNotFoundException($"Solution file {solutionName} not found in {searchDirectoryStart} or it's parent->c |
| | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Installs a template relative to the solution file |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="relativePath">relative path to the template</param> |
| | 77 | | /// <param name="cancellationToken"></param> |
| | 78 | | public async Task<TemplateInstallation> InstallTemplateAsync(string relativePath, CancellationToken cancellationToken) |
| | 79 | | { |
| 7 | 80 | | var fullPath = Solution.PathTranslator.GetAbsolutePath(relativePath); |
| 7 | 81 | | if (!Directory.Exists(fullPath.OriginalPath)) |
| 1 | 82 | | throw new DirectoryNotFoundException($"{Solution.Directory.VirtualPath} + {relativePath}"); |
| | 83 | |
|
| 6 | 84 | | return await TemplateInstallation.CreateAsync(new ProjectTemplatingContext(fullPath), cancellationToken).ConfigureAw |
| 6 | 85 | | } |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Finds the folders with template.json files in them |
| | 89 | | /// </summary> |
| | 90 | | /// <param name="searchFolder">relative path to the folder you want to run discovery in</param> |
| | 91 | | /// <returns></returns> |
| | 92 | | /// <exception cref="DirectoryNotFoundException"></exception> |
| | 93 | | public CrossPlatformPath[] DiscoverTemplates(string searchFolder) |
| | 94 | | { |
| 4 | 95 | | var folder = Solution.PathTranslator.GetAbsolutePath(searchFolder); |
| 4 | 96 | | if (!Directory.Exists(folder.OriginalPath)) |
| 1 | 97 | | throw new DirectoryNotFoundException($"{Solution.Directory.VirtualPath} + {searchFolder}"); |
| | 98 | |
|
| 3 | 99 | | var templateFiles = Directory.EnumerateFiles(folder.OriginalPath, "template.json", SearchOption.AllDirectories); |
| | 100 | |
|
| 3 | 101 | | return templateFiles |
| 6 | 102 | | .Select(path => PathHelper.AbsoluteTrimPathEnd(path, 2)) |
| 6 | 103 | | .Select(path => Solution.PathTranslator.GetRelativePath(path)) |
| 6 | 104 | | .OrderBy(d => d.VirtualPath) |
| 3 | 105 | | .ToArray(); |
| | 106 | | } |
| | 107 | |
|
| | 108 | | /// <summary> |
| | 109 | | /// Installs all templates that can be found in the given directory |
| | 110 | | /// </summary> |
| | 111 | | /// <param name="relativePath"></param> |
| | 112 | | /// <param name="cancellationToken"></param> |
| | 113 | | /// <returns></returns> |
| | 114 | | public async Task<TemplateInstallationGroup> InstallTemplatesFromDirectoryAsync(string relativePath, CancellationToken |
| | 115 | | { |
| 2 | 116 | | var group = new TemplateInstallationGroup(); |
| 12 | 117 | | foreach (var projectPath in DiscoverTemplates(relativePath)) |
| | 118 | | { |
| 4 | 119 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 120 | | group.Add(await InstallTemplateAsync(projectPath.OriginalPath, cancellationToken)); |
| | 121 | | } |
| | 122 | |
|
| 2 | 123 | | return group; |
| 2 | 124 | | } |
| | 125 | | } |