public async Task LoadAnadAnalyzeProject(FileInfo projectFile, AnalyzerReport report) //TODO: Add async suffix { var workspace = MSBuildWorkspace.Create(); workspace.WorkspaceFailed += (o, e) => { Console.WriteLine(e.Diagnostic.Message); }; var solution = await workspace.OpenSolutionAsync(projectFile.FullName); var analyzers = this.GetAnalyzers(solution); await AnalyzeProject(solution, analyzers, report); }
public static void Main(string[] args) { // Attempt to set the version of MSBuild. VisualStudioInstance[] visualStudioInstances = MSBuildLocator.QueryVisualStudioInstances().ToArray(); VisualStudioInstance instance = visualStudioInstances.Length == 1 // If there is only one instance of MSBuild on this machine, set that as the one to use. ? visualStudioInstances[0] // Handle selecting the version of MSBuild you want to use. : (VisualStudioInstance)SelectVisualStudioInstance(visualStudioInstances); Console.WriteLine($"Using MSBuild at '{instance.MSBuildPath}' to load projects."); // NOTE: Be sure to register an instance with the MSBuildLocator // before calling MSBuildWorkspace.Create() // otherwise, MSBuildWorkspace won't MEF compose. MSBuildLocator.RegisterInstance(instance); try { if (args.Length <= 0) { return; } var startTime = DateTime.Now; var fileName = args[0]; var fileInfo = new FileInfo(fileName); //NOTE: This could be configurable via the CLI at some point var report = new AnalyzerReport(); report.AddExporter(new ConsoleAnalyzerExporter()); report.AddExporter(new JsonAnalyzerExporter()); report.InitializeReport(fileInfo); var tasks = new List<Task>(); if (fileInfo.Exists) { var solutionAnalyzer = new SolutionAnalyzer(); var analyzeTask = solutionAnalyzer.LoadAnadAnalyzeProject(fileInfo, report); tasks.Add(analyzeTask); } Task.WaitAll(tasks.ToArray()); var endTime = DateTime.Now; var duration = endTime - startTime; report.FinalizeReport(duration); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } catch (Exception generalException) { Console.WriteLine("There was an exception running the analysis"); Console.WriteLine(generalException.ToString()); } }
private async Task AnalyzeProject(Solution solution, ImmutableArray <DiagnosticAnalyzer> analyzers, AnalyzerReport report) { foreach (Project project in solution.Projects) { var compilation = await project.GetCompilationAsync(); var diagnosticResults = await compilation.WithAnalyzers(analyzers).GetAnalyzerDiagnosticsAsync(); report.AppendDiagnostics(diagnosticResults); } }