10
10
// THE SOFTWARE.
11
11
//
12
12
13
+ using System . Text . RegularExpressions ;
13
14
using Microsoft . Windows . Powershell . ScriptAnalyzer . Generic ;
14
15
using System ;
15
16
using System . Collections . Generic ;
@@ -272,7 +273,28 @@ private void AnalyzeFile(string filePath)
272
273
IEnumerable < Ast > funcDefAsts ;
273
274
274
275
// Use a List of KVP rather than dictionary, since for a script containing inline functions with same signature, keys clash
275
- List < KeyValuePair < CommandInfo , IScriptExtent > > cmdInfoTable = new List < KeyValuePair < CommandInfo , IScriptExtent > > ( ) ;
276
+ List < KeyValuePair < CommandInfo , IScriptExtent > > cmdInfoTable = new List < KeyValuePair < CommandInfo , IScriptExtent > > ( ) ;
277
+
278
+ //Check wild card input for the Include/ExcludeRules and create regex match patterns
279
+ List < Regex > includeRegexList = new List < Regex > ( ) ;
280
+ List < Regex > excludeRegexList = new List < Regex > ( ) ;
281
+ if ( includeRule != null )
282
+ {
283
+ foreach ( string rule in includeRule )
284
+ {
285
+ Regex includeRegex = new Regex ( String . Format ( "^{0}$" , Regex . Escape ( rule ) . Replace ( @"\*" , ".*" ) ) , RegexOptions . IgnoreCase ) ;
286
+ includeRegexList . Add ( includeRegex ) ;
287
+ }
288
+ }
289
+ if ( excludeRule != null )
290
+ {
291
+ foreach ( string rule in excludeRule )
292
+ {
293
+ Regex excludeRegex = new Regex ( String . Format ( "^{0}$" , Regex . Escape ( rule ) . Replace ( @"\*" , ".*" ) ) , RegexOptions . IgnoreCase ) ;
294
+ excludeRegexList . Add ( excludeRegex ) ;
295
+ }
296
+ }
297
+
276
298
277
299
//Parse the file
278
300
if ( File . Exists ( filePath ) )
@@ -316,12 +338,30 @@ private void AnalyzeFile(string filePath)
316
338
#region Run ScriptRules
317
339
//Trim down to the leaf element of the filePath and pass it to Diagnostic Record
318
340
string fileName = System . IO . Path . GetFileName ( filePath ) ;
341
+
319
342
if ( ScriptAnalyzer . Instance . ScriptRules != null )
320
343
{
321
344
foreach ( IScriptRule scriptRule in ScriptAnalyzer . Instance . ScriptRules )
322
345
{
323
- if ( ( includeRule == null || includeRule . Contains ( scriptRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) &&
324
- ( excludeRule == null || ! excludeRule . Contains ( scriptRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) )
346
+ bool includeRegexMatch = false ;
347
+ bool excludeRegexMatch = false ;
348
+ foreach ( Regex include in includeRegexList )
349
+ {
350
+ if ( include . IsMatch ( scriptRule . GetName ( ) ) )
351
+ {
352
+ includeRegexMatch = true ;
353
+ break ;
354
+ }
355
+ }
356
+ foreach ( Regex exclude in excludeRegexList )
357
+ {
358
+ if ( exclude . IsMatch ( scriptRule . GetName ( ) ) )
359
+ {
360
+ excludeRegexMatch = true ;
361
+ break ;
362
+ }
363
+ }
364
+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
325
365
{
326
366
WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , scriptRule . GetName ( ) ) ) ;
327
367
@@ -334,7 +374,6 @@ private void AnalyzeFile(string filePath)
334
374
catch ( Exception scriptRuleException )
335
375
{
336
376
WriteError ( new ErrorRecord ( scriptRuleException , Strings . RuleError , ErrorCategory . InvalidOperation , filePath ) ) ;
337
- continue ;
338
377
}
339
378
}
340
379
}
@@ -379,8 +418,25 @@ private void AnalyzeFile(string filePath)
379
418
{
380
419
foreach ( ICommandRule commandRule in ScriptAnalyzer . Instance . CommandRules )
381
420
{
382
- if ( ( includeRule == null || includeRule . Contains ( commandRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) &&
383
- ( excludeRule == null || ! excludeRule . Contains ( commandRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) )
421
+ bool includeRegexMatch = false ;
422
+ bool excludeRegexMatch = false ;
423
+ foreach ( Regex include in includeRegexList )
424
+ {
425
+ if ( include . IsMatch ( commandRule . GetName ( ) ) )
426
+ {
427
+ includeRegexMatch = true ;
428
+ break ;
429
+ }
430
+ }
431
+ foreach ( Regex exclude in excludeRegexList )
432
+ {
433
+ if ( exclude . IsMatch ( commandRule . GetName ( ) ) )
434
+ {
435
+ excludeRegexMatch = true ;
436
+ break ;
437
+ }
438
+ }
439
+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
384
440
{
385
441
foreach ( KeyValuePair < CommandInfo , IScriptExtent > commandInfo in cmdInfoTable )
386
442
{
@@ -395,7 +451,6 @@ private void AnalyzeFile(string filePath)
395
451
catch ( Exception commandRuleException )
396
452
{
397
453
WriteError ( new ErrorRecord ( commandRuleException , Strings . RuleError , ErrorCategory . InvalidOperation , fileName ) ) ;
398
- continue ;
399
454
}
400
455
}
401
456
}
@@ -410,8 +465,25 @@ private void AnalyzeFile(string filePath)
410
465
{
411
466
foreach ( ITokenRule tokenRule in ScriptAnalyzer . Instance . TokenRules )
412
467
{
413
- if ( ( includeRule == null || includeRule . Contains ( tokenRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) &&
414
- ( excludeRule == null || ! excludeRule . Contains ( tokenRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) )
468
+ bool includeRegexMatch = false ;
469
+ bool excludeRegexMatch = false ;
470
+ foreach ( Regex include in includeRegexList )
471
+ {
472
+ if ( include . IsMatch ( tokenRule . GetName ( ) ) )
473
+ {
474
+ includeRegexMatch = true ;
475
+ break ;
476
+ }
477
+ }
478
+ foreach ( Regex exclude in excludeRegexList )
479
+ {
480
+ if ( exclude . IsMatch ( tokenRule . GetName ( ) ) )
481
+ {
482
+ excludeRegexMatch = true ;
483
+ break ;
484
+ }
485
+ }
486
+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
415
487
{
416
488
WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , tokenRule . GetName ( ) ) ) ;
417
489
@@ -424,7 +496,6 @@ private void AnalyzeFile(string filePath)
424
496
catch ( Exception tokenRuleException )
425
497
{
426
498
WriteError ( new ErrorRecord ( tokenRuleException , Strings . RuleError , ErrorCategory . InvalidOperation , fileName ) ) ;
427
- continue ;
428
499
}
429
500
}
430
501
}
@@ -438,8 +509,25 @@ private void AnalyzeFile(string filePath)
438
509
// Run DSC Class rule
439
510
foreach ( IDSCResourceRule dscResourceRule in ScriptAnalyzer . Instance . DSCResourceRules )
440
511
{
441
- if ( ( includeRule == null || includeRule . Contains ( dscResourceRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) &&
442
- ( excludeRule == null || ! excludeRule . Contains ( dscResourceRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) )
512
+ bool includeRegexMatch = false ;
513
+ bool excludeRegexMatch = false ;
514
+ foreach ( Regex include in includeRegexList )
515
+ {
516
+ if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
517
+ {
518
+ includeRegexMatch = true ;
519
+ break ;
520
+ }
521
+ }
522
+ foreach ( Regex exclude in excludeRegexList )
523
+ {
524
+ if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
525
+ {
526
+ excludeRegexMatch = true ;
527
+ break ;
528
+ }
529
+ }
530
+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || excludeRegexMatch ) )
443
531
{
444
532
WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
445
533
@@ -452,7 +540,6 @@ private void AnalyzeFile(string filePath)
452
540
catch ( Exception dscResourceRuleException )
453
541
{
454
542
WriteError ( new ErrorRecord ( dscResourceRuleException , Strings . RuleError , ErrorCategory . InvalidOperation , filePath ) ) ;
455
- continue ;
456
543
}
457
544
}
458
545
}
@@ -480,8 +567,24 @@ private void AnalyzeFile(string filePath)
480
567
// Run all DSC Rules
481
568
foreach ( IDSCResourceRule dscResourceRule in ScriptAnalyzer . Instance . DSCResourceRules )
482
569
{
483
- if ( ( includeRule == null || includeRule . Contains ( dscResourceRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) &&
484
- ( excludeRule == null || ! excludeRule . Contains ( dscResourceRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) )
570
+ bool includeRegexMatch = false ;
571
+ bool excludeRegexMatch = false ;
572
+ foreach ( Regex include in includeRegexList )
573
+ {
574
+ if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
575
+ {
576
+ includeRegexMatch = true ;
577
+ break ;
578
+ }
579
+ }
580
+ foreach ( Regex exclude in excludeRegexList )
581
+ {
582
+ if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
583
+ {
584
+ excludeRegexMatch = true ;
585
+ }
586
+ }
587
+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
485
588
{
486
589
WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
487
590
@@ -494,7 +597,6 @@ private void AnalyzeFile(string filePath)
494
597
catch ( Exception dscResourceRuleException )
495
598
{
496
599
WriteError ( new ErrorRecord ( dscResourceRuleException , Strings . RuleError , ErrorCategory . InvalidOperation , filePath ) ) ;
497
- continue ;
498
600
}
499
601
}
500
602
}
0 commit comments