forked from NuGet/NuGet.Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestoreTask.cs
169 lines (145 loc) · 5.58 KB
/
RestoreTask.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
// 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.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
using NuGet.Commands;
using NuGet.Common;
namespace NuGet.Build.Tasks
{
/// <summary>
/// .NET Core compatible restore task for PackageReference and UWP project.json projects.
/// </summary>
public class RestoreTask : Microsoft.Build.Utilities.Task, ICancelableTask, IDisposable
{
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
private bool _disposed = false;
/// <summary>
/// DG file entries
/// </summary>
[Required]
public ITaskItem[] RestoreGraphItems { get; set; }
/// <summary>
/// Disable parallel project restores and downloads
/// </summary>
public bool RestoreDisableParallel { get; set; }
/// <summary>
/// Disable the web cache
/// </summary>
public bool RestoreNoCache { get; set; }
/// <summary>
/// Ignore errors from package sources
/// </summary>
public bool RestoreIgnoreFailedSources { get; set; }
/// <summary>
/// Restore all projects.
/// </summary>
public bool RestoreRecursive { get; set; }
/// <summary>
/// Force restore, skip no op
/// </summary>
public bool RestoreForce { get; set; }
/// <summary>
/// Do not display Errors and Warnings to the user.
/// The Warnings and Errors are written into the assets file and will be read by an sdk target.
/// </summary>
public bool HideWarningsAndErrors { get; set; }
/// <summary>
/// Set this property if you want to get an interactive restore
/// </summary>
public bool Interactive { get; set; }
/// <summary>
/// Reevaluate resotre graph even with a lock file, skip no op as well.
/// </summary>
public bool RestoreForceEvaluate { get; set; }
/// <summary>
/// Restore projects using packages.config for dependencies.
/// </summary>
/// <returns></returns>
public bool RestorePackagesConfig { get; set; }
public override bool Execute()
{
#if DEBUG
var debugRestoreTask = Environment.GetEnvironmentVariable("DEBUG_RESTORE_TASK");
if (!string.IsNullOrEmpty(debugRestoreTask) && debugRestoreTask.Equals(bool.TrueString, StringComparison.OrdinalIgnoreCase))
{
Debugger.Launch();
}
#endif
var log = new MSBuildLogger(Log);
// Log inputs
log.LogDebug($"(in) RestoreGraphItems Count '{RestoreGraphItems?.Count() ?? 0}'");
log.LogDebug($"(in) RestoreDisableParallel '{RestoreDisableParallel}'");
log.LogDebug($"(in) RestoreNoCache '{RestoreNoCache}'");
log.LogDebug($"(in) RestoreIgnoreFailedSources '{RestoreIgnoreFailedSources}'");
log.LogDebug($"(in) RestoreRecursive '{RestoreRecursive}'");
log.LogDebug($"(in) RestoreForce '{RestoreForce}'");
log.LogDebug($"(in) HideWarningsAndErrors '{HideWarningsAndErrors}'");
log.LogDebug($"(in) RestoreForceEvaluate '{RestoreForceEvaluate}'");
log.LogDebug($"(in) RestorePackagesConfig '{RestorePackagesConfig}'");
try
{
return ExecuteAsync(log).Result;
}
catch (AggregateException ex) when (_cts.Token.IsCancellationRequested && ex.InnerException is TaskCanceledException)
{
// Canceled by user
log.LogError(Strings.RestoreCanceled);
return false;
}
catch (Exception e)
{
ExceptionUtilities.LogException(e, log);
return false;
}
}
private async Task<bool> ExecuteAsync(Common.ILogger log)
{
if (RestoreGraphItems.Length < 1 && !HideWarningsAndErrors)
{
log.LogWarning(Strings.NoProjectsProvidedToTask);
return true;
}
// Convert to the internal wrapper
var wrappedItems = RestoreGraphItems.Select(MSBuildUtility.WrapMSBuildItem);
var dgFile = MSBuildRestoreUtility.GetDependencySpec(wrappedItems);
return await BuildTasksUtility.RestoreAsync(
dependencyGraphSpec: dgFile,
interactive: Interactive,
recursive: RestoreRecursive,
noCache: RestoreNoCache,
ignoreFailedSources: RestoreIgnoreFailedSources,
disableParallel: RestoreDisableParallel,
force: RestoreForce,
forceEvaluate: RestoreForceEvaluate,
hideWarningsAndErrors: HideWarningsAndErrors,
restorePC: RestorePackagesConfig,
log: log,
cancellationToken: _cts.Token);
}
public void Cancel()
{
_cts.Cancel();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
_cts.Dispose();
}
_disposed = true;
}
}
}