@@ -339,63 +339,60 @@ public bool IsValidFilterExpression(string filter)
339
339
return true ;
340
340
}
341
341
342
- public bool IsModuleExcluded ( string module , string [ ] excludeFilters )
342
+ public IEnumerable < string > SelectModules ( IEnumerable < string > modules , string [ ] includeFilters , string [ ] excludeFilters )
343
343
{
344
- if ( excludeFilters == null || excludeFilters . Length == 0 )
345
- return false ;
344
+ const char escapeSymbol = '!' ;
345
+ ILookup < string , string > modulesLookup = modules . Where ( x => x != null )
346
+ . ToLookup ( x => $ "{ escapeSymbol } { Path . GetFileNameWithoutExtension ( x ) } { escapeSymbol } ") ;
346
347
347
- module = Path . GetFileNameWithoutExtension ( module ) ;
348
- if ( module == null )
349
- return false ;
348
+ string moduleKeys = string . Join ( Environment . NewLine , modulesLookup . Select ( x => x . Key ) ) ;
349
+ string includedModuleKeys = GetModuleKeysForIncludeFilters ( includeFilters , escapeSymbol , moduleKeys ) ;
350
+ string excludedModuleKeys = GetModuleKeysForExcludeFilters ( excludeFilters , escapeSymbol , includedModuleKeys ) ;
350
351
351
- foreach ( string filter in excludeFilters )
352
- {
353
- #pragma warning disable IDE0057 // Use range operator
354
- string typePattern = filter . Substring ( filter . IndexOf ( ']' ) + 1 ) ;
355
-
356
- if ( typePattern != "*" )
357
- continue ;
352
+ IEnumerable < string > moduleKeysToInclude = includedModuleKeys
353
+ . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . RemoveEmptyEntries )
354
+ . Except ( excludedModuleKeys . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . RemoveEmptyEntries ) ) ;
358
355
359
- string modulePattern = filter . Substring ( 1 , filter . IndexOf ( ']' ) - 1 ) ;
360
- #pragma warning restore IDE0057 // Use range operator
361
- modulePattern = WildcardToRegex ( modulePattern ) ;
362
-
363
- var regex = new Regex ( modulePattern , s_regexOptions , TimeSpan . FromSeconds ( 10 ) ) ;
364
-
365
- if ( regex . IsMatch ( module ) )
366
- return true ;
367
- }
368
-
369
- return false ;
356
+ return moduleKeysToInclude . SelectMany ( x => modulesLookup [ x ] ) ;
370
357
}
371
358
372
- public bool IsModuleIncluded ( string module , string [ ] includeFilters )
359
+ private string GetModuleKeysForIncludeFilters ( IEnumerable < string > filters , char escapeSymbol , string moduleKeys )
373
360
{
374
- if ( includeFilters == null || includeFilters . Length == 0 )
375
- return true ;
361
+ string [ ] validFilters = GetValidFilters ( filters ) ;
376
362
377
- module = Path . GetFileNameWithoutExtension ( module ) ;
378
- if ( module == null )
379
- return false ;
363
+ return ! validFilters . Any ( ) ? moduleKeys : GetModuleKeysForValidFilters ( escapeSymbol , moduleKeys , validFilters ) ;
364
+ }
380
365
381
- foreach ( string filter in includeFilters )
382
- {
383
- #pragma warning disable IDE0057 // Use range operator
384
- string modulePattern = filter . Substring ( 1 , filter . IndexOf ( ']' ) - 1 ) ;
385
- #pragma warning restore IDE0057 // Use range operator
366
+ private string GetModuleKeysForExcludeFilters ( IEnumerable < string > filters , char escapeSymbol , string moduleKeys )
367
+ {
368
+ string [ ] validFilters = GetValidFilters ( filters ) ;
386
369
387
- if ( modulePattern == "*" )
388
- return true ;
370
+ return ! validFilters . Any ( ) ? string . Empty : GetModuleKeysForValidFilters ( escapeSymbol , moduleKeys , validFilters ) ;
371
+ }
389
372
390
- modulePattern = WildcardToRegex ( modulePattern ) ;
373
+ private static string GetModuleKeysForValidFilters ( char escapeSymbol , string moduleKeys , string [ ] validFilters )
374
+ {
375
+ string pattern = CreateRegexPattern ( validFilters , escapeSymbol ) ;
376
+ IEnumerable < Match > matches = Regex . Matches ( moduleKeys , pattern , RegexOptions . IgnoreCase ) . Cast < Match > ( ) ;
391
377
392
- var regex = new Regex ( modulePattern , s_regexOptions , TimeSpan . FromSeconds ( 10 ) ) ;
378
+ return string . Join (
379
+ Environment . NewLine ,
380
+ matches . Where ( x => x . Success ) . Select ( x => x . Groups [ 0 ] . Value ) ) ;
381
+ }
393
382
394
- if ( regex . IsMatch ( module ) )
395
- return true ;
396
- }
383
+ private string [ ] GetValidFilters ( IEnumerable < string > filters )
384
+ {
385
+ return ( filters ?? Array . Empty < string > ( ) )
386
+ . Where ( IsValidFilterExpression )
387
+ . Where ( x => x . EndsWith ( "*" ) )
388
+ . ToArray ( ) ;
389
+ }
397
390
398
- return false ;
391
+ private static string CreateRegexPattern ( IEnumerable < string > filters , char escapeSymbol )
392
+ {
393
+ IEnumerable < string > regexPatterns = filters . Select ( x =>
394
+ $ "{ escapeSymbol } { WildcardToRegex ( x . Substring ( 1 , x . IndexOf ( ']' ) - 1 ) ) . Trim ( '^' , '$' ) } { escapeSymbol } ") ;
395
+ return string . Join ( "|" , regexPatterns ) ;
399
396
}
400
397
401
398
public bool IsTypeExcluded ( string module , string type , string [ ] excludeFilters )
0 commit comments