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