BaseCompiler.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // System.Web.Compilation.BaseCompiler
  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.Text;
  13. namespace System.Web.Compilation
  14. {
  15. abstract class BaseCompiler
  16. {
  17. //FIXME: configurable?
  18. static string default_assemblies = "System.Web.dll, System.Data.dll, System.Drawing.dll";
  19. static Random rnd = new Random ((int) DateTime.Now.Ticks);
  20. string randomName;
  21. protected Hashtable options;
  22. protected ArrayList dependencies;
  23. protected BaseCompiler ()
  24. {
  25. }
  26. public virtual Type GetCompiledType ()
  27. {
  28. return null;
  29. }
  30. public virtual string Key {
  31. get {
  32. return null;
  33. }
  34. }
  35. public virtual string SourceFile {
  36. get {
  37. return null;
  38. }
  39. }
  40. public virtual string [] Dependencies {
  41. get {
  42. if (dependencies == null)
  43. return new string [0];
  44. return (string []) dependencies.ToArray (typeof (string));
  45. }
  46. }
  47. public virtual void AddDependency (string filename)
  48. {
  49. if (dependencies == null)
  50. dependencies = new ArrayList ();
  51. dependencies.Add (filename);
  52. }
  53. public virtual string CompilerOptions {
  54. get {
  55. string assemblies = default_assemblies;
  56. string privatePath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
  57. //FIXME: remove the next line once multiple appdomains can work together
  58. if (privatePath == null) privatePath = "bin";
  59. //
  60. if (privatePath != null && Directory.Exists (privatePath)) {
  61. StringBuilder sb = new StringBuilder (assemblies);
  62. foreach (string fileName in Directory.GetFiles (privatePath, "*.dll"))
  63. sb.AppendFormat (", {0}", fileName);
  64. assemblies = sb.ToString ();
  65. sb = null;
  66. }
  67. string [] split = assemblies.Split (',');
  68. StringBuilder result = new StringBuilder ();
  69. foreach (string assembly in split)
  70. result.AppendFormat ("/r:{0} ", assembly.TrimStart ());
  71. if (options == null)
  72. return result.ToString ();
  73. string compilerOptions = options ["CompilerOptions"] as string;
  74. if (compilerOptions != null) {
  75. result.Append (' ');
  76. result.Append (compilerOptions);
  77. }
  78. string references = options ["References"] as string;
  79. if (references == null)
  80. return result.ToString ();
  81. split = references.Split ('|');
  82. foreach (string s in split)
  83. result.AppendFormat (" /r:\"{0}\"", s);
  84. return result.ToString ();
  85. }
  86. }
  87. static string GetRandomFileName ()
  88. {
  89. string output;
  90. do {
  91. output = "tmp" + rnd.Next () + ".dll";
  92. } while (File.Exists (output));
  93. return output;
  94. }
  95. public virtual string TargetFile {
  96. get {
  97. if (randomName == null)
  98. randomName = GetRandomFileName ();
  99. return randomName;
  100. }
  101. }
  102. }
  103. }