Пример #1
0
        private async Task <IReadOnlyList <CompileToCSharpResult> > CompileToCSharpAsync(
            ICollection <CodeFile> codeFiles,
            Func <string, Task> updateStatusFunc)
        {
            // The first phase won't include any metadata references for component discovery. This mirrors what the build does.
            var projectEngine = this.CreateRazorProjectEngine(Array.Empty <MetadataReference>());

            // Result of generating declarations
            var declarations = new CompileToCSharpResult[codeFiles.Count];
            var index        = 0;

            foreach (var codeFile in codeFiles)
            {
                if (codeFile.Type == CodeFileType.Razor)
                {
                    var projectItem = CreateRazorProjectItem(codeFile.Path, MudBlazorServices + codeFile.Content);

                    var codeDocument   = projectEngine.ProcessDeclarationOnly(projectItem);
                    var cSharpDocument = codeDocument.GetCSharpDocument();

                    declarations[index] = new CompileToCSharpResult
                    {
                        FilePath    = codeFile.Path,
                        ProjectItem = projectItem,
                        Code        = cSharpDocument.GeneratedCode,
                        Diagnostics = cSharpDocument.Diagnostics.Select(CompilationDiagnostic.FromRazorDiagnostic).ToList(),
                    };
                }
                else
                {
                    declarations[index] = new CompileToCSharpResult
                    {
                        FilePath    = codeFile.Path,
                        Code        = codeFile.Content,
                        Diagnostics = Enumerable.Empty <CompilationDiagnostic>(), // Will actually be evaluated later
                    };
                }

                index++;
            }

            // Result of doing 'temp' compilation
            var tempAssembly = CompileToAssembly(declarations);

            if (tempAssembly.Diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error))
            {
                return(new[] { new CompileToCSharpResult {
                                   Diagnostics = tempAssembly.Diagnostics
                               } });
            }

            // Add the 'temp' compilation as a metadata reference
            var references = new List <MetadataReference>(baseCompilation.References)
            {
                tempAssembly.Compilation.ToMetadataReference()
            };

            projectEngine = this.CreateRazorProjectEngine(references);

            await(updateStatusFunc?.Invoke("Preparing Project") ?? Task.CompletedTask);

            var results = new CompileToCSharpResult[declarations.Length];

            for (index = 0; index < declarations.Length; index++)
            {
                var declaration        = declarations[index];
                var isRazorDeclaration = declaration.ProjectItem != null;

                if (isRazorDeclaration)
                {
                    var codeDocument   = projectEngine.Process(declaration.ProjectItem);
                    var cSharpDocument = codeDocument.GetCSharpDocument();

                    results[index] = new CompileToCSharpResult
                    {
                        FilePath    = declaration.FilePath,
                        ProjectItem = declaration.ProjectItem,
                        Code        = cSharpDocument.GeneratedCode,
                        Diagnostics = cSharpDocument.Diagnostics.Select(CompilationDiagnostic.FromRazorDiagnostic).ToList(),
                    };
                }
                else
                {
                    results[index] = declaration;
                }
            }

            return(results);
        }