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;
45 public int Run (string[] args)
51 Console.WriteLine ("Mono Xml Serializer Generator Tool");
52 Console.WriteLine ("Mono version " + Environment.Version);
56 if (unknownArg != null)
58 Console.WriteLine ("Unknown option: " + unknownArg);
65 Console.WriteLine ("Usage: sgen [options]");
70 if (assembly == null) {
71 Console.WriteLine ("Assembly name not provided");
79 asm = Assembly.Load (assembly);
85 asm = Assembly.LoadFrom (assembly);
87 Console.WriteLine ("Specified assembly cannot be loaded.");
92 ArrayList userTypes = new ArrayList ();
93 ArrayList maps = new ArrayList ();
94 XmlReflectionImporter imp = new XmlReflectionImporter ();
97 Console.WriteLine ("Generating serializer for the following types:");
100 foreach (Type t in asm.GetTypes ()) {
102 maps.Add (imp.ImportTypeMapping (t));
105 Console.WriteLine( " - " + t );
106 } catch (InvalidOperationException ex) {
108 Console.WriteLine (" - Warning: " + ex.Message);
109 } catch (NotImplementedException ex) {
111 Console.WriteLine (" - Warning: ignoring '" + t + "'");
112 Console.WriteLine (" " + ex.Message);
117 foreach (string type in types) {
119 Type t = asm.GetType (type);
120 maps.Add (imp.ImportTypeMapping (t));
123 Console.WriteLine (" - " + t);
124 } catch (InvalidOperationException ex) {
126 Console.WriteLine (" - Warning: " + ex.Message);
127 } catch (NotImplementedException ex) {
129 Console.WriteLine (" - Warning: ignoring '" + type + "'");
130 Console.WriteLine (" " + ex.Message);
137 Console.WriteLine ();
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;
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);
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 ();
156 XmlSerializer.GenerateSerializer (
157 (Type[]) userTypes.ToArray (typeof(Type)),
158 (XmlTypeMapping[]) maps.ToArray (typeof(XmlTypeMapping)),
162 Console.WriteLine ("Generated assembly: " + file);
163 Console.WriteLine ();
169 public int Run (string[] args)
171 Console.WriteLine ("This tool is only supported in 2.0 profile.");
177 void ParseArgs (string[] args)
179 foreach (string arg in args)
181 int index = arg.Length > 2 && arg [0] == '-' && arg [1] == '-' ? 2 : -1;
182 index = index >= 0 ? index : arg.Length > 0 && arg [0] == '/' ? 1 : -1;
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") {
196 else if (op == "type" || op == "t") {
197 if (types == null) types = new ArrayList ();
200 else if (op == "reference" || op == "r") {
201 references.Add (param);
203 else if (op == "compiler" || op == "c") {
204 compilerOptions = param;
206 else if (op == "proxytypes" || op == "p") {
209 else if (op == "debug" || op == "d") {
212 else if (op == "keep" || op == "k") {
215 else if (op == "force" || op == "f") {
218 else if (op == "out" || op == "o") {
221 else if (op == "?" || op == "help") {
224 else if (op == "nologo" || op == "n") {
227 else if (op == "silent" || op == "s") {
230 else if (op == "verbose" || op == "v") {
233 else if (arg.StartsWith ("/") && (arg.EndsWith (".dll") || arg.EndsWith (".exe")) && arg.IndexOfAny (Path.InvalidPathChars) == -1)