AppResourcesCompiler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // System.Web.Compilation.AppResourcesCompiler: Support for compilation of .resx files into a satellite assembly
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2006 Marek Habersack
  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.Web;
  36. using System.Web.Configuration;
  37. namespace System.Web.Compilation
  38. {
  39. internal abstract class AppResourcesCompiler: AppResourceFilesCompiler
  40. {
  41. protected string resourceDirName = null;
  42. public AppResourcesCompiler ()
  43. : base ()
  44. {}
  45. public AppResourcesCompiler (string [] filePaths)
  46. : base (filePaths)
  47. {}
  48. public AppResourcesCompiler (string [] filePaths, FcCodeGenerator fg)
  49. : base (filePaths, fg)
  50. {}
  51. public string DynamicDirectory {
  52. get { return AppDomain.CurrentDomain.DynamicDirectory; }
  53. }
  54. public string ResourceDirectory {
  55. get {
  56. if (resourceDirName == null)
  57. return null;
  58. string dir = Path.Combine (TopPath, resourceDirName);
  59. if (!Directory.Exists (dir))
  60. return null;
  61. return dir;
  62. }
  63. }
  64. public bool CompilationPossible {
  65. get { return (ResourceDirectory != null); }
  66. }
  67. public abstract string TopPath {
  68. get;
  69. }
  70. virtual public CompilerResults Compile ()
  71. {
  72. string dir = ResourceDirectory;
  73. if (dir == null)
  74. return null;
  75. DirectoryInfo di = new DirectoryInfo (dir);
  76. if (di == null)
  77. return null;
  78. FileInfo[] fileinfos = di.GetFiles ("*.resx");
  79. if (fileinfos != null && fileinfos.Length > 0) {
  80. List<string> al = new List<string> (fileinfos.Length);
  81. foreach (FileInfo fi in fileinfos)
  82. al.Add (fi.FullName);
  83. filePaths = al.ToArray ();
  84. } else
  85. return null;
  86. CodeCompileUnit unit = FilesToDom ();
  87. if (unit == null || resourceFiles == null)
  88. return null;
  89. CompilerParameters cp = new CompilerParameters ();
  90. foreach (string rf in resourceFiles)
  91. cp.EmbeddedResources.Add (rf);
  92. cp.IncludeDebugInformation = false;
  93. cp.GenerateExecutable = false;
  94. cp.TreatWarningsAsErrors = false;
  95. cp.OutputAssembly = GenRandomFileName (TempDir, "dll");
  96. CodeDomProvider provider = GetCodeProvider ();
  97. StringWriter sw = new StringWriter ();
  98. provider.GenerateCodeFromCompileUnit (unit, sw, new CodeGeneratorOptions ());
  99. CompilerResults ret = provider.CompileAssemblyFromDom (cp, unit);
  100. if (ret.Errors.Count != 0) {
  101. Console.WriteLine ("Failed to compile {0}/*.resx. Errors:", ResourceDirectory);
  102. foreach (CompilerError ce in ret.Errors)
  103. Console.WriteLine("{5} {0} ({1} {2}:{3}): {4}", ce.ErrorNumber,
  104. ce.FileName, ce.Line, ce.Column, ce.ErrorText,
  105. ce.IsWarning ? "warning" : "error");
  106. } else {
  107. WebConfigurationManager.ExtraAssemblies.Add(ret.PathToAssembly);
  108. BuildManager.TopLevelAssemblies.Add (ret.CompiledAssembly);
  109. BuildManager.HaveResources = true;
  110. }
  111. return ret;
  112. }
  113. }
  114. internal sealed class AppGlobalResourcesCompiler: AppResourcesCompiler
  115. {
  116. public AppGlobalResourcesCompiler ()
  117. : base ()
  118. {
  119. this.resourceDirName = "App_GlobalResources";
  120. }
  121. public override string TopPath {
  122. get { return HttpRuntime.AppDomainAppPath; }
  123. }
  124. }
  125. //FIXME: it seems that local resources are NOT strongly typed
  126. // see http://msdn2.microsoft.com/en-us/library/ms227427.aspx
  127. //
  128. // Local resources should probably be only put in a satellite assembly and referenced
  129. // from there.
  130. internal sealed class AppLocalResourcesCompiler: AppResourcesCompiler
  131. {
  132. public AppLocalResourcesCompiler ()
  133. : base ()
  134. {
  135. this.resourceDirName = "App_LocalResources";
  136. }
  137. public override string TopPath {
  138. get {
  139. return Path.GetDirectoryName (
  140. HttpContext.Current.Request.MapPath (
  141. HttpContext.Current.Request.CurrentExecutionFilePath));
  142. }
  143. }
  144. }
  145. }
  146. #endif