forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetAssemblyAttributes.cs
54 lines (47 loc) · 2.42 KB
/
GetAssemblyAttributes.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Microsoft.NET.Build.Tasks
{
public class GetAssemblyAttributes : TaskBase
{
[Required]
public string PathToTemplateFile { get; set; }
[Output]
public ITaskItem[] AssemblyAttributes { get; private set; }
protected override void ExecuteCore()
{
var fileVersionInfo = FileVersionInfo.GetVersionInfo(Path.GetFullPath(PathToTemplateFile));
Version assemblyVersion = FileUtilities.TryGetAssemblyVersion(Path.GetFullPath(PathToTemplateFile));
AssemblyAttributes = FormatToAttributes(AssemblyAttributesNameByFieldInFileVersionInfo: new Dictionary<string, string>
{
["System.Reflection.AssemblyCompanyAttribute"] = fileVersionInfo.CompanyName,
["System.Reflection.AssemblyCopyrightAttribute"] = fileVersionInfo.LegalCopyright,
["System.Reflection.AssemblyDescriptionAttribute"] = fileVersionInfo.Comments,
["System.Reflection.AssemblyFileVersionAttribute"] = fileVersionInfo.FileVersion,
["System.Reflection.AssemblyInformationalVersionAttribute"] = fileVersionInfo.ProductVersion,
["System.Reflection.AssemblyProductAttribute"] = fileVersionInfo.ProductName,
["System.Reflection.AssemblyTitleAttribute"] = fileVersionInfo.FileDescription,
["System.Reflection.AssemblyVersionAttribute"] = assemblyVersion != null ? assemblyVersion.ToString() : string.Empty
});
}
private ITaskItem[] FormatToAttributes(IDictionary<string, string> AssemblyAttributesNameByFieldInFileVersionInfo)
{
if (AssemblyAttributesNameByFieldInFileVersionInfo == null)
{
AssemblyAttributesNameByFieldInFileVersionInfo = new Dictionary<string, string>();
}
return AssemblyAttributesNameByFieldInFileVersionInfo
.Where(kv => !string.IsNullOrEmpty(kv.Value))
.Select(kv =>
{
var item = new TaskItem(kv.Key);
item.SetMetadata("_Parameter1", kv.Value);
return item;
})
.ToArray();
}
}
}