1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
//------------------------------------------------------------------------------
// <copyright file="CodeDirectoryCompiler.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Compilation {
using System;
using System.IO;
using System.Collections;
using System.CodeDom.Compiler;
using System.Configuration;
using System.Globalization;
using System.Web.Configuration;
using System.Reflection;
using System.Web.Hosting;
using System.Web.Util;
using System.Web.UI;
// The different types of directory that we treat as 'Code' (with minor differences)
internal enum CodeDirectoryType {
MainCode, // The main /code directory
SubCode, // Code subdirectories registered to be compiled separately
AppResources, // The /Resources directory
LocalResources, // A /LocalResources directory (at any level)
WebReferences // The /WebReferences directory
}
internal class CodeDirectoryCompiler {
private VirtualPath _virtualDir;
private CodeDirectoryType _dirType;
private StringSet _excludedSubdirectories;
private BuildProvidersCompiler _bpc;
private BuildProviderSet _buildProviders = new BuildProviderSet();
private bool _onlyBuildLocalizedResources;
static internal BuildResultMainCodeAssembly _mainCodeBuildResult;
internal static bool IsResourceCodeDirectoryType(CodeDirectoryType dirType) {
return dirType == CodeDirectoryType.AppResources || dirType == CodeDirectoryType.LocalResources;
}
internal static Assembly GetCodeDirectoryAssembly(VirtualPath virtualDir,
CodeDirectoryType dirType, string assemblyName,
StringSet excludedSubdirectories, bool isDirectoryAllowed) {
string physicalDir = virtualDir.MapPath();
if (!isDirectoryAllowed) {
// The directory should never exist in a precompiled app
if (Directory.Exists(physicalDir)) {
throw new HttpException(SR.GetString(SR.Bar_dir_in_precompiled_app, virtualDir));
}
}
bool supportLocalization = IsResourceCodeDirectoryType(dirType);
// Determine the proper cache key based on the type of directory we're processing
string cacheKey = assemblyName;
// Try the cache first
BuildResult result = BuildManager.GetBuildResultFromCache(cacheKey);
Assembly resultAssembly = null;
// If it's cached, just return it
if (result != null) {
// It should always be a BuildResultCompiledAssembly, though if there is
// a VirtualPathProvider doing very bad things, it may not (VSWhidbey 341701)
Debug.Assert(result is BuildResultCompiledAssembly);
if (result is BuildResultCompiledAssembly) {
// If it's the main code assembly, keep track of it so we can later call
// the AppInitialize method
if (result is BuildResultMainCodeAssembly) {
Debug.Assert(dirType == CodeDirectoryType.MainCode);
Debug.Assert(_mainCodeBuildResult == null);
_mainCodeBuildResult = (BuildResultMainCodeAssembly) result;
}
resultAssembly = ((BuildResultCompiledAssembly)result).ResultAssembly;
if (!supportLocalization)
return resultAssembly;
// We found a preserved resource assembly. However, we may not be done,
// as the culture specific files may have changed.
// But don't make any further checks if the directory is not allowed (precomp secenario).
// In that case, we should always return the assembly (VSWhidbey 533498)
if (!isDirectoryAllowed)
return resultAssembly;
BuildResultResourceAssembly buildResultResAssembly = (BuildResultResourceAssembly)result;
string newResourcesDependenciesHash = HashCodeCombiner.GetDirectoryHash(virtualDir);
// If the resources hash (which includes satellites) is up to date, we're done
if (newResourcesDependenciesHash == buildResultResAssembly.ResourcesDependenciesHash)
return resultAssembly;
}
}
// If app was precompiled, don't attempt compilation
if (!isDirectoryAllowed)
return null;
// Check whether the virtual dir is mapped to a different application,
// which we don't support (VSWhidbey 218603). But don't do this for LocalResource (VSWhidbey 237935)
if (dirType != CodeDirectoryType.LocalResources && !StringUtil.StringStartsWithIgnoreCase(physicalDir, HttpRuntime.AppDomainAppPathInternal)) {
throw new HttpException(SR.GetString(SR.Virtual_codedir, virtualDir.VirtualPathString));
}
// If the directory doesn't exist, we may be done
if (!Directory.Exists(physicalDir)) {
// We're definitely done if it's not the main code dir
if (dirType != CodeDirectoryType.MainCode)
return null;
// If it is the main code dir, we're only done is there is no profile to compile
// since the profice gets built as part of the main assembly.
if (!ProfileBuildProvider.HasCompilableProfile)
return null;
}
// Otherwise, compile it
BuildManager.ReportDirectoryCompilationProgress(virtualDir);
DateTime utcStart = DateTime.UtcNow;
CodeDirectoryCompiler cdc = new CodeDirectoryCompiler(virtualDir,
dirType, excludedSubdirectories);
string outputAssemblyName = null;
if (resultAssembly != null) {
// If resultAssembly is not null, we are in the case where we just need to build
// the localized resx file in a resources dir (local or global)
Debug.Assert(supportLocalization);
outputAssemblyName = resultAssembly.GetName().Name;
cdc._onlyBuildLocalizedResources = true;
}
else {
outputAssemblyName = BuildManager.GenerateRandomAssemblyName(assemblyName);
}
BuildProvidersCompiler bpc =
new BuildProvidersCompiler(virtualDir, supportLocalization, outputAssemblyName);
cdc._bpc = bpc;
// Find all the build provider we want to compile from the code directory
cdc.FindBuildProviders();
// Give them to the BuildProvidersCompiler
bpc.SetBuildProviders(cdc._buildProviders);
// Compile them into an assembly
CompilerResults results = bpc.PerformBuild();
// Did we just compile something?
if (results != null) {
Debug.Assert(result == null);
Debug.Assert(resultAssembly == null);
// If there is already a loaded module with the same path, try to wait for it to be unloaded.
// Otherwise, we would end up loading this old assembly instead of the new one (VSWhidbey 554697)
DateTime waitLimit = DateTime.UtcNow.AddMilliseconds(3000);
for (;;) {
IntPtr hModule = UnsafeNativeMethods.GetModuleHandle(results.PathToAssembly);
if (hModule == IntPtr.Zero)
break;
Debug.Trace("CodeDirectoryCompiler", results.PathToAssembly + " is already loaded. Waiting a bit");
System.Threading.Thread.Sleep(250);
// Stop trying if the timeout was reached
if (DateTime.UtcNow > waitLimit) {
Debug.Trace("CodeDirectoryCompiler", "Timeout waiting for old assembly to unload: " + results.PathToAssembly);
throw new HttpException(SR.GetString(SR.Assembly_already_loaded, results.PathToAssembly));
}
}
resultAssembly = results.CompiledAssembly;
}
// It is possible that there was nothing to compile (and we're not in the
// satellite resources case)
if (resultAssembly == null)
return null;
// For the main code directory, use a special BuildResult that takes care of
// calling AppInitialize if it finds one
if (dirType == CodeDirectoryType.MainCode) {
// Keep track of it so we can later call the AppInitialize method
_mainCodeBuildResult = new BuildResultMainCodeAssembly(resultAssembly);
result = _mainCodeBuildResult;
}
else if (supportLocalization) {
result = new BuildResultResourceAssembly(resultAssembly);
}
else {
result = new BuildResultCompiledAssembly(resultAssembly);
}
result.VirtualPath = virtualDir;
// If compilations are optimized, we need to include the right dependencies, since we can no longer
// rely on everything getting wiped out when something in App_Code changes.
// But don't do this for local resources, since they have their own special way of
// dealing with dependencies (in BuildResultResourceAssembly.ComputeSourceDependenciesHashCode).
// It's crucial *not* to do it as it triggers a tricky infinite recursion due to the fact
// that GetBuildResultFromCacheInternal calls EnsureFirstTimeDirectoryInitForDependencies if
// there is at least one dependency
if (BuildManager.OptimizeCompilations && dirType != CodeDirectoryType.LocalResources) {
result.AddVirtualPathDependencies(new SingleObjectCollection(virtualDir.AppRelativeVirtualPathString));
}
// Top level assembly should not be cached to memory. But LocalResources are *not*
// top level files, and do benefit from memory caching
if (dirType != CodeDirectoryType.LocalResources)
result.CacheToMemory = false;
// Cache it for next time
BuildManager.CacheBuildResult(cacheKey, result, utcStart);
return resultAssembly;
}
// Call the AppInitialize method in the Code assembly if there is one
internal static void CallAppInitializeMethod() {
if (_mainCodeBuildResult != null)
_mainCodeBuildResult.CallAppInitializeMethod();
}
internal const string sourcesDirectoryPrefix = "Sources_";
internal static void GetCodeDirectoryInformation(
VirtualPath virtualDir, CodeDirectoryType dirType, StringSet excludedSubdirectories, int index,
out Type codeDomProviderType, out CompilerParameters compilerParameters,
out string generatedFilesDir) {
// Compute the full path to the directory we'll use to generate all
// the code files
generatedFilesDir = HttpRuntime.CodegenDirInternal + "\\" +
sourcesDirectoryPrefix + virtualDir.FileName;
bool supportLocalization = IsResourceCodeDirectoryType(dirType);
// the index is used to retrieve the correct referenced assemblies
BuildProvidersCompiler bpc = new BuildProvidersCompiler(virtualDir, supportLocalization,
generatedFilesDir, index);
CodeDirectoryCompiler cdc = new CodeDirectoryCompiler(virtualDir,
dirType, excludedSubdirectories);
cdc._bpc = bpc;
// Find all the build provider we want to compile from the code directory
cdc.FindBuildProviders();
// Give them to the BuildProvidersCompiler
bpc.SetBuildProviders(cdc._buildProviders);
// Generate all the sources into the directory generatedFilesDir
bpc.GenerateSources(out codeDomProviderType, out compilerParameters);
}
private CodeDirectoryCompiler(VirtualPath virtualDir, CodeDirectoryType dirType,
StringSet excludedSubdirectories) {
_virtualDir = virtualDir;
_dirType = dirType;
_excludedSubdirectories = excludedSubdirectories;
}
private void FindBuildProviders() {
// If we need to build the profile, add its build provider
if (_dirType == CodeDirectoryType.MainCode && ProfileBuildProvider.HasCompilableProfile) {
_buildProviders.Add(ProfileBuildProvider.Create());
}
VirtualDirectory vdir = HostingEnvironment.VirtualPathProvider.GetDirectory(_virtualDir);
ProcessDirectoryRecursive(vdir, true /*topLevel*/);
}
private void AddFolderLevelBuildProviders(VirtualDirectory vdir, FolderLevelBuildProviderAppliesTo appliesTo) {
BuildManager.AddFolderLevelBuildProviders(_buildProviders, vdir.VirtualPathObject,
appliesTo, _bpc.CompConfig, _bpc.ReferencedAssemblies);
}
private void ProcessDirectoryRecursive(VirtualDirectory vdir, bool topLevel) {
// If it's a WebReferences directory, handle it using a single WebReferencesBuildProvider
// instead of creating a different BuildProvider for each file.
if (_dirType == CodeDirectoryType.WebReferences) {
// Create a build provider for the current directory
BuildProvider buildProvider = new WebReferencesBuildProvider(vdir);
buildProvider.SetVirtualPath(vdir.VirtualPathObject);
_buildProviders.Add(buildProvider);
AddFolderLevelBuildProviders(vdir, FolderLevelBuildProviderAppliesTo.WebReferences);
}
else if (_dirType == CodeDirectoryType.AppResources) {
AddFolderLevelBuildProviders(vdir, FolderLevelBuildProviderAppliesTo.GlobalResources);
}
else if (_dirType == CodeDirectoryType.LocalResources) {
AddFolderLevelBuildProviders(vdir, FolderLevelBuildProviderAppliesTo.LocalResources);
}
else if (_dirType == CodeDirectoryType.MainCode || _dirType == CodeDirectoryType.SubCode) {
AddFolderLevelBuildProviders(vdir, FolderLevelBuildProviderAppliesTo.Code);
}
// Go through all the files in the directory
foreach (VirtualFileBase child in vdir.Children) {
if (child.IsDirectory) {
// If we are at the top level of this code directory, and the current
// subdirectory is in the exclude list, skip it
if (topLevel && _excludedSubdirectories != null &&
_excludedSubdirectories.Contains(child.Name)) {
continue;
}
// Exclude the special FrontPage directory (VSWhidbey 116727)
if (child.Name == "_vti_cnf")
continue;
ProcessDirectoryRecursive(child as VirtualDirectory, false /*topLevel*/);
continue;
}
// Don't look at individual files for WebReferences directories
if (_dirType == CodeDirectoryType.WebReferences)
continue;
// Skip neutral files if _onlyBuildLocalizedResources is true
if (IsResourceCodeDirectoryType(_dirType)) {
if (_onlyBuildLocalizedResources && System.Web.UI.Util.GetCultureName(child.VirtualPath) == null) {
continue;
}
}
BuildProvider buildProvider = BuildManager.CreateBuildProvider(child.VirtualPathObject,
(IsResourceCodeDirectoryType(_dirType)) ?
BuildProviderAppliesTo.Resources : BuildProviderAppliesTo.Code,
_bpc.CompConfig,
_bpc.ReferencedAssemblies, false /*failIfUnknown*/);
// Non-supported file type
if (buildProvider == null)
continue;
// For Page resources, don't generate a strongly typed class
if (_dirType == CodeDirectoryType.LocalResources && buildProvider is BaseResourcesBuildProvider) {
((BaseResourcesBuildProvider)buildProvider).DontGenerateStronglyTypedClass();
}
_buildProviders.Add(buildProvider);
}
}
}
}
|