5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // Copyright (C) 2003 Ximian, Inc.
11 using System.Xml.Serialization;
13 using System.Reflection;
14 using System.Collections;
15 using System.Collections.Specialized;
16 using System.CodeDom.Compiler;
20 static int Main (string[] args)
22 Driver d = new Driver();
27 ArrayList references = new ArrayList ();
29 string compilerOptions;
43 public int Run (string[] args)
49 Console.WriteLine ("Mono Xml Serializer Generator Tool");
50 Console.WriteLine ("Mono version " + Environment.Version);
54 if (unknownArg != null)
56 Console.WriteLine ("Unknown option: " + unknownArg);
63 Console.WriteLine ("Usage: sgen [options]");
68 if (assembly == null) {
69 Console.WriteLine ("Assembly name not provided");
77 asm = Assembly.Load (assembly);
82 asm = Assembly.LoadFrom (assembly);
85 ArrayList userTypes = new ArrayList ();
86 ArrayList maps = new ArrayList ();
87 XmlReflectionImporter imp = new XmlReflectionImporter ();
90 Console.WriteLine ("Generating serializer for the following types:");
92 foreach (Type t in asm.GetTypes())
95 maps.Add (imp.ImportTypeMapping (t));
96 if (types == null || types.Contains (t.ToString())) {
98 if (verbose) Console.WriteLine (" - " + t);
104 Console.WriteLine (" - Warning: ignoring '" + t + "'");
105 Console.WriteLine (" " + ex.Message);
111 Console.WriteLine ();
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;
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);
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 ();
130 XmlSerializer.GenerateSerializer (
131 (Type[]) userTypes.ToArray (typeof(Type)),
132 (XmlTypeMapping[]) maps.ToArray (typeof(XmlTypeMapping)),
136 Console.WriteLine ("Generated assembly: " + file);
137 Console.WriteLine ();
143 public int Run (string[] args)
145 Console.WriteLine ("This tool is only supported in Mono 2.0");
151 void ParseArgs (string[] args)
153 foreach (string arg in args)
156 if (!arg.StartsWith ("--") && !arg.StartsWith ("/"))
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) : "";
167 if (op == "assembly" || op == "a") {
170 else if (op == "type" || op == "t") {
171 if (types == null) types = new ArrayList ();
174 else if (op == "reference" || op == "r") {
175 references.Add (param);
177 else if (op == "compiler" || op == "c") {
178 compilerOptions = param;
180 else if (op == "proxytypes" || op == "p") {
183 else if (op == "debug" || op == "d") {
186 else if (op == "keep" || op == "k") {
189 else if (op == "force" || op == "f") {
192 else if (op == "out" || op == "o") {
195 else if (op == "?" || op == "help") {
198 else if (op == "nologo" || op == "n") {
201 else if (op == "silent" || op == "s") {
204 else if (op == "verbose" || op == "v") {
207 else if (arg.StartsWith ("/") && (arg.EndsWith (".dll") || arg.EndsWith (".exe")) && arg.IndexOfAny (Path.InvalidPathChars) == -1)