AssemblyBuilder.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. //TODO: there should be a Compile () method here which is where all the compilation exceptions are thrown from.
  50. internal AssemblyBuilder (string virtualPath, CodeDomProvider provider)
  51. {
  52. this.provider = provider;
  53. this.virtual_path = virtualPath;
  54. units = new List <CodeCompileUnit> ();
  55. units.Add (new CodeCompileUnit ());
  56. temp_files = new TempFileCollection ();
  57. CompilationSection section;
  58. section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation", virtualPath);
  59. string tempdir = section.TempDirectory;
  60. if (tempdir == null || tempdir == "")
  61. tempdir = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
  62. temp_files = new TempFileCollection (tempdir, KeepFiles);
  63. }
  64. internal TempFileCollection TempFiles {
  65. get { return temp_files; }
  66. }
  67. internal CodeCompileUnit [] GetUnitsAsArray ()
  68. {
  69. CodeCompileUnit [] result = new CodeCompileUnit [units.Count];
  70. units.CopyTo (result, 0);
  71. return result;
  72. }
  73. List <string> SourceFiles {
  74. get {
  75. if (source_files == null)
  76. source_files = new List <string> ();
  77. return source_files;
  78. }
  79. }
  80. Dictionary <string, string> ResourceFiles {
  81. get {
  82. if (resource_files == null)
  83. resource_files = new Dictionary <string, string> ();
  84. return resource_files;
  85. }
  86. }
  87. public void AddAssemblyReference (Assembly a)
  88. {
  89. if (a == null)
  90. throw new ArgumentNullException ("a");
  91. StringCollection coll = units [units.Count - 1].ReferencedAssemblies;
  92. string location = a.Location;
  93. if (coll.IndexOf (location) == -1)
  94. coll.Add (location);
  95. }
  96. [MonoTODO ("Do something with the buildProvider argument")]
  97. public void AddCodeCompileUnit (BuildProvider buildProvider, CodeCompileUnit compileUnit)
  98. {
  99. if (buildProvider == null)
  100. throw new ArgumentNullException ("buildProvider");
  101. if (compileUnit == null)
  102. throw new ArgumentNullException ("compileUnit");
  103. units.Add (compileUnit);
  104. }
  105. [MonoTODO ("Anything to do with the buildProvider argument?")]
  106. public TextWriter CreateCodeFile (BuildProvider buildProvider)
  107. {
  108. if (buildProvider == null)
  109. throw new ArgumentNullException ("buildProvider");
  110. string filename = temp_files.AddExtension ("temp", true);
  111. SourceFiles.Add (filename);
  112. return new StreamWriter (File.OpenWrite (filename), WebEncoding.FileEncoding);
  113. }
  114. [MonoTODO ("Anything to do with the buildProvider argument?")]
  115. public Stream CreateEmbeddedResource (BuildProvider buildProvider, string name)
  116. {
  117. if (buildProvider == null)
  118. throw new ArgumentNullException ("buildProvider");
  119. if (name == null || name == "")
  120. throw new ArgumentNullException ("name");
  121. string filename = temp_files.AddExtension ("resource", true);
  122. Stream stream = File.OpenWrite (filename);
  123. ResourceFiles [name] = filename;
  124. return stream;
  125. }
  126. [MonoTODO]
  127. public void GenerateTypeFactory (string typeName)
  128. {
  129. // Do nothing by now.
  130. }
  131. public string GetTempFilePhysicalPath (string extension)
  132. {
  133. if (extension == null)
  134. throw new ArgumentNullException ("extension");
  135. return temp_files.AddExtension (extension, true);
  136. }
  137. public CodeDomProvider CodeDomProvider {
  138. get { return provider; }
  139. }
  140. internal CompilerResults BuildAssembly (string virtualPath, CompilerParameters options)
  141. {
  142. CompilerResults results;
  143. CodeCompileUnit [] units = GetUnitsAsArray ();
  144. results = provider.CompileAssemblyFromDom (options, units);
  145. // FIXME: generate the code and display it
  146. if (results.NativeCompilerReturnValue != 0)
  147. throw new CompilationException (virtualPath, results.Errors, "");
  148. Assembly assembly = results.CompiledAssembly;
  149. if (assembly == null) {
  150. if (!File.Exists (options.OutputAssembly)) {
  151. results.TempFiles.Delete ();
  152. throw new CompilationException (virtualPath, results.Errors,
  153. "No assembly returned after compilation!?");
  154. }
  155. results.CompiledAssembly = Assembly.LoadFrom (options.OutputAssembly);
  156. }
  157. results.TempFiles.Delete ();
  158. return results;
  159. }
  160. }
  161. }
  162. #endif