2 // System.Web.Compilation.GlobalAsaxCompiler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
10 using System.Collections;
12 using System.Reflection;
15 using System.Web.Util;
17 namespace System.Web.Compilation
19 class GlobalAsaxCompiler : BaseCompiler
25 private GlobalAsaxCompiler (string filename)
27 this.filename = filename;
30 public override Type GetCompiledType ()
32 sourceFile = GenerateSourceFile ();
34 CachingCompiler compiler = new CachingCompiler (this);
35 CompilationResult result = new CompilationResult ();
36 if (compiler.Compile (result) == false)
37 throw new CompilationException (result);
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);
44 result.Data = types [0];
48 public override string Key {
54 public override string SourceFile {
60 public override string CompilerOptions {
63 return base.CompilerOptions;
65 StringBuilder sb = new StringBuilder (base.CompilerOptions);
66 string compilerOptions = options ["CompilerOptions"] as string;
67 if (compilerOptions != null) {
69 sb.Append (compilerOptions);
72 string references = options ["References"] as string;
73 if (references == null)
74 return sb.ToString ();
76 string [] split = references.Split (' ');
77 foreach (string s in split)
78 sb.AppendFormat (" /r:{0}", s);
80 return sb.ToString ();
84 public static Type CompileApplicationType (string filename)
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;
91 throw new CompilationException (item.Result);
94 GlobalAsaxCompiler gac = new GlobalAsaxCompiler (filename);
95 return gac.GetCompiledType ();
98 string GenerateSourceFile ()
100 Stream input = File.OpenRead (filename);
101 AspParser parser = new AspParser (filename, input);
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;
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);