CachingCompiler.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // System.Web.Compilation.CachingCompiler
  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.Diagnostics;
  12. using System.IO;
  13. using System.Text;
  14. using System.Web.UI;
  15. using System.Web.Util;
  16. namespace System.Web.Compilation
  17. {
  18. //TODO: caching should be done using System.Web.Caching, but that namespace still need some work.
  19. internal class CompilationCacheItem
  20. {
  21. CompilationResult result;
  22. DateTime reference;
  23. bool invalidated;
  24. public CompilationCacheItem (CompilationResult result)
  25. {
  26. this.result = result;
  27. this.reference = File.GetLastWriteTime (result.OutputFile);
  28. }
  29. public bool CheckDependencies ()
  30. {
  31. if (invalidated || result.Dependencies == null)
  32. return false;
  33. if (!File.Exists (result.OutputFile))
  34. return false;
  35. foreach (string s in result.Dependencies) {
  36. if (!File.Exists (s) || File.GetLastWriteTime (s) > reference) {
  37. invalidated = true;
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. public CompilationResult Result
  44. {
  45. get { return result; }
  46. }
  47. }
  48. internal class CompilationCache
  49. {
  50. static Hashtable cache;
  51. static CompilationCache instance = new CompilationCache ();
  52. private CompilationCache ()
  53. {
  54. }
  55. static CompilationCache ()
  56. {
  57. cache = new Hashtable ();
  58. }
  59. public static CompilationCache GetInstance ()
  60. {
  61. return instance;
  62. }
  63. public CompilationCacheItem this [string key]
  64. {
  65. get {
  66. return cache [key] as CompilationCacheItem;
  67. }
  68. set {
  69. cache [key] = value;
  70. }
  71. }
  72. }
  73. internal class CachingCompiler
  74. {
  75. static CompilationCache cache = CompilationCache.GetInstance ();
  76. string key;
  77. BaseCompiler compiler;
  78. public CachingCompiler (BaseCompiler compiler)
  79. {
  80. this.compiler = compiler;
  81. this.key = compiler.Key;
  82. }
  83. public static CompilationCacheItem GetCached (string key)
  84. {
  85. if (key == null)
  86. throw new ArgumentNullException ("key");
  87. CompilationCacheItem item = cache [key];
  88. if (item != null && item.CheckDependencies ())
  89. return item;
  90. return null;
  91. }
  92. static object compilationLock = new object ();
  93. public bool Compile (CompilationResult result)
  94. {
  95. if (compiler.SourceFile == null)
  96. throw new ArgumentException ("No source to compile!");
  97. result.Reset ();
  98. CompilationCacheItem item;
  99. item = GetCached (key);
  100. if (item != null) {
  101. result.CopyFrom (item.Result);
  102. return true;
  103. }
  104. lock (compilationLock) {
  105. item = GetCached (key);
  106. if (item != null) {
  107. result.CopyFrom (item.Result);
  108. return true;
  109. }
  110. RealCompile (result);
  111. cache [key] = new CompilationCacheItem (result);
  112. }
  113. return (result.ExitCode == 0);
  114. }
  115. void RealCompile (CompilationResult result)
  116. {
  117. StringBuilder options = new StringBuilder ("/target:library ");
  118. if (compiler.CompilerOptions != null)
  119. options.Append (compiler.CompilerOptions + ' ');
  120. options.AppendFormat ("/out:{0} ", compiler.TargetFile);
  121. options.Append (compiler.SourceFile);
  122. //Console.WriteLine ("mcs {0}", options);
  123. Process proc = new Process ();
  124. if (System.IO.Path.DirectorySeparatorChar == '\\')
  125. proc.StartInfo.FileName = "mcs.bat";
  126. else
  127. proc.StartInfo.FileName = "mcs";
  128. proc.StartInfo.Arguments = options.ToString ();
  129. proc.StartInfo.UseShellExecute = false;
  130. proc.StartInfo.RedirectStandardOutput = true;
  131. WebTrace.WriteLine ("{0} {1}", proc.StartInfo.FileName, options.ToString ());
  132. proc.Start ();
  133. string poutput = proc.StandardOutput.ReadToEnd();
  134. proc.WaitForExit ();
  135. result.ExitCode = proc.ExitCode;
  136. proc.Close ();
  137. proc = null;
  138. result.CompilerOutput = poutput;
  139. //Console.WriteLine ("output: {0}\n", poutput);
  140. result.OutputFile = compiler.TargetFile;
  141. }
  142. }
  143. }