AppResourcesAssemblyBuilder.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // System.Web.Compilation.AppResourceAseemblyBuilder
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2007 Novell, Inc
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.CodeDom;
  32. using System.CodeDom.Compiler;
  33. using System.Collections.Generic;
  34. using System.IO;
  35. using System.Reflection;
  36. using System.Web;
  37. using System.Web.Configuration;
  38. namespace System.Web.Compilation
  39. {
  40. internal class AppResourcesAssemblyBuilder
  41. {
  42. CompilationSection config;
  43. CompilerInfo ci;
  44. CodeDomProvider _provider;
  45. string baseAssemblyPath;
  46. string baseAssemblyDirectory;
  47. string canonicAssemblyName;
  48. Assembly mainAssembly;
  49. AppResourcesCompiler appResourcesCompiler;
  50. public CodeDomProvider Provider {
  51. get {
  52. if (_provider == null)
  53. _provider = ci.CreateProvider ();
  54. else
  55. return _provider;
  56. if (_provider == null)
  57. throw new ApplicationException ("Failed to instantiate the default compiler.");
  58. return _provider;
  59. }
  60. }
  61. public Assembly MainAssembly {
  62. get { return mainAssembly; }
  63. }
  64. public AppResourcesAssemblyBuilder (string canonicAssemblyName, string baseAssemblyPath, AppResourcesCompiler appres)
  65. {
  66. this.appResourcesCompiler = appres;
  67. this.baseAssemblyPath = baseAssemblyPath;
  68. this.baseAssemblyDirectory = Path.GetDirectoryName (baseAssemblyPath);
  69. this.canonicAssemblyName = canonicAssemblyName;
  70. config = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
  71. if (config == null || !CodeDomProvider.IsDefinedLanguage (config.DefaultLanguage))
  72. throw new ApplicationException ("Could not get the default compiler.");
  73. ci = CodeDomProvider.GetCompilerInfo (config.DefaultLanguage);
  74. if (ci == null || !ci.IsCodeDomProviderTypeValid)
  75. throw new ApplicationException ("Failed to obtain the default compiler information.");
  76. }
  77. public void Build ()
  78. {
  79. Build (null);
  80. }
  81. public void Build (CodeCompileUnit unit)
  82. {
  83. Dictionary <string, List <string>> cultures = appResourcesCompiler.CultureFiles;
  84. string defaultAssemblyKey = AppResourcesCompiler.DefaultCultureKey;
  85. foreach (KeyValuePair <string, List <string>> kvp in cultures)
  86. BuildAssembly (kvp.Key, kvp.Value, defaultAssemblyKey, unit);
  87. }
  88. void BuildAssembly (string cultureName, List <string> files, string defaultAssemblyKey, CodeCompileUnit unit)
  89. {
  90. bool defaultAssembly = cultureName == defaultAssemblyKey;
  91. AssemblyBuilder abuilder = new AssemblyBuilder (Provider);
  92. if (unit != null && defaultAssembly)
  93. abuilder.AddCodeCompileUnit (unit);
  94. string assemblyPath = defaultAssembly ? baseAssemblyPath : BuildAssemblyPath (cultureName, abuilder);
  95. CompilerParameters cp = ci.CreateDefaultCompilerParameters ();
  96. cp.OutputAssembly = assemblyPath;
  97. cp.GenerateExecutable = false;
  98. cp.TreatWarningsAsErrors = true;
  99. cp.IncludeDebugInformation = config.Debug;
  100. foreach (string f in files)
  101. cp.EmbeddedResources.Add (f);
  102. CompilerResults results = abuilder.BuildAssembly (cp);
  103. Assembly ret = null;
  104. if (results.NativeCompilerReturnValue == 0) {
  105. ret = results.CompiledAssembly;
  106. if (defaultAssembly) {
  107. BuildManager.TopLevelAssemblies.Add (ret);
  108. mainAssembly = ret;
  109. }
  110. } else {
  111. if (HttpContext.Current.IsCustomErrorEnabled)
  112. throw new ApplicationException ("An error occurred while compiling global resources.");
  113. throw new CompilationException (null, results.Errors, null);
  114. }
  115. if (defaultAssembly) {
  116. HttpRuntime.WritePreservationFile (ret, canonicAssemblyName);
  117. HttpRuntime.EnableAssemblyMapping (true);
  118. }
  119. }
  120. string BuildAssemblyPath (string cultureName, AssemblyBuilder abuilder)
  121. {
  122. string baseDir = Path.Combine (baseAssemblyDirectory, cultureName);
  123. if (!Directory.Exists (baseDir))
  124. Directory.CreateDirectory (baseDir);
  125. string baseFileName = Path.GetFileNameWithoutExtension (baseAssemblyPath);
  126. string fileName = String.Concat (baseFileName, ".resources.dll");
  127. fileName = Path.Combine (baseDir, fileName);
  128. CodeCompileUnit assemblyInfo = GenerateAssemblyInfo (cultureName);
  129. if (assemblyInfo != null)
  130. abuilder.AddCodeCompileUnit (assemblyInfo);
  131. return fileName;
  132. }
  133. CodeCompileUnit GenerateAssemblyInfo (string cultureName)
  134. {
  135. CodeAttributeArgument[] args = new CodeAttributeArgument [1];
  136. args [0] = new CodeAttributeArgument (new CodePrimitiveExpression (cultureName));
  137. CodeCompileUnit unit = new CodeCompileUnit ();
  138. unit.AssemblyCustomAttributes.Add (
  139. new CodeAttributeDeclaration (
  140. new CodeTypeReference ("System.Reflection.AssemblyCultureAttribute"),
  141. args));
  142. args = new CodeAttributeArgument [2];
  143. args [0] = new CodeAttributeArgument (new CodePrimitiveExpression ("ASP.NET"));
  144. args [1] = new CodeAttributeArgument (new CodePrimitiveExpression (Environment.Version.ToString ()));
  145. unit.AssemblyCustomAttributes.Add (
  146. new CodeAttributeDeclaration (
  147. new CodeTypeReference ("System.CodeDom.Compiler.GeneratedCodeAttribute"),
  148. args));
  149. return unit;
  150. }
  151. }
  152. }
  153. #endif