@@ -34,36 +34,20 @@ public class CSharpAutobuilder : Autobuilder<CSharpAutobuildOptions>
34
34
private const string buildCommandDocsUrl =
35
35
"https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-the-codeql-workflow-for-compiled-languages" ;
36
36
37
- private DotNetRule ? dotNetRule ;
38
-
39
- private MsBuildRule ? msBuildRule ;
40
-
41
- private BuildCommandAutoRule ? buildCommandAutoRule ;
37
+ private readonly AutoBuildRule autoBuildRule ;
42
38
43
39
private readonly DiagnosticClassifier diagnosticClassifier ;
44
40
45
41
protected override DiagnosticClassifier DiagnosticClassifier => diagnosticClassifier ;
46
42
47
- public CSharpAutobuilder ( IBuildActions actions , CSharpAutobuildOptions options ) : base ( actions , options ) =>
48
- diagnosticClassifier = new CSharpDiagnosticClassifier ( ) ;
43
+ public CSharpAutobuilder ( IBuildActions actions , CSharpAutobuildOptions options ) : base ( actions , options )
44
+ {
45
+ this . autoBuildRule = new AutoBuildRule ( this ) ;
46
+ this . diagnosticClassifier = new CSharpDiagnosticClassifier ( ) ;
47
+ }
49
48
50
49
public override BuildScript GetBuildScript ( )
51
50
{
52
- /// <summary>
53
- /// A script that checks that the C# extractor has been executed.
54
- /// </summary>
55
- BuildScript CheckExtractorRun ( bool warnOnFailure ) =>
56
- BuildScript . Create ( actions =>
57
- {
58
- if ( actions . FileExists ( Extractor . GetCSharpLogPath ( ) ) )
59
- return 0 ;
60
-
61
- if ( warnOnFailure )
62
- Log ( Severity . Error , "No C# code detected during build." ) ;
63
-
64
- return 1 ;
65
- } ) ;
66
-
67
51
var attempt = BuildScript . Failure ;
68
52
switch ( GetCSharpBuildStrategy ( ) )
69
53
{
@@ -81,51 +65,9 @@ BuildScript CheckExtractorRun(bool warnOnFailure) =>
81
65
attempt = new DotNetRule ( ) . Analyse ( this , false ) & CheckExtractorRun ( true ) ;
82
66
break ;
83
67
case CSharpBuildStrategy . Auto :
84
- var cleanTrapFolder =
85
- BuildScript . DeleteDirectory ( TrapDir ) ;
86
- var cleanSourceArchive =
87
- BuildScript . DeleteDirectory ( SourceArchiveDir ) ;
88
- var tryCleanExtractorArgsLogs =
89
- BuildScript . Create ( actions =>
90
- {
91
- foreach ( var file in Extractor . GetCSharpArgsLogs ( ) )
92
- {
93
- try
94
- {
95
- actions . FileDelete ( file ) ;
96
- }
97
- catch // lgtm[cs/catch-of-all-exceptions] lgtm[cs/empty-catch-block]
98
- { }
99
- }
100
-
101
- return 0 ;
102
- } ) ;
103
- var attemptExtractorCleanup =
104
- BuildScript . Try ( cleanTrapFolder ) &
105
- BuildScript . Try ( cleanSourceArchive ) &
106
- tryCleanExtractorArgsLogs &
107
- BuildScript . DeleteFile ( Extractor . GetCSharpLogPath ( ) ) ;
108
-
109
- /// <summary>
110
- /// Execute script `s` and check that the C# extractor has been executed.
111
- /// If either fails, attempt to cleanup any artifacts produced by the extractor,
112
- /// and exit with code 1, in order to proceed to the next attempt.
113
- /// </summary>
114
- BuildScript IntermediateAttempt ( BuildScript s ) =>
115
- ( s & CheckExtractorRun ( false ) ) |
116
- ( attemptExtractorCleanup & BuildScript . Failure ) ;
117
-
118
- this . dotNetRule = new DotNetRule ( ) ;
119
- this . msBuildRule = new MsBuildRule ( ) ;
120
- this . buildCommandAutoRule = new BuildCommandAutoRule ( DotNetRule . WithDotNet ) ;
121
-
122
68
attempt =
123
- // First try .NET Core
124
- IntermediateAttempt ( dotNetRule . Analyse ( this , true ) ) |
125
- // Then MSBuild
126
- ( ( ) => IntermediateAttempt ( msBuildRule . Analyse ( this , true ) ) ) |
127
- // And finally look for a script that might be a build script
128
- ( ( ) => this . buildCommandAutoRule . Analyse ( this , true ) & CheckExtractorRun ( true ) ) |
69
+ // Attempt a few different build strategies to see if one works
70
+ this . autoBuildRule . Analyse ( this , true ) |
129
71
// All attempts failed: print message
130
72
AutobuildFailure ( ) ;
131
73
break ;
@@ -134,24 +76,38 @@ BuildScript IntermediateAttempt(BuildScript s) =>
134
76
return attempt ;
135
77
}
136
78
79
+ /// <summary>
80
+ /// A script that checks that the C# extractor has been executed.
81
+ /// </summary>
82
+ public BuildScript CheckExtractorRun ( bool warnOnFailure ) =>
83
+ BuildScript . Create ( actions =>
84
+ {
85
+ if ( actions . FileExists ( Extractor . GetCSharpLogPath ( ) ) )
86
+ return 0 ;
87
+
88
+ if ( warnOnFailure )
89
+ Log ( Severity . Error , "No C# code detected during build." ) ;
90
+
91
+ return 1 ;
92
+ } ) ;
93
+
137
94
protected override void AutobuildFailureDiagnostic ( )
138
95
{
139
96
// if `ScriptPath` is not null here, the `BuildCommandAuto` rule was
140
97
// run and found at least one script to execute
141
- if ( this . buildCommandAutoRule is not null &&
142
- this . buildCommandAutoRule . ScriptPath is not null )
98
+ if ( this . autoBuildRule . BuildCommandAutoRule . ScriptPath is not null )
143
99
{
144
100
DiagnosticMessage message ;
145
101
146
102
// if we found multiple build scripts in the project directory, then we can say
147
103
// as much to indicate that we may have picked the wrong one;
148
104
// otherwise, we just report that the one script we found didn't work
149
- if ( this . buildCommandAutoRule . CandidatePaths . Count ( ) > 1 )
105
+ if ( this . autoBuildRule . BuildCommandAutoRule . CandidatePaths . Count ( ) > 1 )
150
106
{
151
107
message = MakeDiagnostic ( "multiple-build-scripts" , "There are multiple potential build scripts" ) ;
152
108
message . MarkdownMessage =
153
109
"CodeQL found multiple potential build scripts for your project and " +
154
- $ "attempted to run `{ buildCommandAutoRule . ScriptPath } `, which failed. " +
110
+ $ "attempted to run `{ autoBuildRule . BuildCommandAutoRule . ScriptPath } `, which failed. " +
155
111
"This may not be the right build script for your project. " +
156
112
$ "Set up a [manual build command]({ buildCommandDocsUrl } ).";
157
113
}
@@ -160,7 +116,7 @@ protected override void AutobuildFailureDiagnostic()
160
116
message = MakeDiagnostic ( "script-failure" , "Unable to build project using build script" ) ;
161
117
message . MarkdownMessage =
162
118
"CodeQL attempted to build your project using a script located at " +
163
- $ "`{ buildCommandAutoRule . ScriptPath } `, which failed. " +
119
+ $ "`{ autoBuildRule . BuildCommandAutoRule . ScriptPath } `, which failed. " +
164
120
$ "Set up a [manual build command]({ buildCommandDocsUrl } ).";
165
121
}
166
122
@@ -180,37 +136,37 @@ protected override void AutobuildFailureDiagnostic()
180
136
181
137
AddDiagnostic ( message ) ;
182
138
}
183
- else if ( dotNetRule is not null && dotNetRule . NotDotNetProjects . Any ( ) )
139
+ else if ( autoBuildRule . DotNetRule . NotDotNetProjects . Any ( ) )
184
140
{
185
141
var message = MakeDiagnostic ( "dotnet-incompatible-projects" , "Some projects are incompatible with .NET Core" ) ;
186
142
message . MarkdownMessage =
187
143
"CodeQL found some projects which cannot be built with .NET Core:\n " +
188
- string . Join ( '\n ' , dotNetRule . NotDotNetProjects . Select ( p => $ "- `{ p . FullPath } `") ) ;
144
+ string . Join ( '\n ' , autoBuildRule . DotNetRule . NotDotNetProjects . Select ( p => $ "- `{ p . FullPath } `") ) ;
189
145
message . Severity = DiagnosticMessage . TspSeverity . Warning ;
190
146
191
147
AddDiagnostic ( message ) ;
192
148
}
193
149
194
150
// report any projects that failed to build with .NET Core
195
- if ( dotNetRule is not null && dotNetRule . FailedProjectsOrSolutions . Any ( ) )
151
+ if ( autoBuildRule . DotNetRule . FailedProjectsOrSolutions . Any ( ) )
196
152
{
197
153
var message = MakeDiagnostic ( "dotnet-build-failure" , "Some projects or solutions failed to build using .NET Core" ) ;
198
154
message . MarkdownMessage =
199
155
"CodeQL was unable to build the following projects using .NET Core:\n " +
200
- string . Join ( '\n ' , dotNetRule . FailedProjectsOrSolutions . Select ( p => $ "- `{ p . FullPath } `") ) +
156
+ string . Join ( '\n ' , autoBuildRule . DotNetRule . FailedProjectsOrSolutions . Select ( p => $ "- `{ p . FullPath } `") ) +
201
157
$ "\n Set up a [manual build command]({ buildCommandDocsUrl } ).";
202
158
message . Severity = DiagnosticMessage . TspSeverity . Error ;
203
159
204
160
AddDiagnostic ( message ) ;
205
161
}
206
162
207
163
// report any projects that failed to build with MSBuild
208
- if ( msBuildRule is not null && msBuildRule . FailedProjectsOrSolutions . Any ( ) )
164
+ if ( autoBuildRule . MsBuildRule . FailedProjectsOrSolutions . Any ( ) )
209
165
{
210
166
var message = MakeDiagnostic ( "msbuild-build-failure" , "Some projects or solutions failed to build using MSBuild" ) ;
211
167
message . MarkdownMessage =
212
168
"CodeQL was unable to build the following projects using MSBuild:\n " +
213
- string . Join ( '\n ' , msBuildRule . FailedProjectsOrSolutions . Select ( p => $ "- `{ p . FullPath } `") ) +
169
+ string . Join ( '\n ' , autoBuildRule . MsBuildRule . FailedProjectsOrSolutions . Select ( p => $ "- `{ p . FullPath } `") ) +
214
170
$ "\n Set up a [manual build command]({ buildCommandDocsUrl } ).";
215
171
message . Severity = DiagnosticMessage . TspSeverity . Error ;
216
172
0 commit comments