|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Xml.Linq; |
| 10 | +using Microsoft.Build.Framework; |
| 11 | +using Microsoft.Build.Utilities; |
| 12 | + |
| 13 | +namespace RepoTasks |
| 14 | +{ |
| 15 | + public class CreateFrameworkListFile : Task |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Files to extract basic information from and include in the list. |
| 19 | + /// </summary> |
| 20 | + [Required] |
| 21 | + public ITaskItem[] Files { get; set; } |
| 22 | + |
| 23 | + [Required] |
| 24 | + public string TargetFile { get; set; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Extra attributes to place on the root node. |
| 28 | + /// |
| 29 | + /// %(Identity): Attribute name. |
| 30 | + /// %(Value): Attribute value. |
| 31 | + /// </summary> |
| 32 | + public ITaskItem[] RootAttributes { get; set; } |
| 33 | + |
| 34 | + public override bool Execute() |
| 35 | + { |
| 36 | + XAttribute[] rootAttributes = RootAttributes |
| 37 | + ?.Select(item => new XAttribute(item.ItemSpec, item.GetMetadata("Value"))) |
| 38 | + .ToArray(); |
| 39 | + |
| 40 | + var frameworkManifest = new XElement("FileList", rootAttributes); |
| 41 | + |
| 42 | + var usedFileProfiles = new HashSet<string>(); |
| 43 | + |
| 44 | + foreach (var f in Files |
| 45 | + .Select(item => new |
| 46 | + { |
| 47 | + Item = item, |
| 48 | + Filename = Path.GetFileName(item.ItemSpec), |
| 49 | + TargetPath = item.GetMetadata("TargetPath"), |
| 50 | + AssemblyName = FileUtilities.GetAssemblyName(item.ItemSpec), |
| 51 | + FileVersion = FileUtilities.GetFileVersion(item.ItemSpec), |
| 52 | + IsNative = item.GetMetadata("IsNativeImage") == "true", |
| 53 | + IsSymbolFile = item.GetMetadata("IsSymbolFile") == "true" |
| 54 | + }) |
| 55 | + .Where(f => |
| 56 | + !f.IsSymbolFile && |
| 57 | + (f.Filename.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || f.IsNative)) |
| 58 | + .OrderBy(f => f.TargetPath, StringComparer.Ordinal) |
| 59 | + .ThenBy(f => f.Filename, StringComparer.Ordinal)) |
| 60 | + { |
| 61 | + var element = new XElement( |
| 62 | + "File", |
| 63 | + new XAttribute("Type", f.IsNative ? "Native" : "Managed"), |
| 64 | + new XAttribute( |
| 65 | + "Path", |
| 66 | + Path.Combine(f.TargetPath, f.Filename).Replace('\\', '/'))); |
| 67 | + |
| 68 | + if (f.AssemblyName != null) |
| 69 | + { |
| 70 | + byte[] publicKeyToken = f.AssemblyName.GetPublicKeyToken(); |
| 71 | + string publicKeyTokenHex; |
| 72 | + |
| 73 | + if (publicKeyToken != null) |
| 74 | + { |
| 75 | + publicKeyTokenHex = BitConverter.ToString(publicKeyToken) |
| 76 | + .ToLowerInvariant() |
| 77 | + .Replace("-", ""); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + Log.LogError($"No public key token found for assembly {f.Item.ItemSpec}"); |
| 82 | + publicKeyTokenHex = ""; |
| 83 | + } |
| 84 | + |
| 85 | + element.Add( |
| 86 | + new XAttribute("AssemblyName", f.AssemblyName.Name), |
| 87 | + new XAttribute("PublicKeyToken", publicKeyTokenHex), |
| 88 | + new XAttribute("AssemblyVersion", f.AssemblyName.Version)); |
| 89 | + } |
| 90 | + else if (!f.IsNative) |
| 91 | + { |
| 92 | + // This file isn't managed and isn't native. Leave it off the list. |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + element.Add(new XAttribute("FileVersion", f.FileVersion)); |
| 97 | + |
| 98 | + frameworkManifest.Add(element); |
| 99 | + } |
| 100 | + |
| 101 | + Directory.CreateDirectory(Path.GetDirectoryName(TargetFile)); |
| 102 | + File.WriteAllText(TargetFile, frameworkManifest.ToString()); |
| 103 | + |
| 104 | + return !Log.HasLoggedErrors; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments