fc842469f21dd48ce6517a086abd1d1acb7353c2
[mono.git] / mcs / tools / type-reflector / ConsoleOutput.cs
1 //
2 // ConsoleOutput.cs: 
3 //   Finds types and (optionally) shows reflection information about 
4 //   the types.
5 //
6 // Author: Jonathan Pryor (jonpryor@vt.edu)
7 //
8 // (C) 2002 Jonathan Pryor
9 //
10
11 // #define TRACE
12
13 using System;
14 using System.Collections;
15 using System.IO;
16 using System.Diagnostics;
17 using System.Reflection;
18 using System.Text;
19 using System.Text.RegularExpressions;
20
21 namespace Mono.TypeReflector
22 {
23         public class ConsoleOutput {
24
25                 private static BooleanSwitch console = new BooleanSwitch ("console",
26                                 "console-specific and command-line handling output");
27
28                 private static void TraceStringArray (string message, IEnumerable contents)
29                 {
30                         Trace.WriteLineIf (console.Enabled, message);
31                         foreach (string s in contents) {
32                                 Trace.WriteLineIf (console.Enabled, "  " + s);
33                         }
34                 }
35
36                 private static void PrintVersion ()
37                 {
38                         Console.WriteLine ("type-reflector 0.5");
39                         Console.WriteLine ("Written by Jonathan Pryor.");
40                         Console.WriteLine ();
41                         Console.WriteLine ("Copyright (C) 2002 Jonathan Pryor.");
42                 }
43
44                 private static void InitFactory ()
45                 {
46                         // TypeDisplayerFactory.Add ("explicit", typeof(ExplicitTypeDisplayer));
47                         // TypeDisplayerFactory.Add ("reflection", typeof(ReflectionTypeDisplayer));
48                         // TypeDisplayerFactory.Add ("c#", typeof(CSharpTypeDisplayer));
49                         Factories.FormatterFactory.Add ("default", typeof (DefaultNodeFormatter));
50                         Factories.FormatterFactory.Add ("csharp", typeof (CSharpNodeFormatter));
51                         Factories.FinderFactory.Add ("explicit", typeof (ExplicitNodeFinder));
52                         Factories.FinderFactory.Add ("reflection", typeof (ReflectionNodeFinder));
53                 }
54
55                 public static void Main (string[] args)
56                 {
57                         InitFactory ();
58
59                         TypeReflectorOptions options = new TypeReflectorOptions ();
60
61                         try {
62                                 options.ParseOptions (args);
63                         } catch (Exception e) {
64                                 Console.WriteLine (e.Message);
65                                 Console.WriteLine ("See `{0} --help' for more information", ProgramOptions.ProgramName);
66                                 // Console.WriteLine ("** Full Message continues:\n" + e);
67                                 return;
68                         }
69
70                         if (options.FoundHelp) {
71                                 Console.WriteLine (options.OptionsHelp);
72                                 return;
73                         }
74
75                         if (options.DefaultAssemblies) {
76                                 Console.WriteLine ("The default search assemblies are:");
77                                 foreach (string s in TypeReflectorOptions.GetDefaultAssemblies ()) {
78                                         Console.WriteLine ("  {0}", s);
79                                 }
80                                 return;
81                         }
82
83                         if (options.Version) {
84                                 PrintVersion ();
85                                 return;
86                         }
87
88                         if (options.Types.Count == 0) {
89                                 Console.WriteLine ("No types specified.");
90                                 Console.WriteLine ("See `{0} --help' for more information", ProgramOptions.ProgramName);
91                                 return;
92                         }
93
94                         TraceStringArray ("Search Assemblies: ", options.Assemblies);
95                         TraceStringArray ("Search for Types: ", options.Types);
96
97                         TypeLoader loader = new TypeLoader (options.Assemblies);
98                         loader.MatchBase = options.MatchBase;
99                         loader.MatchFullName = options.MatchFullName;
100                         loader.MatchClassName = options.MatchClassName;
101                         loader.MatchNamespace = options.MatchNamespace;
102                         loader.MatchMethodReturnType = options.MatchReturnType;
103
104                         IndentingTextWriter writer = new IndentingTextWriter (Console.Out);
105
106                         int depth = options.MaxDepth;
107
108                         INodeFormatter formatter = Factories.FormatterFactory.Create (options.Formatter);
109                         if (formatter == null) {
110                                 Console.WriteLine ("Error: invalid formatter: " + options.Formatter);
111                                 return;
112                         }
113
114                         NodeFinder f = (NodeFinder) Factories.FinderFactory.Create (options.Finder);
115                         if (f == null) {
116                                 Console.WriteLine ("Error: invalid finder: " + options.Finder);
117                                 return;
118                         }
119
120                         f.VerboseOutput = options.VerboseOutput;
121                         f.ShowBase = options.ShowBase;
122                         f.ShowConstructors = options.ShowConstructors;
123                         f.ShowEvents = options.ShowEvents;
124                         f.ShowFields = options.ShowFields;
125                         f.ShowInterfaces = options.ShowInterfaces;
126                         f.ShowMethods = options.ShowMethods;
127                         f.ShowProperties = options.ShowProperties;
128                         f.ShowTypeProperties = options.ShowTypeProperties;
129                         f.ShowInheritedMembers = options.ShowInheritedMembers;
130                         f.ShowNonPublic = options.ShowNonPublic;
131                         f.ShowMonoBroken = options.ShowMonoBroken;
132                         f.FlattenHierarchy = options.FlattenHierarchy;
133                         f.MaxDepth = options.MaxDepth;
134
135                         foreach (string t in options.Types) {
136                                 try {
137                                         ICollection typesFound = loader.LoadTypes (t);
138                                         if (typesFound.Count > 0)
139                                                 foreach (Type type in loader.LoadTypes(t)) {
140                                                         // Console.WriteLine ("** displaying type: " + type);
141                                                         Node root = new Node (formatter, 
142                                                                         f);
143                                                                         // new GroupingNodeFinder (f));
144                                                                         // new ExplicitNodeFinder());
145                                                         // root.Extra = new NodeInfo (null, type, NodeTypes.Type);
146                                                         root.NodeInfo = new NodeInfo (null, type);
147                                                         ShowNode (root, writer, depth);
148                                                 }
149                                         else
150                                                 Console.WriteLine ("Unable to find type `{0}'.", t);
151                                 } catch (Exception e) {
152                                         Console.WriteLine ("Unable to display type `{0}': {1}.", t, e.ToString());
153                                 }
154                         }
155                 }
156
157                 private static void ShowNode (Node root, IndentingTextWriter writer, int maxDepth)
158                 {
159                         // Console.WriteLine ("** current max depth: " + maxDepth);
160                         writer.WriteLine (root.Description);
161                         if (maxDepth > 0) {
162                                 using (Indenter i = new Indenter (writer)) {
163                                         foreach (Node child in root.GetChildren()) {
164                                                 ShowNode (child, writer, maxDepth-1);
165                                         }
166                                 }
167                         }
168                 }
169         }
170 }
171