public static async Task ProcessAsync(RunContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } context.Log.WriteLine("Rule IDs:"); foreach (var rule in context.RuleIds) { context.Log.WriteFormatted($" - {rule}"); } context.Log.WriteLine(); var counter = 0; var numSolutions = context.SolutionPaths.Length; var cache = AnalyzerCache.Create(context.RuleIds, context.LoadDir, context.LoadList); foreach (var solutionPath in context.SolutionPaths) { context.Log.WriteFormatted($"* Solution: {Path.GetFileName(solutionPath)} ({counter + 1}/{numSolutions})..."); try { using var workspace = await LoadSolutionIntoNewWorkspace(context, solutionPath, context.CancellationToken).ConfigureAwait(false); ConfigureWorkspace(workspace); var solution = workspace.CurrentSolution; var csharpProjects = solution.Projects.Where(p => p.Language == LanguageNames.CSharp).ToArray(); if (csharpProjects.Length == 0) { context.Log.WriteLine(" - No C# projects! Moving on to next solution...", LogLevel.Warning); } else { context.Log.WriteFormatted($" - Found {csharpProjects.Length} projects.", LogLevel.Info); var processor = new SolutionProcessor(context, cache, workspace); await processor.ProcessSolutionAsync().ConfigureAwait(false); } } catch (WorkspaceLoadException ex) { context.Log.WriteLine(ex.ToString(), LogLevel.Error); } catch (CodeFixException ex) { context.Log.WriteLine(ex.ToString(), LogLevel.Error); } counter++; } }
public SolutionProcessor(RunContext context, AnalyzerCache cache, Workspace workspace) { this.context = context; this.cache = cache; this.workspace = workspace; }
async Task <ImmutableDictionary <ProjectId, ImmutableArray <Diagnostic> > > GetAnalyzerDiagnosticsAsync(Solution solution, AnalyzerCache cache) { var projectDiagnosticTasks = new List <KeyValuePair <ProjectId, Task <ImmutableArray <Diagnostic> > > >(); foreach (var project in solution.Projects) { if (project.Language != LanguageNames.CSharp) { context.Log.WriteFormatted($" - Skipping {project.Name} as it is not a C# project. (Language == '{project.Language}')", LogLevel.Warning); continue; } var analyzers = cache.GetAnalyzers(project); if (analyzers.Length == 0) { context.Log.WriteLine($" - Skipping {project.Name} as it has no analyzers registered.", LogLevel.Warning); continue; } var task = GetProjectAnalyzerDiagnosticsAsync(project, analyzers); projectDiagnosticTasks.Add(new KeyValuePair <ProjectId, Task <ImmutableArray <Diagnostic> > >(project.Id, task)); } var projectDiagnosticBuilder = ImmutableDictionary.CreateBuilder <ProjectId, ImmutableArray <Diagnostic> >(); foreach (var task in projectDiagnosticTasks) { projectDiagnosticBuilder.Add(task.Key, await task.Value.ConfigureAwait(false)); } return(projectDiagnosticBuilder.ToImmutable()); }