-
Notifications
You must be signed in to change notification settings - Fork 696
/
Copy pathRestoreSummary.cs
220 lines (195 loc) · 7.27 KB
/
RestoreSummary.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
// 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.Globalization;
using System.IO;
using System.Linq;
using NuGet.Common;
using NuGet.Protocol.Core.Types;
using NuGet.Shared;
namespace NuGet.Commands
{
public class RestoreSummary
{
public bool Success { get; }
public bool NoOpRestore { get; }
public string InputPath { get; }
public IReadOnlyList<string> ConfigFiles { get; }
public IReadOnlyList<string> FeedsUsed { get; }
public int InstallCount { get; }
/// <summary>
/// All the warnings and errors that were produced as a result of the restore.
/// </summary>
public IReadOnlyList<IRestoreLogMessage> Errors { get; }
public RestoreSummary(bool success)
{
Success = success;
NoOpRestore = false;
InputPath = null;
ConfigFiles = Array.Empty<string>();
FeedsUsed = Array.Empty<string>();
InstallCount = 0;
Errors = Array.Empty<IRestoreLogMessage>();
}
public RestoreSummary(
RestoreResult result,
string inputPath,
IEnumerable<string> configFiles,
IEnumerable<SourceRepository> sourceRepositories,
IEnumerable<RestoreLogMessage> errors)
{
Success = result.Success;
NoOpRestore = result is NoOpRestoreResult;
InputPath = inputPath;
ConfigFiles = configFiles.AsList().AsReadOnly();
FeedsUsed = sourceRepositories
.Select(source => source.PackageSource.Source)
.ToList()
.AsReadOnly();
InstallCount = result.GetAllInstalled().Count;
Errors = errors.ToArray();
}
public RestoreSummary(
bool success,
string inputPath,
IReadOnlyList<string> configFiles,
IReadOnlyList<string> feedsUsed,
int installCount,
IReadOnlyList<IRestoreLogMessage> errors)
{
Success = success;
InputPath = inputPath;
ConfigFiles = configFiles;
FeedsUsed = feedsUsed;
InstallCount = installCount;
Errors = errors;
}
public static void Log(ILogger logger, IReadOnlyList<RestoreSummary> restoreSummaries, bool logErrors = false)
{
if (restoreSummaries.Count == 0)
{
return;
}
var noOpCount = 0;
var feedsUsed = new HashSet<string>();
var configFiles = new HashSet<string>();
var installed = new Dictionary<string, int>(restoreSummaries.Count, PathUtility.GetStringComparerBasedOnOS());
foreach (RestoreSummary restoreSummary in restoreSummaries)
{
if (restoreSummary.NoOpRestore)
{
noOpCount++;
}
foreach (var feed in restoreSummary.FeedsUsed)
{
feedsUsed.Add(feed);
}
foreach (var configFile in restoreSummary.ConfigFiles)
{
configFiles.Add(configFile);
}
if (!string.IsNullOrEmpty(restoreSummary.InputPath))
{
if (installed.ContainsKey(restoreSummary.InputPath))
{
installed[restoreSummary.InputPath] += restoreSummary.InstallCount;
}
else
{
installed[restoreSummary.InputPath] = restoreSummary.InstallCount;
}
}
}
// This should only be true by nuget exe since it does not have msbuild logger
if (logErrors)
{
// Display the errors summary
foreach (var restoreSummary in restoreSummaries)
{
// log errors
LogErrorsToConsole(
restoreSummary,
string.Format(CultureInfo.CurrentCulture, Strings.Log_ErrorSummary, restoreSummary.InputPath),
logger);
}
}
// Display the information summary
if (configFiles.Any())
{
logger.LogInformationSummary(string.Empty);
logger.LogInformationSummary(Strings.Log_ConfigFileSummary);
foreach (var configFile in configFiles)
{
logger.LogInformationSummary($" {configFile}");
}
}
if (feedsUsed.Any())
{
logger.LogInformationSummary(string.Empty);
logger.LogInformationSummary(Strings.Log_FeedsUsedSummary);
foreach (var feedUsed in feedsUsed)
{
logger.LogInformationSummary($" {feedUsed}");
}
}
if (installed.Any(i => i.Value > 0))
{
logger.LogInformationSummary(string.Empty);
logger.LogInformationSummary(Strings.Log_InstalledSummary);
foreach (var pair in installed.Where(i => i.Value > 0))
{
logger.LogInformationSummary(" " + string.Format(
CultureInfo.CurrentCulture,
Strings.Log_InstalledSummaryCount,
pair.Value,
pair.Key));
}
}
if (!RuntimeEnvironmentHelper.IsRunningInVisualStudio)
{
if (noOpCount == restoreSummaries.Count)
{
logger.LogMinimal(Strings.Log_AllProjectsUpToDate);
}
else if (noOpCount > 0)
{
logger.LogMinimal(string.Format(CultureInfo.CurrentCulture, Strings.Log_ProjectUpToDateSummary, noOpCount, restoreSummaries.Count));
}
}
}
private static void LogErrorsToConsole(
RestoreSummary restoreSummary,
string logHeading,
ILogger logger)
{
var logs = restoreSummary
.Errors
.Where(m => m.Level == LogLevel.Error)
.ToList();
if (logs.Count > 0)
{
logger.LogInformation(string.Empty);
logger.LogError(logHeading);
foreach (var error in logs)
{
foreach (var line in IndentLines(error.FormatWithCode()))
{
logger.LogError(line);
}
}
}
}
private static IEnumerable<string> IndentLines(string input)
{
using (var reader = new StringReader(input))
{
string line;
while ((line = reader.ReadLine()) != null)
{
yield return $" {line.TrimEnd()}";
}
}
}
}
}