From 47160a1652534dc55bd8fa1fff155d427f7efbcc Mon Sep 17 00:00:00 2001 From: Dave Glick Date: Wed, 20 Dec 2023 11:25:06 -0500 Subject: [PATCH] Changes how projects are added to the `Workspace` in Buildalyzer.Workspaces to be based on the Solution order, if there is a Solution (#241, https://github.com/eNeRGy164/LivingDocumentation/issues/56) --- RELEASE.md | 4 ++++ .../AnalyzerManagerExtensions.cs | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index b04db7db..84957504 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,3 +1,7 @@ +# 6.0.2 + +- Changes how projects are added to the `Workspace` in Buildalyzer.Workspaces to be based on the Solution order, if there is a Solution (#241, thanks @AndreasKim). + # 6.0.1 - Added the ability to specify an alternate working directory for running the build in the environment options (#233). diff --git a/src/Buildalyzer.Workspaces/AnalyzerManagerExtensions.cs b/src/Buildalyzer.Workspaces/AnalyzerManagerExtensions.cs index 6d255760..9cee3ed5 100644 --- a/src/Buildalyzer.Workspaces/AnalyzerManagerExtensions.cs +++ b/src/Buildalyzer.Workspaces/AnalyzerManagerExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.Build.Construction; using Microsoft.CodeAnalysis; using Microsoft.Extensions.Logging; @@ -35,18 +36,28 @@ public static AdhocWorkspace GetWorkspace(this IAnalyzerManager manager) .Where(x => x != null) .ToList(); - // Add each result to a new workspace + // Create a new workspace and add the solution (if there was one) AdhocWorkspace workspace = manager.CreateWorkspace(); - if (!string.IsNullOrEmpty(manager.SolutionFilePath)) { SolutionInfo solutionInfo = SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Default, manager.SolutionFilePath); workspace.AddSolution(solutionInfo); + + // Sort the projects so the order that they're added to the workspace in the same order as the solution file + List projectsInOrder = manager.SolutionFile.ProjectsInOrder.ToList(); + results = results + .OrderBy(p => projectsInOrder.FindIndex(g => g.AbsolutePath == p.ProjectFilePath)) + .ToList(); } - foreach (AnalyzerResult result in results) + // Add each result to the new workspace (sorted in solution order above, if we have a solution) + foreach (IAnalyzerResult result in results) { - result.AddToWorkspace(workspace); + // Check for duplicate project files and don't add them + if (workspace.CurrentSolution.Projects.All(p => p.FilePath != result.ProjectFilePath)) + { + result.AddToWorkspace(workspace); + } } return workspace; }