Skip to content

Commit bfa1c54

Browse files
committed
Implement SupportShouldProcess for InvokeScriptAnalyzerCommand down to a per process path and per file basis.
Propagating it further down would not make sense because then it would need to analyze all files, in this case SupportShouldContinue could be implemented in the future.
1 parent f129846 commit bfa1c54

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
3838
[Cmdlet(VerbsLifecycle.Invoke,
3939
"ScriptAnalyzer",
4040
DefaultParameterSetName = "File",
41+
SupportsShouldProcess = true,
4142
HelpUri = "http://go.microsoft.com/fwlink/?LinkId=525914")]
4243
public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
4344
{
@@ -390,11 +391,13 @@ private void ProcessInput()
390391
{
391392
if (fix)
392393
{
393-
diagnosticsList = ScriptAnalyzer.Instance.AnalyzeAndFixPath(p, this.recurse);
394+
ShouldProcess(p, $"Analyzing and fixing path with Recurse={this.recurse}");
395+
diagnosticsList = ScriptAnalyzer.Instance.AnalyzeAndFixPath(p, this.ShouldProcess, this.recurse);
394396
}
395397
else
396398
{
397-
diagnosticsList = ScriptAnalyzer.Instance.AnalyzePath(p, this.recurse);
399+
ShouldProcess(p, $"Analyzing path with Recurse={this.recurse}");
400+
diagnosticsList = ScriptAnalyzer.Instance.AnalyzePath(p, this.ShouldProcess, this.recurse);
398401
}
399402
WriteToOutput(diagnosticsList);
400403
}

Engine/ScriptAnalyzer.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,28 +1444,32 @@ public Dictionary<string, List<string>> CheckRuleExtension(string[] path, PathIn
14441444
return results;
14451445
}
14461446

1447-
#endregion
1447+
#endregion
14481448

14491449

14501450
/// <summary>
14511451
/// Analyzes a script file or a directory containing script files.
14521452
/// </summary>
14531453
/// <param name="path">The path of the file or directory to analyze.</param>
1454+
/// <param name="shouldProcess">Whether the action should be executed.</param>
14541455
/// <param name="searchRecursively">
14551456
/// If true, recursively searches the given file path and analyzes any
14561457
/// script files that are found.
14571458
/// </param>
14581459
/// <returns>An enumeration of DiagnosticRecords that were found by rules.</returns>
1459-
public IEnumerable<DiagnosticRecord> AnalyzePath(string path, bool searchRecursively = false)
1460+
public IEnumerable<DiagnosticRecord> AnalyzePath(string path, Func<string, string, bool> shouldProcess, bool searchRecursively = false)
14601461
{
14611462
List<string> scriptFilePaths = ScriptPathList(path, searchRecursively);
14621463

14631464
foreach (string scriptFilePath in scriptFilePaths)
14641465
{
1465-
// Yield each record in the result so that the caller can pull them one at a time
1466-
foreach (var diagnosticRecord in this.AnalyzeFile(scriptFilePath))
1466+
if (shouldProcess(scriptFilePath, $"Analyzing file {scriptFilePath}"))
14671467
{
1468-
yield return diagnosticRecord;
1468+
// Yield each record in the result so that the caller can pull them one at a time
1469+
foreach (var diagnosticRecord in this.AnalyzeFile(scriptFilePath))
1470+
{
1471+
yield return diagnosticRecord;
1472+
}
14691473
}
14701474
}
14711475
}
@@ -1474,25 +1478,29 @@ public IEnumerable<DiagnosticRecord> AnalyzePath(string path, bool searchRecursi
14741478
/// Analyzes a script file or a directory containing script files and fixes warning where possible.
14751479
/// </summary>
14761480
/// <param name="path">The path of the file or directory to analyze.</param>
1481+
/// <param name="shouldProcess">Whether the action should be executed.</param>
14771482
/// <param name="searchRecursively">
14781483
/// If true, recursively searches the given file path and analyzes any
14791484
/// script files that are found.
14801485
/// </param>
14811486
/// <returns>An enumeration of DiagnosticRecords that were found by rules and could not be fixed automatically.</returns>
1482-
public IEnumerable<DiagnosticRecord> AnalyzeAndFixPath(string path, bool searchRecursively = false)
1487+
public IEnumerable<DiagnosticRecord> AnalyzeAndFixPath(string path, Func<string, string, bool> shouldProcess, bool searchRecursively = false)
14831488
{
14841489
List<string> scriptFilePaths = ScriptPathList(path, searchRecursively);
14851490

14861491
foreach (string scriptFilePath in scriptFilePaths)
14871492
{
1488-
var fileEncoding = GetFileEncoding(scriptFilePath);
1489-
var scriptFileContentWithFixes = Fix(File.ReadAllText(scriptFilePath, fileEncoding));
1490-
File.WriteAllText(scriptFilePath, scriptFileContentWithFixes, fileEncoding);
1491-
1492-
// Yield each record in the result so that the caller can pull them one at a time
1493-
foreach (var diagnosticRecord in this.AnalyzeFile(scriptFilePath))
1493+
if (shouldProcess(scriptFilePath, $"Analyzing and fixing file {scriptFilePath}"))
14941494
{
1495-
yield return diagnosticRecord;
1495+
var fileEncoding = GetFileEncoding(scriptFilePath);
1496+
var scriptFileContentWithFixes = Fix(File.ReadAllText(scriptFilePath, fileEncoding));
1497+
File.WriteAllText(scriptFilePath, scriptFileContentWithFixes, fileEncoding);
1498+
1499+
// Yield each record in the result so that the caller can pull them one at a time
1500+
foreach (var diagnosticRecord in this.AnalyzeFile(scriptFilePath))
1501+
{
1502+
yield return diagnosticRecord;
1503+
}
14961504
}
14971505
}
14981506
}

0 commit comments

Comments
 (0)