CheckRepoGraph.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.Packaging.Core;
  14. using NuGet.Versioning;
  15. using RepoTools.BuildGraph;
  16. using RepoTasks.ProjectModel;
  17. using RepoTasks.Utilities;
  18. namespace RepoTasks
  19. {
  20. public class CheckRepoGraph : Task, ICancelableTask
  21. {
  22. private readonly CancellationTokenSource _cts = new CancellationTokenSource();
  23. [Required]
  24. public ITaskItem[] Solutions { get; set; }
  25. [Required]
  26. public ITaskItem[] Artifacts { get; set; }
  27. [Required]
  28. public ITaskItem[] Repositories { get; set; }
  29. [Required]
  30. public string Properties { get; set; }
  31. public void Cancel()
  32. {
  33. _cts.Cancel();
  34. }
  35. public override bool Execute()
  36. {
  37. var packageArtifacts = Artifacts.Select(ArtifactInfo.Parse)
  38. .OfType<ArtifactInfo.Package>()
  39. .Where(p => !p.IsSymbolsArtifact)
  40. .ToDictionary(p => p.PackageInfo.Id, p => p, StringComparer.OrdinalIgnoreCase);
  41. var factory = new SolutionInfoFactory(Log, BuildEngine5);
  42. var props = MSBuildListSplitter.GetNamedProperties(Properties);
  43. if (!props.TryGetValue("Configuration", out var defaultConfig))
  44. {
  45. defaultConfig = "Debug";
  46. }
  47. var solutions = factory.Create(Solutions, props, defaultConfig, _cts.Token).OrderBy(f => f.Directory).ToList();
  48. Log.LogMessage($"Found {solutions.Count} and {solutions.Sum(p => p.Projects.Count)} projects");
  49. if (_cts.IsCancellationRequested)
  50. {
  51. return false;
  52. }
  53. var repoGraph = new AdjacencyMatrix(solutions.Count);
  54. var packageToProjectMap = new Dictionary<PackageIdentity, ProjectInfo>();
  55. for (var i = 0; i < solutions.Count; i++)
  56. {
  57. var sln = repoGraph[i] = solutions[i];
  58. foreach (var proj in sln.Projects)
  59. {
  60. if (!proj.IsPackable
  61. || proj.FullPath.Contains("samples")
  62. || proj.FullPath.Contains("tools/Microsoft.VisualStudio.Web.CodeGeneration.Design"))
  63. {
  64. continue;
  65. }
  66. var id = new PackageIdentity(proj.PackageId, new NuGetVersion(proj.PackageVersion));
  67. if (packageToProjectMap.TryGetValue(id, out var otherProj))
  68. {
  69. Log.LogError($"Both {proj.FullPath} and {otherProj.FullPath} produce {id}");
  70. continue;
  71. }
  72. packageToProjectMap.Add(id, proj);
  73. }
  74. var sharedSrc = Path.Combine(sln.Directory, "shared");
  75. if (Directory.Exists(sharedSrc))
  76. {
  77. foreach (var dir in Directory.GetDirectories(sharedSrc, "*.Sources"))
  78. {
  79. var id = GetDirectoryName(dir);
  80. var artifactInfo = packageArtifacts[id];
  81. var sharedSrcProj = new ProjectInfo(dir,
  82. Array.Empty<ProjectFrameworkInfo>(),
  83. Array.Empty<DotNetCliReferenceInfo>(),
  84. true,
  85. artifactInfo.PackageInfo.Id,
  86. artifactInfo.PackageInfo.Version.ToNormalizedString());
  87. sharedSrcProj.SolutionInfo = sln;
  88. var identity = new PackageIdentity(artifactInfo.PackageInfo.Id, artifactInfo.PackageInfo.Version);
  89. packageToProjectMap.Add(identity, sharedSrcProj);
  90. }
  91. }
  92. }
  93. if (Log.HasLoggedErrors)
  94. {
  95. return false;
  96. }
  97. for (var i = 0; i < solutions.Count; i++)
  98. {
  99. var src = repoGraph[i];
  100. foreach (var proj in src.Projects)
  101. {
  102. if (!proj.IsPackable
  103. || proj.FullPath.Contains("samples"))
  104. {
  105. continue;
  106. }
  107. foreach (var dep in proj.Frameworks.SelectMany(f => f.Dependencies.Values))
  108. {
  109. if (packageToProjectMap.TryGetValue(new PackageIdentity(dep.Id, new NuGetVersion(dep.Version)), out var target))
  110. {
  111. var j = repoGraph.FindIndex(target.SolutionInfo);
  112. repoGraph.SetLink(i, j);
  113. }
  114. }
  115. foreach (var toolDep in proj.Tools)
  116. {
  117. if (packageToProjectMap.TryGetValue(new PackageIdentity(toolDep.Id, new NuGetVersion(toolDep.Version)), out var target))
  118. {
  119. var j = repoGraph.FindIndex(target.SolutionInfo);
  120. repoGraph.SetLink(i, j);
  121. }
  122. }
  123. }
  124. }
  125. var repos = Repositories.ToDictionary(i => i.ItemSpec, i => i, StringComparer.OrdinalIgnoreCase);
  126. for (var i = 0; i < repoGraph.Count; i++)
  127. {
  128. var src = repoGraph[i];
  129. var repoName = GetDirectoryName(src.Directory);
  130. var repo = repos[repoName];
  131. var policy = Enum.Parse<PatchPolicy>(repo.GetMetadata("PatchPolicy"));
  132. if ((policy & PatchPolicy.AlwaysUpdate) != 0 && !src.IsPatching)
  133. {
  134. Log.LogError($"{repoName} is not currently set to patch, but it should because the policy is set to always include this in servicing updates. Update the configuration in submodule.props.");
  135. continue;
  136. }
  137. var srcShouldCascade = (policy & PatchPolicy.CascadeVersions) != 0;
  138. for (var j = 0; j < repoGraph.Count; j++)
  139. {
  140. if (j == i) continue;
  141. if (repoGraph.HasLink(i, j))
  142. {
  143. var target = repoGraph[j];
  144. var targetRepoName = GetDirectoryName(target.Directory);
  145. var targetRepo = repos[targetRepoName];
  146. if (srcShouldCascade && !src.IsPatching && target.IsPatching)
  147. {
  148. Log.LogError($"{repoName} should be patching because it depend on {targetRepoName} and its patch policy is to cascade version changes. Update the configuration in submodule.props.");
  149. }
  150. }
  151. }
  152. }
  153. return !Log.HasLoggedErrors;
  154. }
  155. private static string GetDirectoryName(string path)
  156. => Path.GetFileName(path.TrimEnd(new[] { '\\', '/' }));
  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. }