|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Xml.Linq; |
| 5 | + |
| 6 | +namespace Snitch.Analysis.Utilities |
| 7 | +{ |
| 8 | + internal static class SlnxParser |
| 9 | + { |
| 10 | + public static List<string> GetProjectsFromSlnx(string slnxPath) |
| 11 | + { |
| 12 | + if (!File.Exists(slnxPath)) |
| 13 | + { |
| 14 | + throw new FileNotFoundException($"Solution file not found: {slnxPath}"); |
| 15 | + } |
| 16 | + |
| 17 | + var slnxDirectory = Path.GetDirectoryName(slnxPath) |
| 18 | + ?? throw new InvalidOperationException("Could not determine solution directory."); |
| 19 | + |
| 20 | + var doc = XDocument.Load(slnxPath); |
| 21 | + var solution = doc.Root; |
| 22 | + |
| 23 | + if (solution == null || solution.Name.LocalName != "Solution") |
| 24 | + { |
| 25 | + throw new InvalidOperationException("Invalid slnx file: missing Solution root element."); |
| 26 | + } |
| 27 | + |
| 28 | + var projects = new List<string>(); |
| 29 | + CollectProjects(solution, slnxDirectory, projects); |
| 30 | + |
| 31 | + return projects; |
| 32 | + } |
| 33 | + |
| 34 | + private static void CollectProjects(XElement element, string slnxDirectory, List<string> projects) |
| 35 | + { |
| 36 | + foreach (var child in element.Elements()) |
| 37 | + { |
| 38 | + if (child.Name.LocalName == "Project") |
| 39 | + { |
| 40 | + var pathAttr = child.Attribute("Path"); |
| 41 | + if (pathAttr != null && !string.IsNullOrWhiteSpace(pathAttr.Value)) |
| 42 | + { |
| 43 | + var projectPath = pathAttr.Value; |
| 44 | + |
| 45 | + // Only include MSBuild project files (.csproj, .fsproj, .vbproj) |
| 46 | + if (IsMSBuildProject(projectPath)) |
| 47 | + { |
| 48 | + var absolutePath = Path.GetFullPath(Path.Combine(slnxDirectory, projectPath)); |
| 49 | + if (!projects.Contains(absolutePath)) |
| 50 | + { |
| 51 | + projects.Add(absolutePath); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + else if (child.Name.LocalName == "Folder") |
| 57 | + { |
| 58 | + // Recursively collect projects from folders |
| 59 | + CollectProjects(child, slnxDirectory, projects); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static bool IsMSBuildProject(string path) |
| 65 | + { |
| 66 | + return path.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) |
| 67 | + || path.EndsWith(".fsproj", StringComparison.OrdinalIgnoreCase) |
| 68 | + || path.EndsWith(".vbproj", StringComparison.OrdinalIgnoreCase); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments