void AddVirtualDir(VirtualDirectory vdir, BuildProviderCollection bpcoll, Dictionary <string, bool> cache) { if (vdir == null) { return; } BuildProvider bp; IDictionary <string, bool> deps; var dirs = new List <string> (); string fileVirtualPath; foreach (VirtualFile file in vdir.Files) { fileVirtualPath = file.VirtualPath; if (BuildManager.IgnoreVirtualPath(fileVirtualPath)) { continue; } bp = GetBuildProvider(fileVirtualPath, bpcoll); if (bp == null) { continue; } if (!AddBuildProvider(bp)) { continue; } deps = bp.ExtractDependencies(); if (deps == null) { continue; } string depDir, s; dirs.Clear(); foreach (var dep in deps) { s = dep.Key; depDir = VirtualPathUtility.GetDirectory(s); // dependencies are assumed to contain absolute paths if (cache.ContainsKey(depDir)) { continue; } cache.Add(depDir, true); AddVirtualDir(GetVirtualDirectory(s), bpcoll, cache); } } }
void AddVirtualFile(VirtualFile file, BuildProviderCollection bpcoll) { if (file == null || BuildManager.IgnoreVirtualPath(file.VirtualPath)) { return; } BuildProvider bp = GetBuildProvider(file.VirtualPath, bpcoll); if (bp == null) { return; } AddBuildProvider(bp); }
BuildProvider GetBuildProviderFor(string file, BuildProviderCollection buildProviders) { if (file == null || file.Length == 0 || buildProviders == null || buildProviders.Count == 0) { return(null); } BuildProvider ret = buildProviders.GetProviderInstanceForExtension(Path.GetExtension(file)); if (ret != null && IsCorrectBuilderType(ret)) { ret.SetVirtualPath(PhysicalToVirtual(file)); return(ret); } return(null); }
public static BuildProvider GetBuildProvider(VirtualPath virtualPath, BuildProviderCollection coll) { if (virtualPath == null || String.IsNullOrEmpty(virtualPath.Original) || coll == null) { return(null); } string extension = virtualPath.Extension; BuildProvider bp = coll.GetProviderForExtension(extension); if (bp == null) { if (String.Compare(extension, ".asax", StringComparison.OrdinalIgnoreCase) == 0) { bp = new ApplicationFileBuildProvider(); } else if (StrUtils.StartsWith(virtualPath.AppRelative, "~/App_Themes/")) { bp = new ThemeDirectoryBuildProvider(); } if (bp != null) { bp.SetVirtualPath(virtualPath); } return(bp); } object[] attrs = bp.GetType().GetCustomAttributes(typeof(BuildProviderAppliesToAttribute), true); if (attrs == null || attrs.Length == 0) { return(bp); } BuildProviderAppliesTo appliesTo = ((BuildProviderAppliesToAttribute)attrs [0]).AppliesTo; if ((appliesTo & BuildProviderAppliesTo.Web) == 0) { return(null); } bp.SetVirtualPath(virtualPath); return(bp); }
internal static Type GetProviderTypeForExtension(string extension) { if (String.IsNullOrEmpty(extension)) { return(null); } Type type = null; if (registeredBuildProviderTypes == null || !registeredBuildProviderTypes.TryGetValue(extension, out type) || type == null) { var cs = WebConfigurationManager.GetSection("system.web/compilation") as CompilationSection; BuildProviderCollection bpcoll = cs != null ? cs.BuildProviders : null; global::System.Web.Configuration.BuildProvider bpcfg = bpcoll != null ? bpcoll [extension] : null; if (bpcfg != null) { type = HttpApplication.LoadType(bpcfg.Type); } } return(type); }
public List <BuildProviderGroup> Build(bool single) { if (StrUtils.StartsWith(virtualPath.AppRelative, "~/App_Themes/")) { var themebp = new ThemeDirectoryBuildProvider(); themebp.SetVirtualPath(virtualPath); return(GetSingleBuildProviderGroup(themebp)); } CompilationSection section = CompilationSection; BuildProviderCollection bpcoll = section != null ? section.BuildProviders : null; if (bpcoll == null || bpcoll.Count == 0) { return(null); } if (virtualPath.IsFake) { BuildProvider bp = GetBuildProvider(virtualPath, bpcoll); if (bp == null) { return(null); } return(GetSingleBuildProviderGroup(bp)); } if (single) { AddVirtualFile(GetVirtualFile(virtualPath.Absolute), bpcoll); } else { var cache = new Dictionary <string, bool> (dictionaryComparer); AddVirtualDir(GetVirtualDirectory(virtualPath.Absolute), bpcoll, cache); cache = null; if (buildProviders == null || buildProviders.Count == 0) { AddVirtualFile(GetVirtualFile(virtualPath.Absolute), bpcoll); } } if (buildProviders == null || buildProviders.Count == 0) { return(null); } var buildProviderGroups = new List <BuildProviderGroup> (); foreach (BuildProvider bp in buildProviders.Values) { AssignToGroup(bp, buildProviderGroups); } if (buildProviderGroups == null || buildProviderGroups.Count == 0) { buildProviderGroups = null; return(null); } // We need to reverse the order, so that the build happens from the least // dependant assemblies to the most dependant ones, more or less. buildProviderGroups.Reverse(); return(buildProviderGroups); }
public static BuildProvider GetBuildProvider(string virtualPath, BuildProviderCollection coll) { return(GetBuildProvider(new VirtualPath(virtualPath), coll)); }
// Build and add the assembly to the BuildManager's // CodeAssemblies collection public void Build(string[] binAssemblies) { Type compilerProvider = null; CompilerInfo compilerInfo = null, cit; string extension, language, cpfile = null; List <string> knownfiles = new List <string>(); List <string> unknownfiles = new List <string>(); // First make sure all the files are in the same // language bool known = false; foreach (string f in files) { known = true; language = null; extension = Path.GetExtension(f); if (String.IsNullOrEmpty(extension) || !CodeDomProvider.IsDefinedExtension(extension)) { known = false; } if (known) { language = CodeDomProvider.GetLanguageFromExtension(extension); if (!CodeDomProvider.IsDefinedLanguage(language)) { known = false; } } if (!known || language == null) { unknownfiles.Add(f); continue; } cit = CodeDomProvider.GetCompilerInfo(language); if (cit == null || !cit.IsCodeDomProviderTypeValid) { continue; } if (compilerProvider == null) { cpfile = f; compilerProvider = cit.CodeDomProviderType; compilerInfo = cit; } else if (compilerProvider != cit.CodeDomProviderType) { throw new HttpException( String.Format( "Files {0} and {1} are in different languages - they cannot be compiled into the same assembly", Path.GetFileName(cpfile), Path.GetFileName(f))); } knownfiles.Add(f); } CodeDomProvider provider = null; CompilationSection compilationSection = WebConfigurationManager.GetWebApplicationSection("system.web/compilation") as CompilationSection; if (compilerInfo == null) { if (!CodeDomProvider.IsDefinedLanguage(compilationSection.DefaultLanguage)) { throw new HttpException("Failed to retrieve default source language"); } compilerInfo = CodeDomProvider.GetCompilerInfo(compilationSection.DefaultLanguage); if (compilerInfo == null || !compilerInfo.IsCodeDomProviderTypeValid) { throw new HttpException("Internal error while initializing application"); } } provider = compilerInfo.CreateProvider(); if (provider == null) { throw new HttpException("A code provider error occurred while initializing application."); } AssemblyBuilder abuilder = new AssemblyBuilder(provider); foreach (string file in knownfiles) { abuilder.AddCodeFile(file); } foreach (CodeCompileUnit unit in units) { abuilder.AddCodeCompileUnit(unit); } BuildProvider bprovider; CompilerParameters parameters = compilerInfo.CreateDefaultCompilerParameters(); parameters.IncludeDebugInformation = compilationSection.Debug; if (binAssemblies != null && binAssemblies.Length > 0) { StringCollection parmRefAsm = parameters.ReferencedAssemblies; foreach (string binAsm in binAssemblies) { if (parmRefAsm.Contains(binAsm)) { continue; } parmRefAsm.Add(binAsm); } } if (compilationSection != null) { foreach (AssemblyInfo ai in compilationSection.Assemblies) { if (ai.Assembly != "*") { try { parameters.ReferencedAssemblies.Add( AssemblyPathResolver.GetAssemblyPath(ai.Assembly)); } catch (Exception ex) { throw new HttpException( String.Format("Could not find assembly {0}.", ai.Assembly), ex); } } } BuildProviderCollection buildProviders = compilationSection.BuildProviders; foreach (string file in unknownfiles) { bprovider = GetBuildProviderFor(file, buildProviders); if (bprovider == null) { continue; } bprovider.GenerateCode(abuilder); } } if (knownfiles.Count == 0 && unknownfiles.Count == 0 && units.Count == 0) { return; } outputAssemblyName = (string)FileUtils.CreateTemporaryFile( AppDomain.CurrentDomain.SetupInformation.DynamicBase, name, "dll", OnCreateTemporaryAssemblyFile); parameters.OutputAssembly = outputAssemblyName; foreach (Assembly a in BuildManager.TopLevelAssemblies) { parameters.ReferencedAssemblies.Add(a.Location); } CompilerResults results = abuilder.BuildAssembly(parameters); if (results == null) { return; } if (results.NativeCompilerReturnValue == 0) { BuildManager.CodeAssemblies.Add(results.CompiledAssembly); BuildManager.TopLevelAssemblies.Add(results.CompiledAssembly); HttpRuntime.WritePreservationFile(results.CompiledAssembly, name); } else { if (HttpContext.Current.IsCustomErrorEnabled) { throw new HttpException("An error occurred while initializing application."); } throw new CompilationException(null, results.Errors, null); } }
internal static long GetRecompilationHash(CompilationSection ps) { HashCodeCombiner combiner = new HashCodeCombiner(); combiner.AddObject(ps.Debug); combiner.AddObject(ps.TargetFramework); combiner.AddObject(ps.Strict); combiner.AddObject(ps.Explicit); combiner.AddObject(ps.Batch); combiner.AddObject(ps.OptimizeCompilations); combiner.AddObject(ps.BatchTimeout); combiner.AddObject(ps.MaxBatchGeneratedFileSize); combiner.AddObject(ps.MaxBatchSize); combiner.AddObject(ps.NumRecompilesBeforeAppRestart); combiner.AddObject(ps.DefaultLanguage); combiner.AddObject(ps.UrlLinePragmas); if (ps.AssemblyPostProcessorTypeInternal != null) { combiner.AddObject(ps.AssemblyPostProcessorTypeInternal.FullName); } foreach (Compiler compiler in ps.Compilers) { combiner.AddObject(compiler.Language); combiner.AddObject(compiler.Extension); combiner.AddObject(compiler.Type); combiner.AddObject(compiler.WarningLevel); combiner.AddObject(compiler.CompilerOptions); } foreach (System.Web.Configuration.ExpressionBuilder builder in ps.ExpressionBuilders) { combiner.AddObject(builder.ExpressionPrefix); combiner.AddObject(builder.Type); } AssemblyCollection assemblies = ps.Assemblies; if (assemblies.Count == 0) { combiner.AddObject("__clearassemblies"); } else { foreach (AssemblyInfo info in assemblies) { combiner.AddObject(info.Assembly); } } BuildProviderCollection buildProviders = ps.BuildProviders; if (buildProviders.Count == 0) { combiner.AddObject("__clearbuildproviders"); } else { foreach (System.Web.Configuration.BuildProvider provider in buildProviders) { combiner.AddObject(provider.Type); combiner.AddObject(provider.Extension); } } FolderLevelBuildProviderCollection folderLevelBuildProviders = ps.FolderLevelBuildProviders; if (folderLevelBuildProviders.Count == 0) { combiner.AddObject("__clearfolderlevelbuildproviders"); } else { foreach (FolderLevelBuildProvider provider2 in folderLevelBuildProviders) { combiner.AddObject(provider2.Type); combiner.AddObject(provider2.Name); } } CodeSubDirectoriesCollection codeSubDirectories = ps.CodeSubDirectories; if (codeSubDirectories.Count == 0) { combiner.AddObject("__clearcodesubdirs"); } else { foreach (CodeSubDirectory directory in codeSubDirectories) { combiner.AddObject(directory.DirectoryName); } } CompilerInfo[] allCompilerInfo = CodeDomProvider.GetAllCompilerInfo(); if (allCompilerInfo != null) { foreach (CompilerInfo info2 in allCompilerInfo) { if (info2.IsCodeDomProviderTypeValid) { string compilerOptions = info2.CreateDefaultCompilerParameters().CompilerOptions; if (!string.IsNullOrEmpty(compilerOptions)) { Type codeDomProviderType = info2.CodeDomProviderType; if (codeDomProviderType != null) { combiner.AddObject(codeDomProviderType.FullName); } combiner.AddObject(compilerOptions); } if (info2.CodeDomProviderType != null) { IDictionary <string, string> providerOptions = GetProviderOptions(info2); if ((providerOptions != null) && (providerOptions.Count > 0)) { string fullName = info2.CodeDomProviderType.FullName; foreach (string str3 in providerOptions.Keys) { string str4 = providerOptions[str3]; combiner.AddObject(fullName + ":" + str3 + "=" + str4); } } } } } } return(combiner.CombinedHash); }