-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathGetAssemblyIdentity.cs
107 lines (96 loc) · 3.25 KB
/
GetAssemblyIdentity.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Utilities;
#if !NET
using System.Globalization;
using System.Text;
#endif
#nullable disable
namespace Microsoft.Build.Tasks
{
/// <summary>
/// Sniffs input files for their assembly identities, and outputs a set of items with the identity information.
/// </summary>
/// <comment>
/// Input: Assembly Include="foo.exe"
/// Output: Identity Include="Foo, Version=1.0.0.0", Name="Foo, Version="1.0.0.0"
/// </comment>
public class GetAssemblyIdentity : TaskExtension
{
private ITaskItem[] _assemblyFiles;
[Required]
public ITaskItem[] AssemblyFiles
{
get
{
ErrorUtilities.VerifyThrowArgumentNull(_assemblyFiles, nameof(AssemblyFiles));
return _assemblyFiles;
}
set => _assemblyFiles = value;
}
[Output]
public ITaskItem[] Assemblies { get; set; }
private static string ByteArrayToHex(Byte[] a)
{
if (a == null)
{
return null;
}
#if NET
return Convert.ToHexString(a);
#else
var s = new StringBuilder(a.Length * 2);
foreach (Byte b in a)
{
s.Append(b.ToString("X02", CultureInfo.InvariantCulture));
}
return s.ToString();
#endif
}
public override bool Execute()
{
var list = new List<ITaskItem>();
foreach (ITaskItem item in AssemblyFiles)
{
AssemblyName an;
try
{
an = AssemblyName.GetAssemblyName(item.ItemSpec);
}
catch (BadImageFormatException e)
{
Log.LogErrorWithCodeFromResources("GetAssemblyIdentity.CouldNotGetAssemblyName", item.ItemSpec, e.Message);
continue;
}
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
{
Log.LogErrorWithCodeFromResources("GetAssemblyIdentity.CouldNotGetAssemblyName", item.ItemSpec, e.Message);
continue;
}
ITaskItem newItem = new TaskItem(an.FullName);
newItem.SetMetadata("Name", an.Name);
if (an.Version != null)
{
newItem.SetMetadata("Version", an.Version.ToString());
}
if (an.GetPublicKeyToken() != null)
{
newItem.SetMetadata("PublicKeyToken", ByteArrayToHex(an.GetPublicKeyToken()));
}
if (an.CultureInfo != null)
{
newItem.SetMetadata("Culture", an.CultureInfo.ToString());
}
item.CopyMetadataTo(newItem);
list.Add(newItem);
}
Assemblies = list.ToArray();
return !Log.HasLoggedErrors;
}
}
}