ProjectLoader.cs 3.8 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[] _coreProjects = new[] { "PixiEditor.AvaloniaUI" };
  16. public ProjectLoader(string projectPath)
  17. {
  18. MSBuildLocator.RegisterDefaults();
  19. Dictionary<string, string> props = new Dictionary<string, string>();
  20. Workspace = MSBuildWorkspace.Create(props);
  21. Workspace.LoadMetadataForReferencedProjects = true;
  22. /*PackageReferences = LoadPackageReferences(projectPath, out List<string> projects);
  23. ReferencedProjectPaths = projects;*/
  24. ProjectPath = projectPath;
  25. }
  26. private List<PackageReference> LoadPackageReferences(string projectPath, out List<string> projects)
  27. {
  28. projects = DigProjectReferences(projectPath, new List<string>());
  29. List<PackageReference> packageReferences = new List<PackageReference>();
  30. foreach (var project in projects)
  31. {
  32. packageReferences.AddRange(LoadForProject(project, packageReferences));
  33. }
  34. return packageReferences;
  35. }
  36. private List<string> DigProjectReferences(string projectPath, List<string> existingProjects)
  37. {
  38. string xml = File.ReadAllText(projectPath);
  39. var doc = XDocument.Parse(xml);
  40. var projectReferences = doc.XPathSelectElements("//ProjectReference")
  41. .Select(pr => pr.Attribute("Include").Value).Except(existingProjects).ToList();
  42. foreach (var projectReference in projectReferences)
  43. {
  44. string projectFullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(projectPath)!, projectReference));
  45. var references = DigProjectReferences(projectFullPath, existingProjects);
  46. foreach (var reference in references)
  47. {
  48. if (!existingProjects.Contains(reference))
  49. {
  50. existingProjects.Add(reference);
  51. }
  52. }
  53. }
  54. existingProjects.Add(projectPath);
  55. return existingProjects;
  56. }
  57. private static List<PackageReference> LoadForProject(string projectPath, List<PackageReference> existingPackages)
  58. {
  59. string xml = File.ReadAllText(projectPath);
  60. var doc = XDocument.Parse(xml);
  61. var packageReferences = doc.XPathSelectElements("//PackageReference")
  62. .Select(pr => new PackageReference
  63. {
  64. Include = pr.Attribute("Include").Value,
  65. Version = new Version(pr.Attribute("Version").Value)
  66. });
  67. return packageReferences.Where(pr => existingPackages.All(ep => ep.Include != pr.Include))
  68. .ToList();
  69. }
  70. public void LoadProjects()
  71. {
  72. AllProjects = new List<Project>();
  73. /*foreach (var projectPath in ReferencedProjectPaths)
  74. {
  75. if(IsAnimacoCoreProject(projectPath))
  76. continue;
  77. AllProjects.Add(await Workspace.OpenProjectAsync(projectPath));
  78. }*/
  79. AllProjects.Add(Workspace.OpenProjectAsync(ProjectPath).Result);
  80. TargetProject = AllProjects[0];
  81. }
  82. private bool IsAnimacoCoreProject(string projectPath)
  83. {
  84. return _coreProjects.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. }