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);
113 } catch (NotSupportedException ex) {
115 Console.WriteLine (" - Warning: " + ex.Message);
119 foreach (string type in types) {
121 Type t = asm.GetType (type);
122 maps.Add (imp.ImportTypeMapping (t));
125 Console.WriteLine (" - " + t);
126 } catch (InvalidOperationException ex) {
128 Console.WriteLine (" - Warning: " + ex.Message);
129 } catch (NotImplementedException ex) {
131 Console.WriteLine (" - Warning: ignoring '" + type + "'");
132 Console.WriteLine (" " + ex.Message);
134 } catch (NotSupportedException ex) {
136 Console.WriteLine (" - Warning: " + ex.Message);
142 Console.WriteLine ();
144 CompilerParameters parameters = new CompilerParameters ();
145 parameters.GenerateInMemory = false;
146 parameters.IncludeDebugInformation = debug;
147 parameters.ReferencedAssemblies.AddRange ((string[])references.ToArray(typeof(string)));
148 parameters.TempFiles = new TempFileCollection (Environment.CurrentDirectory, keep);
149 parameters.CompilerOptions = compilerOptions;
151 string file = Path.GetFileNameWithoutExtension (asm.Location) + ".XmlSerializers.dll";
152 if (outDir == null) outDir = Path.GetDirectoryName (asm.Location);
153 parameters.OutputAssembly = Path.Combine (outDir, file);
155 if (File.Exists (parameters.OutputAssembly) && !force) {
156 Console.WriteLine ("Cannot generate assembly '" + parameters.OutputAssembly + "' because it already exist. Use /force option to overwrite the existing assembly");
157 Console.WriteLine ();
161 XmlSerializer.GenerateSerializer (
162 (Type[]) userTypes.ToArray (typeof(Type)),
163 (XmlTypeMapping[]) maps.ToArray (typeof(XmlTypeMapping)),
167 Console.WriteLine ("Generated assembly: " + file);
168 Console.WriteLine ();
174 void ParseArgs (string[] args)
176 foreach (string arg in args)
178 int index = arg.Length > 2 && arg [0] == '-' && arg [1] == '-' ? 2 : -1;
179 index = index >= 0 ? index : arg.Length > 0 && arg [0] == '/' ? 1 : -1;
186 int i = arg.IndexOf (':', index);
187 if (i == -1) i = arg.Length;
188 string op = arg.Substring (index, i - index).ToLowerInvariant ();
189 string param = (i < arg.Length - index) ? arg.Substring (i + 1) : "";
190 if (op == "assembly" || op == "a") {
193 else if (op == "type" || op == "t") {
194 if (types == null) types = new ArrayList ();
197 else if (op == "reference" || op == "r") {
198 references.Add (param);
200 else if (op == "compiler" || op == "c") {
201 compilerOptions = param;
203 else if (op == "proxytypes" || op == "p") {
206 else if (op == "debug" || op == "d") {
209 else if (op == "keep" || op == "k") {
212 else if (op == "force" || op == "f") {
215 else if (op == "out" || op == "o") {
218 else if (op == "?" || op == "help") {
221 else if (op == "nologo" || op == "n") {
224 else if (op == "silent" || op == "s") {
227 else if (op == "verbose" || op == "v") {
230 else if (arg.StartsWith ("/") && (arg.EndsWith (".dll") || arg.EndsWith (".exe")) && arg.IndexOfAny (Path.InvalidPathChars) == -1)