@@ -20,6 +20,14 @@ interface IBuildRule
2020 BuildScript Analyse ( Autobuilder builder , bool auto ) ;
2121 }
2222
23+ /// <summary>
24+ /// Exception indicating that environment variables are missing or invalid.
25+ /// </summary>
26+ class InvalidEnvironmentException : Exception
27+ {
28+ public InvalidEnvironmentException ( string m ) : base ( m ) { }
29+ }
30+
2331 /// <summary>
2432 /// Main application logic, containing all data
2533 /// gathered from the project and filesystem.
@@ -69,7 +77,7 @@ public class Autobuilder
6977 /// List of project/solution files to build.
7078 /// </summary>
7179 public IList < IProjectOrSolution > ProjectsOrSolutionsToBuild => projectsOrSolutionsToBuildLazy . Value ;
72- readonly Lazy < IList < IProjectOrSolution > > projectsOrSolutionsToBuildLazy ;
80+ private readonly Lazy < IList < IProjectOrSolution > > projectsOrSolutionsToBuildLazy ;
7381
7482 /// <summary>
7583 /// Holds if a given path was found.
@@ -129,7 +137,7 @@ public Autobuilder(IBuildActions actions, AutobuildOptions options)
129137
130138 projectsOrSolutionsToBuildLazy = new Lazy < IList < IProjectOrSolution > > ( ( ) =>
131139 {
132- List < IProjectOrSolution > ret ;
140+ List < IProjectOrSolution > ? ret ;
133141 if ( options . Solution . Any ( ) )
134142 {
135143 ret = new List < IProjectOrSolution > ( ) ;
@@ -143,7 +151,7 @@ public Autobuilder(IBuildActions actions, AutobuildOptions options)
143151 return ret ;
144152 }
145153
146- IEnumerable < IProjectOrSolution > FindFiles ( string extension , Func < string , ProjectOrSolution > create )
154+ IEnumerable < IProjectOrSolution > ? FindFiles ( string extension , Func < string , ProjectOrSolution > create )
147155 {
148156 var matchingFiles = GetExtensions ( extension ) .
149157 Select ( p => ( ProjectOrSolution : create ( p . Item1 ) , DistanceFromRoot : p . Item2 ) ) .
@@ -178,18 +186,39 @@ IEnumerable<IProjectOrSolution> FindFiles(string extension, Func<string, Project
178186
179187 CodeQLExtractorCSharpRoot = Actions . GetEnvironmentVariable ( "CODEQL_EXTRACTOR_CSHARP_ROOT" ) ;
180188
181- CodeQLJavaHome = Actions . GetEnvironmentVariable ( "CODEQL_JAVA_HOME" ) ;
182-
183189 SemmleDist = Actions . GetEnvironmentVariable ( "SEMMLE_DIST" ) ;
184190
191+ CodeQLJavaHome = Actions . GetEnvironmentVariable ( "CODEQL_JAVA_HOME" ) ;
192+
185193 SemmleJavaHome = Actions . GetEnvironmentVariable ( "SEMMLE_JAVA_HOME" ) ;
186194
195+ if ( CodeQLJavaHome is null && SemmleJavaHome is null )
196+ throw new InvalidEnvironmentException ( "The environment variables CODEQL_JAVA_HOME and SEMMLE_JAVA_HOME have not been set." ) ;
197+
187198 SemmlePlatformTools = Actions . GetEnvironmentVariable ( "SEMMLE_PLATFORM_TOOLS" ) ;
188199
189- if ( CodeQLExtractorCSharpRoot == null && SemmleDist == null )
190- Log ( Severity . Error , "The environment variables CODEQL_EXTRACTOR_CSHARP_ROOT and SEMMLE_DIST have not been set." ) ;
200+ if ( CodeQLExtractorCSharpRoot is null && SemmleDist is null )
201+ throw new InvalidEnvironmentException ( "The environment variables CODEQL_EXTRACTOR_CSHARP_ROOT and SEMMLE_DIST have not been set." ) ;
202+
203+ var trapDir = Actions . GetEnvironmentVariable ( "CODEQL_EXTRACTOR_CSHARP_TRAP_DIR" ) ?? Actions . GetEnvironmentVariable ( "TRAP_FOLDER" ) ;
204+
205+ if ( trapDir is null )
206+ throw new InvalidEnvironmentException ( "The environment variables CODEQL_EXTRACTOR_CSHARP_TRAP_DIR and TRAP_FOLDER have not been set." ) ;
207+
208+ TrapDir = trapDir ;
209+
210+ var sourceArchiveDir = Actions . GetEnvironmentVariable ( "CODEQL_EXTRACTOR_CSHARP_SOURCE_ARCHIVE_DIR" ) ?? Actions . GetEnvironmentVariable ( "SOURCE_ARCHIVE" ) ;
211+
212+ if ( sourceArchiveDir is null )
213+ throw new InvalidEnvironmentException ( "The environment variables CODEQL_EXTRACTOR_CSHARP_SOURCE_ARCHIVE_DIR and SOURCE_ARCHIVE have not been set." ) ;
214+
215+ SourceArchiveDir = sourceArchiveDir ;
191216 }
192217
218+ private string TrapDir { get ; }
219+
220+ private string SourceArchiveDir { get ; }
221+
193222 readonly ILogger logger = new ConsoleLogger ( Verbosity . Info ) ;
194223
195224 /// <summary>
@@ -271,9 +300,9 @@ BuildScript CheckExtractorRun(bool warnOnFailure) =>
271300 break ;
272301 case CSharpBuildStrategy . Auto :
273302 var cleanTrapFolder =
274- BuildScript . DeleteDirectory ( Actions . GetEnvironmentVariable ( "CODEQL_EXTRACTOR_CSHARP_TRAP_DIR" ) ?? Actions . GetEnvironmentVariable ( "TRAP_FOLDER" ) ) ;
303+ BuildScript . DeleteDirectory ( TrapDir ) ;
275304 var cleanSourceArchive =
276- BuildScript . DeleteDirectory ( Actions . GetEnvironmentVariable ( "CODEQL_EXTRACTOR_CSHARP_SOURCE_ARCHIVE_DIR" ) ?? Actions . GetEnvironmentVariable ( "SOURCE_ARCHIVE" ) ) ;
305+ BuildScript . DeleteDirectory ( SourceArchiveDir ) ;
277306 var tryCleanExtractorArgsLogs =
278307 BuildScript . Create ( actions =>
279308 {
@@ -376,38 +405,43 @@ BuildScript AutobuildFailure() =>
376405 /// <summary>
377406 /// Value of CODEQL_EXTRACTOR_CSHARP_ROOT environment variable.
378407 /// </summary>
379- public string CodeQLExtractorCSharpRoot { get ; private set ; }
408+ private string ? CodeQLExtractorCSharpRoot { get ; }
380409
381410 /// <summary>
382411 /// Value of CODEQL_JAVA_HOME environment variable.
383412 /// </summary>
384- public string CodeQLJavaHome { get ; private set ; }
413+ private string ? CodeQLJavaHome { get ; }
385414
386415 /// <summary>
387416 /// Value of SEMMLE_DIST environment variable.
388417 /// </summary>
389- public string SemmleDist { get ; private set ; }
418+ private string ? SemmleDist { get ; }
419+
420+ public string Distribution => CodeQLExtractorCSharpRoot ?? SemmleDist ! ;
421+
422+ public string JavaHome => CodeQLJavaHome ?? SemmleJavaHome ! ;
390423
391424 /// <summary>
392425 /// Value of SEMMLE_JAVA_HOME environment variable.
393426 /// </summary>
394- public string SemmleJavaHome { get ; private set ; }
427+ public string ? SemmleJavaHome { get ; private set ; }
395428
396429 /// <summary>
397430 /// Value of SEMMLE_PLATFORM_TOOLS environment variable.
398431 /// </summary>
399- public string SemmlePlatformTools { get ; private set ; }
432+ public string ? SemmlePlatformTools { get ; private set ; }
400433
401434 /// <summary>
402435 /// The absolute path of the odasa executable.
436+ /// null if we are running in CodeQL.
403437 /// </summary>
404- public string Odasa => SemmleDist == null ? null : Actions . PathCombine ( SemmleDist , "tools" , "odasa" ) ;
438+ public string ? Odasa => SemmleDist is null ? null : Actions . PathCombine ( SemmleDist , "tools" , "odasa" ) ;
405439
406440 /// <summary>
407441 /// Construct a command that executed the given <paramref name="cmd"/> wrapped in
408442 /// an <code>odasa --index</code>, unless indexing has been disabled, in which case
409443 /// <paramref name="cmd"/> is run directly.
410444 /// </summary>
411- internal CommandBuilder MaybeIndex ( CommandBuilder builder , string cmd ) => Options . Indexing ? builder . IndexCommand ( Odasa , cmd ) : builder . RunCommand ( cmd ) ;
445+ internal CommandBuilder MaybeIndex ( CommandBuilder builder , string cmd ) => Options . Indexing && ! ( Odasa is null ) ? builder . IndexCommand ( Odasa , cmd ) : builder . RunCommand ( cmd ) ;
412446 }
413447}
0 commit comments