Skip to content

Commit aa0acfd

Browse files
committed
Add /apply option to write /fixall changes back to disk
1 parent 64533e1 commit aa0acfd

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

StyleCop.Analyzers/StyleCopTester/Program.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ private static async Task MainAsync(string[] args, CancellationToken cancellatio
6969
}
7070
else
7171
{
72+
bool applyChanges = args.Contains("/apply");
73+
if (applyChanges)
74+
{
75+
if (!args.Contains("/fixall"))
76+
{
77+
Console.Error.WriteLine("Error: /apply can only be used with /fixall");
78+
return;
79+
}
80+
}
81+
7282
Stopwatch stopwatch = Stopwatch.StartNew();
7383
var analyzers = GetAllAnalyzers();
7484

@@ -137,7 +147,7 @@ private static async Task MainAsync(string[] args, CancellationToken cancellatio
137147

138148
if (args.Contains("/fixall"))
139149
{
140-
await TestFixAllAsync(stopwatch, solution, diagnostics, cancellationToken).ConfigureAwait(true);
150+
await TestFixAllAsync(stopwatch, solution, diagnostics, applyChanges, cancellationToken).ConfigureAwait(true);
141151
}
142152
}
143153
}
@@ -174,7 +184,7 @@ private static void WriteDiagnosticResults(ImmutableArray<Tuple<ProjectId, Diagn
174184
File.WriteAllText(uniqueFileName, uniqueOutput.ToString(), Encoding.UTF8);
175185
}
176186

177-
private static async Task TestFixAllAsync(Stopwatch stopwatch, Solution solution, ImmutableDictionary<ProjectId, ImmutableArray<Diagnostic>> diagnostics, CancellationToken cancellationToken)
187+
private static async Task TestFixAllAsync(Stopwatch stopwatch, Solution solution, ImmutableDictionary<ProjectId, ImmutableArray<Diagnostic>> diagnostics, bool applyChanges, CancellationToken cancellationToken)
178188
{
179189
Console.WriteLine("Calculating fixes");
180190

@@ -188,6 +198,11 @@ private static async Task TestFixAllAsync(Stopwatch stopwatch, Solution solution
188198
}
189199

190200
Console.WriteLine($"Found {equivalenceGroups.Count} equivalence groups.");
201+
if (applyChanges && equivalenceGroups.Count > 1)
202+
{
203+
Console.Error.WriteLine("/apply can only be used with a single equivalence group.");
204+
return;
205+
}
191206

192207
Console.WriteLine("Calculating changes");
193208

@@ -197,7 +212,24 @@ private static async Task TestFixAllAsync(Stopwatch stopwatch, Solution solution
197212
{
198213
stopwatch.Restart();
199214
Console.WriteLine($"Calculating fix for {fix.CodeFixEquivalenceKey} using {fix.FixAllProvider} for {fix.NumberOfDiagnostics} instances.");
200-
await fix.GetOperationsAsync(cancellationToken).ConfigureAwait(true);
215+
var operations = await fix.GetOperationsAsync(cancellationToken).ConfigureAwait(true);
216+
if (applyChanges)
217+
{
218+
var applyOperations = operations.OfType<ApplyChangesOperation>().ToList();
219+
if (applyOperations.Count > 1)
220+
{
221+
Console.Error.WriteLine("/apply can only apply a single code action operation.");
222+
}
223+
else if (applyOperations.Count == 0)
224+
{
225+
Console.WriteLine("No changes were found to apply.");
226+
}
227+
else
228+
{
229+
applyOperations[0].Apply(solution.Workspace, cancellationToken);
230+
}
231+
}
232+
201233
WriteLine($"Calculating changes completed in {stopwatch.ElapsedMilliseconds}ms. This is {fix.NumberOfDiagnostics / stopwatch.Elapsed.TotalSeconds:0.000} instances/second.", ConsoleColor.Yellow);
202234
}
203235
catch (Exception ex)
@@ -458,6 +490,7 @@ private static void PrintHelp()
458490
Console.WriteLine("/codefixes Test single code fixes");
459491
Console.WriteLine("/fixall Test fix all providers");
460492
Console.WriteLine("/id:<id> Enable analyzer with diagnostic ID < id > (when this is specified, only this analyzer is enabled)");
493+
Console.WriteLine("/apply Write code fix changes back to disk");
461494
Console.WriteLine("/force Force an analyzer to be enabled, regardless of the configured rule set(s) for the solution");
462495
}
463496
}

0 commit comments

Comments
 (0)