-
Notifications
You must be signed in to change notification settings - Fork 694
/
DependencyGraphRestoreUtility.cs
446 lines (396 loc) · 18.4 KB
/
DependencyGraphRestoreUtility.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// 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.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Commands;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.ProjectManagement;
using NuGet.ProjectManagement.Projects;
using NuGet.ProjectModel;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
namespace NuGet.PackageManagement
{
/// <summary>
/// Supporting methods for restoring sets of projects that implement <see cref="IDependencyGraphProject"/>. This
/// code is used by Visual Studio to execute restores for solutions that have mixtures of UWP project.json,
/// packages.config, and PackageReference-type projects.
/// </summary>
public static class DependencyGraphRestoreUtility
{
/// <summary>
/// Restore a solution and cache the dg spec to context.
/// </summary>
public static Task<IReadOnlyList<RestoreSummary>> RestoreAsync(
ISolutionManager solutionManager,
DependencyGraphSpec dgSpec,
DependencyGraphCacheContext context,
RestoreCommandProvidersCache providerCache,
Action<SourceCacheContext> cacheContextModifier,
IEnumerable<SourceRepository> sources,
Guid parentId,
bool forceRestore,
bool isRestoreOriginalAction,
ILogger log,
CancellationToken token)
{
return RestoreAsync(
dgSpec,
context,
providerCache,
cacheContextModifier,
sources,
parentId,
forceRestore,
isRestoreOriginalAction,
additionalMessages: null,
progressReporter: null,
log,
token);
}
/// <summary>
/// Restore a solution and cache the dg spec to context.
/// </summary>
[Obsolete("This method will be removed in a future release. Use other one of the other RestoreAsync methods.")]
public static Task<IReadOnlyList<RestoreSummary>> RestoreAsync(
ISolutionManager solutionManager,
DependencyGraphSpec dgSpec,
DependencyGraphCacheContext context,
RestoreCommandProvidersCache providerCache,
Action<SourceCacheContext> cacheContextModifier,
IEnumerable<SourceRepository> sources,
Guid parentId,
bool forceRestore,
bool isRestoreOriginalAction,
IReadOnlyList<IAssetsLogMessage> additionalMessages,
ILogger log,
CancellationToken token)
{
return RestoreAsync(
dgSpec,
context,
providerCache,
cacheContextModifier,
sources,
parentId,
forceRestore,
isRestoreOriginalAction,
additionalMessages,
progressReporter: null,
log,
token
);
}
/// <summary>
/// Restore a solution and cache the dg spec to context.
/// </summary>
public static async Task<IReadOnlyList<RestoreSummary>> RestoreAsync(
DependencyGraphSpec dgSpec,
DependencyGraphCacheContext context,
RestoreCommandProvidersCache providerCache,
Action<SourceCacheContext> cacheContextModifier,
IEnumerable<SourceRepository> sources,
Guid parentId,
bool forceRestore,
bool isRestoreOriginalAction,
IReadOnlyList<IAssetsLogMessage> additionalMessages,
IRestoreProgressReporter progressReporter,
ILogger log,
CancellationToken token)
{
// Check if there are actual projects to restore before running.
if (dgSpec.Restore.Count > 0)
{
using (var sourceCacheContext = new SourceCacheContext())
{
// Update cache context
cacheContextModifier(sourceCacheContext);
var restoreContext = GetRestoreArgs(
context,
providerCache,
sourceCacheContext,
sources,
dgSpec,
parentId,
forceRestore,
isRestoreOriginalAction,
restoreForceEvaluate: false,
additionalMessages,
progressReporter: progressReporter);
var restoreSummaries = await RestoreRunner.RunAsync(restoreContext, token);
RestoreSummary.Log(log, restoreSummaries);
return restoreSummaries;
}
}
return new List<RestoreSummary>();
}
[Obsolete]
public static string GetDefaultDGSpecFileName()
{
return Path.Combine(
NuGetEnvironment.GetFolderPath(NuGetFolderPath.Temp),
"nuget-dg",
"nugetSpec.dg");
}
/// <summary>
/// Restore a project without writing the lock file
/// </summary>
internal static async Task<RestoreResultPair> PreviewRestoreAsync(
ISolutionManager solutionManager,
BuildIntegratedNuGetProject project,
PackageSpec packageSpec,
DependencyGraphCacheContext context,
RestoreCommandProvidersCache providerCache,
Action<SourceCacheContext> cacheContextModifier,
IEnumerable<SourceRepository> sources,
Guid parentId,
CancellationToken token)
{
token.ThrowIfCancellationRequested();
// Add the new spec to the dg file and fill in the rest.
var dgFile = await GetSolutionRestoreSpec(solutionManager, context);
dgFile = dgFile.WithoutRestores()
.WithReplacedSpec(packageSpec);
dgFile.AddRestore(project.MSBuildProjectPath);
using (var sourceCacheContext = new SourceCacheContext())
{
// Update cache context
cacheContextModifier(sourceCacheContext);
// Settings passed here will be used to populate the restore requests.
var restoreContext = GetRestoreArgs(
context,
providerCache,
sourceCacheContext,
sources,
dgFile,
parentId,
forceRestore: true,
isRestoreOriginalAction: false,
restoreForceEvaluate: true,
additionalMessasges: null,
progressReporter: null);
var requests = await RestoreRunner.GetRequests(restoreContext);
var results = await RestoreRunner.RunWithoutCommit(requests, restoreContext);
return results.Single();
}
}
/// <summary>
/// Restore many projects without writing the lock file
/// SourceRepositories(sources) is only used for the CachingSourceProvider, the project-specific sources will still be resolved in RestoreRunner.
/// </summary>
internal static async Task<IEnumerable<RestoreResultPair>> PreviewRestoreProjectsAsync(
ISolutionManager solutionManager,
IEnumerable<BuildIntegratedNuGetProject> projects,
IEnumerable<PackageSpec> updatedNugetPackageSpecs,
DependencyGraphCacheContext context,
RestoreCommandProvidersCache providerCache,
Action<SourceCacheContext> cacheContextModifier,
IEnumerable<SourceRepository> sources,
Guid parentId,
ILogger log,
CancellationToken token)
{
token.ThrowIfCancellationRequested();
// Add the new spec to the dg file and fill in the rest.
var dgFile = await GetSolutionRestoreSpec(solutionManager, context);
dgFile = dgFile.WithoutRestores()
.WithPackageSpecs(updatedNugetPackageSpecs);
foreach (var project in projects)
{
dgFile.AddRestore(project.MSBuildProjectPath);
}
using (var sourceCacheContext = new SourceCacheContext())
{
// Update cache context
cacheContextModifier(sourceCacheContext);
// Settings passed here will be used to populate the restore requests.
var restoreContext = GetRestoreArgs(
context,
providerCache,
sourceCacheContext,
sources,
dgFile,
parentId,
forceRestore: true,
isRestoreOriginalAction: false,
restoreForceEvaluate: true,
additionalMessasges: null,
progressReporter: null);
var requests = await RestoreRunner.GetRequests(restoreContext);
var results = await RestoreRunner.RunWithoutCommit(requests, restoreContext);
return results;
}
}
/// <summary>
/// Restore a build integrated project(PackageReference and Project.Json only) and update the assets file
/// </summary>
[Obsolete("This is an unused method and will be removed in a future release.")]
public static async Task<RestoreResult> RestoreProjectAsync(
ISolutionManager solutionManager,
BuildIntegratedNuGetProject project,
DependencyGraphCacheContext context,
RestoreCommandProvidersCache providerCache,
Action<SourceCacheContext> cacheContextModifier,
IEnumerable<SourceRepository> sources,
Guid parentId,
ILogger log,
CancellationToken token)
{
if (project == null)
{
throw new ArgumentNullException(nameof(project));
}
// Restore
var specs = await project.GetPackageSpecsAsync(context);
var spec = specs.Single(e => e.RestoreMetadata.ProjectStyle == ProjectStyle.PackageReference
|| e.RestoreMetadata.ProjectStyle == ProjectStyle.ProjectJson); // Do not restore global tools Project Style in VS.
var result = await PreviewRestoreAsync(
solutionManager,
project,
spec,
context,
providerCache,
cacheContextModifier,
sources,
parentId,
token);
// Throw before writing if this has been canceled
token.ThrowIfCancellationRequested();
// Write out the lock file and msbuild files
var summary = await RestoreRunner.CommitAsync(result, token);
RestoreSummary.Log(log, new[] { summary });
return result.Result;
}
public static bool IsRestoreRequired(
DependencyGraphSpec solutionDgSpec)
{
if (solutionDgSpec.Restore.Count < 1)
{
// Nothing to restore
return false;
}
// NO Op will be checked in the restore command
return true;
}
public static async Task<PackageSpec> GetProjectSpec(IDependencyGraphProject project, DependencyGraphCacheContext context)
{
var specs = await project.GetPackageSpecsAsync(context);
var projectSpec = specs.Where(e => e.RestoreMetadata.ProjectStyle != ProjectStyle.Standalone
&& e.RestoreMetadata.ProjectStyle != ProjectStyle.DotnetCliTool)
.FirstOrDefault();
return projectSpec;
}
public static async Task<DependencyGraphSpec> GetSolutionRestoreSpec(
ISolutionManager solutionManager,
DependencyGraphCacheContext context)
{
var (dgSpec, _) = await GetSolutionRestoreSpecAndAdditionalMessages(solutionManager, context);
return dgSpec;
}
public static async Task<(DependencyGraphSpec dgSpec, IReadOnlyList<IAssetsLogMessage> additionalMessages)> GetSolutionRestoreSpecAndAdditionalMessages(
ISolutionManager solutionManager,
DependencyGraphCacheContext context)
{
var dgSpec = new DependencyGraphSpec();
List<IAssetsLogMessage> allAdditionalMessages = null;
var projects = (await solutionManager.GetNuGetProjectsAsync()).OfType<IDependencyGraphProject>().ToList();
var knownProjects = new HashSet<string>(projects.Select(e => e.MSBuildProjectPath), PathUtility.GetStringComparerBasedOnOS());
for (var i = 0; i < projects.Count; i++)
{
var (packageSpecs, projectAdditionalMessages) = await projects[i].GetPackageSpecsAndAdditionalMessagesAsync(context);
if (projectAdditionalMessages != null && projectAdditionalMessages.Count > 0)
{
if (allAdditionalMessages == null)
{
allAdditionalMessages = new List<IAssetsLogMessage>();
}
allAdditionalMessages.AddRange(projectAdditionalMessages);
}
foreach (var packageSpec in packageSpecs)
{
dgSpec.AddProject(packageSpec);
if (packageSpec.RestoreMetadata.ProjectStyle == ProjectStyle.PackageReference ||
packageSpec.RestoreMetadata.ProjectStyle == ProjectStyle.ProjectJson ||
packageSpec.RestoreMetadata.ProjectStyle == ProjectStyle.DotnetCliTool ||
packageSpec.RestoreMetadata.ProjectStyle == ProjectStyle.Standalone) // Don't add global tools to restore specs for solutions
{
dgSpec.AddRestore(packageSpec.RestoreMetadata.ProjectUniqueName);
var projFileName = Path.GetFileName(packageSpec.RestoreMetadata.ProjectPath);
var dgFileName = DependencyGraphSpec.GetDGSpecFileName(projFileName);
var outputPath = packageSpec.RestoreMetadata.OutputPath;
if (!string.IsNullOrEmpty(outputPath))
{
for (int frameworkCount = 0; frameworkCount < packageSpec.RestoreMetadata.TargetFrameworks.Count; frameworkCount++)
{
for (var projectReferenceCount = 0; projectReferenceCount < packageSpec.RestoreMetadata.TargetFrameworks[frameworkCount].ProjectReferences.Count; projectReferenceCount++)
{
if (!knownProjects.Contains(packageSpec.RestoreMetadata.TargetFrameworks[frameworkCount].ProjectReferences[projectReferenceCount].ProjectPath))
{
var persistedDGSpecPath = Path.Combine(outputPath, dgFileName);
if (File.Exists(persistedDGSpecPath))
{
var persistedDGSpec = DependencyGraphSpec.Load(persistedDGSpecPath);
foreach (var dependentPackageSpec in persistedDGSpec.Projects.Where(e => !knownProjects.Contains(e.RestoreMetadata.ProjectPath)))
{
// Include all the missing projects from the closure.
// Figuring out exactly what we need would be too and an overkill. That will happen later in the DependencyGraphSpecRequestProvider
knownProjects.Add(dependentPackageSpec.RestoreMetadata.ProjectPath);
dgSpec.AddProject(dependentPackageSpec);
}
}
}
}
}
}
}
}
}
// Return dg file
return (dgSpec, allAdditionalMessages);
}
/// <summary>
/// Create a restore args.
/// </summary>
private static RestoreArgs GetRestoreArgs(
DependencyGraphCacheContext context,
RestoreCommandProvidersCache providerCache,
SourceCacheContext sourceCacheContext,
IEnumerable<SourceRepository> sources,
DependencyGraphSpec dgFile,
Guid parentId,
bool forceRestore,
bool isRestoreOriginalAction,
bool restoreForceEvaluate,
IReadOnlyList<IAssetsLogMessage> additionalMessasges,
IRestoreProgressReporter progressReporter)
{
#pragma warning disable CS0618 // Type or member is obsolete
var caching = new CachingSourceProvider(new PackageSourceProvider(context.Settings, enablePackageSourcesChangedEvent: false));
#pragma warning restore CS0618 // Type or member is obsolete
foreach (var source in sources)
{
caching.AddSourceRepository(source);
}
var dgProvider = new DependencyGraphSpecRequestProvider(providerCache, dgFile, context.Settings);
var restoreContext = new RestoreArgs()
{
CacheContext = sourceCacheContext,
PreLoadedRequestProviders = new List<IPreLoadedRestoreRequestProvider>() { dgProvider },
Log = context.Logger,
AllowNoOp = !forceRestore,
CachingSourceProvider = caching,
ParentId = parentId,
IsRestoreOriginalAction = isRestoreOriginalAction,
RestoreForceEvaluate = restoreForceEvaluate,
AdditionalMessages = additionalMessasges,
ProgressReporter = progressReporter,
};
return restoreContext;
}
}
}