@@ -1452,40 +1452,44 @@ public Dictionary<string, List<string>> CheckRuleExtension(string[] path, PathIn
1452
1452
/// </summary>
1453
1453
/// <param name="path">The path of the file or directory to analyze.</param>
1454
1454
/// <param name="searchRecursively">
1455
- /// <param name="fix">
1456
1455
/// If true, recursively searches the given file path and analyzes any
1457
1456
/// script files that are found.
1458
1457
/// </param>
1459
1458
/// <returns>An enumeration of DiagnosticRecords that were found by rules.</returns>
1460
- public IEnumerable < DiagnosticRecord > AnalyzePath ( string path , bool searchRecursively = false , bool fix = false )
1459
+ public IEnumerable < DiagnosticRecord > AnalyzePath ( string path , bool searchRecursively = false )
1461
1460
{
1462
- List < string > scriptFilePaths = new List < string > ( ) ;
1461
+ List < string > scriptFilePaths = ScriptPathList ( path , searchRecursively ) ;
1463
1462
1464
- if ( path == null )
1463
+ foreach ( string scriptFilePath in scriptFilePaths )
1465
1464
{
1466
- this . outputWriter . ThrowTerminatingError (
1467
- new ErrorRecord (
1468
- new FileNotFoundException ( ) ,
1469
- string . Format ( CultureInfo . CurrentCulture , Strings . FileNotFound , path ) ,
1470
- ErrorCategory . InvalidArgument ,
1471
- this ) ) ;
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 ) )
1467
+ {
1468
+ yield return diagnosticRecord ;
1469
+ }
1472
1470
}
1471
+ }
1472
+
1473
+ /// <summary>
1474
+ /// Analyzes a script file or a directory containing script files and fixes warning where possible.
1475
+ /// </summary>
1476
+ /// <param name="path">The path of the file or directory to analyze.</param>
1477
+ /// <param name="searchRecursively">
1478
+ /// If true, recursively searches the given file path and analyzes any
1479
+ /// script files that are found.
1480
+ /// </param>
1481
+ /// <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 )
1483
+ {
1484
+ List < string > scriptFilePaths = ScriptPathList ( path , searchRecursively ) ;
1473
1485
1474
- // Create in advance the list of script file paths to analyze. This
1475
- // is an optimization over doing the whole operation at once
1476
- // and calling .Concat on IEnumerables to join results.
1477
- this . BuildScriptPathList ( path , searchRecursively , scriptFilePaths ) ;
1478
1486
foreach ( string scriptFilePath in scriptFilePaths )
1479
1487
{
1480
- if ( fix )
1481
- {
1482
- var fileEncoding = GetFileEncoding ( scriptFilePath ) ;
1483
- var scriptFileContentWithFixes = Fix ( File . ReadAllText ( scriptFilePath , fileEncoding ) ) ;
1484
- File . WriteAllText ( scriptFilePath , scriptFileContentWithFixes , fileEncoding ) ; // although this preserves the encoding, it will add a BOM to UTF8 files
1485
- }
1488
+ var fileEncoding = GetFileEncoding ( scriptFilePath ) ;
1489
+ var scriptFileContentWithFixes = Fix ( File . ReadAllText ( scriptFilePath , fileEncoding ) ) ;
1490
+ File . WriteAllText ( scriptFilePath , scriptFileContentWithFixes , fileEncoding ) ;
1486
1491
1487
- // Yield each record in the result so that the
1488
- // caller can pull them one at a time
1492
+ // Yield each record in the result so that the caller can pull them one at a time
1489
1493
foreach ( var diagnosticRecord in this . AnalyzeFile ( scriptFilePath ) )
1490
1494
{
1491
1495
yield return diagnosticRecord ;
@@ -1676,6 +1680,28 @@ private static EditableText Fix(
1676
1680
return text ;
1677
1681
}
1678
1682
1683
+ private List < string > ScriptPathList ( string path , bool searchRecursively )
1684
+ {
1685
+ List < string > scriptFilePaths = new List < string > ( ) ;
1686
+
1687
+ if ( path == null )
1688
+ {
1689
+ this . outputWriter . ThrowTerminatingError (
1690
+ new ErrorRecord (
1691
+ new FileNotFoundException ( ) ,
1692
+ string . Format ( CultureInfo . CurrentCulture , Strings . FileNotFound , path ) ,
1693
+ ErrorCategory . InvalidArgument ,
1694
+ this ) ) ;
1695
+ }
1696
+
1697
+ // Create in advance the list of script file paths to analyze. This
1698
+ // is an optimization over doing the whole operation at once
1699
+ // and calling .Concat on IEnumerables to join results.
1700
+ this . BuildScriptPathList ( path , searchRecursively , scriptFilePaths ) ;
1701
+
1702
+ return scriptFilePaths ;
1703
+ }
1704
+
1679
1705
private void BuildScriptPathList (
1680
1706
string path ,
1681
1707
bool searchRecursively ,
0 commit comments