ProjectLoader.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Xml.Linq;
  2. using System.Xml.XPath;
  3. using Microsoft.Build.Locator;
  4. using Microsoft.CodeAnalysis;
  5. using Microsoft.CodeAnalysis.MSBuild;
  6. namespace PixiEditor.DevTools.CsharpCoding;
  7. public class ProjectLoader
  8. {
  9. public MSBuildWorkspace Workspace { get; private set; }
  10. public string ProjectPath { get; private set; }
  11. public List<PackageReference> PackageReferences { get; private set; }
  12. public Project TargetProject { get; private set; }
  13. public List<Project> AllProjects { get; private set; }
  14. public List<string> ReferencedProjectPaths { get; private set; }
  15. private static readonly string[] _animacoCoreProjects = new[] { "Animaco.Core", "Animaco.Rendering", "Animaco.FFmpegRenderer", "Animaco.GUIPreview",
  16. "Animaco", "Animaco.AvaloniaUI" };
  17. public ProjectLoader(string projectPath)
  18. {
  19. MSBuildLocator.RegisterDefaults();
  20. Dictionary<string, string> props = new Dictionary<string, string>();
  21. Workspace = MSBuildWorkspace.Create(props);
  22. Workspace.LoadMetadataForReferencedProjects = true;
  23. PackageReferences = LoadPackageReferences(projectPath, out List<string> projects);
  24. ReferencedProjectPaths = projects;
  25. ProjectPath = projectPath;
  26. }
  27. private List<PackageReference> LoadPackageReferences(string projectPath, out List<string> projects)
  28. {
  29. projects = DigProjectReferences(projectPath, new List<string>());
  30. List<PackageReference> packageReferences = new List<PackageReference>();
  31. foreach (var project in projects)
  32. {
  33. packageReferences.AddRange(LoadForProject(project, packageReferences));
  34. }
  35. return packageReferences;
  36. }
  37. private List<string> DigProjectReferences(string projectPath, List<string> existingProjects)
  38. {
  39. string xml = File.ReadAllText(projectPath);
  40. var doc = XDocument.Parse(xml);
  41. var projectReferences = doc.XPathSelectElements("//ProjectReference")
  42. .Select(pr => pr.Attribute("Include").Value).Except(existingProjects).ToList();
  43. foreach (var projectReference in projectReferences)
  44. {
  45. string projectFullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(projectPath)!, projectReference));
  46. var references = DigProjectReferences(projectFullPath, existingProjects);
  47. foreach (var reference in references)
  48. {
  49. if (!existingProjects.Contains(reference))
  50. {
  51. existingProjects.Add(reference);
  52. }
  53. }
  54. }
  55. existingProjects.Add(projectPath);
  56. return existingProjects;
  57. }
  58. private static List<PackageReference> LoadForProject(string projectPath, List<PackageReference> existingPackages)
  59. {
  60. string xml = File.ReadAllText(projectPath);
  61. var doc = XDocument.Parse(xml);
  62. var packageReferences = doc.XPathSelectElements("//PackageReference")
  63. .Select(pr => new PackageReference
  64. {
  65. Include = pr.Attribute("Include").Value,
  66. Version = new Version(pr.Attribute("Version").Value)
  67. });
  68. return packageReferences.Where(pr => existingPackages.All(ep => ep.Include != pr.Include))
  69. .ToList();
  70. }
  71. public async Task LoadProjectsAsync()
  72. {
  73. AllProjects = new List<Project>();
  74. foreach (var projectPath in ReferencedProjectPaths)
  75. {
  76. if(IsAnimacoCoreProject(projectPath))
  77. continue;
  78. AllProjects.Add(await Workspace.OpenProjectAsync(projectPath));
  79. }
  80. TargetProject = AllProjects.First(x => x.FilePath == ProjectPath);
  81. }
  82. private bool IsAnimacoCoreProject(string projectPath)
  83. {
  84. return _animacoCoreProjects.Any(x => projectPath.EndsWith($"{x}.csproj"));
  85. }
  86. }
  87. public class PackageReference
  88. {
  89. public string Include { get; set; }
  90. public Version Version { get; set; }
  91. }