CreateFrameworkListFile.cs 3.9 KB

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