Files
mono/mcs/class/System.Web/System.Web.Compilation/AppResourcesAssemblyBuilder.cs
T
Marek Habersack 28be7561f9 2007-10-01 Marek Habersack <[email protected]>
* AppResourcesCompiler.cs: resources are no longer compiled into a
	single assembly. Instead, the common assembly construction code
	has been moved to AppResourcesAssemblyBuilder to use satellite
	assemblies.

	* AppResourcesAssemblyBuilder.cs: new resource assembly
	construction code which takes care of outputting satellite
	assemblies.

svn path=/trunk/mcs/; revision=86697
2007-10-01 12:30:36 +00:00

174 lines
6.0 KiB
C#

//
// System.Web.Compilation.AppResourceAseemblyBuilder
//
// Authors:
// Marek Habersack ([email protected])
//
// (C) 2007 Novell, Inc
//
//
// 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.Reflection;
using System.Web;
using System.Web.Configuration;
namespace System.Web.Compilation
{
internal class AppResourcesAssemblyBuilder
{
CompilationSection config;
CompilerInfo ci;
CodeDomProvider _provider;
string baseAssemblyPath;
string baseAssemblyDirectory;
string canonicAssemblyName;
Assembly mainAssembly;
AppResourcesCompiler appResourcesCompiler;
public CodeDomProvider Provider {
get {
if (_provider == null)
_provider = ci.CreateProvider ();
else
return _provider;
if (_provider == null)
throw new ApplicationException ("Failed to instantiate the default compiler.");
return _provider;
}
}
public Assembly MainAssembly {
get { return mainAssembly; }
}
public AppResourcesAssemblyBuilder (string canonicAssemblyName, string baseAssemblyPath, AppResourcesCompiler appres)
{
this.appResourcesCompiler = appres;
this.baseAssemblyPath = baseAssemblyPath;
this.baseAssemblyDirectory = Path.GetDirectoryName (baseAssemblyPath);
this.canonicAssemblyName = canonicAssemblyName;
config = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
if (config == null || !CodeDomProvider.IsDefinedLanguage (config.DefaultLanguage))
throw new ApplicationException ("Could not get the default compiler.");
ci = CodeDomProvider.GetCompilerInfo (config.DefaultLanguage);
if (ci == null || !ci.IsCodeDomProviderTypeValid)
throw new ApplicationException ("Failed to obtain the default compiler information.");
}
public void Build ()
{
Build (null);
}
public void Build (CodeCompileUnit unit)
{
Dictionary <string, List <string>> cultures = appResourcesCompiler.CultureFiles;
string defaultAssemblyKey = AppResourcesCompiler.DefaultCultureKey;
foreach (KeyValuePair <string, List <string>> kvp in cultures)
BuildAssembly (kvp.Key, kvp.Value, defaultAssemblyKey, unit);
}
void BuildAssembly (string cultureName, List <string> files, string defaultAssemblyKey, CodeCompileUnit unit)
{
bool defaultAssembly = cultureName == defaultAssemblyKey;
AssemblyBuilder abuilder = new AssemblyBuilder (Provider);
if (unit != null && defaultAssembly)
abuilder.AddCodeCompileUnit (unit);
string assemblyPath = defaultAssembly ? baseAssemblyPath : BuildAssemblyPath (cultureName, abuilder);
CompilerParameters cp = ci.CreateDefaultCompilerParameters ();
cp.OutputAssembly = assemblyPath;
cp.GenerateExecutable = false;
cp.TreatWarningsAsErrors = true;
cp.IncludeDebugInformation = config.Debug;
foreach (string f in files)
cp.EmbeddedResources.Add (f);
CompilerResults results = abuilder.BuildAssembly (cp);
Assembly ret = null;
if (results.NativeCompilerReturnValue == 0) {
ret = results.CompiledAssembly;
BuildManager.TopLevelAssemblies.Add (ret);
if (defaultAssembly)
mainAssembly = ret;
} else {
if (HttpContext.Current.IsCustomErrorEnabled)
throw new ApplicationException ("An error occurred while compiling global resources.");
throw new CompilationException (null, results.Errors, null);
}
HttpRuntime.WritePreservationFile (ret, canonicAssemblyName);
HttpRuntime.EnableAssemblyMapping (true);
}
string BuildAssemblyPath (string cultureName, AssemblyBuilder abuilder)
{
string baseDir = Path.Combine (baseAssemblyDirectory, cultureName);
if (!Directory.Exists (baseDir))
Directory.CreateDirectory (baseDir);
string baseFileName = Path.GetFileNameWithoutExtension (baseAssemblyPath);
string fileName = String.Format ("{0}.resources.dll", baseFileName);
fileName = Path.Combine (baseDir, fileName);
CodeCompileUnit assemblyInfo = GenerateAssemblyInfo (cultureName);
if (assemblyInfo != null)
abuilder.AddCodeCompileUnit (assemblyInfo);
return fileName;
}
CodeCompileUnit GenerateAssemblyInfo (string cultureName)
{
CodeAttributeArgument[] args = new CodeAttributeArgument [1];
args [0] = new CodeAttributeArgument (new CodePrimitiveExpression (cultureName));
CodeCompileUnit unit = new CodeCompileUnit ();
unit.AssemblyCustomAttributes.Add (
new CodeAttributeDeclaration (
new CodeTypeReference ("System.Reflection.AssemblyCultureAttribute"),
args));
args = new CodeAttributeArgument [2];
args [0] = new CodeAttributeArgument (new CodePrimitiveExpression ("ASP.NET"));
args [1] = new CodeAttributeArgument (new CodePrimitiveExpression (Environment.Version.ToString ()));
unit.AssemblyCustomAttributes.Add (
new CodeAttributeDeclaration (
new CodeTypeReference ("System.CodeDom.Compiler.GeneratedCodeAttribute"),
args));
return unit;
}
}
}
#endif