private ViewCompilerWorkItem CreatePrecompiledWorkItem(string normalizedPath, CompiledViewDescriptor precompiledView) { // We have a precompiled view - but we're not sure that we can use it yet. // // We need to determine first if we have enough information to 'recompile' this view. If that's the case // we'll create change tokens for all of the files. // // Then we'll attempt to validate if any of those files have different content than the original sources // based on checksums. if (precompiledView.Item == null || !PublicChecksumValidator.IsRecompilationSupported(precompiledView.Item)) { return(new ViewCompilerWorkItem { // If we don't have a checksum for the primary source file we can't recompile. SupportsCompilation = false, ExpirationTokens = Array.Empty <IChangeToken>(), // Never expire because we can't recompile. Descriptor = precompiledView, // This will be used as-is. }); } var item = new ViewCompilerWorkItem { SupportsCompilation = true, Descriptor = precompiledView, // This might be used, if the checksums match. // Used to validate and recompile NormalizedPath = normalizedPath, ExpirationTokens = GetExpirationTokens(precompiledView) }; // We also need to create a new descriptor, because the original one doesn't have expiration tokens on // it. These will be used by the view location cache, which is like an L1 cache for views (this class is // the L2 cache). item.Descriptor = new CompiledViewDescriptor() { ExpirationTokens = item.ExpirationTokens, Item = precompiledView.Item, RelativePath = precompiledView.RelativePath, }; return(item); }
private ViewCompilerWorkItem CreateRuntimeCompilationWorkItem(string normalizedPath, CompiledViewDescriptor originalDescriptor) { if (originalDescriptor is object) { if (originalDescriptor.Item == null || !PublicChecksumValidator.IsRecompilationSupported(originalDescriptor.Item)) { return(new ViewCompilerWorkItem { // If we don't have a checksum for the primary source file we can't recompile. SupportsCompilation = false, ExpirationTokens = Array.Empty <IChangeToken>(), // Never expire because we can't recompile. Descriptor = originalDescriptor, // This will be used as-is. }); } var assembly = originalDescriptor.Item.Type?.Assembly.GetName().Name; if (!string.IsNullOrEmpty(assembly) && this.projectEngines.TryGetValue(assembly, out var origEngine)) { var projectItem = origEngine.FileSystem.GetItem(normalizedPath, fileKind: null); if (!projectItem.Exists) { originalDescriptor = null; //force recompilation } } } var allTokens = new List <IChangeToken>(); var exists = false; foreach (var engine in this.projectEngines) { var fileProvider = GetProviderFromRazorProjectEngine(engine.Value); IList <IChangeToken> expirationTokens = new List <IChangeToken> { fileProvider.Watch(normalizedPath), }; var projectItem = engine.Value.FileSystem.GetItem(normalizedPath, fileKind: null); if (projectItem.Exists) { exists = true; var importFeature = engine.Value.ProjectFeatures.OfType <IImportProjectFeature>().ToArray(); foreach (var feature in importFeature) { foreach (var file in feature.GetImports(projectItem)) { if (file.FilePath != null) { expirationTokens.Add(GetProviderFromRazorProjectEngine(engine.Value).Watch(file.FilePath)); } } } } allTokens.AddRange(expirationTokens); // _logger.ViewCompilerFoundFileToCompile(normalizedPath); } if (originalDescriptor is object) { originalDescriptor.ExpirationTokens = allTokens; } return(new ViewCompilerWorkItem() { SupportsCompilation = exists, NormalizedPath = normalizedPath, ExpirationTokens = allTokens, Descriptor = exists ? default : new CompiledViewDescriptor() { RelativePath = normalizedPath, ExpirationTokens = allTokens }, OriginalDescriptor = originalDescriptor });