Beispiel #1
0
        private static (AnalyzerRunner, CodeFixRunner) LoadAnalyzers(Config config)
        {
            var analyzerRunner = new AnalyzerRunner();
            var codeFixRunner  = new CodeFixRunner();

            foreach (var x in config.Analyzers)
            {
                var asm = Assembly.LoadFrom(x);
                analyzerRunner.RegisterAnalyzersFromAssembly(asm);
                codeFixRunner.RegisterCodeFixProvidersFromAssembly(asm);
            }

            return(analyzerRunner, codeFixRunner);
        }
Beispiel #2
0
        private static Task <ProjectAnalysisResult[]> RunAnalysises(Config config, AnalyzerRunner analyzerRunner, CodeFixRunner codeFixRunner)
        {
            var workspace = MSBuildWorkspace.Create(config.BuildProperties);

            workspace.WorkspaceFailed += (sender, e) =>
            {
                Console.ForegroundColor = e.Diagnostic.Kind == WorkspaceDiagnosticKind.Failure
                    ? ConsoleColor.Red
                    : ConsoleColor.DarkYellow;
                Console.Error.WriteLine(e.Diagnostic);
                Console.ResetColor();
            };

            return(Task.WhenAll(
                       config.Projects
                       .Select(x => Task.Run(async() =>
            {
                try
                {
                    var project = await workspace.OpenProjectAsync(x).ConfigureAwait(false);
                    var compilation = await project.GetCompilationAsync().ConfigureAwait(false);
                    var analysisResult = await analyzerRunner.RunAnalyzersAsync(compilation).ConfigureAwait(false);
                    var diagnostics = analysisResult.GetAllDiagnostics();
                    var codeFixes = await codeFixRunner.RunCodeFixesAsync(project, diagnostics).ConfigureAwait(false);
                    return new ProjectAnalysisResult(project, diagnostics, analysisResult.AnalyzerTelemetryInfo, codeFixes);
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Error.WriteLine("Failed to load " + Path.GetFileName(x) + ": " + ex.ToString());
                    Console.ResetColor();
                    return null;
                }
            }))
                       ));
        }