Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / tools / nuget-hash-extractor / nuget-hash-extractor.cs
1 using System;
2 using System.Xml.Linq;
3 using System.Linq;
4 using System.IO;
5 using System.IO.Compression;
6 using System.Reflection;
7
8 class Driver {
9         static ZipArchiveEntry FindSpecFile (ZipArchive zip) {
10                 foreach (var entry in zip.Entries) {
11                         if (entry.Name.EndsWith (".nuspec"))
12                                 return entry;
13                 }
14                 throw new Exception ("Could not find nuspec file");
15         }
16
17         static void DumpNuget (string nupkg) {
18                 var zip = new ZipArchive(new FileStream (nupkg, FileMode.Open));
19
20                 var nuspec = FindSpecFile (zip);
21                 var l = XElement.Load (new StreamReader (nuspec.Open ()));
22                 var version = (from el in l.Descendants() where el.Name.LocalName == "version" select el.Value).FirstOrDefault ();
23
24                 //
25                 // Logic copied from https://github.com/NuGet/NuGet.Client/blob/4cccb13833ad29d6a0bcff055460d964f1b49cfe/src/NuGet.Core/NuGet.Frameworks/DefaultFrameworkMappings.cs#L385
26                 //
27                 var entries = from e in zip.Entries where e.FullName.StartsWith ("lib/net4") && e.Name.EndsWith (".dll") select e;
28                 if (!entries.Any ()) {
29                         entries = from e in zip.Entries where e.FullName.StartsWith ("lib/netstandard1") && e.Name.EndsWith (".dll") select e;
30                 }
31
32                 foreach (var et in entries) {
33                         LoadAndDump (et, version);
34                 }
35         }
36
37         static bool dump_asm, dump_ver, dump_guids_for_msbuild;
38         static void Main (string[] args) {
39
40                 if (args.Length > 1) {
41                         dump_asm = args [1].Equals ("asm");
42                         dump_ver = args [1].Equals ("ver");
43                         dump_guids_for_msbuild = args [1].Equals ("guids_for_msbuild");
44                 } else {
45                         dump_asm = true;
46                 }
47                 foreach (var f in Directory.GetFiles (args [0], "*.nupkg")) {
48                         DumpNuget (f);
49                 }
50         }
51
52         static byte[] StreamToArray (Stream s) {
53                 using(var ms = new MemoryStream ()) {
54                         s.CopyTo (ms);
55                         return ms.ToArray ();
56                 }
57         }
58
59         static int domain_id = 1;
60         static void LoadAndDump (ZipArchiveEntry entry, string version) {
61                 // Console.WriteLine ("Dumping {0}", entry);
62                 var data = StreamToArray (entry.Open ());
63                 AppDomain ad = AppDomain.CreateDomain ("parse_" + ++domain_id);
64                 DoParse p = (DoParse)ad.CreateInstanceAndUnwrap (typeof (DoParse).Assembly.FullName, typeof (DoParse).FullName);
65                 p.ParseAssembly (data, version, entry.Name, entry.FullName, dump_asm, dump_ver, dump_guids_for_msbuild);
66                 AppDomain.Unload (ad);
67         }
68 }
69
70 class DoParse : MarshalByRefObject {
71
72         static int Hash (string str) {
73                 int h = 5381;
74         for (int i = 0;  i < str.Length; ++i)
75             h = ((h << 5) + h) ^ str[i];
76                 return h;
77         }
78         static string FileToEnum (string name) {
79                 switch (name) {
80                 case "System.Runtime.InteropServices.RuntimeInformation.dll": return "SYS_RT_INTEROP_RUNTIME_INFO";
81                 case "System.Globalization.Extensions.dll": return "SYS_GLOBALIZATION_EXT";
82                 case "System.IO.Compression.dll": return "SYS_IO_COMPRESSION";
83                 case "System.Net.Http.dll": return "SYS_NET_HTTP";
84                 case "System.Text.Encoding.CodePages.dll": return "SYS_TEXT_ENC_CODEPAGES";
85                 case "System.Reflection.DispatchProxy.dll": return "SYS_REF_DISP_PROXY";
86                 case "System.Threading.Overlapped.dll": return "SYS_THREADING_OVERLAPPED";
87                 default: throw new Exception ($"No idea what to do with {name}");
88                 }
89         }
90
91         static string FileToMoniker (string p) {
92                 var parts = p.Split (Path.DirectorySeparatorChar);
93                 return parts[parts.Length - 2];
94         }
95
96         public void ParseAssembly (byte[] data, string version, string name, string fullname, bool dump_asm, bool dump_ver, bool dump_guids_for_msbuild) {
97                 var a = Assembly.ReflectionOnlyLoad (data);
98                 var m = a.GetModules ()[0];
99                 var id = m.ModuleVersionId.ToString ().ToUpper ();
100                 var hash_code = Hash (id).ToString ("X");
101                 var str = FileToEnum (name);
102
103                 string ver_str = version + " " + FileToMoniker (fullname);      
104
105                 if (dump_asm)
106                         Console.WriteLine ($"IGNORED_ASSEMBLY (0x{hash_code}, {str}, \"{id}\", \"{ver_str}\"),");
107
108                 //IGNORED_ASM_VER (SYS_IO_COMPRESSION, 4, 1, 2, 0),
109                 var ver = a.GetName ().Version;
110                 if (dump_ver) {
111                         Console.WriteLine ($"IGNORED_ASM_VER ({str}, {ver.Major}, {ver.Minor}, {ver.Build}, {ver.Revision}),");
112                 } else if (dump_guids_for_msbuild) {
113                         // This needs to be kept in sync with FilterDeniedAssemblies msbuild task in msbuild
114                         Console.WriteLine ($"{name},{id},{ver.Major},{ver.Minor},{ver.Build},{ver.Revision}");
115                 }
116                 
117         }
118 }