2001-07-12 Joe Shaw <joe@ximian.com>
[mono.git] / status / compare-assembly.cs
1 /*
2 Tool #1:
3
4         compare file1.dll file2.dll annotations.xml
5
6         file1.dll: This is an assembly created by Microsoft.
7
8         file2.dll: This is a Mono assembly (currently we have none
9         that build).
10
11         annotations.xml: contains comments about a class:
12
13         <class name="System.Object">
14                 <maintainer>
15                         <email>miguel@ximian.com</email>
16                         <name>Miguel de Icaza</name>
17                 </maintainer>
18                 <status test-suite="no" percent="XX">
19         </class>
20
21         That would generate an XML file with all the classes that are
22         implemented in the second library.  If there is nothing for a
23         given class, it should generate an emtpy group:
24
25         <class name="System.Object">
26         </class>
27
28 Tool #2:
29
30         Using a Perl script that can grok XML, generate HTML pages
31         that we can put on the web site:
32
33                 Per assembly status.
34                 Per maintainer status.
35                 Per Percent status.
36
37 */
38 namespace Mapper
39 {
40     using System;
41         using System.Collections;
42         using System.Reflection;
43
44     /// <summary>
45     ///    Summary description for Class1.
46     /// </summary>
47     public class Mapper
48     {
49                 Assembly a;
50                 Hashtable nshash = new Hashtable();
51                 int indent = 0;
52
53         public Mapper(string name)
54                 {
55                         a = Assembly.LoadFrom (name);
56                 }
57
58                 void o (string s)
59                 {
60                         Console.WriteLine (s.PadLeft (s.Length + indent, ' '));
61                 }
62
63                 void DumpMember (MemberInfo mi)
64                 {
65                         string kind;
66                         string more="";
67
68                         switch (mi.MemberType)
69                         {
70                                 case MemberTypes.Field:
71                                         kind = "field";
72                                         break;
73                                 case MemberTypes.Method:
74                                         if (((MethodInfo)mi).IsSpecialName) {
75                                                 return;
76                                         }
77                                         kind = "method";
78                                         more = " signature='" + mi.ToString() +"'";
79                                         break;
80                                 case MemberTypes.Event:
81                                         kind = "event";
82                                         break;
83                                 case MemberTypes.Property:
84                                         kind = "property";
85                                         break;
86                                 default:
87                                         kind = "***UNKOWN***";
88                                         break;
89                         }
90
91                         o ("<" + kind + " name='" + mi.Name + "'" + more + "/>");
92                 }
93
94                 void DumpType (Type t)
95                 {
96                         string kind, name, attrs = "";
97
98                         name = t.Name;
99
100                         if (t.IsClass) {
101                                 kind = "class";
102                         } else if (t.IsInterface) {
103                                 kind = "interface";
104                         } else if (t.IsValueType) {
105                                 kind = "valueType";
106                         } else if (t.IsEnum) {
107                                 kind = "enum";
108                         } else return;
109
110                         if (t.IsAbstract) {
111                                 attrs += "abstract='true'";
112                         } else if (t.IsSealed) {
113                                 attrs += "sealed='true'";
114                         } else if (t.IsCOMObject) {
115                                 attrs += "comobject='true'";
116                         }
117
118                         o ("<" + kind + " name='" + name + (attrs == "" ? "'" : "' ") + attrs + ">");
119
120                         indent += 4;
121
122                         /*o ("<maintainer></maintainer>");
123                         o ("<description></description>");*/
124
125                         foreach (Type type in t.GetNestedTypes ())
126                         {
127                                 DumpType(type);
128                         }
129
130                         foreach (FieldInfo field in t.GetFields ())
131                         {
132                                 DumpMember (field);
133                         }
134
135                         foreach (MethodInfo method in t.GetMethods ())
136                         {
137                                 DumpMember (method);
138                         }
139
140                         indent -= 4;
141
142                         o ("</" + kind + ">");
143                 }
144         
145                 void LoadTypeList (Type [] types)
146                 {
147                         foreach (Type t in types)
148                         {
149                                 ArrayList list = (ArrayList) nshash [t.Namespace];
150                                 if (list == null)
151                                 {
152                                         list = new ArrayList ();
153                                         nshash.Add (t.Namespace, list);
154                                 }
155                                 list.Add (t);
156                         }
157                 }
158         
159                 void DumpTypeList (Type [] types)
160                 {
161                         LoadTypeList (types);
162
163                         foreach (string ns in nshash.Keys)
164                         {
165                                 o ("<namespace " + "name='" + ns + "'>");
166
167                                 indent += 4;
168
169                                 foreach (Type t in (ArrayList) nshash [ns])
170                                 {
171                                         DumpType (t);
172                                 }
173
174                                 indent -= 4;
175
176                                 o ("</namespace>");
177                         }
178                 }
179
180                 public void Map ()
181                 {
182                         string name;
183                         Type [] types;
184                         Module [] modules;
185
186                         name = a.GetName ().Name;
187                         types = a.GetExportedTypes ();
188                         modules = a.GetModules ();
189
190                         o ("<assembly name='" + name + "'>");
191
192                         indent += 4;
193
194                         /*o ("<maintainer></maintainer>");
195                         o ("<description></description>");*/
196
197                         DumpTypeList (types);
198
199                         indent -= 4;
200
201                         o ("</assembly>");
202                 }
203
204                 public static int Main(string[] args)
205         {
206                         Mapper m;
207                         string basedir = "c:\\WINDOWS\\Microsoft.NET\\Framework\\v1.0.2914\\";
208
209                         if (args.Length > 0) {
210                                 foreach (string s in args){
211                                         try {
212                                                 m = new Mapper (basedir + s);
213                                                 m.Map ();
214                                         } catch (Exception e) {
215                                                 Console.WriteLine("Error: "+e.ToString());
216                                         }
217                                 }
218                         } else {
219                                         try {
220                                 m = new Mapper (basedir + "mscorlib.dll");
221                                 m.Map ();
222                                         } catch (Exception e) {
223                                                 Console.WriteLine("Error: "+e.ToString());
224                                         }
225                         }
226
227             return 0;
228         }
229     }
230 }
231