forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NETSdkError.cs
55 lines (47 loc) · 1.94 KB
/
NETSdkError.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
// 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 Newtonsoft.Json;
using System;
using System.Globalization;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Provides a localizable mechanism for logging an error from the SDK targets.
/// </summary>
public class NETSdkError : TaskBase
{
/// <summary>
/// The name of the resource in Strings.resx that contains the desired error message.
/// </summary>
[Required]
public string ResourceName { get; set; }
/// <summary>
/// The arguments provided to <see cref="string.Format"/> along with the retrieved resource as the format.
/// </summary>
public string[] FormatArguments { get; set; }
public bool WarningOnly { get; set; }
private static readonly string[] EmptyArguments = new[] { "" };
protected override void ExecuteCore()
{
if (FormatArguments == null || FormatArguments.Length == 0)
{
// We use a single-item array with one empty string in this case so that
// it is possible to interpret FormatArguments="$(EmptyVariable)" as a request
// to pass an empty string on to string.Format. Note if there are not placeholders
// in the string, then the empty string arg will be ignored.
FormatArguments = EmptyArguments;
}
string format = Strings.ResourceManager.GetString(ResourceName, Strings.Culture);
string message = string.Format(CultureInfo.CurrentCulture, format, FormatArguments);
if (WarningOnly)
{
Log.LogWarning(message);
}
else
{
Log.LogError(message);
}
}
}
}