BaseCompiler.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 BaseCompiler ()
  23. {
  24. }
  25. public virtual Type GetCompiledType ()
  26. {
  27. return null;
  28. }
  29. public virtual string Key {
  30. get {
  31. return null;
  32. }
  33. }
  34. public virtual string SourceFile {
  35. get {
  36. return null;
  37. }
  38. }
  39. public virtual string [] Dependencies {
  40. get {
  41. return null;
  42. }
  43. }
  44. public virtual string CompilerOptions {
  45. get {
  46. string assemblies = default_assemblies;
  47. string privatePath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
  48. //FIXME: remove the next line once multiple appdomains can work together
  49. if (privatePath == null) privatePath = "bin";
  50. //
  51. if (privatePath != null && Directory.Exists (privatePath)) {
  52. StringBuilder sb = new StringBuilder (assemblies);
  53. foreach (string fileName in Directory.GetFiles (privatePath, "*.dll"))
  54. sb.AppendFormat (", {0}", fileName);
  55. assemblies = sb.ToString ();
  56. sb = null;
  57. }
  58. string [] split = assemblies.Split (',');
  59. StringBuilder result = new StringBuilder ();
  60. foreach (string assembly in split)
  61. result.AppendFormat ("/r:{0} ", assembly.TrimStart ());
  62. if (options == null)
  63. return result.ToString ();
  64. string compilerOptions = options ["CompilerOptions"] as string;
  65. if (compilerOptions != null) {
  66. result.Append (' ');
  67. result.Append (compilerOptions);
  68. }
  69. string references = options ["References"] as string;
  70. if (references == null)
  71. return result.ToString ();
  72. split = references.Split (' ');
  73. foreach (string s in split)
  74. result.AppendFormat (" /r:{0}", s);
  75. return result.ToString ();
  76. }
  77. }
  78. static string GetRandomFileName ()
  79. {
  80. string output;
  81. do {
  82. output = "tmp" + rnd.Next () + ".dll";
  83. } while (File.Exists (output));
  84. return output;
  85. }
  86. public virtual string TargetFile {
  87. get {
  88. if (randomName == null)
  89. randomName = GetRandomFileName ();
  90. return randomName;
  91. }
  92. }
  93. }
  94. }