5 // Lluis Sanchez Gual (lluis@ximian.com)
6 // Atsushi Enomoto (atsushi@ximian.com)
8 // Copyright (C) 2003 Ximian, Inc.
9 // Copyright (C) 2006 Novell, Inc.
13 using System.Xml.Serialization;
15 using System.Reflection;
16 using System.Collections;
17 using System.Collections.Specialized;
18 using System.CodeDom.Compiler;
22 static int Main (string[] args)
24 Driver d = new Driver();
29 ArrayList references = new ArrayList ();
31 string compilerOptions;
44 public int Run (string[] args)
50 Console.WriteLine ("Mono Xml Serializer Generator Tool");
51 Console.WriteLine ("Mono version " + Environment.Version);
55 if (unknownArg != null)
57 Console.WriteLine ("Unknown option: " + unknownArg);
64 Console.WriteLine ("Usage: sgen [options]");
69 if (assembly == null) {
70 Console.WriteLine ("Assembly name not provided");
78 asm = Assembly.Load (assembly);
84 asm = Assembly.LoadFrom (assembly);
86 Console.WriteLine ("Specified assembly cannot be loaded.");
91 ArrayList userTypes = new ArrayList ();
92 ArrayList maps = new ArrayList ();
93 XmlReflectionImporter imp = new XmlReflectionImporter ();
96 Console.WriteLine ("Generating serializer for the following types:");
99 foreach (Type t in asm.GetTypes ()) {
101 maps.Add (imp.ImportTypeMapping (t));
104 Console.WriteLine( " - " + t );
105 } catch (InvalidOperationException ex) {
107 Console.WriteLine (" - Warning: " + ex.Message);
108 } catch (NotImplementedException ex) {
110 Console.WriteLine (" - Warning: ignoring '" + t + "'");
111 Console.WriteLine (" " + ex.Message);
116 foreach (string type in types) {
118 Type t = asm.GetType (type);
119 maps.Add (imp.ImportTypeMapping (t));
122 Console.WriteLine (" - " + t);
123 } catch (InvalidOperationException ex) {
125 Console.WriteLine (" - Warning: " + ex.Message);
126 } catch (NotImplementedException ex) {
128 Console.WriteLine (" - Warning: ignoring '" + type + "'");
129 Console.WriteLine (" " + ex.Message);
136 Console.WriteLine ();
138 CompilerParameters parameters = new CompilerParameters ();
139 parameters.GenerateInMemory = false;
140 parameters.IncludeDebugInformation = debug;
141 parameters.ReferencedAssemblies.AddRange ((string[])references.ToArray(typeof(string)));
142 parameters.TempFiles = new TempFileCollection (Environment.CurrentDirectory, keep);
143 parameters.CompilerOptions = compilerOptions;
145 string file = Path.GetFileNameWithoutExtension (asm.Location) + ".XmlSerializers.dll";
146 if (outDir == null) outDir = Path.GetDirectoryName (asm.Location);
147 parameters.OutputAssembly = Path.Combine (outDir, file);
149 if (File.Exists (parameters.OutputAssembly) && !force) {
150 Console.WriteLine ("Cannot generate assembly '" + parameters.OutputAssembly + "' because it already exist. Use /force option to overwrite the existing assembly");
151 Console.WriteLine ();
155 XmlSerializer.GenerateSerializer (
156 (Type[]) userTypes.ToArray (typeof(Type)),
157 (XmlTypeMapping[]) maps.ToArray (typeof(XmlTypeMapping)),
161 Console.WriteLine ("Generated assembly: " + file);
162 Console.WriteLine ();
168 void ParseArgs (string[] args)
170 foreach (string arg in args)
172 int index = arg.Length > 2 && arg [0] == '-' && arg [1] == '-' ? 2 : -1;
173 index = index >= 0 ? index : arg.Length > 0 && arg [0] == '/' ? 1 : -1;
180 int i = arg.IndexOf (':', index);
181 if (i == -1) i = arg.Length;
182 string op = arg.Substring (index, i - index).ToLowerInvariant ();
183 string param = (i < arg.Length - index) ? arg.Substring (i + 1) : "";
184 if (op == "assembly" || op == "a") {
187 else if (op == "type" || op == "t") {
188 if (types == null) types = new ArrayList ();
191 else if (op == "reference" || op == "r") {
192 references.Add (param);
194 else if (op == "compiler" || op == "c") {
195 compilerOptions = param;
197 else if (op == "proxytypes" || op == "p") {
200 else if (op == "debug" || op == "d") {
203 else if (op == "keep" || op == "k") {
206 else if (op == "force" || op == "f") {
209 else if (op == "out" || op == "o") {
212 else if (op == "?" || op == "help") {
215 else if (op == "nologo" || op == "n") {
218 else if (op == "silent" || op == "s") {
221 else if (op == "verbose" || op == "v") {
224 else if (arg.StartsWith ("/") && (arg.EndsWith (".dll") || arg.EndsWith (".exe")) && arg.IndexOfAny (Path.InvalidPathChars) == -1)