22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System . Collections . Concurrent ;
5- using System . Collections . Immutable ;
65using Microsoft . Build . Evaluation ;
76using Microsoft . Build . Execution ;
8- using Microsoft . Build . Framework ;
9- using Microsoft . Build . Logging ;
7+ using Microsoft . DotNet . Tools ;
108using Microsoft . DotNet . Tools . Common ;
119using Microsoft . VisualStudio . SolutionPersistence . Model ;
1210
@@ -27,10 +25,7 @@ public static (IEnumerable<Module> Projects, bool IsBuiltOrRestored) GetProjects
2725 }
2826
2927 bool isBuiltOrRestored = BuildOrRestoreProjectOrSolution (
30- solutionFilePath ,
31- projectCollection ,
32- buildOptions ,
33- GetCommands ( buildOptions . HasNoRestore , buildOptions . HasNoBuild ) ) ;
28+ solutionFilePath , buildOptions . MSBuildArgs , buildOptions . HasNoRestore , buildOptions . HasNoBuild ) ;
3429
3530 ConcurrentBag < Module > projects = GetProjectsProperties ( projectCollection , solutionModel . SolutionProjects . Select ( p => Path . Combine ( rootDirectory , p . FilePath ) ) , buildOptions ) ;
3631 return ( projects , isBuiltOrRestored ) ;
@@ -39,41 +34,39 @@ public static (IEnumerable<Module> Projects, bool IsBuiltOrRestored) GetProjects
3934 public static ( IEnumerable < Module > Projects , bool IsBuiltOrRestored ) GetProjectsFromProject ( string projectFilePath , BuildOptions buildOptions )
4035 {
4136 var projectCollection = new ProjectCollection ( ) ;
42- bool isBuiltOrRestored = true ;
4337
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 ) ;
6139
6240 IEnumerable < Module > projects = SolutionAndProjectUtility . GetProjectProperties ( projectFilePath , GetGlobalProperties ( buildOptions ) , projectCollection ) ;
6341
6442 return ( projects , isBuiltOrRestored ) ;
6543 }
6644
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 )
6855 {
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+ }
7161
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" ) ;
7366
74- BuildResult buildResult = BuildManager . DefaultBuildManager . Build ( parameters , buildRequestData ) ;
67+ int result = new RestoringCommand ( arguments , hasNoRestore || hasNoBuild ) . Execute ( ) ;
7568
76- return buildResult . OverallResult == BuildResultCode . Success ;
69+ return result == ( int ) BuildResultCode . Success ;
7770 }
7871
7972 private static ConcurrentBag < Module > GetProjectsProperties ( ProjectCollection projectCollection , IEnumerable < string > projects , BuildOptions buildOptions )
@@ -107,71 +100,6 @@ private static string HandleFilteredSolutionFilePath(string solutionFilterFilePa
107100 return solutionFullPath ;
108101 }
109102
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-
175103 private static Dictionary < string , string > GetGlobalProperties ( BuildOptions buildOptions )
176104 {
177105 var globalProperties = new Dictionary < string , string > ( ) ;
@@ -188,22 +116,5 @@ private static Dictionary<string, string> GetGlobalProperties(BuildOptions build
188116
189117 return globalProperties ;
190118 }
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- }
208119 }
209120}
0 commit comments