forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeLibraryDictionaryBuilder.cs
70 lines (66 loc) · 2.67 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
65
66
67
68
69
70
// 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 List<string>();
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;
}
}
}