@@ -17,16 +17,74 @@ public interface INugetDependenciesGenerator
1717public class TreeGeneratorConfiguration
1818{
1919 public string SlnFilePath { get ; }
20- public string [ ] PackagePrefixes { get ; init ; }
2120 public PackageFilterMode Mode { get ; init ; }
2221 public bool IncludeDependentProjects { get ; init ; }
22+
23+ private readonly List < Func < string , bool > > _filters ;
24+ private const char Mask = '*' ;
2325
24- public TreeGeneratorConfiguration ( string slnFilePath )
26+ public TreeGeneratorConfiguration ( string slnFilePath , string [ ] packageFilters )
2527 {
28+ _filters = new List < Func < string , bool > > ( ) ;
2629 SlnFilePath = slnFilePath ;
27- PackagePrefixes = Array . Empty < string > ( ) ;
2830 Mode = PackageFilterMode . None ;
2931 IncludeDependentProjects = false ;
32+ BuildFilters ( packageFilters ) ;
33+ }
34+
35+ public bool IsPackageEnabled ( string packageName )
36+ {
37+ if ( Mode == PackageFilterMode . None || _filters . Count == 0 )
38+ return true ;
39+
40+ var isMatched = _filters . Any ( f => f ( packageName ) ) ;
41+ var shouldBeProcessed = Mode == PackageFilterMode . Include ? isMatched : ! isMatched ;
42+
43+ return shouldBeProcessed ;
44+ }
45+
46+ private void BuildFilters ( string [ ] packagePrefixes )
47+ {
48+ foreach ( var filter in packagePrefixes . Where ( p => ! string . IsNullOrWhiteSpace ( p ) ) . Select ( p => p . Trim ( ) ) )
49+ {
50+ var split = filter . Split ( Mask ) ;
51+ if ( split . Length == 1 ) // filter
52+ {
53+ _filters . Add ( s => split [ 0 ] . Equals ( s ) ) ;
54+ return ;
55+ }
56+
57+ if ( split . Length == 2 )
58+ {
59+ if ( split . First ( ) != string . Empty && split . Last ( ) != string . Empty ) // fil*ter
60+ {
61+ _filters . Add ( s => s . StartsWith ( split . First ( ) ) && filter . EndsWith ( split . Last ( ) ) ) ;
62+
63+ }
64+ else if ( split . First ( ) == string . Empty && split . Last ( ) == string . Empty ) // *
65+ {
66+ throw new ArgumentException ( $ "Unknown filter specified: { filter } ") ;
67+ }
68+ else if ( split . First ( ) == string . Empty ) // *filter
69+ {
70+ _filters . Add ( s => s . EndsWith ( split . Last ( ) ) ) ;
71+ }
72+ else // filter*
73+ {
74+ _filters . Add ( s => s . StartsWith ( split . First ( ) ) ) ;
75+ }
76+
77+ return ;
78+ }
79+
80+ if ( split . Length == 3 && split . First ( ) == string . Empty && split . Last ( ) == string . Empty )
81+ {
82+ _filters . Add ( s => s . Contains ( split [ 1 ] ) ) ;
83+ return ;
84+ }
85+
86+ throw new ArgumentException ( $ "Unknown filter specified: { filter } ") ;
87+ }
3088 }
3189}
3290
0 commit comments