From 862f81d2be251dab58747463dcd70f542c3fc7b2 Mon Sep 17 00:00:00 2001 From: "Matthew Parker [SSW]" <61717342+MattParkerDev@users.noreply.github.com> Date: Wed, 21 Feb 2024 02:00:40 +1000 Subject: [PATCH] add generating solution file --- DotNetSolutionTools.Core/GenerateSln.cs | 89 +++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 DotNetSolutionTools.Core/GenerateSln.cs diff --git a/DotNetSolutionTools.Core/GenerateSln.cs b/DotNetSolutionTools.Core/GenerateSln.cs new file mode 100644 index 0000000..5147834 --- /dev/null +++ b/DotNetSolutionTools.Core/GenerateSln.cs @@ -0,0 +1,89 @@ +using System.Diagnostics; +using Microsoft.Build.Construction; + +namespace DotNetSolutionTools.Core; + +public static class GenerateSln +{ + private const string BasePath = @"C:\Users\Matthew\Documents\Git\folder"; + + public static async Task GenerateSlnWithDependenciesFromProjects(List csprojPathList) + { + csprojPathList = [$"""{BasePath}/src/project1/project1.csproj"""]; + // Get csproj objects + var projectObjects = csprojPathList.Select(ProjectRootElement.Open).ToList(); + + // Initialize dependencies with the root projects + var allDependencies = new HashSet(csprojPathList); + + // Recursively find all dependencies + FindAllDependenciesAsync(projectObjects, allDependencies); + + // At this point, allDependencies contains paths to all projects and their dependencies + // Further processing to generate the solution file can be done here + Console.WriteLine(allDependencies.Count); + + // delete the solution file if it exists + if (File.Exists(BasePath + "\\Global.sln")) + { + File.Delete(BasePath + "\\Global.sln"); + } + + // powershell + await RunPowerShellCommandAsync($"dotnet new sln -n Global -o {BasePath}"); + + foreach (var project in allDependencies) + { + await RunPowerShellCommandAsync($"dotnet sln {BasePath}/Global.sln add {project}"); + } + } + + private static void FindAllDependenciesAsync(List projects, HashSet allDependencies) + { + var newDependencies = new List(); + + foreach (var project in projects) + { + var dependencyPaths = project + .Items.Where(x => x.ItemType == "ProjectReference") + .Select(s => s.Include) + .ToList(); + + // projectrootelement can't handle paths with ../ so we need to remove them, but resolve them correctly + dependencyPaths = dependencyPaths.Select(x => Path.GetFullPath(x, project.DirectoryPath)).ToList(); + + foreach (var path in dependencyPaths) + { + // Add to the HashSet to prevent duplicates and check if the path was already processed + if (allDependencies.Add(path)) + { + newDependencies.Add(ProjectRootElement.Open(path)); + } + } + } + + if (newDependencies.Any()) + { + // Recursively process new dependencies found + FindAllDependenciesAsync(newDependencies, allDependencies); + } + } + + private static async Task RunPowerShellCommandAsync(string command) + { + var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "powershell", + Arguments = command, + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true + } + }; + process.Start(); + await process.WaitForExitAsync(); + Console.WriteLine(await process.StandardOutput.ReadToEndAsync()); + } +}