CreateFrameworkListFile.cs 3.9 KB

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