PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/viewengines/aspview/branches/MultipleViewAssembliesSupport/Castle.MonoRail.Views.AspView/AspViewCompiler.cs

https://github.com/castleprojectcontrib/Projects
C# | 216 lines | 179 code | 24 blank | 13 comment | 23 complexity | 207767de7a7e1091b798b92ab6a67aa7 MD5 | raw file
  1. // Copyright 2004-2005 Castle Project - http://www.castleproject.org/
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. namespace Castle.MonoRail.Views.AspView
  15. {
  16. using System;
  17. using System.Text;
  18. using System.IO;
  19. using System.Collections.Generic;
  20. using System.Collections.Specialized;
  21. using System.CodeDom.Compiler;
  22. using Microsoft.CSharp;
  23. using Microsoft.VisualBasic;
  24. using Castle.MonoRail.Framework;
  25. public class AspViewCompiler
  26. {
  27. #region members
  28. private CompilerParameters parameters;
  29. private AspViewCompilerOptions options;
  30. private AspViewPreProcessor preProcessor;
  31. private static readonly ReferencedAssembly[] defaultAddedReferences = null;
  32. private static readonly string defaultSiteRoot = AppDomain.CurrentDomain.BaseDirectory;
  33. private static readonly string defaultBinDirectory = Path.Combine(defaultSiteRoot, "bin");
  34. private string targetDirectory = null;
  35. private string binDirectory = null;
  36. public string PathToAssembly = null;
  37. #endregion
  38. public AspViewCompiler(AspViewCompilerOptions options)
  39. {
  40. this.options = options;
  41. preProcessor = new AspViewPreProcessor();
  42. InitializeCompilerParameters();
  43. }
  44. public void CompileSite()
  45. {
  46. CompileSite(defaultSiteRoot);
  47. }
  48. public void CompileSite(string siteRoot)
  49. {
  50. binDirectory = Path.Combine(siteRoot, string.IsNullOrEmpty(options.ReferencesPath) ? "bin" : options.ReferencesPath);
  51. targetDirectory = binDirectory;
  52. CompileSite(siteRoot, defaultAddedReferences);
  53. }
  54. public void CompileSite(string siteRoot, ReferencedAssembly[] references)
  55. {
  56. List<AspViewFile> files = GetViewFiles(siteRoot);
  57. if (files.Count == 0)
  58. return;
  59. preProcessor.Process(files);
  60. List<AspViewFile> vbFiles = files.FindAll(delegate(AspViewFile file) { return (file.Language == ScriptingLanguage.VbNet); });
  61. List<AspViewFile> csFiles = files.FindAll(delegate(AspViewFile file) { return (file.Language == ScriptingLanguage.CSharp); });
  62. if (options.KeepTemporarySourceFiles)
  63. {
  64. string targetTemporarySourceFilesDirectory = options.TemporarySourceFilesDirectory;
  65. if (!Path.IsPathRooted(targetTemporarySourceFilesDirectory))
  66. targetTemporarySourceFilesDirectory = Path.Combine(targetDirectory, targetTemporarySourceFilesDirectory);
  67. if (!Directory.Exists(targetTemporarySourceFilesDirectory))
  68. Directory.CreateDirectory(targetTemporarySourceFilesDirectory);
  69. foreach (AspViewFile file in files)
  70. SaveFile(file, targetTemporarySourceFilesDirectory);
  71. }
  72. PathToAssembly = Compile(targetDirectory, csFiles, vbFiles, references);
  73. }
  74. private string Compile(string targetDirectory, List<AspViewFile> csFiles, List<AspViewFile> vbFiles, ReferencedAssembly[] references)
  75. {
  76. if (vbFiles.Count == 0)
  77. return CompileCSharpModule(targetDirectory, csFiles, references, true, null);
  78. if (csFiles.Count == 0)
  79. return CompileVbModule(targetDirectory, vbFiles, references, true, null);
  80. string vbModulePath = CompileVbModule(targetDirectory, vbFiles, references, false, null);
  81. return CompileCSharpModule(targetDirectory, csFiles, references, true, new string[1] { vbModulePath });
  82. }
  83. private List<AspViewFile> GetViewFiles(string siteRoot)
  84. {
  85. List<AspViewFile> files = new List<AspViewFile>();
  86. string viewsDirectory = Path.Combine(siteRoot, "Views");
  87. string[] fileNames = Directory.GetFiles(viewsDirectory, "*.aspx", SearchOption.AllDirectories);
  88. foreach (string fileName in fileNames)
  89. {
  90. AspViewFile file = new AspViewFile();
  91. file.ViewName = fileName.Replace(viewsDirectory, "");
  92. file.ClassName = AspViewEngine.GetClassName(file.ViewName);
  93. file.ViewSource = ReadFile(fileName);
  94. files.Add(file);
  95. }
  96. return files;
  97. }
  98. private static string ReadFile(string fileName)
  99. {
  100. string source = string.Empty;
  101. using (StreamReader sr = new StreamReader(fileName))
  102. {
  103. source = sr.ReadToEnd();
  104. }
  105. return source;
  106. }
  107. private static void SaveFile(AspViewFile file, string targetDirectory)
  108. {
  109. string fileName = Path.Combine(targetDirectory, file.FileName);
  110. using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8))
  111. {
  112. sw.Write(file.ConcreteClass);
  113. sw.Flush();
  114. }
  115. }
  116. private void InitializeCompilerParameters()
  117. {
  118. parameters = new CompilerParameters();
  119. parameters.GenerateInMemory = options.InMemory;
  120. parameters.GenerateExecutable = false;
  121. parameters.IncludeDebugInformation = options.Debug;
  122. }
  123. private string CompileModule(
  124. CodeDomProvider codeProvider, string targetDirectory, List<AspViewFile> files,
  125. ReferencedAssembly[] references, bool createAssembly, string[] modulesToAdd)
  126. {
  127. string prefix = string.IsNullOrEmpty(options.GeneratedAssemblyNamePrefix) ? null : options.GeneratedAssemblyNamePrefix + ".";
  128. if (!createAssembly)
  129. {
  130. parameters.CompilerOptions = "/t:module";
  131. parameters.OutputAssembly = Path.Combine(targetDirectory,
  132. string.Format("{0}CompiledViews.{1}.netmodule", prefix, codeProvider.FileExtension));
  133. }
  134. else
  135. parameters.OutputAssembly = Path.Combine(targetDirectory, prefix + "CompiledViews.dll");
  136. List<ReferencedAssembly> actualReferences = new List<ReferencedAssembly>();
  137. if (options.References != null)
  138. actualReferences.AddRange(options.References);
  139. if (references != null)
  140. actualReferences.AddRange(references);
  141. foreach (ReferencedAssembly reference in actualReferences)
  142. {
  143. string assemblyName = reference.Name;
  144. if (reference.Source == ReferencedAssembly.AssemblySource.BinDirectory)
  145. assemblyName = Path.Combine(targetDirectory, assemblyName);
  146. parameters.CompilerOptions += " /r:\"" + assemblyName + "\"";
  147. }
  148. if (modulesToAdd != null && modulesToAdd.Length > 0)
  149. {
  150. StringBuilder sb = new StringBuilder();
  151. sb.Append(" /addmodule: ");
  152. foreach (string moduleToAdd in modulesToAdd)
  153. sb.Append(Path.Combine(targetDirectory, moduleToAdd));
  154. parameters.CompilerOptions += "\"" + sb.ToString() + "\"";
  155. }
  156. CompilerResults results;
  157. if (options.KeepTemporarySourceFiles)
  158. {
  159. string targetTemporarySourceFilesDirectory = Path.Combine(targetDirectory, options.TemporarySourceFilesDirectory);
  160. List<string> fileNames = new List<string>(files.Count);
  161. foreach (AspViewFile file in files)
  162. fileNames.Add(Path.Combine(targetTemporarySourceFilesDirectory, file.FileName));
  163. results = codeProvider.CompileAssemblyFromFile(parameters, fileNames.ToArray());
  164. }
  165. else
  166. {
  167. List<string> sources = new List<string>(files.Count);
  168. foreach (AspViewFile file in files)
  169. sources.Add(file.ConcreteClass);
  170. results = codeProvider.CompileAssemblyFromSource(parameters, sources.ToArray());
  171. }
  172. if (results.Errors.Count > 0)
  173. {
  174. StringBuilder message = new StringBuilder();
  175. foreach (CompilerError err in results.Errors)
  176. message.AppendLine(err.ToString());
  177. throw new Exception(string.Format(
  178. "Error while compiling'':\r\n{0}",
  179. message.ToString()));
  180. }
  181. return results.PathToAssembly;
  182. }
  183. private string CompileCSharpModule(string targetDirectory, List<AspViewFile> files, ReferencedAssembly[] references, bool createAssembly, string[] modulesToAdd)
  184. {
  185. return CompileModule(new CSharpCodeProvider(), targetDirectory, files, references, createAssembly, modulesToAdd);
  186. }
  187. private string CompileVbModule(string targetDirectory, List<AspViewFile> files, ReferencedAssembly[] references, bool createAssembly, string[] modulesToAdd)
  188. {
  189. return CompileModule(new VBCodeProvider(), targetDirectory, files, references, createAssembly, modulesToAdd);
  190. }
  191. }
  192. }