GenerateSubmoduleGraph.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.IO;
  8. using System.Text;
  9. using System.Threading;
  10. using Microsoft.Build.Framework;
  11. using Microsoft.Build.Utilities;
  12. using NuGet.Frameworks;
  13. using NuGet.Versioning;
  14. using RepoTools.BuildGraph;
  15. using RepoTasks.ProjectModel;
  16. using RepoTasks.Utilities;
  17. using RepoTasks.CodeGen;
  18. using NuGet.Packaging.Core;
  19. namespace RepoTasks
  20. {
  21. public class GenerateSubmoduleGraph : Task, ICancelableTask
  22. {
  23. private readonly CancellationTokenSource _cts = new CancellationTokenSource();
  24. /// <summary>
  25. /// Repositories that we are building new versions of.
  26. /// </summary>
  27. [Required]
  28. public ITaskItem[] Solutions { get; set; }
  29. [Required]
  30. public ITaskItem[] Artifacts { get; set; }
  31. [Required]
  32. public ITaskItem[] Repositories { get; set; }
  33. [Required]
  34. public string RepositoryRoot { get; set; }
  35. [Required]
  36. public string Properties { get; set; }
  37. public void Cancel()
  38. {
  39. _cts.Cancel();
  40. }
  41. public override bool Execute()
  42. {
  43. var packageArtifacts = Artifacts.Select(ArtifactInfo.Parse)
  44. .OfType<ArtifactInfo.Package>()
  45. .Where(p => !p.IsSymbolsArtifact)
  46. .ToDictionary(p => p.PackageInfo.Id, p => p, StringComparer.OrdinalIgnoreCase);
  47. var factory = new SolutionInfoFactory(Log, BuildEngine5);
  48. var props = MSBuildListSplitter.GetNamedProperties(Properties);
  49. Log.LogMessage(MessageImportance.High, $"Beginning cross-repo analysis on {Solutions.Length} solutions. Hang tight...");
  50. if (!props.TryGetValue("Configuration", out var defaultConfig))
  51. {
  52. defaultConfig = "Debug";
  53. }
  54. var solutions = factory.Create(Solutions, props, defaultConfig, _cts.Token).OrderBy(f => f.Directory).ToList();
  55. Log.LogMessage($"Found {solutions.Count} and {solutions.Sum(p => p.Projects.Count)} projects");
  56. if (_cts.IsCancellationRequested)
  57. {
  58. return false;
  59. }
  60. return GenerateGraph(packageArtifacts, solutions);
  61. }
  62. private bool GenerateGraph(IDictionary<string, ArtifactInfo.Package> packageArtifacts, IReadOnlyList<SolutionInfo> solutions)
  63. {
  64. var repoGraph = new AdjacencyMatrix(solutions.Count);
  65. var packageToProjectMap = new Dictionary<PackageIdentity, ProjectInfo>();
  66. for (var i = 0; i < solutions.Count; i++)
  67. {
  68. var sln = repoGraph[i] = solutions[i];
  69. foreach (var proj in sln.Projects)
  70. {
  71. if (!proj.IsPackable || proj.FullPath.Contains("samples"))
  72. {
  73. continue;
  74. }
  75. var id = new PackageIdentity(proj.PackageId, new NuGetVersion(proj.PackageVersion));
  76. if (packageToProjectMap.TryGetValue(id, out var otherProj))
  77. {
  78. Log.LogError($"Both {proj.FullPath} and {otherProj.FullPath} produce {id}");
  79. continue;
  80. }
  81. packageToProjectMap.Add(id, proj);
  82. }
  83. var sharedSrc = Path.Combine(sln.Directory, "shared");
  84. if (Directory.Exists(sharedSrc))
  85. {
  86. foreach (var dir in Directory.GetDirectories(sharedSrc, "*.Sources"))
  87. {
  88. var id = Path.GetFileName(dir);
  89. var artifactInfo = packageArtifacts[id];
  90. var sharedSrcProj = new ProjectInfo(dir,
  91. Array.Empty<ProjectFrameworkInfo>(),
  92. Array.Empty<DotNetCliReferenceInfo>(),
  93. true,
  94. artifactInfo.PackageInfo.Id,
  95. artifactInfo.PackageInfo.Version.ToNormalizedString());
  96. sharedSrcProj.SolutionInfo = sln;
  97. var identity = new PackageIdentity(artifactInfo.PackageInfo.Id, artifactInfo.PackageInfo.Version);
  98. packageToProjectMap.Add(identity, sharedSrcProj);
  99. }
  100. }
  101. }
  102. if (Log.HasLoggedErrors)
  103. {
  104. return false;
  105. }
  106. for (var i = 0; i < solutions.Count; i++)
  107. {
  108. var sln = repoGraph[i];
  109. var deps = from proj in sln.Projects
  110. from tfm in proj.Frameworks
  111. from dep in tfm.Dependencies.Values
  112. select dep;
  113. foreach (var dep in deps)
  114. {
  115. if (packageToProjectMap.TryGetValue(new PackageIdentity(dep.Id, new NuGetVersion(dep.Version)), out var target))
  116. {
  117. var j = repoGraph.FindIndex(target.SolutionInfo);
  118. repoGraph.SetLink(i, j);
  119. }
  120. }
  121. var toolDeps = from proj in sln.Projects
  122. from tool in proj.Tools
  123. select tool;
  124. foreach (var toolDep in toolDeps)
  125. {
  126. if (packageToProjectMap.TryGetValue(new PackageIdentity(toolDep.Id, new NuGetVersion(toolDep.Version)), out var target))
  127. {
  128. var j = repoGraph.FindIndex(target.SolutionInfo);
  129. repoGraph.SetLink(i, j);
  130. }
  131. }
  132. }
  133. CreateDgml(repoGraph);
  134. return !Log.HasLoggedErrors;
  135. }
  136. private void CreateDgml(AdjacencyMatrix repoGraph)
  137. {
  138. var dgml = new DirectedGraphXml();
  139. for (var i = 0; i < repoGraph.Count; i++)
  140. {
  141. var node = repoGraph[i];
  142. var nodeName = Path.GetFileName(node.Directory);
  143. dgml.AddNode(nodeName);
  144. for (var j = 0; j < repoGraph.Count; j++)
  145. {
  146. if (j == i) continue;
  147. if (repoGraph.HasLink(i, j))
  148. {
  149. var target = repoGraph[j];
  150. var targetName = Path.GetFileName(target.Directory);
  151. dgml.AddLink(nodeName, targetName);
  152. }
  153. }
  154. }
  155. dgml.Save(Path.Combine(RepositoryRoot, "modules", "SubmoduleGraph.dgml"));
  156. }
  157. private class AdjacencyMatrix
  158. {
  159. private readonly bool[,] _matrix;
  160. private readonly SolutionInfo[] _items;
  161. public AdjacencyMatrix(int size)
  162. {
  163. _matrix = new bool[size, size];
  164. _items = new SolutionInfo[size];
  165. Count = size;
  166. }
  167. public SolutionInfo this[int idx]
  168. {
  169. get => _items[idx];
  170. set => _items[idx] = value;
  171. }
  172. public int FindIndex(SolutionInfo item)
  173. {
  174. return Array.FindIndex(_items, t => t.Equals(item));
  175. }
  176. public int Count { get; }
  177. public bool HasLink(int source, int target) => _matrix[source, target];
  178. public void SetLink(int source, int target)
  179. {
  180. _matrix[source, target] = true;
  181. }
  182. }
  183. }
  184. }