22using Microsoft . Build . Graph ;
33using System . Collections . Generic ;
44using System . Linq ;
5+ using System . Text . RegularExpressions ;
56
67namespace DotnetAffected . Core . Processor
78{
@@ -18,27 +19,41 @@ internal abstract class AffectedProcessorBase
1819 public AffectedSummary Process ( AffectedProcessorContext context )
1920 {
2021 // Get files that changed according to changes provider.
21- context . ChangedFiles = DiscoverChangedFiles ( context ) . ToArray ( ) ;
22+ context . ChangedFiles = DiscoverChangedFiles ( context )
23+ . ToArray ( ) ;
2224
2325 // Map the files that changed to their corresponding project/s.
24- context . ChangedProjects = DiscoverProjectsForFiles ( context ) . ToArray ( ) ;
26+ var excludedProjects = new List < ProjectGraphNode > ( ) ;
27+ context . ChangedProjects = ApplyExclusionPattern (
28+ DiscoverProjectsForFiles ( context ) ,
29+ context . Options ,
30+ excludedProjects ) ;
2531
2632 // Get packages that have changed, either from central package management or from the project file
2733 context . ChangedPackages = DiscoverPackageChanges ( context ) ;
2834
2935 // Determine which projects are affected by the projects and packages that have changed.
30- context . AffectedProjects = DiscoverAffectedProjects ( context ) ;
36+ context . AffectedProjects = ApplyExclusionPattern (
37+ DiscoverAffectedProjects ( context ) ,
38+ context . Options ,
39+ excludedProjects ) ;
3140
3241 // Output a summary of the operation.
33- return new AffectedSummary ( context . ChangedFiles , context . ChangedProjects , context . AffectedProjects , context . ChangedPackages ) ;
42+ return new AffectedSummary (
43+ context . ChangedFiles ,
44+ context . ChangedProjects ,
45+ context . AffectedProjects ,
46+ excludedProjects . Distinct ( )
47+ . ToArray ( ) ,
48+ context . ChangedPackages ) ;
3449 }
35-
50+
3651 /// <summary>
3752 /// Discover which files have changes
3853 /// </summary>
3954 /// <param name="context"></param>
4055 /// <returns></returns>
41- protected virtual IEnumerable < string > DiscoverChangedFiles ( AffectedProcessorContext context )
56+ protected virtual IEnumerable < string > DiscoverChangedFiles ( AffectedProcessorContext context )
4257 => context . ChangesProvider . GetChangedFiles ( context . RepositoryPath , context . FromRef , context . ToRef ) ;
4358
4459 /// <summary>
@@ -49,11 +64,43 @@ protected virtual IEnumerable<string> DiscoverChangedFiles(AffectedProcessorCont
4964 protected virtual IEnumerable < ProjectGraphNode > DiscoverProjectsForFiles ( AffectedProcessorContext context )
5065 {
5166 // We init now because we want the graph to initialize late (lazy)
52- var provider = context . ChangedProjectsProvider ?? new PredictionChangedProjectsProvider ( context . Graph , context . Options ) ;
67+ var provider = context . ChangedProjectsProvider ??
68+ new PredictionChangedProjectsProvider ( context . Graph , context . Options ) ;
5369 // Match which files belong to which of our known projects
5470 return provider . GetReferencingProjects ( context . ChangedFiles ) ;
5571 }
5672
73+ /// <summary>
74+ /// Applies the <see cref="AffectedOptions.ExclusionRegex"/> to exclude
75+ /// projects that matches the regular expression.
76+ /// </summary>
77+ /// <param name="inputProjects">List of projects that changed.</param>
78+ /// <param name="options">Affected options.</param>
79+ /// <param name="excludedProjects">Collection of excluded projects</param>
80+ /// <returns>Project lis excluding the ones that matches the exclusion regex.</returns>
81+ protected virtual ProjectGraphNode [ ] ApplyExclusionPattern (
82+ IEnumerable < ProjectGraphNode > inputProjects ,
83+ AffectedOptions options ,
84+ ICollection < ProjectGraphNode > excludedProjects )
85+ {
86+ var pattern = options . ExclusionRegex ;
87+
88+ if ( string . IsNullOrEmpty ( pattern ) )
89+ return inputProjects . ToArray ( ) ;
90+
91+ var changedProjects = new List < ProjectGraphNode > ( ) ;
92+ var regex = new Regex ( pattern ) ;
93+ foreach ( var project in inputProjects )
94+ {
95+ if ( regex . IsMatch ( project . GetFullPath ( ) ) )
96+ excludedProjects . Add ( project ) ;
97+ else
98+ changedProjects . Add ( project ) ;
99+ }
100+
101+ return changedProjects . ToArray ( ) ;
102+ }
103+
57104 /// <summary>
58105 /// Discover which packages have changed. <br/>
59106 /// </summary>
0 commit comments