-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
ProjectCheckerUtil.cs
277 lines (240 loc) · 11.2 KB
/
ProjectCheckerUtil.cs
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace BuildBoss
{
internal sealed class ProjectCheckerUtil : ICheckerUtil
{
private readonly ProjectData _data;
private readonly ProjectUtil _projectUtil;
private readonly Dictionary<ProjectKey, ProjectData> _solutionMap;
private readonly bool _isPrimarySolution;
internal ProjectFileType ProjectType => _data.ProjectFileType;
internal string ProjectFilePath => _data.FilePath;
internal ProjectCheckerUtil(ProjectData data, Dictionary<ProjectKey, ProjectData> solutionMap, bool isPrimarySolution)
{
_data = data;
_projectUtil = data.ProjectUtil;
_solutionMap = solutionMap;
_isPrimarySolution = isPrimarySolution;
}
public bool Check(TextWriter textWriter)
{
var allGood = true;
if (ProjectType is ProjectFileType.CSharp or ProjectFileType.Basic)
{
if (!_projectUtil.IsNewSdk())
{
textWriter.WriteLine($"Project must new .NET SDK based");
allGood = false;
}
// Properties that aren't related to build but instead artifacts of Visual Studio.
allGood &= CheckForProperty(textWriter, "RestorePackages");
allGood &= CheckForProperty(textWriter, "SolutionDir");
allGood &= CheckForProperty(textWriter, "FileAlignment");
allGood &= CheckForProperty(textWriter, "FileUpgradeFlags");
allGood &= CheckForProperty(textWriter, "UpgradeBackupLocation");
allGood &= CheckForProperty(textWriter, "OldToolsVersion");
allGood &= CheckForProperty(textWriter, "SchemaVersion");
// Centrally controlled properties
allGood &= CheckForProperty(textWriter, "Configuration");
allGood &= CheckForProperty(textWriter, "CheckForOverflowUnderflow");
allGood &= CheckForProperty(textWriter, "RemoveIntegerChecks");
allGood &= CheckForProperty(textWriter, "Deterministic");
allGood &= CheckForProperty(textWriter, "HighEntropyVA");
allGood &= CheckForProperty(textWriter, "DocumentationFile");
// Items which are not necessary anymore in the new SDK
allGood &= CheckForProperty(textWriter, "ProjectGuid");
allGood &= CheckForProperty(textWriter, "ProjectTypeGuids");
allGood &= CheckForProperty(textWriter, "TargetFrameworkProfile");
allGood &= CheckTargetFrameworks(textWriter);
allGood &= CheckProjectReferences(textWriter);
if (_isPrimarySolution)
{
allGood &= CheckInternalsVisibleTo(textWriter);
}
allGood &= CheckDeploymentSettings(textWriter);
}
return allGood;
}
private bool CheckForProperty(TextWriter textWriter, string propertyName)
{
foreach (var element in _projectUtil.GetAllPropertyGroupElements())
{
if (element.Name.LocalName == propertyName)
{
textWriter.WriteLine($"\tDo not use {propertyName}");
return false;
}
}
return true;
}
private bool CheckProjectReferences(TextWriter textWriter)
{
var allGood = true;
var declaredEntryList = _projectUtil.GetDeclaredProjectReferences();
var declaredList = declaredEntryList.Select(x => x.ProjectKey).ToList();
allGood &= CheckProjectReferencesComplete(textWriter, declaredList);
allGood &= CheckUnitTestReferenceRestriction(textWriter, declaredList);
allGood &= CheckNoGuidsOnProjectReferences(textWriter, declaredEntryList);
return allGood;
}
private bool CheckNoGuidsOnProjectReferences(TextWriter textWriter, List<ProjectReferenceEntry> entryList)
{
var allGood = true;
foreach (var entry in entryList)
{
if (entry.Project != null)
{
textWriter.WriteLine($"Project reference for {entry.ProjectKey.FileName} should not have a GUID");
allGood = false;
}
}
return allGood;
}
private bool CheckInternalsVisibleTo(TextWriter textWriter)
{
var allGood = true;
foreach (var internalsVisibleTo in _projectUtil.GetInternalsVisibleTo())
{
if (string.Equals(internalsVisibleTo.LoadsWithinVisualStudio, "false", StringComparison.OrdinalIgnoreCase))
{
// IVTs explicitly declared with LoadsWithinVisualStudio="false" are allowed
continue;
}
if (_projectUtil.Key.FileName.StartsWith("Microsoft.CodeAnalysis.ExternalAccess."))
{
// External access layer may have external IVTs
continue;
}
if (!string.IsNullOrEmpty(internalsVisibleTo.WorkItem))
{
if (!Uri.TryCreate(internalsVisibleTo.WorkItem, UriKind.Absolute, out _))
{
textWriter.WriteLine($"InternalsVisibleTo for external assembly '{internalsVisibleTo.TargetAssembly}' does not have a valid URI specified for {nameof(InternalsVisibleTo.WorkItem)}.");
allGood = false;
}
// A work item is tracking elimination of this IVT
continue;
}
var builtByThisRepository = _solutionMap.Values.Any(projectData => GetAssemblyName(projectData) == internalsVisibleTo.TargetAssembly);
if (!builtByThisRepository)
{
textWriter.WriteLine($"InternalsVisibleTo not allowed for external assembly '{internalsVisibleTo.TargetAssembly}' that may load within Visual Studio.");
allGood = false;
}
}
return allGood;
// Local functions
static string GetAssemblyName(ProjectData projectData)
{
return projectData.ProjectUtil.FindSingleProperty("AssemblyName")?.Value.Trim()
?? Path.GetFileNameWithoutExtension(projectData.FileName);
}
}
private bool CheckDeploymentSettings(TextWriter textWriter)
{
var allGood = CheckForProperty(textWriter, "CopyNuGetImplementations");
allGood &= CheckForProperty(textWriter, "UseCommonOutputDirectory");
return allGood;
}
/// <summary>
/// It's important that every reference be included in the solution. MSBuild does not necessarily
/// apply all configuration entries to projects which are compiled via references but not included
/// in the solution.
/// </summary>
private bool CheckProjectReferencesComplete(TextWriter textWriter, IEnumerable<ProjectKey> declaredReferences)
{
var allGood = true;
foreach (var key in declaredReferences)
{
if (!_solutionMap.ContainsKey(key))
{
textWriter.WriteLine($"Project reference {key.FileName} is not included in the solution");
allGood = false;
}
}
return allGood;
}
/// <summary>
/// Unit test projects should not reference each other. In order for unit tests to be run / F5 they must be
/// modeled as deployment projects. Having Unit Tests reference each other hurts that because it ends up
/// putting two copies of the unit test DLL into the UnitTest folder:
///
/// 1. UnitTests\Current\TheUnitTest\TheUnitTest.dll
/// 2. UnitTests\Current\TheOtherTests\
/// TheUnitTests.dll
/// TheOtherTests.dll
///
/// This is problematic as all of our tools do directory based searches for unit test DLLs. Hence they end up
/// getting counted twice.
///
/// Consideration was given to fixing up all of the tools but it felt like fighting against the grain. Pretty
/// much every repo has this practice.
/// </summary>
private bool CheckUnitTestReferenceRestriction(TextWriter textWriter, IEnumerable<ProjectKey> declaredReferences)
{
if (!_data.IsTestProject)
{
return true;
}
var allGood = true;
foreach (var key in declaredReferences)
{
if (!_solutionMap.TryGetValue(key, out var projectData))
{
continue;
}
if (projectData.ProjectUtil.IsTestProject)
{
textWriter.WriteLine($"Cannot reference {key.FileName} as it is another unit test project");
allGood = false;
}
}
return allGood;
}
private bool CheckTargetFrameworks(TextWriter textWriter)
{
var allGood = true;
foreach (var targetFramework in _projectUtil.GetAllTargetFrameworks())
{
// !!!NOTE!!!
// This check ensures that projects match the target framework expectations laid out in
// Target Framework Strategy.md. Before changing this list, even simply adding a new
// tfm, please consult with the infrastructure team so they can validate the change is in
// line with how the product is constructed.
switch (targetFramework)
{
case "net472":
case "netstandard2.0":
case "$(NetRoslyn)":
case "$(NetRoslynNext)":
case "$(NetRoslynSourceBuild)":
case "$(NetRoslynToolset)":
case "$(NetRoslynAll)":
case "$(NetVS)":
case "$(NetVS)-windows":
case "$(NetVSCode)":
case "$(NetVSShared)":
continue;
case "$(NetRoslynBuildHostNetCoreVersion)":
{
// This property should only be used in one specific project
if (_data.FileName == "Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.csproj")
continue;
else
break;
}
}
textWriter.WriteLine($"TargetFramework {targetFramework} is not supported in this build");
allGood = false;
}
return allGood;
}
}
}