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)));
        }
Beispiel #2
0
        protected virtual IEnumerable <RazorFileInfo> CreateCompilationDescriptors(
            [NotNull] IBeforeCompileContext context)
        {
            var filesToProcess = new List <RelativeFileInfo>();

            GetFileInfosRecursive(root: string.Empty, razorFiles: filesToProcess);

            var razorFiles      = new RazorFileInfo[filesToProcess.Count];
            var syntaxTrees     = new SyntaxTree[filesToProcess.Count];
            var parallelOptions = new ParallelOptions {
                MaxDegreeOfParallelism = MaxDegreesOfParallelism
            };
            var diagnosticsLock = new object();

            Parallel.For(0, filesToProcess.Count, parallelOptions, index =>
            {
                var file       = filesToProcess[index];
                var cacheEntry = PreCompilationCache.GetOrSet(file.RelativePath,
                                                              file,
                                                              OnCacheMiss);
                if (cacheEntry != null)
                {
                    if (cacheEntry.Success)
                    {
                        syntaxTrees[index] = cacheEntry.SyntaxTree;
                        razorFiles[index]  = cacheEntry.FileInfo;
                    }
                    else
                    {
                        lock (diagnosticsLock)
                        {
                            foreach (var diagnostic in cacheEntry.Diagnostics)
                            {
                                context.Diagnostics.Add(diagnostic);
                            }
                        }
                    }
                }
            });

            context.CSharpCompilation = context.CSharpCompilation
                                        .AddSyntaxTrees(syntaxTrees.Where(tree => tree != null));
            return(razorFiles.Where(file => file != null));
        }