Exemplo n.º 1
0
        public void ClearTrees() =>
        WithCompilation(c =>
        {
            Logger.LogInformation("Clearing syntax trees");

            RawTrees.Clear();
            return(c.RemoveAllSyntaxTrees());
        });
Exemplo n.º 2
0
        public EmittedAssembly StageFile(ChangedFile file, bool silent = false)
        {
            var sw = Stopwatch.StartNew();

            var syntaxTree =
                CSharpSyntaxTree.ParseText(
                    file.Contents,
                    CSharpParseOptions.Default
                    .WithLanguageVersion(LanguageVersion.Preview)
                    .WithKind(SourceCodeKind.Regular)
                    .WithPreprocessorSymbols(_options.PreprocessorSymbols.ToArray()),
                    path: file.Path,
                    Encoding.Default);

            EmittedAssembly emittedAssembly = null;

            WithCompilation(c =>
            {
                var newC = RawTrees.TryGetValue(file.Path, out var oldSyntaxTree)
                    ? c.ReplaceSyntaxTree(oldSyntaxTree, syntaxTree)
                    : c.AddSyntaxTrees(syntaxTree);

                RawTrees[file.Path] = syntaxTree;

                if (silent)
                {
                    Logger.LogInformation(
                        "Stage '{FileName}' without emit, Duration: {Duration:N0}ms, Types: [ {Types} ]",
                        file, sw.ElapsedMilliseconds, syntaxTree.GetContainedTypes());

                    return(newC);
                }

                var result = EmitAssembly(newC, out emittedAssembly);

                if (!String.IsNullOrWhiteSpace(_options.WriteAssembliesPath))
                {
                    WriteEmittedAssembly(emittedAssembly);
                }

                var elapsed = sw.ElapsedMilliseconds;

                Logger.LogInformationAsync(
                    "Stage '{FileName}' and emit - Success: {Success}, Duration: {Duration:N0}ms, Types: [ {Types} ], Diagnostics: {@Diagnostics}",
                    Path.GetFileName(file.Path), result.Success, elapsed, syntaxTree.GetContainedTypes(),
                    result.Success
                        ? ""
                        : String.Join(
                        Environment.NewLine,
                        result.Diagnostics
                        .Where(x => x.Severity == DiagnosticSeverity.Error)
                        .Select(x => x.GetMessage())));

                return(newC);
            });

            return(emittedAssembly);
        }