-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathTypeLibraryDictionaryBuilder.cs
64 lines (60 loc) · 2.5 KB
/
TypeLibraryDictionaryBuilder.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Build.Framework;
namespace Microsoft.NET.Build.Tasks
{
static class TypeLibraryDictionaryBuilder
{
public static bool TryCreateTypeLibraryIdDictionary(ITaskItem[] typeLibraries, out Dictionary<int, string> typeLibraryIdMap, out IEnumerable<string> errors)
{
typeLibraryIdMap = null;
errors = Enumerable.Empty<string>();
List<string> errorsLocal = new();
if (typeLibraries is null || typeLibraries.Length == 0)
{
return true;
}
else if (typeLibraries.Length == 1)
{
int id = 1;
string idMetadata = typeLibraries[0].GetMetadata("Id");
if (!string.IsNullOrEmpty(idMetadata))
{
if (!int.TryParse(idMetadata, out id) || id == 0)
{
errorsLocal.Add(string.Format(Strings.InvalidTypeLibraryId, idMetadata, typeLibraries[0].ItemSpec));
errors = errorsLocal;
return false;
}
}
typeLibraryIdMap = new Dictionary<int, string> { { id, typeLibraries[0].ItemSpec } };
return true;
}
typeLibraryIdMap = new Dictionary<int, string>();
foreach (ITaskItem typeLibrary in typeLibraries)
{
string idMetadata = typeLibrary.GetMetadata("Id");
if (string.IsNullOrEmpty(idMetadata))
{
errorsLocal.Add(string.Format(Strings.MissingTypeLibraryId, typeLibrary.ItemSpec));
continue;
}
if (!int.TryParse(idMetadata, out int id) || id == 0)
{
errorsLocal.Add(string.Format(Strings.InvalidTypeLibraryId, idMetadata, typeLibrary.ItemSpec));
continue;
}
if (typeLibraryIdMap.ContainsKey(id))
{
errorsLocal.Add(string.Format(Strings.DuplicateTypeLibraryIds, idMetadata, typeLibraryIdMap[id], typeLibrary.ItemSpec));
}
else
{
typeLibraryIdMap[id] = typeLibrary.ItemSpec;
}
}
errors = errorsLocal;
return errorsLocal.Count == 0;
}
}
}