public override SyntaxTree GetSyntaxTree(string sourcePath, Stream sourceStream, CSharpParseOptions parseOptions)
        {
            try
            {
                var viewFullPath    = sourcePath;
                var viewVirtualPath = GetRelativeUri(sourcePath, Compilation.CurrentDirectory.FullName);
                var viewConfig      = WebConfigurationManager.OpenMappedWebConfiguration(_configMap, viewVirtualPath);
                var razorConfig     = viewConfig.GetSectionGroup("system.web.webPages.razor") as RazorWebSectionGroup;
                var host            = razorConfig == null
                    ? WebRazorHostFactory.CreateDefaultHost(viewVirtualPath, viewFullPath)
                    : WebRazorHostFactory.CreateHostFromConfig(razorConfig, viewVirtualPath, viewFullPath);

                using (var rdr = new StreamReader(sourceStream, Compilation.Encoding, detectEncodingFromByteOrderMarks: true))
                    using (var provider = CodeDomProvider.CreateProvider("csharp"))
                        using (var generatedStream = new MemoryStream())
                            using (var generatedWriter = new StreamWriter(generatedStream, Compilation.Encoding))
                            {
                                var engine   = new RazorTemplateEngine(host);
                                var razorOut = engine.GenerateCode(rdr, null, null, viewFullPath);

                                // add the CompiledFromFileAttribute to the generated class
                                razorOut.GeneratedCode
                                .Namespaces.OfType <CodeNamespace>().FirstOrDefault()?
                                .Types.OfType <CodeTypeDeclaration>().FirstOrDefault()?
                                .CustomAttributes.Add(
                                    new CodeAttributeDeclaration(
                                        new CodeTypeReference(typeof(CompiledFromFileAttribute)),
                                        new CodeAttributeArgument(new CodePrimitiveExpression(viewFullPath))
                                        ));

                                var codeGenOptions = new CodeGeneratorOptions {
                                    VerbatimOrder = true, ElseOnClosing = false, BlankLinesBetweenMembers = false
                                };
                                provider.GenerateCodeFromCompileUnit(razorOut.GeneratedCode, generatedWriter, codeGenOptions);

                                // rewind
                                generatedWriter.Flush();
                                generatedStream.Position = 0;

                                return(base.GetSyntaxTree(sourcePath, generatedStream, parseOptions));
                            }
            }
            catch (Exception ex)
            {
                Compilation.Diagnostics.Add(Diagnostic.Create(Compilation.ViewGenerationFailed, Compilation.AsLocation(sourcePath), ex.ToString()));
                return(null);
            }
        }
        private async Task BackgroundWorker()
        {
            foreach (var loader in _workItems.GetConsumingEnumerable(_cancellationToken))
            {
                if (_cancellationToken.IsCancellationRequested)
                {
                    loader.Result.SetCanceled();
                    continue;
                }
                try
                {
                    var originalText = await loader.OriginalLoader.LoadTextAndVersionAsync(_workspace, null, default(CancellationToken));

                    var viewFullPath    = originalText.FilePath;
                    var viewVirtualPath = GetRelativeUri(originalText.FilePath, _compilation.CurrentDirectory.FullName);
                    var viewConfig      = WebConfigurationManager.OpenMappedWebConfiguration(_configMap, viewVirtualPath);
                    var host            = viewConfig.GetSectionGroup("system.web.webPages.razor") is RazorWebSectionGroup razorConfig
                        ? WebRazorHostFactory.CreateHostFromConfig(razorConfig, viewVirtualPath, viewFullPath)
                        : WebRazorHostFactory.CreateDefaultHost(viewVirtualPath, viewFullPath);

                    // having this as a field would require the ASP.NET MVC dependency even for console apps...
                    RazorWorker worker = RazorWorker;
                    if (_cacheDirectory != null)
                    {
                        worker = CachedRazorWorker;
                    }

                    using (var stream = await worker(host, originalText))
                    {
                        var generatedText = TextAndVersion.Create(
                            SourceText.From(stream, _compilation.Encoding, _compilation.CscArgs.ChecksumAlgorithm, canBeEmbedded: originalText.Text.CanBeEmbedded, throwIfBinaryDetected: true),
                            originalText.Version,
                            originalText.FilePath);

                        loader.Result.TrySetResult(generatedText);
                    }
                }
                catch (Exception ex)
                {
                    loader.Result.TrySetException(ex);
                }
            }
        }
        private static WebPageRazorHost GetRazorWebPageRazorHost(string virtualPath, string physicalPath)
        {
            WebPageRazorHost webPageRazorHost = null;

            try
            {
                string physicalDirectory = physicalPath.Substring(0, physicalPath.Length - virtualPath.Length);
                string text = virtualPath.Replace('\\', '/');
                if (!text.StartsWith("/", StringComparison.Ordinal))
                {
                    text = "/" + text;
                }
                int num = text.LastIndexOf('/');
                text = text.Substring(0, (num == 0) ? 1 : num);
                WebConfigurationFileMap arg_62_0 = new WebConfigurationFileMap();
                VirtualDirectoryMapping mapping  = new VirtualDirectoryMapping(physicalDirectory, true);
                arg_62_0.VirtualDirectories.Add("/", mapping);
                Configuration configuration = WebConfigurationManager.OpenMappedWebConfiguration(arg_62_0, text);
                if (configuration != null)
                {
                    RazorWebSectionGroup razorWebSectionGroup = (RazorWebSectionGroup)configuration.GetSectionGroup(RazorWebSectionGroup.GroupName);
                    if (razorWebSectionGroup != null)
                    {
                        webPageRazorHost = WebRazorHostFactory.CreateHostFromConfig(razorWebSectionGroup, virtualPath, physicalPath);
                    }
                }
            }
            catch (Exception)
            {
            }
            if (webPageRazorHost == null)
            {
                webPageRazorHost = WebRazorHostFactory.CreateDefaultHost(virtualPath, physicalPath);
            }
            return(webPageRazorHost);
        }
        private void WrapCshtmlReader(string sourcePath, TextReader sourceReader, MemoryStream generatedStream)
        {
            var viewFullPath    = sourcePath;
            var viewVirtualPath = GetRelativeUri(sourcePath, _compilation.CurrentDirectory.FullName);
            var viewConfig      = WebConfigurationManager.OpenMappedWebConfiguration(_configMap, viewVirtualPath);
            var razorConfig     = viewConfig.GetSectionGroup("system.web.webPages.razor") as RazorWebSectionGroup;
            var host            = razorConfig == null
                ? WebRazorHostFactory.CreateDefaultHost(viewVirtualPath, viewFullPath)
                : WebRazorHostFactory.CreateHostFromConfig(razorConfig, viewVirtualPath, viewFullPath);

            using (var provider = CodeDomProvider.CreateProvider("csharp"))
                using (var generatedWriter = new StreamWriter(generatedStream, _compilation.Encoding, 1024, leaveOpen: true))
                {
                    var engine   = new RazorTemplateEngine(host);
                    var razorOut = engine.GenerateCode(sourceReader, null, null, viewFullPath);

                    // add the CompiledFromFileAttribute to the generated class
                    razorOut.GeneratedCode
                    .Namespaces.OfType <CodeNamespace>().FirstOrDefault()?
                    .Types.OfType <CodeTypeDeclaration>().FirstOrDefault()?
                    .CustomAttributes.Add(
                        new CodeAttributeDeclaration(
                            new CodeTypeReference(typeof(CompiledFromFileAttribute)),
                            new CodeAttributeArgument(new CodePrimitiveExpression(viewFullPath))
                            ));

                    var codeGenOptions = new CodeGeneratorOptions {
                        VerbatimOrder = true, ElseOnClosing = false, BlankLinesBetweenMembers = false
                    };
                    provider.GenerateCodeFromCompileUnit(razorOut.GeneratedCode, generatedWriter, codeGenOptions);

                    // rewind
                    generatedWriter.Flush();
                    generatedStream.Position = 0;
                }
        }