public void Migrate(BreakingChange breakingChange) { var stopwatch = Stopwatch.StartNew(); var project = GetOrLoadProject(); var documentDiagnosticsMap = ComputeDiagnostics(project, breakingChange); Console.WriteLine($"{breakingChange.EquivalenceKey} - Analysis done in {stopwatch.ElapsedMilliseconds}ms"); Console.WriteLine($"{breakingChange.EquivalenceKey} - Found {documentDiagnosticsMap.Sum(x => x.Value.Count())} counts of diagnostics to fix"); if (documentDiagnosticsMap.Any()) { ApplyFixes(documentDiagnosticsMap, breakingChange); } Console.WriteLine($"{breakingChange.EquivalenceKey} - Finished migration in {stopwatch.ElapsedMilliseconds}ms (analysis and fix)"); Console.WriteLine(); }
private void ApplyFixes(Dictionary <Document, List <Diagnostic> > documentDiagnosticsMap, BreakingChange breakingChange) { if (!documentDiagnosticsMap.Any()) { return; } var cancellationToken = new CancellationToken(); var diagnosticProvider = new CustomDiagnosticProvider(documentDiagnosticsMap); var fixAllContext = new FixAllContext(documentDiagnosticsMap.First().Key, breakingChange.CodeFix, FixAllScope.Project, breakingChange.EquivalenceKey, breakingChange.DiagnosticIds, diagnosticProvider, cancellationToken); var fixes = WellKnownFixAllProviders.BatchFixer.GetFixAsync(fixAllContext).Result; var operations = fixes.GetOperationsAsync(cancellationToken).Result; foreach (var operation in operations) { operation.Apply(_workspace, cancellationToken); } }
private Dictionary <Document, List <Diagnostic> > ComputeDiagnostics(Project project, BreakingChange breakingChange) { var documentDiagnosticsMap = new Dictionary <Document, List <Diagnostic> >(); var compilation = project.GetCompilationAsync().Result; var diagnosticResults = compilation.WithAnalyzers(ImmutableArray.Create(breakingChange.Analyzer)).GetAnalyzerDiagnosticsAsync().Result; var interestingResults = diagnosticResults.Where(x => x.Severity != DiagnosticSeverity.Hidden).ToArray(); foreach (var diagnostic in interestingResults) { if (diagnostic.Severity != DiagnosticSeverity.Hidden) { var doc = project.GetDocument(diagnostic.Location.SourceTree); if (!documentDiagnosticsMap.ContainsKey(doc)) { documentDiagnosticsMap[doc] = new List <Diagnostic>(); } documentDiagnosticsMap[doc].Add(diagnostic); } } return(documentDiagnosticsMap); }