2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using System . Collections . Concurrent ;
5
- using System . Collections . Immutable ;
6
5
using Microsoft . Build . Evaluation ;
7
6
using Microsoft . Build . Execution ;
8
- using Microsoft . Build . Framework ;
9
- using Microsoft . Build . Logging ;
7
+ using Microsoft . DotNet . Tools ;
10
8
using Microsoft . DotNet . Tools . Common ;
11
9
using Microsoft . VisualStudio . SolutionPersistence . Model ;
12
10
@@ -27,10 +25,7 @@ public static (IEnumerable<Module> Projects, bool IsBuiltOrRestored) GetProjects
27
25
}
28
26
29
27
bool isBuiltOrRestored = BuildOrRestoreProjectOrSolution (
30
- solutionFilePath ,
31
- projectCollection ,
32
- buildOptions ,
33
- GetCommands ( buildOptions . HasNoRestore , buildOptions . HasNoBuild ) ) ;
28
+ solutionFilePath , buildOptions . MSBuildArgs , buildOptions . HasNoRestore , buildOptions . HasNoBuild ) ;
34
29
35
30
ConcurrentBag < Module > projects = GetProjectsProperties ( projectCollection , solutionModel . SolutionProjects . Select ( p => Path . Combine ( rootDirectory , p . FilePath ) ) , buildOptions ) ;
36
31
return ( projects , isBuiltOrRestored ) ;
@@ -39,41 +34,39 @@ public static (IEnumerable<Module> Projects, bool IsBuiltOrRestored) GetProjects
39
34
public static ( IEnumerable < Module > Projects , bool IsBuiltOrRestored ) GetProjectsFromProject ( string projectFilePath , BuildOptions buildOptions )
40
35
{
41
36
var projectCollection = new ProjectCollection ( ) ;
42
- bool isBuiltOrRestored = true ;
43
37
44
- if ( ! buildOptions . HasNoRestore )
45
- {
46
- isBuiltOrRestored = BuildOrRestoreProjectOrSolution (
47
- projectFilePath ,
48
- projectCollection ,
49
- buildOptions ,
50
- [ CliConstants . RestoreCommand ] ) ;
51
- }
52
-
53
- if ( ! buildOptions . HasNoBuild )
54
- {
55
- isBuiltOrRestored = isBuiltOrRestored && BuildOrRestoreProjectOrSolution (
56
- projectFilePath ,
57
- projectCollection ,
58
- buildOptions ,
59
- [ CliConstants . BuildCommand ] ) ;
60
- }
38
+ bool isBuiltOrRestored = BuildOrRestoreProjectOrSolution ( projectFilePath , buildOptions . MSBuildArgs , buildOptions . HasNoRestore , buildOptions . HasNoBuild ) ;
61
39
62
40
IEnumerable < Module > projects = SolutionAndProjectUtility . GetProjectProperties ( projectFilePath , GetGlobalProperties ( buildOptions ) , projectCollection ) ;
63
41
64
42
return ( projects , isBuiltOrRestored ) ;
65
43
}
66
44
67
- private static bool BuildOrRestoreProjectOrSolution ( string filePath , ProjectCollection projectCollection , BuildOptions buildOptions , string [ ] commands )
45
+ public static IEnumerable < string > GetPropertyTokens ( IEnumerable < string > unmatchedTokens )
46
+ {
47
+ return unmatchedTokens . Where ( token =>
48
+ token . StartsWith ( "--property:" , StringComparison . OrdinalIgnoreCase ) ||
49
+ token . StartsWith ( "/property:" , StringComparison . OrdinalIgnoreCase ) ||
50
+ token . StartsWith ( "-p:" , StringComparison . OrdinalIgnoreCase ) ||
51
+ token . StartsWith ( "/p:" , StringComparison . OrdinalIgnoreCase ) ) ;
52
+ }
53
+
54
+ public static IEnumerable < string > GetBinaryLoggerTokens ( IEnumerable < string > args )
68
55
{
69
- var parameters = GetBuildParameters ( projectCollection , buildOptions ) ;
70
- var globalProperties = GetGlobalProperties ( buildOptions ) ;
56
+ return args . Where ( arg =>
57
+ arg . StartsWith ( "/bl:" , StringComparison . OrdinalIgnoreCase ) || arg . Equals ( "/bl" , StringComparison . OrdinalIgnoreCase ) ||
58
+ arg . StartsWith ( "--binaryLogger:" , StringComparison . OrdinalIgnoreCase ) || arg . Equals ( "--binaryLogger" , StringComparison . OrdinalIgnoreCase ) ||
59
+ arg . StartsWith ( "-bl:" , StringComparison . OrdinalIgnoreCase ) || arg . Equals ( "-bl" , StringComparison . OrdinalIgnoreCase ) ) ;
60
+ }
71
61
72
- var buildRequestData = new BuildRequestData ( filePath , globalProperties , null , commands , null ) ;
62
+ private static bool BuildOrRestoreProjectOrSolution ( string filePath , List < string > arguments , bool hasNoRestore , bool hasNoBuild )
63
+ {
64
+ arguments . Add ( filePath ) ;
65
+ arguments . Add ( "-target:_MTPBuild" ) ;
73
66
74
- BuildResult buildResult = BuildManager . DefaultBuildManager . Build ( parameters , buildRequestData ) ;
67
+ int result = new RestoringCommand ( arguments , hasNoRestore || hasNoBuild ) . Execute ( ) ;
75
68
76
- return buildResult . OverallResult == BuildResultCode . Success ;
69
+ return result == ( int ) BuildResultCode . Success ;
77
70
}
78
71
79
72
private static ConcurrentBag < Module > GetProjectsProperties ( ProjectCollection projectCollection , IEnumerable < string > projects , BuildOptions buildOptions )
@@ -107,71 +100,6 @@ private static string HandleFilteredSolutionFilePath(string solutionFilterFilePa
107
100
return solutionFullPath ;
108
101
}
109
102
110
- internal static bool IsBinaryLoggerEnabled ( ref List < string > args , out string binLogFileName )
111
- {
112
- binLogFileName = string . Empty ;
113
- var binLogArgs = new List < string > ( ) ;
114
-
115
- foreach ( var arg in args )
116
- {
117
- if ( arg . StartsWith ( "/bl:" ) || arg . Equals ( "/bl" )
118
- || arg . StartsWith ( "--binaryLogger:" ) || arg . Equals ( "--binaryLogger" )
119
- || arg . StartsWith ( "-bl:" ) || arg . Equals ( "-bl" ) )
120
- {
121
- binLogArgs . Add ( arg ) ;
122
-
123
- }
124
- }
125
-
126
- if ( binLogArgs . Count > 0 )
127
- {
128
- // Remove all BinLog args from the list of args
129
- args . RemoveAll ( arg => binLogArgs . Contains ( arg ) ) ;
130
-
131
- // Get BinLog filename
132
- var binLogArg = binLogArgs . LastOrDefault ( ) ;
133
-
134
- if ( binLogArg . Contains ( CliConstants . Colon ) )
135
- {
136
- var parts = binLogArg . Split ( CliConstants . Colon , 2 ) ;
137
- binLogFileName = ! string . IsNullOrEmpty ( parts [ 1 ] ) ? parts [ 1 ] : CliConstants . BinLogFileName ;
138
- }
139
- else
140
- {
141
- binLogFileName = CliConstants . BinLogFileName ;
142
- }
143
-
144
- return true ;
145
- }
146
-
147
- return false ;
148
- }
149
-
150
- private static BuildParameters GetBuildParameters ( ProjectCollection projectCollection , BuildOptions buildOptions )
151
- {
152
- BuildParameters parameters = new ( projectCollection )
153
- {
154
- Loggers = [ new ConsoleLogger ( LoggerVerbosity . Quiet ) ]
155
- } ;
156
-
157
- if ( ! buildOptions . AllowBinLog )
158
- return parameters ;
159
-
160
- parameters . Loggers =
161
- [
162
- .. parameters . Loggers ,
163
- .. new [ ]
164
- {
165
- new BinaryLogger
166
- {
167
- Parameters = buildOptions . BinLogFileName
168
- }
169
- } ,
170
- ] ;
171
-
172
- return parameters ;
173
- }
174
-
175
103
private static Dictionary < string , string > GetGlobalProperties ( BuildOptions buildOptions )
176
104
{
177
105
var globalProperties = new Dictionary < string , string > ( ) ;
@@ -188,22 +116,5 @@ private static Dictionary<string, string> GetGlobalProperties(BuildOptions build
188
116
189
117
return globalProperties ;
190
118
}
191
-
192
- private static string [ ] GetCommands ( bool hasNoRestore , bool hasNoBuild )
193
- {
194
- var commands = ImmutableArray . CreateBuilder < string > ( ) ;
195
-
196
- if ( ! hasNoRestore )
197
- {
198
- commands . Add ( CliConstants . RestoreCommand ) ;
199
- }
200
-
201
- if ( ! hasNoBuild )
202
- {
203
- commands . Add ( CliConstants . BuildCommand ) ;
204
- }
205
-
206
- return [ .. commands ] ;
207
- }
208
119
}
209
120
}
0 commit comments