forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReportAssetsLogMessages.cs
118 lines (96 loc) · 3.3 KB
/
ReportAssetsLogMessages.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Build.Framework;
using NuGet.Common;
using NuGet.ProjectModel;
using System.Linq;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Raise log messages in the assets file as DiagnosticMessage items
/// that can be consumed downstream (e.g. by the dependency node in
/// the solution explorer)
/// </summary>
public sealed class ReportAssetsLogMessages : TaskBase
{
private LockFile _lockFile;
private DiagnosticsHelper _diagnostics;
#region Outputs
[Output]
public ITaskItem[] DiagnosticMessages => _diagnostics.GetDiagnosticMessages();
#endregion
#region Inputs
/// <summary>
/// The assets file to process
/// </summary>
public string ProjectAssetsFile
{
get; set;
}
#endregion
public ReportAssetsLogMessages()
{
_diagnostics = new DiagnosticsHelper();
}
#region Test Support
internal ReportAssetsLogMessages(LockFile lockFile) : this()
{
_lockFile = lockFile;
}
#endregion
private LockFile LockFile
{
get
{
if (_lockFile == null)
{
_lockFile = new LockFileCache(this).GetLockFile(ProjectAssetsFile);
}
return _lockFile;
}
}
protected override void ExecuteCore()
{
foreach (var message in LockFile.LogMessages)
{
AddMessage(message);
}
}
private void AddMessage(IAssetsLogMessage message)
{
var targetGraphs = message.GetTargetGraphs(LockFile);
targetGraphs = targetGraphs.Any() ? targetGraphs : new LockFileTarget[] { null };
foreach (var target in targetGraphs)
{
var targetLib = message.LibraryId == null ? null : target?.GetTargetLibrary(message.LibraryId);
_diagnostics.Add(
message.Code.ToString(),
message.Message,
message.FilePath,
FromLogLevel(message.Level),
message.StartLineNumber,
message.StartColumnNumber,
message.EndLineNumber,
message.EndColumnNumber,
target?.Name,
targetLib == null ? null : $"{targetLib.Name}/{targetLib.Version.ToNormalizedString()}");
}
}
private static DiagnosticMessageSeverity FromLogLevel(LogLevel level)
{
switch (level)
{
case LogLevel.Error:
return DiagnosticMessageSeverity.Error;
case LogLevel.Warning:
return DiagnosticMessageSeverity.Warning;
case LogLevel.Debug:
case LogLevel.Verbose:
case LogLevel.Information:
case LogLevel.Minimal:
default:
return DiagnosticMessageSeverity.Info;
}
}
}
}