protected virtual RazorFileInfoCollection CreateFileInfoCollection() { var filesToProcess = new List <RelativeFileInfo>(); GetFileInfosRecursive(root: string.Empty, razorFiles: filesToProcess); if (filesToProcess.Count == 0) { return(null); } var razorFiles = new RazorFileInfo[filesToProcess.Count]; var syntaxTrees = new SyntaxTree[filesToProcess.Count]; var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = MaxDegreesOfParallelism }; var diagnosticsLock = new object(); var hasErrors = false; Parallel.For(0, filesToProcess.Count, parallelOptions, index => { var file = filesToProcess[index]; PrecompilationCacheEntry cacheEntry; if (!PreCompilationCache.TryGetValue(file.RelativePath, out cacheEntry)) { cacheEntry = GetCacheEntry(file); PreCompilationCache.Set( file.RelativePath, cacheEntry, GetMemoryCacheEntryOptions(file, cacheEntry)); } if (cacheEntry != null) { if (cacheEntry.Success) { syntaxTrees[index] = cacheEntry.SyntaxTree; razorFiles[index] = cacheEntry.FileInfo; } else { hasErrors = true; lock (diagnosticsLock) { AddRange(CompileContext.Diagnostics, cacheEntry.Diagnostics); } } } }); if (hasErrors) { // If any of the Razor files had syntax errors, don't emit the precompiled views assembly. return(null); } return(GeneratePrecompiledAssembly( syntaxTrees.Where(tree => tree != null), razorFiles.Where(file => file != null))); }
protected virtual string GenerateFile([NotNull] RazorFileInfo fileInfo) { return(string.Format(FileFormat, fileInfo.LastModified.ToFileTime(), fileInfo.Length, fileInfo.RelativePath, fileInfo.FullTypeName, fileInfo.Hash, fileInfo.HashAlgorithmVersion)); }
protected virtual PrecompilationCacheEntry GetCacheEntry(RelativeFileInfo fileInfo) { if (fileInfo == null) { throw new ArgumentNullException(nameof(fileInfo)); } using (var stream = fileInfo.FileInfo.CreateReadStream()) { using (var host = GetRazorHost()) { var results = host.GenerateCode(fileInfo.RelativePath, stream); if (results.Success) { var syntaxTree = SyntaxTreeGenerator.Generate( results.GeneratedCode, fileInfo.FileInfo.PhysicalPath, CompilationSettings); var fullTypeName = results.GetMainClassName(host, syntaxTree); if (fullTypeName != null) { var razorFileInfo = new RazorFileInfo { RelativePath = fileInfo.RelativePath, FullTypeName = fullTypeName }; return(new PrecompilationCacheEntry(razorFileInfo, syntaxTree)); } } else { var diagnostics = results .ParserErrors .Select(error => error.ToDiagnostics(fileInfo.FileInfo.PhysicalPath)) .ToList(); return(new PrecompilationCacheEntry(diagnostics)); } } } return(null); }
/// <summary> /// Initializes a new instance of <see cref="PrecompilationCacheEntry"/> for a successful parse. /// </summary> /// <param name="fileInfo">The <see cref="RazorFileInfo"/> of the file being cached.</param> /// <param name="syntaxTree">The <see cref="CodeAnalysis.SyntaxTree"/> to cache.</param> public PrecompilationCacheEntry( RazorFileInfo fileInfo, SyntaxTree syntaxTree) { if (fileInfo == null) { throw new ArgumentNullException(nameof(fileInfo)); } if (syntaxTree == null) { throw new ArgumentNullException(nameof(syntaxTree)); } FileInfo = fileInfo; SyntaxTree = syntaxTree; }
/// <summary> /// Initializes a new instance of <see cref="PrecompilationCacheEntry"/> for a successful parse. /// </summary> /// <param name="fileInfo">The <see cref="RazorFileInfo"/> of the file being cached.</param> /// <param name="syntaxTree">The <see cref="CodeAnalysis.SyntaxTree"/> to cache.</param> public PrecompilationCacheEntry( RazorFileInfo fileInfo, SyntaxTree syntaxTree) { if (fileInfo == null) { throw new ArgumentNullException(nameof(fileInfo)); } if (syntaxTree == null) { throw new ArgumentNullException(nameof(syntaxTree)); } FileInfo = fileInfo; SyntaxTree = syntaxTree; }
protected virtual PrecompilationCacheEntry GetCacheEntry([NotNull] RelativeFileInfo fileInfo) { using (var stream = fileInfo.FileInfo.CreateReadStream()) { var host = GetRazorHost(); var results = host.GenerateCode(fileInfo.RelativePath, stream); if (results.Success) { var syntaxTree = SyntaxTreeGenerator.Generate(results.GeneratedCode, fileInfo.FileInfo.PhysicalPath, CompilationSettings); var fullTypeName = results.GetMainClassName(host, syntaxTree); if (fullTypeName != null) { var hashAlgorithmVersion = RazorFileHash.HashAlgorithmVersion1; var hash = RazorFileHash.GetHash(fileInfo.FileInfo, hashAlgorithmVersion); var razorFileInfo = new RazorFileInfo { RelativePath = fileInfo.RelativePath, LastModified = fileInfo.FileInfo.LastModified, Length = fileInfo.FileInfo.Length, FullTypeName = fullTypeName, Hash = hash, HashAlgorithmVersion = hashAlgorithmVersion }; return(new PrecompilationCacheEntry(razorFileInfo, syntaxTree)); } } else { var diagnostics = results.ParserErrors .Select(error => error.ToDiagnostics(fileInfo.FileInfo.PhysicalPath)) .ToList(); return(new PrecompilationCacheEntry(diagnostics)); } } return(null); }
/// <summary> /// Initializes a new instance of <see cref="PrecompilationCacheEntry"/> for a successful parse. /// </summary> /// <param name="fileInfo">The <see cref="RazorFileInfo"/> of the file being cached.</param> /// <param name="syntaxTree">The <see cref="CodeAnalysis.SyntaxTree"/> to cache.</param> public PrecompilationCacheEntry([NotNull] RazorFileInfo fileInfo, [NotNull] SyntaxTree syntaxTree) { FileInfo = fileInfo; SyntaxTree = syntaxTree; }