1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using NuGet.Frameworks;
using NuGet.Versioning;
using RepoTools.BuildGraph;
using RepoTasks.ProjectModel;
using RepoTasks.Utilities;
namespace RepoTasks
{
public class AnalyzeBuildGraph : Task, ICancelableTask
{
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
/// <summary>
/// Repositories that we are building new versions of.
/// </summary>
[Required]
public ITaskItem[] Solutions { get; set; }
[Required]
public ITaskItem[] Artifacts { get; set; }
[Required]
public ITaskItem[] Dependencies { get; set; }
[Required]
public string Properties { get; set; }
public string StartGraphAt { get; set; }
/// <summary>
/// The order in which to build repositories
/// </summary>
[Output]
public ITaskItem[] RepositoryBuildOrder { get; set; }
public void Cancel()
{
_cts.Cancel();
}
public override bool Execute()
{
var packageArtifacts = Artifacts.Select(ArtifactInfo.Parse)
.OfType<ArtifactInfo.Package>()
.Where(p => !p.IsSymbolsArtifact);
var factory = new SolutionInfoFactory(Log, BuildEngine5);
var props = MSBuildListSplitter.GetNamedProperties(Properties);
Log.LogMessage(MessageImportance.High, $"Beginning cross-repo analysis on {Solutions.Length} solutions. Hang tight...");
var solutions = factory.Create(Solutions, props, _cts.Token);
Log.LogMessage($"Found {solutions.Count} and {solutions.Sum(p => p.Projects.Count)} projects");
if (_cts.IsCancellationRequested)
{
return false;
}
EnsureConsistentGraph(packageArtifacts, solutions);
RepositoryBuildOrder = GetRepositoryBuildOrder(packageArtifacts, solutions.Where(s => s.ShouldBuild));
return !Log.HasLoggedErrors;
}
private struct VersionMismatch
{
public SolutionInfo Solution;
public ProjectInfo Project;
public string PackageId;
public string ActualVersion;
public NuGetVersion ExpectedVersion;
}
private void EnsureConsistentGraph(IEnumerable<ArtifactInfo.Package> packages, IEnumerable<SolutionInfo> solutions)
{
// ensure versions cascade
var buildPackageMap = packages.ToDictionary(p => p.PackageInfo.Id, p => p, StringComparer.OrdinalIgnoreCase);
var dependencyMap = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
foreach (var dep in Dependencies)
{
if (!dependencyMap.TryGetValue(dep.ItemSpec, out var versions))
{
dependencyMap[dep.ItemSpec] = versions = new List<string>();
}
else if (dep.GetMetadata("NoWarn") == null || dep.GetMetadata("NoWarn").IndexOf("KRB" + KoreBuildErrors.MultipleExternalDependencyVersions) < 0)
{
Log.LogKoreBuildWarning(
KoreBuildErrors.MultipleExternalDependencyVersions,
message: $"Multiple versions of external dependency '{dep.ItemSpec}' are defined. In most cases, there should only be one version of external dependencies.");
}
versions.Add(dep.GetMetadata("Version"));
}
var inconsistentVersions = new List<VersionMismatch>();
var reposThatShouldPatch = new HashSet<string>();
// TODO cleanup the 4-deep nested loops
foreach (var solution in solutions)
foreach (var project in solution.Projects)
foreach (var tfm in project.Frameworks)
foreach (var dependency in tfm.Dependencies)
{
if (!buildPackageMap.TryGetValue(dependency.Key, out var package))
{
// This dependency is not one of the packages that will be compiled by this run of Universe.
if (!dependencyMap.TryGetValue(dependency.Key, out var externalVersions)
|| !externalVersions.Contains(dependency.Value.Version))
{
Log.LogKoreBuildError(
project.FullPath,
KoreBuildErrors.UndefinedExternalDependency,
message: $"Undefined external dependency on {dependency.Key}/{dependency.Value.Version}");
}
continue;
}
var refVersion = VersionRange.Parse(dependency.Value.Version);
if (refVersion.IsFloating && refVersion.Float.Satisfies(package.PackageInfo.Version))
{
continue;
}
else if (package.PackageInfo.Version.Equals(refVersion.MinVersion))
{
continue;
}
if (!solution.ShouldBuild)
{
reposThatShouldPatch.Add(Path.GetFileName(Path.GetDirectoryName(solution.FullPath)));
}
inconsistentVersions.Add(new VersionMismatch
{
Solution = solution,
Project = project,
PackageId = dependency.Key,
ActualVersion = dependency.Value.Version,
ExpectedVersion = package.PackageInfo.Version,
});
}
if (inconsistentVersions.Count != 0)
{
var sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine($"Repos are inconsistent. The following projects have PackageReferences that should be updated");
foreach (var solution in inconsistentVersions.GroupBy(p => p.Solution.FullPath))
{
sb.Append(" - ").AppendLine(Path.GetFileName(solution.Key));
foreach (var project in solution.GroupBy(p => p.Project.FullPath))
{
sb.Append(" - ").AppendLine(Path.GetFileName(project.Key));
foreach (var mismatchedReference in project)
{
sb.AppendLine($" + {mismatchedReference.PackageId}/{{{mismatchedReference.ActualVersion} => {mismatchedReference.ExpectedVersion}}}");
}
}
}
sb.AppendLine();
Log.LogMessage(MessageImportance.High, sb.ToString());
Log.LogError("Package versions are inconsistent. See build log for details.");
}
foreach (var repo in reposThatShouldPatch)
{
Log.LogError($"{repo} should not be a 'ShippedRepository'. Version changes in other repositories mean it should be patched to perserve cascading version upgrades.");
}
}
private ITaskItem[] GetRepositoryBuildOrder(IEnumerable<ArtifactInfo.Package> artifacts, IEnumerable<SolutionInfo> solutions)
{
var repositories = solutions.Select(s =>
{
var repoName = Path.GetFileName(Path.GetDirectoryName(s.FullPath));
var repo = new Repository(repoName)
{
RootDir = Path.GetDirectoryName(s.FullPath)
};
var packages = artifacts.Where(a => a.RepoName.Equals(repoName, StringComparison.OrdinalIgnoreCase)).ToList();
foreach (var proj in s.Projects)
{
var projectGroup = packages.Any(p => p.PackageInfo.Id == proj.PackageId)
? repo.Projects
: repo.SupportProjects;
projectGroup.Add(new Project(proj.PackageId)
{
Repository = repo,
PackageReferences = new HashSet<string>(proj
.Frameworks
.SelectMany(f => f.Dependencies.Keys)
.Concat(proj.Tools.Select(t => t.Id)), StringComparer.OrdinalIgnoreCase),
});
}
return repo;
}).ToList();
var graph = GraphBuilder.Generate(repositories, StartGraphAt, Log);
var repositoriesWithOrder = new List<(ITaskItem repository, int order)>();
foreach (var repository in repositories)
{
var graphNodeRepository = graph.FirstOrDefault(g => g.Repository.Name == repository.Name);
if (graphNodeRepository == null)
{
// StartGraphAt was specified so the graph is incomplete.
continue;
}
var order = TopologicalSort.GetOrder(graphNodeRepository);
var repositoryTaskItem = new TaskItem(repository.Name);
repositoryTaskItem.SetMetadata("Order", order.ToString());
repositoryTaskItem.SetMetadata("RootPath", repository.RootDir);
repositoriesWithOrder.Add((repositoryTaskItem, order));
}
Log.LogMessage(MessageImportance.High, "Repository build order:");
foreach (var buildGroup in repositoriesWithOrder.GroupBy(r => r.order).OrderBy(g => g.Key))
{
var buildGroupRepos = buildGroup.Select(b => b.repository.ItemSpec);
Log.LogMessage(MessageImportance.High, $"{buildGroup.Key.ToString().PadLeft(2, ' ')}: {string.Join(", ", buildGroupRepos)}");
}
return repositoriesWithOrder
.OrderBy(r => r.order)
.Select(r => r.repository)
.ToArray();
}
}
}
|