* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / tools / xamlc / xamlc.cs
1 //
2 // xamlc.cs
3 //
4 // Author:
5 //   Iain McCoy (iain@mccoy.id.au)
6 //
7 // (C) 2005 Iain McCoy
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.IO;
31 using System.Xml;
32 using System.CodeDom;
33 using System.CodeDom.Compiler;
34 using System.Reflection;
35 using Mono.GetOptions;
36 using Mono.Windows.Serialization;
37
38 [assembly: AssemblyTitle ("xamlc.exe")]
39 [assembly: AssemblyVersion (Consts.MonoVersion)]
40 [assembly: AssemblyDescription ("Compiler from XAML to more conventional languages")]
41 [assembly: AssemblyCopyright ("(c) Iain McCoy")]
42
43 [assembly: Mono.UsageComplement ("")]
44
45 [assembly: Mono.About("Compiler to create normal clr-based high level language source code from XAML")]
46 [assembly: Mono.Author ("Iain McCoy")]
47
48 class XamlOptions : Options {
49         [Option("Whether or not the class should be marked as partial", "p", "partial")]
50         public bool Partial;
51         
52         [Option("the file to output to", "o", "output")]
53         public string OutputFile;
54
55         [Option("the language in which to write the output file", "l", "lang")]
56         public string OutputLanguage;
57 }
58
59 class Driver {
60         public static void process(XamlOptions options, string input) {
61                 if (!input.EndsWith(".xaml")) {
62                         Console.WriteLine("Input filenames must end in .xaml");
63                         return;
64                 }
65                 if (Environment.Version.Major < 2 && options.Partial) {
66                         Console.WriteLine("This runtime version does not support partial classes");
67                         return;
68                 }
69                 if (options.OutputFile == null) {
70                         options.OutputFile = input + ".out";
71                 }
72                 ICodeGenerator generator = getGenerator(options.OutputLanguage);
73                 XmlTextReader xr = new XmlTextReader(input);
74                 try {
75                         string result = ParserToCode.Parse(xr, generator, options.Partial);
76                         TextWriter tw = new StreamWriter(options.OutputFile);
77                         tw.Write(result);
78                         tw.Close();
79                 }
80                 catch (Exception ex) {
81                         Console.WriteLine("Line " + xr.LineNumber + ", Column " + xr.LinePosition);
82                         throw ex;
83                 }
84         }
85
86         private static ICodeGenerator getGenerator(string language)
87         {
88                 if (language == null || language == "cs" || language == "c#") {
89                         return (new Microsoft.CSharp.CSharpCodeProvider()).CreateGenerator();
90                 } else if (language == "vb") {
91                         return (new Microsoft.VisualBasic.VBCodeProvider()).CreateGenerator();
92                 } else {
93                         Console.WriteLine("Unknown language: " + language);
94                         Environment.Exit(1);
95                         return null;
96                 }
97         }
98         
99         public static void Main(string[] args) {
100                 XamlOptions options = new XamlOptions();
101                 options.ProcessArgs(args);
102                 if (options.RemainingArguments.Length != 1) {
103                         Console.WriteLine("Need exactly one input file");
104                         return;
105                 }
106                 process(options, options.RemainingArguments[0]);
107         }
108 }