@@ -35,7 +35,26 @@ public RunDotNetCommandTask(BuildSettings settings, ILogger logger, string[] dot
35
35
_failOnNoProjects = failOnNoProjects ;
36
36
}
37
37
38
- public async Task < int > Run ( IEnumerable < string > affectedProjects )
38
+ public async Task < int > Run ( BuildAnalysisResult buildResult )
39
+ {
40
+ switch ( buildResult )
41
+ {
42
+ case FullSolutionBuildResult full :
43
+ return await RunSolutionBuild ( full . SolutionPath ) ;
44
+ case IncrementalBuildResult incremental :
45
+ return await RunIncrementalBuild ( incremental . AffectedProjects ) ;
46
+ default :
47
+ throw new InvalidOperationException ( $ "Unknown build result type: { buildResult . GetType ( ) } ") ;
48
+ }
49
+ }
50
+
51
+ private async Task < int > RunSolutionBuild ( string solutionPath )
52
+ {
53
+ _logger . LogInformation ( "Running '{0}' against solution {1}" , string . Join ( " " , _dotnetArgs ) , solutionPath ) ;
54
+ return await RunCommand ( solutionPath ) ;
55
+ }
56
+
57
+ private async Task < int > RunIncrementalBuild ( IEnumerable < string > affectedProjects )
39
58
{
40
59
var projects = affectedProjects . ToList ( ) ;
41
60
if ( ! projects . Any ( ) )
@@ -48,69 +67,11 @@ public async Task<int> Run(IEnumerable<string> affectedProjects)
48
67
49
68
var failedProjects = new List < string > ( ) ;
50
69
51
- async Task < bool > RunCommand ( string project )
52
- {
53
- // For dotnet CLI commands like 'build', 'test', etc., the project path comes last
54
- var args = string . Join ( " " , _dotnetArgs ) ;
55
- if ( ! args . Contains ( "--project" ) && ! args . Contains ( "-p" ) )
56
- args = $ "{ args } \" { project } \" ";
57
-
58
- var process = new Process
59
- {
60
- StartInfo = new ProcessStartInfo
61
- {
62
- FileName = "dotnet" ,
63
- Arguments = args ,
64
- UseShellExecute = false ,
65
- RedirectStandardOutput = true ,
66
- RedirectStandardError = true ,
67
- WorkingDirectory = _settings . WorkingDirectory
68
- }
69
- } ;
70
-
71
- process . OutputDataReceived += ( sender , eventArgs ) =>
72
- {
73
- if ( ! string . IsNullOrEmpty ( eventArgs . Data ) )
74
- _logger . LogInformation ( "[{0}] {1}" , project , eventArgs . Data ) ;
75
- } ;
76
-
77
- process . ErrorDataReceived += ( sender , eventArgs ) =>
78
- {
79
- if ( ! string . IsNullOrEmpty ( eventArgs . Data ) )
80
- _logger . LogError ( "[{0}] {1}" , project , eventArgs . Data ) ;
81
- } ;
82
-
83
- try
84
- {
85
- process . Start ( ) ;
86
- process . BeginOutputReadLine ( ) ;
87
- process . BeginErrorReadLine ( ) ;
88
- await process . WaitForExitAsync ( ) ;
89
-
90
- if ( process . ExitCode != 0 )
91
- {
92
- _logger . LogError ( "Command failed for project {0} with exit code {1}" , project , process . ExitCode ) ;
93
- return false ;
94
- }
95
-
96
- return true ;
97
- }
98
- catch ( Exception ex )
99
- {
100
- _logger . LogError ( ex , "Failed to execute command for project {0}" , project ) ;
101
- return false ;
102
- }
103
- finally
104
- {
105
- process . Dispose ( ) ;
106
- }
107
- }
108
-
109
70
if ( _runInParallel )
110
71
{
111
72
var tasks = projects . Select ( async project =>
112
73
{
113
- if ( ! await RunCommand ( project ) )
74
+ if ( await RunCommand ( project ) != 0 )
114
75
{
115
76
failedProjects . Add ( project ) ;
116
77
if ( ! _continueOnError )
@@ -124,7 +85,7 @@ async Task<bool> RunCommand(string project)
124
85
{
125
86
foreach ( var project in projects )
126
87
{
127
- if ( ! await RunCommand ( project ) )
88
+ if ( await RunCommand ( project ) != 0 )
128
89
{
129
90
failedProjects . Add ( project ) ;
130
91
if ( ! _continueOnError )
@@ -141,5 +102,62 @@ async Task<bool> RunCommand(string project)
141
102
142
103
return 0 ;
143
104
}
105
+
106
+ private async Task < int > RunCommand ( string target )
107
+ {
108
+ // For dotnet CLI commands like 'build', 'test', etc., the project/solution path comes last
109
+ var args = string . Join ( " " , _dotnetArgs ) ;
110
+ if ( ! args . Contains ( "--project" ) && ! args . Contains ( "-p" ) )
111
+ args = $ "{ args } \" { target } \" ";
112
+
113
+ var process = new Process
114
+ {
115
+ StartInfo = new ProcessStartInfo
116
+ {
117
+ FileName = "dotnet" ,
118
+ Arguments = args ,
119
+ UseShellExecute = false ,
120
+ RedirectStandardOutput = true ,
121
+ RedirectStandardError = true ,
122
+ WorkingDirectory = _settings . WorkingDirectory
123
+ }
124
+ } ;
125
+
126
+ process . OutputDataReceived += ( sender , eventArgs ) =>
127
+ {
128
+ if ( ! string . IsNullOrEmpty ( eventArgs . Data ) )
129
+ _logger . LogInformation ( "[{0}] {1}" , target , eventArgs . Data ) ;
130
+ } ;
131
+
132
+ process . ErrorDataReceived += ( sender , eventArgs ) =>
133
+ {
134
+ if ( ! string . IsNullOrEmpty ( eventArgs . Data ) )
135
+ _logger . LogError ( "[{0}] {1}" , target , eventArgs . Data ) ;
136
+ } ;
137
+
138
+ try
139
+ {
140
+ process . Start ( ) ;
141
+ process . BeginOutputReadLine ( ) ;
142
+ process . BeginErrorReadLine ( ) ;
143
+ await process . WaitForExitAsync ( ) ;
144
+
145
+ if ( process . ExitCode != 0 )
146
+ {
147
+ _logger . LogError ( "Command failed for {0} with exit code {1}" , target , process . ExitCode ) ;
148
+ }
149
+
150
+ return process . ExitCode ;
151
+ }
152
+ catch ( Exception ex )
153
+ {
154
+ _logger . LogError ( ex , "Failed to execute command for {0}" , target ) ;
155
+ return 1 ;
156
+ }
157
+ finally
158
+ {
159
+ process . Dispose ( ) ;
160
+ }
161
+ }
144
162
}
145
163
}
0 commit comments