1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
// Copyright © Microsoft Corporation.
// This source file is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Compiler;
using Microsoft.Ddue.Tools.CommandLine;
using Microsoft.Ddue.Tools.Reflection;
namespace Microsoft.Ddue.Tools {
public class MRefBuilder {
public static int Main(string[] args) {
// write banner
ConsoleApplication.WriteBanner();
// specify options
OptionCollection options = new OptionCollection();
options.Add(new SwitchOption("?", "Show this help page."));
options.Add(new StringOption("out", "Specify an output file. If unspecified, output goes to the console.", "outputFilePath"));
options.Add(new StringOption("config", "Specify a configuration file. If unspecified, MRefBuilder.config is used", "configFilePath"));
options.Add(new ListOption("dep", "Speficy assemblies to load for dependencies.", "dependencyAssembly"));
// options.Add( new BooleanOption("namespaces", "Control whether information on namespaces in provided.") );
options.Add(new BooleanOption("internal", "Specify whether to document internal as well as externally exposed APIs."));
// process options
ParseArgumentsResult results = options.ParseArguments(args);
if (results.Options["?"].IsPresent) {
Console.WriteLine("MRefBuilder [options] assemblies");
options.WriteOptionSummary(Console.Out);
return (0);
}
// check for invalid options
if (!results.Success) {
results.WriteParseErrors(Console.Out);
return (1);
}
// check for missing or extra assembly directories
if (results.UnusedArguments.Count < 1) {
Console.WriteLine("Specify at least one assembly to reflect.");
return (1);
}
// load the configuration file
XPathDocument config;
string configDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string configFile = Path.Combine(configDirectory, "MRefBuilder.config");
if (results.Options["config"].IsPresent) {
configFile = (string)results.Options["config"].Value;
configDirectory = Path.GetDirectoryName(configFile);
}
try {
config = new XPathDocument(configFile);
} catch (IOException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while attempting to read the configuration file '{0}'. The error message is: {1}", configFile, e.Message));
return (1);
} catch (UnauthorizedAccessException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while attempting to read the configuration file '{0}'. The error message is: {1}", configFile, e.Message));
return (1);
} catch (XmlException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The configuration file '{0}' is not well-formed. The error message is: {1}", configFile, e.Message));
return (1);
}
// adjust the target platform
XPathNodeIterator platformNodes = config.CreateNavigator().Select("/configuration/dduetools/platform");
if (platformNodes.MoveNext()) {
XPathNavigator platformNode = platformNodes.Current;
string version = platformNode.GetAttribute("version", String.Empty);
string path = platformNode.GetAttribute("path", String.Empty);
path = Environment.ExpandEnvironmentVariables(path);
if (!Directory.Exists(path)) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The specifed target platform directory '{0}' does not exist.", path));
return (1);
}
if (version == "2.0") {
TargetPlatform.SetToV2(path);
} else if (version == "1.1") {
TargetPlatform.SetToV1_1(path);
} else if (version == "1.0") {
TargetPlatform.SetToV1(path);
} else {
Console.WriteLine("Unknown target platform version '{0}'.", version);
return (1);
}
}
// create a namer
ApiNamer namer = new OrcasNamer();
XPathNavigator namerNode = config.CreateNavigator().SelectSingleNode("/configuration/dduetools/namer");
if (namerNode != null) {
string assemblyPath = namerNode.GetAttribute("assembly", String.Empty);
string typeName = namerNode.GetAttribute("type", String.Empty);
assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath);
if (!Path.IsPathRooted(assemblyPath)) assemblyPath = Path.Combine(configDirectory, assemblyPath);
try {
Assembly assembly = Assembly.LoadFrom(assemblyPath);
namer = (ApiNamer)assembly.CreateInstance(typeName);
if (namer == null) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' was not found in the component assembly '{1}'.", typeName, assemblyPath));
return (1);
}
} catch (IOException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("A file access error occured while attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message));
return (1);
} catch (UnauthorizedAccessException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("A file access error occured while attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message));
return (1);
} catch (BadImageFormatException) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The component assembly '{0}' is not a valid managed assembly.", assemblyPath));
return (1);
} catch (TypeLoadException) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' was not found in the component assembly '{1}'.", typeName, assemblyPath));
return (1);
} catch (MissingMethodException) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("No appropriate constructor exists for the type'{0}' in the component assembly '{1}'.", typeName, assemblyPath));
return (1);
} catch (TargetInvocationException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while initializing the type '{0}' in the component assembly '{1}'. The error message and stack trace follows: {2}", typeName, assemblyPath, e.InnerException.ToString()));
return (1);
} catch (InvalidCastException) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' in the component assembly '{1}' is not a component type.", typeName, assemblyPath));
return (1);
}
}
// create a resolver
AssemblyResolver resolver = new AssemblyResolver();
XPathNavigator resolverNode = config.CreateNavigator().SelectSingleNode("/configuration/dduetools/resolver");
if (resolverNode != null) {
string assemblyPath = resolverNode.GetAttribute("assembly", String.Empty);
string typeName = resolverNode.GetAttribute("type", String.Empty);
assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath);
if (!Path.IsPathRooted(assemblyPath)) assemblyPath = Path.Combine(configDirectory, assemblyPath);
try {
Assembly assembly = Assembly.LoadFrom(assemblyPath);
resolver = (AssemblyResolver)assembly.CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, null, new Object[1] { resolverNode }, null, null);
if (resolver == null) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' was not found in the component assembly '{1}'.", typeName, assemblyPath));
return (1);
}
} catch (IOException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("A file access error occured while attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message));
return (1);
} catch (UnauthorizedAccessException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("A file access error occured while attempting to load the component assembly '{0}'. The error message is: {1}", assemblyPath, e.Message));
return (1);
} catch (BadImageFormatException) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The component assembly '{0}' is not a valid managed assembly.", assemblyPath));
return (1);
} catch (TypeLoadException) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' was not found in the component assembly '{1}'.", typeName, assemblyPath));
return (1);
} catch (MissingMethodException) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("No appropriate constructor exists for the type'{0}' in the component assembly '{1}'.", typeName, assemblyPath));
return (1);
} catch (TargetInvocationException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while initializing the type '{0}' in the component assembly '{1}'. The error message and stack trace follows: {2}", typeName, assemblyPath, e.InnerException.ToString()));
return (1);
} catch (InvalidCastException) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' in the component assembly '{1}' is not a component type.", typeName, assemblyPath));
return (1);
}
}
resolver.UnresolvedAssemblyReference += new EventHandler < AssemblyReferenceEventArgs >(UnresolvedAssemblyReferenceHandler);
// get a textwriter for output
TextWriter output = Console.Out;
if (results.Options["out"].IsPresent) {
string file = (string)results.Options["out"].Value;
try {
output = new StreamWriter(file, false, Encoding.UTF8);
} catch (IOException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while attempting to create an output file. The error message is: {0}", e.Message));
return (1);
} catch (UnauthorizedAccessException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while attempting to create an output file. The error message is: {0}", e.Message));
return (1);
}
}
// dependency directory
string[] dependencies = new string[0];
if (results.Options["dep"].IsPresent) dependencies = (string[])results.Options["dep"].Value;
try {
// create a builder
ManagedReflectionWriter builder = new ManagedReflectionWriter(output, namer);
// specify the resolver for the builder
builder.Resolver = resolver;
// builder.ApiFilter = new ExternalDocumentedFilter(config.CreateNavigator().SelectSingleNode("/configuration/dduetools"));
// specify the filter for the builder
if (results.Options["internal"].IsPresent && (bool)results.Options["internal"].Value) {
builder.ApiFilter = new AllDocumentedFilter(config.CreateNavigator().SelectSingleNode("/configuration/dduetools"));
} else {
builder.ApiFilter = new ExternalDocumentedFilter(config.CreateNavigator().SelectSingleNode("/configuration/dduetools"));
}
// register add-ins to the builder
XPathNodeIterator addinNodes = config.CreateNavigator().Select("/configuration/dduetools/addins/addin");
foreach (XPathNavigator addinNode in addinNodes) {
string assemblyPath = addinNode.GetAttribute("assembly", String.Empty);
string typeName = addinNode.GetAttribute("type", String.Empty);
assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath);
if (!Path.IsPathRooted(assemblyPath)) assemblyPath = Path.Combine(configDirectory, assemblyPath);
try {
Assembly assembly = Assembly.LoadFrom(assemblyPath);
MRefBuilderAddIn addin = (MRefBuilderAddIn)assembly.CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, null, new Object[2] { builder, addinNode }, null, null);
if (namer == null) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' was not found in the addin assembly '{1}'.", typeName, assemblyPath));
return (1);
}
} catch (IOException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("A file access error occured while attempting to load the addin assembly '{0}'. The error message is: {1}", assemblyPath, e.Message));
return (1);
} catch (BadImageFormatException) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The addin assembly '{0}' is not a valid managed assembly.", assemblyPath));
return (1);
} catch (TypeLoadException) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' was not found in the addin assembly '{1}'.", typeName, assemblyPath));
return (1);
} catch (MissingMethodException) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("No appropriate constructor exists for the type '{0}' in the addin assembly '{1}'.", typeName, assemblyPath));
return (1);
} catch (TargetInvocationException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while initializing the type '{0}' in the addin assembly '{1}'. The error message and stack trace follows: {2}", typeName, assemblyPath, e.InnerException.ToString()));
return (1);
} catch (InvalidCastException) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("The type '{0}' in the addin assembly '{1}' is not an MRefBuilderAddIn type.", typeName, assemblyPath));
return (1);
}
}
try {
// add a handler for unresolved assembly references
//builder.UnresolvedModuleHandler = new System.Compiler.Module.AssemblyReferenceResolver(AssemblyNotFound);
// load dependent bits
foreach (string dependency in dependencies) {
try {
builder.LoadAccessoryAssemblies(dependency);
} catch (IOException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while loading dependency assemblies. The error message is: {0}", e.Message));
return (1);
}
}
// parse the bits
foreach (string dllPath in results.UnusedArguments) {
try {
builder.LoadAssemblies(dllPath);
} catch (IOException e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("An error occured while loading assemblies for reflection. The error message is: {0}", e.Message));
return (1);
}
}
ConsoleApplication.WriteMessage(LogLevel.Info, String.Format("Loaded {0} assemblies for reflection and {1} dependency assemblies.", builder.Assemblies.Length, builder.AccessoryAssemblies.Length));
// register callbacks
//builder.RegisterStartTagCallback("apis", new MRefBuilderCallback(startTestCallback));
//MRefBuilderAddIn addin = new XamlAttachedMembersAddIn(builder, null);
builder.VisitApis();
ConsoleApplication.WriteMessage(LogLevel.Info, String.Format("Wrote information on {0} namespaces, {1} types, and {2} members", builder.Namespaces.Length, builder.Types.Length, builder.Members.Length));
} finally {
builder.Dispose();
}
} finally {
// output.Close();
}
return (0);
}
private static AssemblyNode AssemblyNotFound(AssemblyReference reference, System.Compiler.Module module) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("Unresolved assembly reference: {0} ({1}) required by {2}", reference.Name, reference.StrongName, module.Name));
Environment.Exit(1);
return (null);
}
private static void UnresolvedAssemblyReferenceHandler(Object o, AssemblyReferenceEventArgs e) {
ConsoleApplication.WriteMessage(LogLevel.Error, String.Format("Unresolved assembly reference: {0} ({1}) required by {2}", e.Reference.Name, e.Reference.StrongName, e.Referrer.Name));
Environment.Exit(1);
}
}
}
|