CreateFrameworkListFile.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. string path = Path.Combine(f.PackagePath, f.Filename).Replace('\\', '/');
  53. string type = f.IsNative ? "Native" : "Managed";
  54. var element = new XElement("File", new XAttribute("Path", path));
  55. if (path.StartsWith("analyzers/", StringComparison.Ordinal))
  56. {
  57. type = "Analyzer";
  58. if (path.EndsWith(".resources.dll", StringComparison.Ordinal))
  59. {
  60. // omit analyzer resources
  61. continue;
  62. }
  63. var pathParts = path.Split('/');
  64. if (pathParts.Length < 3 || !pathParts[1].Equals("dotnet", StringComparison.Ordinal) || pathParts.Length > 4)
  65. {
  66. Log.LogError($"Unexpected analyzer path format {path}. Expected 'analyzers/dotnet(/language)/analyzer.dll");
  67. }
  68. // Check if we have enough parts for language directory and include it
  69. if (pathParts.Length > 3)
  70. {
  71. element.Add(new XAttribute("Language", pathParts[2]));
  72. }
  73. }
  74. element.Add(new XAttribute("Type", type));
  75. if (f.AssemblyName != null)
  76. {
  77. byte[] publicKeyToken = f.AssemblyName.GetPublicKeyToken();
  78. string publicKeyTokenHex;
  79. if (publicKeyToken != null)
  80. {
  81. publicKeyTokenHex = BitConverter.ToString(publicKeyToken)
  82. .ToLowerInvariant()
  83. .Replace("-", "");
  84. }
  85. else
  86. {
  87. Log.LogError($"No public key token found for assembly {f.Item.ItemSpec}");
  88. publicKeyTokenHex = "";
  89. }
  90. element.Add(
  91. new XAttribute("AssemblyName", f.AssemblyName.Name),
  92. new XAttribute("PublicKeyToken", publicKeyTokenHex),
  93. new XAttribute("AssemblyVersion", f.AssemblyName.Version));
  94. }
  95. else if (!f.IsNative)
  96. {
  97. // This file isn't managed and isn't native. Leave it off the list.
  98. continue;
  99. }
  100. element.Add(new XAttribute("FileVersion", f.FileVersion));
  101. frameworkManifest.Add(element);
  102. }
  103. Directory.CreateDirectory(Path.GetDirectoryName(TargetFile));
  104. File.WriteAllText(TargetFile, frameworkManifest.ToString());
  105. return !Log.HasLoggedErrors;
  106. }
  107. }
  108. }