From 722412f47bc466270bc93aef62d6babb280da96f Mon Sep 17 00:00:00 2001 From: thepirat000 Date: Mon, 20 Jan 2025 15:14:52 -0600 Subject: [PATCH] Adding default test project to the SolutionGenerator tool --- .../SolutionGenerator/Program.cs | 97 ++++++++++++++++--- .../Properties/launchSettings.json | 2 +- .../SolutionGenerator.csproj | 1 + 3 files changed, 88 insertions(+), 12 deletions(-) diff --git a/tools/SolutionGenerator/SolutionGenerator/Program.cs b/tools/SolutionGenerator/SolutionGenerator/Program.cs index 267790bc..ba0b42d5 100644 --- a/tools/SolutionGenerator/SolutionGenerator/Program.cs +++ b/tools/SolutionGenerator/SolutionGenerator/Program.cs @@ -1,18 +1,23 @@ // MvsSln Doc: https://github.com/3F/MvsSln -using System.Reflection; +using System.Text; + +using Microsoft.Build.Evaluation; + using net.r_eg.MvsSln; using net.r_eg.MvsSln.Core; using net.r_eg.MvsSln.Core.ObjHandlers; using net.r_eg.MvsSln.Core.SlnHandlers; +using ProjectItem = net.r_eg.MvsSln.Core.ProjectItem; + namespace SolutionGenerator { public static class Program { // Args: // 0: Input sln path - // 1: Filters (comma separated) + // 1: Filters (comma separated, e.g.: "entityframework,!core") // 2: Output sln path static void Main(string[] args) { @@ -22,17 +27,21 @@ static void Main(string[] args) return; } + var currentDirectory = new DirectoryInfo(Environment.CurrentDirectory); var inputSlnPath = args[0]; var filters = args[1].Split(",").Where(f => f.Length > 0).ToList(); - var outputSlnPath = args.Length > 2 && args[2].Length > 0 ? args[2] : $"{new DirectoryInfo(Environment.CurrentDirectory).Name}.sln"; + + var outputSlnPath = args.Length > 2 && args[2].Length > 0 ? args[2] : $"{currentDirectory.Name}.sln"; + var outputDir = Path.GetDirectoryName(outputSlnPath)!; if (!outputSlnPath.EndsWith(".sln", StringComparison.OrdinalIgnoreCase)) { outputSlnPath += ".sln"; } - var sln = new Sln(inputSlnPath, SlnItems.AllNoLoad); + using var sln = new Sln(inputSlnPath, SlnItems.AllNoLoad); + // Apply filters var projects = sln.Result.ProjectItems .Where(p => IncludeProject(p, filters)) .ToList(); @@ -48,23 +57,45 @@ static void Main(string[] args) for (int i = 0; i < projects.Count; i++) { Console.WriteLine($"Adding {projects[i].name}"); - + // Replace relative path with full path projects[i] = projects[i] with { path = projects[i].fullPath }; } Console.WriteLine(); + // New project + var testProjectName = Path.GetFileNameWithoutExtension(outputSlnPath) + ".csproj"; + var testProject = new ProjectItem(ProjectType.CsSdk, testProjectName, slnDir: outputDir); + + projects.Add(testProject); + sln.Result.ProjectDependencies.GuidList.Add(testProject.pGuid); + sln.Result.ProjectDependencies.Projects.Add(testProject.pGuid, testProject); + + var projectConfPlat = sln.Result.ProjectConfigurationPlatforms.Values.First() + .Where(pc => projects.Exists(prj => prj.pGuid == pc.PGuid)) + .ToList(); + + var solutionFolders = sln.Result.SolutionFolders; + // Write modified sln Console.WriteLine($"Writing modified sln to {outputSlnPath}"); var handlers = new Dictionary() { [typeof(LProject)] = new(new WProject(projects, sln.Result.ProjectDependencies)), - [typeof(LNestedProjects)] = new(new WNestedProjects(sln.Result.SolutionFolders, projects)) + [typeof(LNestedProjects)] = new(new WNestedProjects(sln.Result.SolutionFolders, projects)), + [typeof(LProjectConfigurationPlatforms)] = new(new WProjectConfigurationPlatforms(projectConfPlat)), + [typeof(LSolutionConfigurationPlatforms)] = new(new WSolutionConfigurationPlatforms(projectConfPlat)), + [typeof(LProjectSolutionItems)] = new(new WProjectSolutionItems(solutionFolders)), }; - using var w = new SlnWriter(outputSlnPath, handlers); + using (var w = new SlnWriter(outputSlnPath, handlers)) + { + w.Options = SlnWriterOptions.CreateProjectsIfNotExist; + w.Write(sln.Result.Map); + } - w.Write(sln.Result.Map); + // Create the new project + CreateTestProject(projects, testProject, outputDir); } static bool IncludeProject(ProjectItem project, List filters) @@ -84,9 +115,53 @@ static bool IncludeProject(ProjectItem project, List filters) return true; } - var found = filters.Exists(filter => project.name.Contains(filter, StringComparison.OrdinalIgnoreCase)); - - return found; + var positiveFilters = filters.FindAll(f => !f.StartsWith("!")); + var negativeFilters = filters.FindAll(f => f.StartsWith("!")).ConvertAll(f => f.TrimStart('!')); + + var included = positiveFilters.Exists(filter => project.name.Contains(filter, StringComparison.OrdinalIgnoreCase)); + var excluded = negativeFilters.Exists(filter => project.name.Contains(filter, StringComparison.OrdinalIgnoreCase)); + + return included && !excluded; + } + + static void CreateTestProject(List projects, ProjectItem testProject, string outputDir) + { + var proj = new Project(NewProjectFileOptions.None); + proj.Xml.Sdk = "Microsoft.NET.Sdk"; + var xp = new XProject(proj); + + xp.SetProperties(new Dictionary() + { + { "OutputType", "Exe" }, + { "TargetFramework", "net9.0" }, + { "LangVersion", "latest" }, + }); + + foreach (var project in projects.Where(p => p.pGuid != testProject.pGuid)) + { + xp.AddProjectReference(project.fullPath, project.pGuid, project.name, false); + } + + var program = """ + using Audit.Core; + + using System; + + namespace Test2 + { + public static class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } + } + """; + File.WriteAllText(Path.Combine(outputDir, "Program.cs"), program); + + // Write the new project + xp.Save(testProject.fullPath, Encoding.UTF8); } } } diff --git a/tools/SolutionGenerator/SolutionGenerator/Properties/launchSettings.json b/tools/SolutionGenerator/SolutionGenerator/Properties/launchSettings.json index 088ae3e0..93a4f588 100644 --- a/tools/SolutionGenerator/SolutionGenerator/Properties/launchSettings.json +++ b/tools/SolutionGenerator/SolutionGenerator/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "SolutionGenerator": { "commandName": "Project", - "commandLineArgs": "\"D:\\MIOS\\Proyectos .NET\\Audit.NET\\Audit.NET.sln\" \"polly\" \"Audit.NET.modified.sln\"" + "commandLineArgs": "\"D:\\MIOS\\Proyectos .NET\\Audit.NET\\Audit.NET.sln\" \"entityframework,!core\" \"C:\\Temp\\test1\\Test2.sln\"" } } } \ No newline at end of file diff --git a/tools/SolutionGenerator/SolutionGenerator/SolutionGenerator.csproj b/tools/SolutionGenerator/SolutionGenerator/SolutionGenerator.csproj index 08c50e0e..9733b3ee 100644 --- a/tools/SolutionGenerator/SolutionGenerator/SolutionGenerator.csproj +++ b/tools/SolutionGenerator/SolutionGenerator/SolutionGenerator.csproj @@ -9,6 +9,7 @@ +