[mkbundle] Remove no longer needed System.Reflection hack
[mono.git] / mcs / tools / sgen / sgen.cs
1 // 
2 // sgen.cs
3 //
4 // Authors:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //   Atsushi Enomoto (atsushi@ximian.com)
7 //
8 // Copyright (C) 2003 Ximian, Inc.
9 // Copyright (C) 2006 Novell, Inc.
10 //
11
12 using System;
13 using System.Xml.Serialization;
14 using System.IO;
15 using System.Reflection;
16 using System.Collections;
17 using System.Collections.Specialized;
18 using System.CodeDom.Compiler;
19
20 public class Driver
21 {
22         static int Main (string[] args)
23         {
24                 Driver d = new Driver();
25                 return d.Run (args);
26         }
27                 
28         string assembly;
29         ArrayList references = new ArrayList ();
30         ArrayList types;
31         string compilerOptions;
32         bool proxyTypes;
33         bool debug;
34         bool keep;
35         bool force;
36         string outDir;
37         bool help;
38         bool silent;
39         bool nologo;
40         bool verbose;
41         string unknownArg;
42         
43 #if NET_2_0
44
45         public int Run (string[] args)
46         {
47                 ParseArgs (args);
48                 
49                 if (!nologo)
50                 {
51                         Console.WriteLine ("Mono Xml Serializer Generator Tool");
52                         Console.WriteLine ("Mono version " + Environment.Version);
53                         Console.WriteLine ();
54                 }
55                 
56                 if (unknownArg != null)
57                 {
58                         Console.WriteLine ("Unknown option: " + unknownArg);
59                         Console.WriteLine ();
60                         return 1;
61                 }
62                 
63                 if (help)
64                 {
65                         Console.WriteLine ("Usage: sgen [options]");
66                         Console.WriteLine ();
67                         return 0;
68                 }
69                 
70                 if (assembly == null) {
71                         Console.WriteLine ("Assembly name not provided");
72                         Console.WriteLine ();
73                         return 1;
74                 }
75                 
76                 Assembly asm = null;
77                 
78                 try {
79                         asm = Assembly.Load (assembly);
80                 }
81                 catch {}
82                 
83                 if (asm == null) {
84                         try {
85                                 asm = Assembly.LoadFrom (assembly);
86                         } catch {
87                                 Console.WriteLine ("Specified assembly cannot be loaded.");
88                                 Console.WriteLine ();
89                                 return 1;
90                         }
91                 }
92                 ArrayList userTypes = new ArrayList ();
93                 ArrayList maps = new ArrayList ();
94                 XmlReflectionImporter imp = new XmlReflectionImporter ();
95                 
96                 if (verbose)
97                         Console.WriteLine ("Generating serializer for the following types:");
98
99                 if (types == null) {
100                         foreach (Type t in asm.GetTypes ()) {
101                                 try {
102                                         maps.Add (imp.ImportTypeMapping (t));
103                                         userTypes.Add (t);
104                                         if (verbose)
105                                                 Console.WriteLine( " - " + t );
106                                 } catch (InvalidOperationException ex) {
107                                         if (verbose)
108                                                 Console.WriteLine (" - Warning: " + ex.Message);
109                                 } catch (NotImplementedException ex) {
110                                         if (verbose) {
111                                                 Console.WriteLine (" - Warning: ignoring '" + t + "'");
112                                                 Console.WriteLine ("   " + ex.Message);
113                                         }
114                                 }
115                         }
116                 } else {
117                         foreach (string type in types) {
118                                 try {
119                                         Type t = asm.GetType (type);
120                                         maps.Add (imp.ImportTypeMapping (t));
121                                         userTypes.Add (t);
122                                         if (verbose)
123                                                 Console.WriteLine (" - " + t);
124                                 } catch (InvalidOperationException ex) {
125                                         if (verbose)
126                                                 Console.WriteLine (" - Warning: " + ex.Message);
127                                 } catch (NotImplementedException ex) {
128                                         if (verbose) {
129                                                 Console.WriteLine (" - Warning: ignoring '" + type + "'");
130                                                 Console.WriteLine ("   " + ex.Message);
131                                         }
132                                 }
133                         }
134                 }
135
136                 if (verbose)
137                         Console.WriteLine ();
138                         
139                 CompilerParameters parameters = new CompilerParameters ();
140                 parameters.GenerateInMemory = false;
141                 parameters.IncludeDebugInformation = debug;
142                 parameters.ReferencedAssemblies.AddRange ((string[])references.ToArray(typeof(string)));
143                 parameters.TempFiles = new TempFileCollection (Environment.CurrentDirectory, keep);
144                 parameters.CompilerOptions = compilerOptions;
145                 
146                 string file = Path.GetFileNameWithoutExtension (asm.Location) + ".XmlSerializers.dll";
147                 if (outDir == null) outDir = Path.GetDirectoryName (asm.Location);
148                 parameters.OutputAssembly = Path.Combine (outDir, file);
149                 
150                 if (File.Exists (parameters.OutputAssembly) && !force) {
151                         Console.WriteLine ("Cannot generate assembly '" + parameters.OutputAssembly + "' because it already exist. Use /force option to overwrite the existing assembly");
152                         Console.WriteLine ();
153                         return 1;
154                 }
155                 
156                 XmlSerializer.GenerateSerializer (
157                                 (Type[]) userTypes.ToArray (typeof(Type)), 
158                                 (XmlTypeMapping[]) maps.ToArray (typeof(XmlTypeMapping)), 
159                                 parameters);
160                                 
161                 if (!silent) {
162                         Console.WriteLine ("Generated assembly: " + file);
163                         Console.WriteLine ();
164                 }
165                 
166                 return 0;
167         }
168 #else
169         public int Run (string[] args)
170         {
171                 Console.WriteLine ("This tool is only supported in 2.0 profile.");
172                 return 1;
173         }
174
175 #endif
176
177         void ParseArgs (string[] args)
178         {
179                 foreach (string arg in args)
180                 {
181                         int index = arg.Length > 2 && arg [0] == '-' && arg [1] == '-' ? 2 : -1;
182                         index = index >= 0 ? index : arg.Length > 0 && arg [0] == '/' ? 1 : -1;
183                         if (index < 0)
184                         {
185                                 assembly = arg;
186                                 continue;
187                         }
188                         
189                         int i = arg.IndexOf (':', index);
190                         if (i == -1) i = arg.Length;
191                         string op = arg.Substring (index, i - index).ToLowerInvariant ();
192                         string param = (i < arg.Length - index) ? arg.Substring (i + 1) : "";
193                         if (op == "assembly" || op == "a") {
194                                 assembly = param;
195                         }
196                         else if (op == "type" || op == "t") {
197                                 if (types == null) types = new ArrayList ();
198                                 types.Add (param);
199                         }
200                         else if (op == "reference" || op == "r") {
201                                 references.Add (param);
202                         }
203                         else if (op == "compiler" || op == "c") {
204                                 compilerOptions = param;
205                         }
206                         else if (op == "proxytypes" || op == "p") {
207                                 proxyTypes = true;
208                         }
209                         else if (op == "debug" || op == "d") {
210                                 debug = true;
211                         }
212                         else if (op == "keep" || op == "k") {
213                                 keep = true;
214                         }
215                         else if (op == "force" || op == "f") {
216                                 force = true;
217                         }
218                         else if (op == "out" || op == "o") {
219                                 outDir = param;
220                         }
221                         else if (op == "?" || op == "help") {
222                                 help = true;
223                         }
224                         else if (op == "nologo" || op == "n") {
225                                 nologo = true;
226                         }
227                         else if (op == "silent" || op == "s") {
228                                 silent = true;
229                         }
230                         else if (op == "verbose" || op == "v") {
231                                 verbose = true;
232                         }
233                         else if (arg.StartsWith ("/") && (arg.EndsWith (".dll") || arg.EndsWith (".exe")) && arg.IndexOfAny (Path.InvalidPathChars) == -1)
234                         {
235                                 assembly = arg;
236                                 continue;
237                         }
238                         else {
239                                 unknownArg = arg;
240                                 return;
241                         }
242                 }
243         }
244 }
245