2002-11-29 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Compilation / GlobalAsaxCompiler.cs
1 //
2 // System.Web.Compilation.GlobalAsaxCompiler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 //
9 using System;
10 using System.Collections;
11 using System.IO;
12 using System.Reflection;
13 using System.Text;
14 using System.Web;
15 using System.Web.Util;
16
17 namespace System.Web.Compilation
18 {
19         class GlobalAsaxCompiler : BaseCompiler
20         {
21                 Hashtable options;
22                 string filename;
23                 string sourceFile;
24
25                 private GlobalAsaxCompiler (string filename)
26                 {
27                         this.filename = filename;
28                 }
29
30                 public override Type GetCompiledType ()
31                 {
32                         sourceFile = GenerateSourceFile ();
33
34                         CachingCompiler compiler = new CachingCompiler (this);
35                         CompilationResult result = new CompilationResult ();
36                         if (compiler.Compile (result) == false)
37                                 throw new CompilationException (result);
38                                 
39                         Assembly assembly = Assembly.LoadFrom (result.OutputFile);
40                         Type [] types = assembly.GetTypes ();
41                         if (types.Length != 1)
42                                 throw new CompilationException ("More than 1 Type in an application?", result);
43
44                         result.Data = types [0];
45                         return types [0];
46                 }
47
48                 public override string Key {
49                         get {
50                                 return filename;
51                         }
52                 }
53
54                 public override string SourceFile {
55                         get {
56                                 return sourceFile;
57                         }
58                 }
59
60                 public override string CompilerOptions {
61                         get {
62                                 if (options == null)
63                                         return base.CompilerOptions;
64
65                                 StringBuilder sb = new StringBuilder (base.CompilerOptions);
66                                 string compilerOptions = options ["CompilerOptions"] as string;
67                                 if (compilerOptions != null) {
68                                         sb.Append (' ');
69                                         sb.Append (compilerOptions);
70                                 }
71
72                                 string references = options ["References"] as string;
73                                 if (references == null)
74                                         return sb.ToString ();
75
76                                 string [] split = references.Split (' ');
77                                 foreach (string s in split)
78                                         sb.AppendFormat (" /r:{0}", s);
79
80                                 return sb.ToString ();
81                         }
82                 }
83
84                 public static Type CompileApplicationType (string filename)
85                 {
86                         CompilationCacheItem item = CachingCompiler.GetCached (filename);
87                         if (item != null && item.Result != null) {
88                                 if (item.Result != null)
89                                         return item.Result.Data as Type;
90
91                                 throw new CompilationException (item.Result);
92                         }
93
94                         GlobalAsaxCompiler gac = new GlobalAsaxCompiler (filename);
95                         return gac.GetCompiledType ();
96                 }
97
98                 string GenerateSourceFile ()
99                 {
100                         Stream input = File.OpenRead (filename);
101                         AspParser parser = new AspParser (filename, input);
102                         parser.Parse ();
103                         AspGenerator generator = new AspGenerator (filename, parser.Elements);
104                         generator.BaseType = typeof (HttpApplication).ToString ();
105                         generator.ProcessElements ();
106                         string generated = generator.GetCode ().ReadToEnd ();
107                         options = generator.Options;
108
109                         //FIXME: should get Tmp dir for this application
110                         string csName = Path.GetTempFileName () + ".cs";
111                         WebTrace.WriteLine ("Writing {0}", csName);
112                         StreamWriter output = new StreamWriter (File.OpenWrite (csName));
113                         output.Write (generated);
114                         output.Close ();
115                         return csName;
116                 }
117         }
118 }
119