AppResourcesCompiler.cs 4.6 KB

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