add ISafeSerializationData
[mcs.git] / tools / sgen / sgen.cs
blobeb4e83acb594e007f08a037b9b633693b077b192
1 //
2 // sgen.cs
3 //
4 // Authors:
5 // Lluis Sanchez Gual (lluis@ximian.com)
6 // Atsushi Enomoto (atsushi@ximian.com)
7 //
8 // Copyright (C) 2003 Ximian, Inc.
9 // Copyright (C) 2006 Novell, Inc.
12 using System;
13 using System.Xml.Serialization;
14 using System.IO;
15 using System.Reflection;
16 using System.Collections;
17 using System.Collections.Specialized;
18 using System.CodeDom.Compiler;
20 public class Driver
22 static int Main (string[] args)
24 Driver d = new Driver();
25 return d.Run (args);
28 string assembly;
29 ArrayList references = new ArrayList ();
30 ArrayList types;
31 string compilerOptions;
32 bool proxyTypes;
33 bool debug;
34 bool keep;
35 bool force;
36 string outDir;
37 bool help;
38 bool silent;
39 bool nologo;
40 bool verbose;
41 string unknownArg;
43 #if NET_2_0
45 public int Run (string[] args)
47 ParseArgs (args);
49 if (!nologo)
51 Console.WriteLine ("Mono Xml Serializer Generator Tool");
52 Console.WriteLine ("Mono version " + Environment.Version);
53 Console.WriteLine ();
56 if (unknownArg != null)
58 Console.WriteLine ("Unknown option: " + unknownArg);
59 Console.WriteLine ();
60 return 1;
63 if (help)
65 Console.WriteLine ("Usage: sgen [options]");
66 Console.WriteLine ();
67 return 0;
70 if (assembly == null) {
71 Console.WriteLine ("Assembly name not provided");
72 Console.WriteLine ();
73 return 1;
76 Assembly asm = null;
78 try {
79 asm = Assembly.Load (assembly);
81 catch {}
83 if (asm == null) {
84 try {
85 asm = Assembly.LoadFrom (assembly);
86 } catch {
87 Console.WriteLine ("Specified assembly cannot be loaded.");
88 Console.WriteLine ();
89 return 1;
92 ArrayList userTypes = new ArrayList ();
93 ArrayList maps = new ArrayList ();
94 XmlReflectionImporter imp = new XmlReflectionImporter ();
96 if (verbose)
97 Console.WriteLine ("Generating serializer for the following types:");
99 if (types == null) {
100 foreach (Type t in asm.GetTypes ()) {
101 try {
102 maps.Add (imp.ImportTypeMapping (t));
103 userTypes.Add (t);
104 if (verbose)
105 Console.WriteLine( " - " + t );
106 } catch (InvalidOperationException ex) {
107 if (verbose)
108 Console.WriteLine (" - Warning: " + ex.Message);
109 } catch (NotImplementedException ex) {
110 if (verbose) {
111 Console.WriteLine (" - Warning: ignoring '" + t + "'");
112 Console.WriteLine (" " + ex.Message);
116 } else {
117 foreach (string type in types) {
118 try {
119 Type t = asm.GetType (type);
120 maps.Add (imp.ImportTypeMapping (t));
121 userTypes.Add (t);
122 if (verbose)
123 Console.WriteLine (" - " + t);
124 } catch (InvalidOperationException ex) {
125 if (verbose)
126 Console.WriteLine (" - Warning: " + ex.Message);
127 } catch (NotImplementedException ex) {
128 if (verbose) {
129 Console.WriteLine (" - Warning: ignoring '" + type + "'");
130 Console.WriteLine (" " + ex.Message);
136 if (verbose)
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 ();
153 return 1;
156 XmlSerializer.GenerateSerializer (
157 (Type[]) userTypes.ToArray (typeof(Type)),
158 (XmlTypeMapping[]) maps.ToArray (typeof(XmlTypeMapping)),
159 parameters);
161 if (!silent) {
162 Console.WriteLine ("Generated assembly: " + file);
163 Console.WriteLine ();
166 return 0;
168 #else
169 public int Run (string[] args)
171 Console.WriteLine ("This tool is only supported in 2.0 profile.");
172 return 1;
175 #endif
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;
183 if (index < 0)
185 assembly = arg;
186 continue;
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") {
194 assembly = param;
196 else if (op == "type" || op == "t") {
197 if (types == null) types = new ArrayList ();
198 types.Add (param);
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") {
207 proxyTypes = true;
209 else if (op == "debug" || op == "d") {
210 debug = true;
212 else if (op == "keep" || op == "k") {
213 keep = true;
215 else if (op == "force" || op == "f") {
216 force = true;
218 else if (op == "out" || op == "o") {
219 outDir = param;
221 else if (op == "?" || op == "help") {
222 help = true;
224 else if (op == "nologo" || op == "n") {
225 nologo = true;
227 else if (op == "silent" || op == "s") {
228 silent = true;
230 else if (op == "verbose" || op == "v") {
231 verbose = true;
233 else if (arg.StartsWith ("/") && (arg.EndsWith (".dll") || arg.EndsWith (".exe")) && arg.IndexOfAny (Path.InvalidPathChars) == -1)
235 assembly = arg;
236 continue;
238 else {
239 unknownArg = arg;
240 return;