-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathEvaluationResult.cs
More file actions
278 lines (227 loc) · 12.1 KB
/
EvaluationResult.cs
File metadata and controls
278 lines (227 loc) · 12.1 KB
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
278
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.Build.Execution;
using Microsoft.Build.Graph;
using Microsoft.DotNet.HotReload;
using Microsoft.Extensions.Logging;
namespace Microsoft.DotNet.Watch;
internal sealed class EvaluationResult(
LoadedProjectGraph projectGraph,
IReadOnlyDictionary<ProjectInstanceId, ProjectInstance> restoredProjectInstances,
IReadOnlyDictionary<string, FileItem> files,
IReadOnlyDictionary<ProjectInstanceId, StaticWebAssetsManifest> staticWebAssetsManifests)
{
public IReadOnlyDictionary<string, FileItem> Files => files;
public LoadedProjectGraph ProjectGraph => projectGraph;
public ProjectBuildManager BuildManager => projectGraph.BuildManager;
public readonly FilePathExclusions ItemExclusions
= projectGraph != null ? FilePathExclusions.Create(projectGraph.Graph) : FilePathExclusions.Empty;
public IReadOnlyDictionary<ProjectInstanceId, StaticWebAssetsManifest> StaticWebAssetsManifests
=> staticWebAssetsManifests;
public IReadOnlyDictionary<ProjectInstanceId, ProjectInstance> RestoredProjectInstances
=> restoredProjectInstances;
public void WatchFileItems(FileWatcher fileWatcher)
{
fileWatcher.WatchContainingDirectories(Files.Keys, includeSubdirectories: true);
fileWatcher.WatchContainingDirectories(
StaticWebAssetsManifests.Values.SelectMany(static manifest => manifest.DiscoveryPatterns.Select(static pattern => pattern.Directory)),
includeSubdirectories: true);
}
public static ImmutableDictionary<string, string> GetGlobalBuildProperties(IEnumerable<string> buildArguments, EnvironmentOptions environmentOptions)
{
// See https://github.com/dotnet/project-system/blob/main/docs/well-known-project-properties.md
return BuildUtilities.ParseBuildProperties(buildArguments)
.ToImmutableDictionary(keySelector: arg => arg.key, elementSelector: arg => arg.value)
.SetItem(PropertyNames.DotNetWatchBuild, "true")
.SetItem(PropertyNames.DesignTimeBuild, "true")
.SetItem(PropertyNames.SkipCompilerExecution, "true")
.SetItem(PropertyNames.ProvideCommandLineArgs, "true")
// this will force CoreCompile task to execute and return command line args even if all inputs and outputs are up to date:
.SetItem(PropertyNames.NonExistentFile, "__NonExistentSubDir__\\__NonExistentFile__");
}
/// <summary>
/// Loads project graph and performs design-time build.
/// </summary>
public static async ValueTask<EvaluationResult?> TryCreateAsync(
LoadedProjectGraph projectGraph,
ILogger logger,
GlobalOptions globalOptions,
EnvironmentOptions environmentOptions,
string? mainProjectTargetFramework,
bool restore,
CancellationToken cancellationToken)
{
logger.Log(MessageDescriptor.LoadingProjects);
var projectLoadingStopwatch = Stopwatch.StartNew();
var stopwatch = Stopwatch.StartNew();
if (restore)
{
var restoreRequests = projectGraph.Graph.GraphRoots.Select(node => BuildRequest.Create(node.ProjectInstance, [TargetNames.Restore])).ToArray();
if (await projectGraph.BuildManager.BuildAsync(
restoreRequests,
onFailure: failedInstance =>
{
logger.LogError("Failed to restore project '{Path}'.", failedInstance.FullPath);
// terminate build on first failure:
return false;
},
operationName: "Restore",
cancellationToken) is [])
{
return null;
}
logger.LogDebug("Projects restored in {Time}s.", stopwatch.Elapsed.TotalSeconds.ToString("0.0"));
}
stopwatch.Restart();
// Capture the snapshot of original project instances after Restore target has been run.
// These instances can be used to evaluate additional targets (e.g. deployment) if needed.
var restoredProjectInstances = projectGraph.Graph.ProjectNodes.ToDictionary(
keySelector: node => node.ProjectInstance.GetId(),
elementSelector: node => node.ProjectInstance.DeepCopy());
// Update the project instances of the graph with design-time build results.
// The properties and items set by DTB will be used by the Workspace to create Roslyn representation of projects.
var buildRequests = CreateDesignTimeBuildRequests(projectGraph.Graph, mainProjectTargetFramework, environmentOptions.SuppressHandlingStaticWebAssets).ToImmutableArray();
var buildResults = await projectGraph.BuildManager.BuildAsync(
buildRequests,
onFailure: failedInstance =>
{
logger.LogError("Failed to build project '{Path}'.", failedInstance.FullPath);
// terminate build on first failure:
return false;
},
operationName: "DesignTimeBuild",
cancellationToken);
if (buildResults is [])
{
return null;
}
logger.LogDebug("Design-time build completed in {Time}s.", stopwatch.Elapsed.TotalSeconds.ToString("0.0"));
logger.Log(MessageDescriptor.LoadedProjects, projectGraph.Graph.ProjectNodes.Count, projectLoadingStopwatch.Elapsed.TotalSeconds);
ProcessBuildResults(buildResults, logger, out var fileItems, out var staticWebAssetManifests);
BuildReporter.ReportWatchedFiles(logger, fileItems);
return new EvaluationResult(projectGraph, restoredProjectInstances, fileItems, staticWebAssetManifests);
}
// internal for testing
internal static IEnumerable<BuildRequest<object?>> CreateDesignTimeBuildRequests(ProjectGraph graph, string? mainProjectTargetFramework, bool suppressStaticWebAssets)
{
return from node in graph.ProjectNodesTopologicallySorted
let targetFramework = node.ProjectInstance.GetTargetFramework()
// skip outer-build projects
where targetFramework != ""
// skip root projects that do not match main project TFM, if specified:
where mainProjectTargetFramework == null ||
targetFramework == mainProjectTargetFramework ||
HasParentWithTargetFramework(node)
let targets = GetBuildTargets(node.ProjectInstance, suppressStaticWebAssets)
where targets is not []
select BuildRequest.Create(node.ProjectInstance, [.. targets]);
static bool HasParentWithTargetFramework(ProjectGraphNode node)
=> node.ReferencingProjects.Any(p => p.ProjectInstance.GetTargetFramework() != "");
}
private static void ProcessBuildResults(
ImmutableArray<BuildResult<object?>> buildResults,
ILogger logger,
out IReadOnlyDictionary<string, FileItem> fileItems,
out IReadOnlyDictionary<ProjectInstanceId, StaticWebAssetsManifest> staticWebAssetManifests)
{
var fileItemsBuilder = new Dictionary<string, FileItem>();
var staticWebAssetManifestsBuilder = new Dictionary<ProjectInstanceId, StaticWebAssetsManifest>();
foreach (var buildResult in buildResults)
{
Debug.Assert(buildResult.IsSuccess);
var projectInstance = buildResult.ProjectInstance;
Debug.Assert(projectInstance != null);
// command line args items should be available:
Debug.Assert(
!Path.GetExtension(projectInstance.FullPath).Equals(".csproj", PathUtilities.OSSpecificPathComparison) ||
projectInstance.GetItems("CscCommandLineArgs").Any());
var projectPath = projectInstance.FullPath;
var projectDirectory = Path.GetDirectoryName(projectPath)!;
if (buildResult.TargetResults.ContainsKey(TargetNames.GenerateComputedBuildStaticWebAssets) &&
projectInstance.GetIntermediateOutputDirectory() is { } outputDir &&
StaticWebAssetsManifest.TryParseFile(Path.Combine(outputDir, StaticWebAsset.ManifestFileName), logger) is { } manifest)
{
staticWebAssetManifestsBuilder.Add(projectInstance.GetId(), manifest);
// watch asset files, but not bundle files as they are regenarated when scoped CSS files are updated:
foreach (var (relativeUrl, filePath) in manifest.UrlToPathMap)
{
if (!StaticWebAsset.IsCompressedAssetFile(filePath) && !StaticWebAsset.IsScopedCssBundleFile(filePath))
{
AddFile(filePath, staticWebAssetRelativeUrl: relativeUrl);
}
}
}
// Adds file items for scoped css files.
// Scoped css files are bundled into a single entry per project that is represented in the static web assets manifest,
// but we need to watch the original individual files.
if (buildResult.TargetResults.ContainsKey(TargetNames.ResolveScopedCssInputs))
{
foreach (var item in projectInstance.GetItems(ItemNames.ScopedCssInput))
{
AddFile(item.EvaluatedInclude, staticWebAssetRelativeUrl: null);
}
}
// Add Watch items after other items so that we don't override properties set above.
var items = projectInstance.GetItems(ItemNames.Compile)
.Concat(projectInstance.GetItems(ItemNames.AdditionalFiles))
.Concat(projectInstance.GetItems(ItemNames.Watch));
foreach (var item in items)
{
AddFile(item.EvaluatedInclude, staticWebAssetRelativeUrl: null);
}
void AddFile(string relativePath, string? staticWebAssetRelativeUrl)
{
var filePath = Path.GetFullPath(Path.Combine(projectDirectory, relativePath));
if (!fileItemsBuilder.TryGetValue(filePath, out var existingFile))
{
fileItemsBuilder.Add(filePath, new FileItem
{
FilePath = filePath,
ContainingProjectPaths = [projectPath],
StaticWebAssetRelativeUrl = staticWebAssetRelativeUrl,
});
}
else if (!existingFile.ContainingProjectPaths.Contains(projectPath))
{
// linked files might be included to multiple projects:
existingFile.ContainingProjectPaths.Add(projectPath);
}
}
}
fileItems = fileItemsBuilder;
staticWebAssetManifests = staticWebAssetManifestsBuilder;
}
private static string[] GetBuildTargets(ProjectInstance projectInstance, bool suppressStaticWebAssets)
{
var compileTarget = projectInstance.Targets.ContainsKey(TargetNames.CompileDesignTime)
? TargetNames.CompileDesignTime
: projectInstance.Targets.ContainsKey(TargetNames.Compile)
? TargetNames.Compile
: null;
if (compileTarget == null)
{
return [];
}
var targets = new List<string>
{
compileTarget
};
if (!suppressStaticWebAssets)
{
// generates static file asset manifest
if (projectInstance.Targets.ContainsKey(TargetNames.GenerateComputedBuildStaticWebAssets))
{
targets.Add(TargetNames.GenerateComputedBuildStaticWebAssets);
}
// populates ScopedCssInput items:
if (projectInstance.Targets.ContainsKey(TargetNames.ResolveScopedCssInputs))
{
targets.Add(TargetNames.ResolveScopedCssInputs);
}
}
targets.AddRange(projectInstance.GetStringListPropertyValue(PropertyNames.CustomCollectWatchItems));
return [.. targets];
}
}