GlobalAsaxCompiler.cs 2.9 KB

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