GlobalAsaxCompiler.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // System.Web.Compilation.GlobalAsaxCompiler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Web;
  14. using System.Web.Util;
  15. namespace System.Web.Compilation
  16. {
  17. class GlobalAsaxCompiler : BaseCompiler
  18. {
  19. string filename;
  20. string sourceFile;
  21. HttpContext context;
  22. private GlobalAsaxCompiler (string filename)
  23. {
  24. this.filename = filename;
  25. }
  26. public override Type GetCompiledType ()
  27. {
  28. sourceFile = GenerateSourceFile ();
  29. CachingCompiler compiler = new CachingCompiler (this);
  30. CompilationResult result = new CompilationResult (sourceFile);
  31. result.Options = options;
  32. if (compiler.Compile (result) == false)
  33. throw new CompilationException (result);
  34. result.Dependencies = dependencies;
  35. Assembly assembly = Assembly.LoadFrom (result.OutputFile);
  36. Type [] types = assembly.GetTypes ();
  37. foreach (Type t in types) {
  38. if (t.IsSubclassOf (typeof (HttpApplication))) {
  39. if (result.Data != null)
  40. throw new CompilationException ("More that 1 app!!!", result);
  41. result.Data = t;
  42. }
  43. }
  44. return result.Data as Type;
  45. }
  46. public override string Key {
  47. get {
  48. return filename;
  49. }
  50. }
  51. public override string SourceFile {
  52. get {
  53. return sourceFile;
  54. }
  55. }
  56. public static Type CompileApplicationType (string filename, HttpContext context)
  57. {
  58. CompilationCacheItem item = CachingCompiler.GetCached (filename);
  59. if (item != null && item.Result != null) {
  60. if (item.Result != null)
  61. return item.Result.Data as Type;
  62. throw new CompilationException (item.Result);
  63. }
  64. GlobalAsaxCompiler gac = new GlobalAsaxCompiler (filename);
  65. gac.context = context;
  66. return gac.GetCompiledType ();
  67. }
  68. string GenerateSourceFile ()
  69. {
  70. AspGenerator generator = new AspGenerator (filename);
  71. generator.Context = context;
  72. generator.BaseType = typeof (HttpApplication).ToString ();
  73. generator.ProcessElements ();
  74. string generated = generator.GetCode ().ReadToEnd ();
  75. options = generator.Options;
  76. dependencies = generator.Dependencies;
  77. //FIXME: should get Tmp dir for this application
  78. string csName = Path.GetTempFileName () + ".cs";
  79. WebTrace.WriteLine ("Writing {0}", csName);
  80. StreamWriter output = new StreamWriter (File.OpenWrite (csName));
  81. output.Write (generated);
  82. output.Close ();
  83. return csName;
  84. }
  85. }
  86. }