diff options
-rw-r--r-- | tools/nuget-hash-extractor/nuget-hash-extractor.cs | 60 |
1 files changed, 40 insertions, 20 deletions
diff --git a/tools/nuget-hash-extractor/nuget-hash-extractor.cs b/tools/nuget-hash-extractor/nuget-hash-extractor.cs index 56e150e8676..f43d5d7cc65 100644 --- a/tools/nuget-hash-extractor/nuget-hash-extractor.cs +++ b/tools/nuget-hash-extractor/nuget-hash-extractor.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Collections.Specialized; using System.Xml.Linq; using System.Linq; using System.IO; @@ -6,6 +8,17 @@ using System.IO.Compression; using System.Reflection; class Driver { + + internal static Dictionary<string, string> BadAssembliesToEnumTable = new Dictionary<string, string> { + {"System.Runtime.InteropServices.RuntimeInformation.dll", "SYS_RT_INTEROP_RUNTIME_INFO"}, + {"System.Globalization.Extensions.dll", "SYS_GLOBALIZATION_EXT"}, + {"System.IO.Compression.dll", "SYS_IO_COMPRESSION"}, + {"System.Net.Http.dll", "SYS_NET_HTTP"}, + {"System.Text.Encoding.CodePages.dll", "SYS_TEXT_ENC_CODEPAGES"}, + {"System.Reflection.DispatchProxy.dll", "SYS_REF_DISP_PROXY"}, + {"System.Threading.Overlapped.dll", "SYS_THREADING_OVERLAPPED"} + }; + static ZipArchiveEntry FindSpecFile (ZipArchive zip) { foreach (var entry in zip.Entries) { if (entry.Name.EndsWith (".nuspec")) @@ -21,14 +34,28 @@ class Driver { var l = XElement.Load (new StreamReader (nuspec.Open ())); var version = (from el in l.Descendants() where el.Name.LocalName == "version" select el.Value).FirstOrDefault (); + var prefixesToCheckInOrder = new string[] { + "lib/net4", + "lib/netstandard1", + "msbuildExtensions/Microsoft/Microsoft.NET.Build.Extensions/net4" + }; + // // Logic copied from https://github.com/NuGet/NuGet.Client/blob/4cccb13833ad29d6a0bcff055460d964f1b49cfe/src/NuGet.Core/NuGet.Frameworks/DefaultFrameworkMappings.cs#L385 // - var entries = from e in zip.Entries where e.FullName.StartsWith ("lib/net4") && e.Name.EndsWith (".dll") select e; + IEnumerable<ZipArchiveEntry> entries = null; + foreach (var prefix in prefixesToCheckInOrder) { + entries = zip.Entries.Where (e => e.FullName.StartsWith (prefix) && e.Name.EndsWith (".dll") && BadAssembliesToEnumTable.ContainsKey (e.Name)); + if (entries.Any ()) + break; + } + if (!entries.Any ()) { - entries = from e in zip.Entries where e.FullName.StartsWith ("lib/netstandard1") && e.Name.EndsWith (".dll") select e; + Console.Error.WriteLine ($"** Warning: No relevant assemblies found for nukpkg: {nupkg}"); + return; } + // Only take assemblies from first prefix foreach (var et in entries) { LoadAndDump (et, version); } @@ -58,7 +85,6 @@ class Driver { static int domain_id = 1; static void LoadAndDump (ZipArchiveEntry entry, string version) { - // Console.WriteLine ("Dumping {0}", entry); var data = StreamToArray (entry.Open ()); AppDomain ad = AppDomain.CreateDomain ("parse_" + ++domain_id); DoParse p = (DoParse)ad.CreateInstanceAndUnwrap (typeof (DoParse).Assembly.FullName, typeof (DoParse).FullName); @@ -75,23 +101,17 @@ class DoParse : MarshalByRefObject { h = ((h << 5) + h) ^ str[i]; return h; } - static string FileToEnum (string name) { - switch (name) { - case "System.Runtime.InteropServices.RuntimeInformation.dll": return "SYS_RT_INTEROP_RUNTIME_INFO"; - case "System.Globalization.Extensions.dll": return "SYS_GLOBALIZATION_EXT"; - case "System.IO.Compression.dll": return "SYS_IO_COMPRESSION"; - case "System.Net.Http.dll": return "SYS_NET_HTTP"; - case "System.Text.Encoding.CodePages.dll": return "SYS_TEXT_ENC_CODEPAGES"; - case "System.Reflection.DispatchProxy.dll": return "SYS_REF_DISP_PROXY"; - case "System.Threading.Overlapped.dll": return "SYS_THREADING_OVERLAPPED"; - default: throw new Exception ($"No idea what to do with {name}"); - } - } - static string FileToMoniker (string p) { - var parts = p.Split (Path.DirectorySeparatorChar); - return parts[parts.Length - 2]; - } + static string FileToMoniker (string path) => + path.Split (Path.DirectorySeparatorChar) + .Where (p => p.StartsWith ("net")) + .FirstOrDefault () + ?? "unknown"; + + static string FileToEnum (string name) => + Driver.BadAssembliesToEnumTable.ContainsKey (name) + ? Driver.BadAssembliesToEnumTable [name] + : throw new Exception ($"No idea what to do with {name}"); public void ParseAssembly (byte[] data, string version, string name, string fullname, bool dump_asm, bool dump_ver, bool dump_guids_for_msbuild) { var a = Assembly.ReflectionOnlyLoad (data); @@ -100,7 +120,7 @@ class DoParse : MarshalByRefObject { var hash_code = Hash (id).ToString ("X"); var str = FileToEnum (name); - string ver_str = version + " " + FileToMoniker (fullname); + string ver_str = version + " " + FileToMoniker (fullname); if (dump_asm) Console.WriteLine ($"IGNORED_ASSEMBLY (0x{hash_code}, {str}, \"{id}\", \"{ver_str}\"),"); |