| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- //
- // System.Web.Compilation.AppResourcesCompiler: Support for compilation of .resx files into a satellite assembly
- //
- // Authors:
- // Marek Habersack ([email protected])
- //
- // (C) 2006 Marek Habersack
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System;
- using System.CodeDom;
- using System.CodeDom.Compiler;
- using System.Collections.Generic;
- using System.IO;
- using System.Web;
- namespace System.Web.Compilation
- {
- internal abstract class AppResourcesCompiler: AppResourceFilesCompiler
- {
- protected string resourceDirName = null;
-
- public AppResourcesCompiler ()
- : base ()
- {}
-
- public AppResourcesCompiler (string [] filePaths)
- : base (filePaths)
- {}
- public AppResourcesCompiler (string [] filePaths, FcCodeGenerator fg)
- : base (filePaths, fg)
- {}
-
- public string DynamicDirectory {
- get { return AppDomain.CurrentDomain.DynamicDirectory; }
- }
- public string ResourceDirectory {
- get {
- if (resourceDirName == null)
- return null;
- string dir = Path.Combine (TopPath, resourceDirName);
- if (!Directory.Exists (dir))
- return null;
- return dir;
- }
- }
-
- public bool CompilationPossible {
- get { return (ResourceDirectory != null); }
- }
- public abstract string TopPath {
- get;
- }
-
- virtual public CompilerResults Compile ()
- {
- string dir = ResourceDirectory;
-
- if (dir == null)
- return null;
- DirectoryInfo di = new DirectoryInfo (dir);
- if (di == null)
- return null;
- List<string> al = new List<string> ();
- foreach (FileInfo fi in di.GetFiles ("*.resx"))
- al.Add (fi.FullName);
- filePaths = al.ToArray ();
- CodeCompileUnit unit = FilesToDom ();
- if (unit == null || resourceFiles == null)
- return null;
- CompilerParameters cp = new CompilerParameters ();
- foreach (string rf in resourceFiles)
- cp.EmbeddedResources.Add (rf);
- cp.IncludeDebugInformation = false;
- cp.GenerateExecutable = false;
- cp.TreatWarningsAsErrors = false;
- cp.OutputAssembly = GenRandomFileName (TempDir, "dll");
-
- CodeDomProvider provider = GetCodeProvider ();
- StringWriter sw = new StringWriter ();
- provider.GenerateCodeFromCompileUnit (unit, sw, new CodeGeneratorOptions ());
- Console.WriteLine ("Generated code:\n{0}", sw.ToString ());
- CompilerResults ret = provider.CompileAssemblyFromDom (cp, unit);
- Console.WriteLine ("Assembly generated");
-
- if (ret.CompiledAssembly == null) {
- Console.WriteLine ("Failed to compile {0}/*.resx. Errors:", ResourceDirectory);
- foreach (CompilerError ce in ret.Errors)
- Console.WriteLine("{5} {0} ({1} {2}:{3}): {4}", ce.ErrorNumber,
- ce.FileName, ce.Line, ce.Column, ce.ErrorText,
- ce.IsWarning ? "warning" : "error");
- }
- return ret;
- }
- }
- internal sealed class AppGlobalResourcesCompiler: AppResourcesCompiler
- {
- public AppGlobalResourcesCompiler ()
- : base ()
- {
- this.resourceDirName = "App_GlobalResources";
- }
-
- public override string TopPath {
- get { return HttpRuntime.AppDomainAppPath; }
- }
- }
- //FIXME: it seems that local resources are NOT strongly typed
- // see http://msdn2.microsoft.com/en-us/library/ms227427.aspx
- //
- // Local resources should probably be only put in a satellite assembly and referenced
- // from there.
- internal sealed class AppLocalResourcesCompiler: AppResourcesCompiler
- {
- public AppLocalResourcesCompiler ()
- : base ()
- {
- this.resourceDirName = "App_LocalResources";
- }
- public override string TopPath {
- get {
- return Path.GetDirectoryName (
- HttpContext.Current.Request.MapPath (
- HttpContext.Current.Request.CurrentExecutionFilePath));
- }
- }
- }
- }
- #endif
|