4
4
using System . Collections . Generic ;
5
5
using System . IO ;
6
6
using System . Linq ;
7
+ using System . Text . Json ;
7
8
using System . Text . RegularExpressions ;
8
9
using System . Xml ;
9
10
using System . Xml . Linq ;
@@ -18,13 +19,13 @@ public static bool TryResolveProjectReferences(Project packageProject, out List<
18
19
includedProjectPaths = null ;
19
20
errorMessage = null ;
20
21
21
- string solutionFolder = FileSystem . Instance . Directory . GetParentDirectory ( packageProject . ProjectDirectory ) ;
22
+ string rootFolder = FileSystem . Instance . Directory . GetParentDirectory ( packageProject . ProjectDirectory ) ;
22
23
const string xmlFileName = "ProjectReferences.xml" ;
23
24
string xmlFilePath = FileSystem . Instance . Path . Combine ( packageProject . ProjectDirectory , "PackageContent" , xmlFileName ) ;
24
25
25
26
try
26
27
{
27
- includedProjectPaths = ResolveProjectReferences ( xmlFilePath , solutionFolder ) ;
28
+ includedProjectPaths = ResolveProjectReferences ( xmlFilePath , rootFolder ) ;
28
29
return true ;
29
30
}
30
31
catch ( FileNotFoundException )
@@ -43,7 +44,7 @@ public static bool TryResolveProjectReferences(Project packageProject, out List<
43
44
return false ;
44
45
}
45
46
46
- private static List < string > ResolveProjectReferences ( string xmlFilePath , string solutionFolder )
47
+ private static List < string > ResolveProjectReferences ( string xmlFilePath , string rootFolder )
47
48
{
48
49
// Load the XML file
49
50
var doc = XDocument . Load ( xmlFilePath ) ;
@@ -55,6 +56,33 @@ private static List<string> ResolveProjectReferences(string xmlFilePath, string
55
56
. Select ( attr => attr . Value )
56
57
. ToList ( ) ;
57
58
59
+ var includeSolutionFilters = doc . Descendants ( ns + "SolutionFilter" )
60
+ . Attributes ( "Include" )
61
+ . Select ( attr => attr . Value )
62
+ . ToList ( ) ;
63
+
64
+ // Readout solution filter files and add them to the include patterns
65
+ foreach ( string includeSolutionFilter in includeSolutionFilters )
66
+ {
67
+ var filters = FileSystem . Instance . Directory . GetFiles ( rootFolder , "*.slnf" , SearchOption . AllDirectories )
68
+ . Where ( path => MatchesPattern ( path , includeSolutionFilter ) ) ;
69
+
70
+ foreach ( string filter in filters )
71
+ {
72
+ string json = FileSystem . Instance . File . ReadAllText ( filter ) ;
73
+ JsonDocument jsonFilter = JsonDocument . Parse ( json ) ;
74
+
75
+ if ( jsonFilter . RootElement . TryGetProperty ( "solution" , out JsonElement solution ) && solution . TryGetProperty ( "projects" , out JsonElement projects )
76
+ && projects . ValueKind == JsonValueKind . Array )
77
+ {
78
+ foreach ( JsonElement project in projects . EnumerateArray ( ) )
79
+ {
80
+ includePatterns . Add ( $ "..\\ { project } ") ;
81
+ }
82
+ }
83
+ }
84
+ }
85
+
58
86
var excludePatterns = doc . Descendants ( ns + "ProjectReference" )
59
87
. Attributes ( "Exclude" )
60
88
. Select ( attr => attr . Value )
@@ -73,8 +101,8 @@ private static List<string> ResolveProjectReferences(string xmlFilePath, string
73
101
var includedProjects = new HashSet < string > ( ) ;
74
102
foreach ( var pattern in includePatterns )
75
103
{
76
- var resolvedPaths = Directory . GetFiles ( solutionFolder , "*.csproj" , SearchOption . AllDirectories )
77
- . Where ( path => MatchesPattern ( path , pattern ) ) ;
104
+ var resolvedPaths = FileSystem . Instance . Directory . GetFiles ( rootFolder , "*.csproj" , SearchOption . AllDirectories )
105
+ . Where ( path => MatchesPattern ( path , pattern ) ) ;
78
106
foreach ( var path in resolvedPaths )
79
107
{
80
108
includedProjects . Add ( path ) ;
@@ -84,15 +112,15 @@ private static List<string> ResolveProjectReferences(string xmlFilePath, string
84
112
// Apply Exclude patterns
85
113
foreach ( var pattern in excludePatterns )
86
114
{
87
- var resolvedPaths = Directory . GetFiles ( solutionFolder , "*.csproj" , SearchOption . AllDirectories )
88
- . Where ( path => MatchesPattern ( path , pattern ) ) ;
115
+ var resolvedPaths = FileSystem . Instance . Directory . GetFiles ( rootFolder , "*.csproj" , SearchOption . AllDirectories )
116
+ . Where ( path => MatchesPattern ( path , pattern ) ) ;
89
117
foreach ( var path in resolvedPaths )
90
118
{
91
119
includedProjects . Remove ( path ) ;
92
120
}
93
121
}
94
122
95
- return includedProjects . ToList ( ) ;
123
+ return includedProjects . Distinct ( ) . ToList ( ) ;
96
124
}
97
125
98
126
private static bool MatchesPattern ( string filePath , string pattern )
@@ -102,5 +130,13 @@ private static bool MatchesPattern(string filePath, string pattern)
102
130
string normalizedPattern = pattern . Replace ( "*" , ".*" ) . Replace ( @"\" , @"\\" ) ;
103
131
return Regex . IsMatch ( filePath , normalizedPattern , RegexOptions . IgnoreCase ) ;
104
132
}
133
+
134
+ private class SolutionFilter
135
+ {
136
+ public string Path { get ; set ; }
137
+
138
+ public string [ ] Projects { get ; set ; }
139
+ }
140
+
105
141
}
106
142
}
0 commit comments