2
0

AssemblyBuilder.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // System.Web.Compilation.AssemblyBuilder
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2006 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using System;
  32. using System.CodeDom;
  33. using System.CodeDom.Compiler;
  34. using System.Collections.Generic;
  35. using System.Collections.Specialized;
  36. using System.IO;
  37. using System.Reflection;
  38. using System.Web.Configuration;
  39. using System.Web.Util;
  40. namespace System.Web.Compilation {
  41. public class AssemblyBuilder {
  42. static bool KeepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
  43. CodeDomProvider provider;
  44. List <CodeCompileUnit> units;
  45. List <string> source_files;
  46. Dictionary <string, string> resource_files;
  47. TempFileCollection temp_files;
  48. string virtual_path;
  49. internal AssemblyBuilder (string virtualPath, CodeDomProvider provider)
  50. {
  51. this.provider = provider;
  52. this.virtual_path = virtualPath;
  53. units = new List <CodeCompileUnit> ();
  54. units.Add (new CodeCompileUnit ());
  55. temp_files = new TempFileCollection ();
  56. CompilationSection section;
  57. section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation", virtualPath);
  58. string tempdir = section.TempDirectory;
  59. if (tempdir == null || tempdir == "")
  60. tempdir = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
  61. temp_files = new TempFileCollection (tempdir, KeepFiles);
  62. }
  63. internal TempFileCollection TempFiles {
  64. get { return temp_files; }
  65. }
  66. internal CodeCompileUnit [] GetUnitsAsArray ()
  67. {
  68. CodeCompileUnit [] result = new CodeCompileUnit [units.Count];
  69. units.CopyTo (result, 0);
  70. return result;
  71. }
  72. List <string> SourceFiles {
  73. get {
  74. if (source_files == null)
  75. source_files = new List <string> ();
  76. return source_files;
  77. }
  78. }
  79. Dictionary <string, string> ResourceFiles {
  80. get {
  81. if (resource_files == null)
  82. resource_files = new Dictionary <string, string> ();
  83. return resource_files;
  84. }
  85. }
  86. public void AddAssemblyReference (Assembly a)
  87. {
  88. if (a == null)
  89. throw new ArgumentNullException ("a");
  90. StringCollection coll = units [units.Count - 1].ReferencedAssemblies;
  91. string location = a.Location;
  92. if (coll.IndexOf (location) == -1)
  93. coll.Add (location);
  94. }
  95. [MonoTODO ("Do something with the buildProvider argument")]
  96. public void AddCodeCompileUnit (BuildProvider buildProvider, CodeCompileUnit compileUnit)
  97. {
  98. if (buildProvider == null)
  99. throw new ArgumentNullException ("buildProvider");
  100. if (compileUnit == null)
  101. throw new ArgumentNullException ("compileUnit");
  102. units.Add (compileUnit);
  103. }
  104. [MonoTODO ("Anything to do with the buildProvider argument?")]
  105. public TextWriter CreateCodeFile (BuildProvider buildProvider)
  106. {
  107. if (buildProvider == null)
  108. throw new ArgumentNullException ("buildProvider");
  109. string filename = temp_files.AddExtension ("temp", true);
  110. SourceFiles.Add (filename);
  111. return new StreamWriter (File.OpenWrite (filename), WebEncoding.FileEncoding);
  112. }
  113. [MonoTODO ("Anything to do with the buildProvider argument?")]
  114. public Stream CreateEmbeddedResource (BuildProvider buildProvider, string name)
  115. {
  116. if (buildProvider == null)
  117. throw new ArgumentNullException ("buildProvider");
  118. if (name == null || name == "")
  119. throw new ArgumentNullException ("name");
  120. string filename = temp_files.AddExtension ("resource", true);
  121. Stream stream = File.OpenWrite (filename);
  122. ResourceFiles [name] = filename;
  123. return stream;
  124. }
  125. [MonoTODO]
  126. public void GenerateTypeFactory (string typeName)
  127. {
  128. // Do nothing by now.
  129. }
  130. public string GetTempFilePhysicalPath (string extension)
  131. {
  132. if (extension == null)
  133. throw new ArgumentNullException ("extension");
  134. return temp_files.AddExtension (extension, true);
  135. }
  136. public CodeDomProvider CodeDomProvider {
  137. get { return provider; }
  138. }
  139. }
  140. }
  141. #endif