|
10 | 10 | using Microsoft.Build.Framework;
|
11 | 11 | using Microsoft.Build.Utilities;
|
12 | 12 |
|
13 |
| -namespace RepoTasks |
| 13 | +namespace RepoTasks; |
| 14 | + |
| 15 | +public class CreateFrameworkListFile : Microsoft.Build.Utilities.Task |
14 | 16 | {
|
15 |
| - public class CreateFrameworkListFile : Microsoft.Build.Utilities.Task |
| 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() |
16 | 35 | {
|
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(); |
| 36 | + XAttribute[] rootAttributes = RootAttributes |
| 37 | + ?.Select(item => new XAttribute(item.ItemSpec, item.GetMetadata("Value"))) |
| 38 | + .ToArray(); |
39 | 39 |
|
40 |
| - var frameworkManifest = new XElement("FileList", rootAttributes); |
| 40 | + var frameworkManifest = new XElement("FileList", rootAttributes); |
41 | 41 |
|
42 |
| - var usedFileProfiles = new HashSet<string>(); |
| 42 | + var usedFileProfiles = new HashSet<string>(); |
43 | 43 |
|
44 |
| - foreach (var f in Files |
45 |
| - .Select(item => new |
46 |
| - { |
47 |
| - Item = item, |
48 |
| - Filename = Path.GetFileName(item.ItemSpec), |
49 |
| - AssemblyName = FileUtilities.GetAssemblyName(item.ItemSpec), |
50 |
| - FileVersion = FileUtilities.GetFileVersion(item.ItemSpec), |
51 |
| - IsNative = item.GetMetadata("IsNativeImage") == "true", |
52 |
| - IsSymbolFile = item.GetMetadata("IsSymbolFile") == "true", |
53 |
| - PackagePath = GetPackagePath(item) |
54 |
| - }) |
55 |
| - .Where(f => |
56 |
| - !f.IsSymbolFile && |
57 |
| - (f.Filename.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || f.IsNative)) |
58 |
| - .OrderBy(f => f.Filename, StringComparer.Ordinal)) |
| 44 | + foreach (var f in Files |
| 45 | + .Select(item => new |
| 46 | + { |
| 47 | + Item = item, |
| 48 | + Filename = Path.GetFileName(item.ItemSpec), |
| 49 | + AssemblyName = FileUtilities.GetAssemblyName(item.ItemSpec), |
| 50 | + FileVersion = FileUtilities.GetFileVersion(item.ItemSpec), |
| 51 | + IsNative = item.GetMetadata("IsNativeImage") == "true", |
| 52 | + IsSymbolFile = item.GetMetadata("IsSymbolFile") == "true", |
| 53 | + PackagePath = GetPackagePath(item) |
| 54 | + }) |
| 55 | + .Where(f => |
| 56 | + !f.IsSymbolFile && |
| 57 | + (f.Filename.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || f.IsNative)) |
| 58 | + .OrderBy(f => f.Filename, StringComparer.Ordinal)) |
| 59 | + { |
| 60 | + string path = Path.Combine(f.PackagePath, f.Filename).Replace('\\', '/'); |
| 61 | + string type = f.IsNative ? "Native" : "Managed"; |
| 62 | + var element = new XElement("File", new XAttribute("Path", path)); |
| 63 | + |
| 64 | + if (path.StartsWith("analyzers/", StringComparison.Ordinal)) |
59 | 65 | {
|
60 |
| - string path = Path.Combine(f.PackagePath, f.Filename).Replace('\\', '/'); |
61 |
| - string type = f.IsNative ? "Native" : "Managed"; |
62 |
| - var element = new XElement("File", new XAttribute("Path", path)); |
| 66 | + type = "Analyzer"; |
63 | 67 |
|
64 |
| - if (path.StartsWith("analyzers/", StringComparison.Ordinal)) |
| 68 | + if (path.EndsWith(".resources.dll", StringComparison.Ordinal)) |
65 | 69 | {
|
66 |
| - type = "Analyzer"; |
67 |
| - |
68 |
| - if (path.EndsWith(".resources.dll", StringComparison.Ordinal)) |
69 |
| - { |
70 |
| - // omit analyzer resources |
71 |
| - continue; |
72 |
| - } |
73 |
| - |
74 |
| - var pathParts = path.Split('/'); |
75 |
| - |
76 |
| - if (pathParts.Length < 3 || !pathParts[1].Equals("dotnet", StringComparison.Ordinal) || pathParts.Length > 5) |
77 |
| - { |
78 |
| - Log.LogError($"Unexpected analyzer path format {path}. Expected 'analyzers/dotnet(/roslyn<version>)(/language)/analyzer.dll"); |
79 |
| - } |
80 |
| - |
81 |
| - // Check if we have enough parts for language directory and include it. |
82 |
| - // There could be a roslyn<version> folder before the language folder. Check for it. |
83 |
| - bool hasRoslynVersion = pathParts[2].StartsWith("roslyn", StringComparison.Ordinal); |
84 |
| - int languageLengthCheck = hasRoslynVersion ? 4 : 3; |
85 |
| - int potentialLanguageIndex = hasRoslynVersion ? 3 : 2; |
86 |
| - if (pathParts.Length > languageLengthCheck) |
87 |
| - { |
88 |
| - element.Add(new XAttribute("Language", pathParts[potentialLanguageIndex])); |
89 |
| - } |
| 70 | + // omit analyzer resources |
| 71 | + continue; |
90 | 72 | }
|
91 | 73 |
|
92 |
| - element.Add(new XAttribute("Type", type)); |
| 74 | + var pathParts = path.Split('/'); |
93 | 75 |
|
94 |
| - if (f.AssemblyName != null) |
| 76 | + if (pathParts.Length < 3 || !pathParts[1].Equals("dotnet", StringComparison.Ordinal) || pathParts.Length > 5) |
95 | 77 | {
|
96 |
| - byte[] publicKeyToken = f.AssemblyName.GetPublicKeyToken(); |
97 |
| - string publicKeyTokenHex; |
98 |
| - |
99 |
| - if (publicKeyToken != null) |
100 |
| - { |
101 |
| - publicKeyTokenHex = BitConverter.ToString(publicKeyToken) |
102 |
| - .ToLowerInvariant() |
103 |
| - .Replace("-", ""); |
104 |
| - } |
105 |
| - else |
106 |
| - { |
107 |
| - Log.LogError($"No public key token found for assembly {f.Item.ItemSpec}"); |
108 |
| - publicKeyTokenHex = ""; |
109 |
| - } |
110 |
| - |
111 |
| - element.Add( |
112 |
| - new XAttribute("AssemblyName", f.AssemblyName.Name), |
113 |
| - new XAttribute("PublicKeyToken", publicKeyTokenHex), |
114 |
| - new XAttribute("AssemblyVersion", f.AssemblyName.Version)); |
| 78 | + Log.LogError($"Unexpected analyzer path format {path}. Expected 'analyzers/dotnet(/roslyn<version>)(/language)/analyzer.dll"); |
115 | 79 | }
|
116 |
| - else if (!f.IsNative) |
| 80 | + |
| 81 | + // Check if we have enough parts for language directory and include it. |
| 82 | + // There could be a roslyn<version> folder before the language folder. Check for it. |
| 83 | + bool hasRoslynVersion = pathParts[2].StartsWith("roslyn", StringComparison.Ordinal); |
| 84 | + int languageLengthCheck = hasRoslynVersion ? 4 : 3; |
| 85 | + int potentialLanguageIndex = hasRoslynVersion ? 3 : 2; |
| 86 | + if (pathParts.Length > languageLengthCheck) |
117 | 87 | {
|
118 |
| - // This file isn't managed and isn't native. Leave it off the list. |
119 |
| - continue; |
| 88 | + element.Add(new XAttribute("Language", pathParts[potentialLanguageIndex])); |
120 | 89 | }
|
| 90 | + } |
| 91 | + |
| 92 | + element.Add(new XAttribute("Type", type)); |
| 93 | + |
| 94 | + if (f.AssemblyName != null) |
| 95 | + { |
| 96 | + byte[] publicKeyToken = f.AssemblyName.GetPublicKeyToken(); |
| 97 | + string publicKeyTokenHex; |
121 | 98 |
|
122 |
| - element.Add(new XAttribute("FileVersion", f.FileVersion)); |
| 99 | + if (publicKeyToken != null) |
| 100 | + { |
| 101 | + publicKeyTokenHex = BitConverter.ToString(publicKeyToken) |
| 102 | + .ToLowerInvariant() |
| 103 | + .Replace("-", ""); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + Log.LogError($"No public key token found for assembly {f.Item.ItemSpec}"); |
| 108 | + publicKeyTokenHex = ""; |
| 109 | + } |
123 | 110 |
|
124 |
| - frameworkManifest.Add(element); |
| 111 | + element.Add( |
| 112 | + new XAttribute("AssemblyName", f.AssemblyName.Name), |
| 113 | + new XAttribute("PublicKeyToken", publicKeyTokenHex), |
| 114 | + new XAttribute("AssemblyVersion", f.AssemblyName.Version)); |
| 115 | + } |
| 116 | + else if (!f.IsNative) |
| 117 | + { |
| 118 | + // This file isn't managed and isn't native. Leave it off the list. |
| 119 | + continue; |
125 | 120 | }
|
126 | 121 |
|
127 |
| - Directory.CreateDirectory(Path.GetDirectoryName(TargetFile)); |
128 |
| - File.WriteAllText(TargetFile, frameworkManifest.ToString()); |
| 122 | + element.Add(new XAttribute("FileVersion", f.FileVersion)); |
129 | 123 |
|
130 |
| - return !Log.HasLoggedErrors; |
| 124 | + frameworkManifest.Add(element); |
131 | 125 | }
|
132 |
| - private static string GetPackagePath(ITaskItem item) |
133 |
| - { |
134 |
| - string packagePath = item.GetMetadata("PackagePath"); |
135 | 126 |
|
136 |
| - // replicate the logic used by PackTask https://github.com/NuGet/NuGet.Client/blob/f24bad0668193ce21a1db8cabd1ce95ba509c7f0/src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTaskLogic.cs#L644-L647 |
137 |
| - string recursiveDir = item.GetMetadata("RecursiveDir"); |
138 |
| - recursiveDir = string.IsNullOrEmpty(recursiveDir) ? item.GetMetadata("NuGetRecursiveDir") : recursiveDir; |
| 127 | + Directory.CreateDirectory(Path.GetDirectoryName(TargetFile)); |
| 128 | + File.WriteAllText(TargetFile, frameworkManifest.ToString()); |
139 | 129 |
|
140 |
| - return string.IsNullOrEmpty(recursiveDir) ? packagePath : |
141 |
| - Path.Combine(packagePath, recursiveDir); |
142 |
| - } |
| 130 | + return !Log.HasLoggedErrors; |
143 | 131 | }
|
| 132 | + private static string GetPackagePath(ITaskItem item) |
| 133 | + { |
| 134 | + string packagePath = item.GetMetadata("PackagePath"); |
| 135 | + |
| 136 | + // replicate the logic used by PackTask https://github.com/NuGet/NuGet.Client/blob/f24bad0668193ce21a1db8cabd1ce95ba509c7f0/src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTaskLogic.cs#L644-L647 |
| 137 | + string recursiveDir = item.GetMetadata("RecursiveDir"); |
| 138 | + recursiveDir = string.IsNullOrEmpty(recursiveDir) ? item.GetMetadata("NuGetRecursiveDir") : recursiveDir; |
144 | 139 |
|
| 140 | + return string.IsNullOrEmpty(recursiveDir) ? packagePath : |
| 141 | + Path.Combine(packagePath, recursiveDir); |
| 142 | + } |
145 | 143 | }
|
0 commit comments