5 // Iain McCoy (iain@mccoy.id.au)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
33 using System.CodeDom.Compiler;
34 using System.Reflection;
35 using Mono.GetOptions;
36 using Mono.Windows.Serialization;
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")]
43 [assembly: Mono.UsageComplement ("")]
45 [assembly: Mono.About("Compiler to create normal clr-based high level language source code from XAML")]
46 [assembly: Mono.Author ("Iain McCoy")]
48 class XamlOptions : Options {
49 [Option("Whether or not the class should be marked as partial", "p", "partial")]
52 [Option("the file to output to", "o", "output")]
53 public string OutputFile;
55 [Option("the language in which to write the output file", "l", "lang")]
56 public string OutputLanguage;
60 public static void process(XamlOptions options, string input) {
61 if (!input.EndsWith(".xaml")) {
62 Console.WriteLine("Input filenames must end in .xaml");
65 if (Environment.Version.Major < 2 && options.Partial) {
66 Console.WriteLine("This runtime version does not support partial classes");
69 if (options.OutputFile == null) {
70 options.OutputFile = input + ".out";
72 ICodeGenerator generator = getGenerator(options.OutputLanguage);
73 XmlTextReader xr = new XmlTextReader(input);
75 string result = ParserToCode.Parse(xr, generator, options.Partial);
76 TextWriter tw = new StreamWriter(options.OutputFile);
80 catch (Exception ex) {
81 Console.WriteLine("Line " + xr.LineNumber + ", Column " + xr.LinePosition);
86 private static ICodeGenerator getGenerator(string language)
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();
93 Console.WriteLine("Unknown language: " + language);
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");
106 process(options, options.RemainingArguments[0]);