Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 467429c

Browse files
committed
Refactor env var code in Autobuilder class
1 parent fe65fb8 commit 467429c

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,26 @@ protected Autobuilder(IBuildActions actions, TAutobuildOptions options)
232232
return ret ?? new List<IProjectOrSolution>();
233233
});
234234

235-
CodeQLExtractorLangRoot = Actions.GetEnvironmentVariable($"CODEQL_EXTRACTOR_{this.Options.Language.UpperCaseName}_ROOT");
236-
CodeQlPlatform = Actions.GetEnvironmentVariable("CODEQL_PLATFORM");
235+
CodeQLExtractorLangRoot = Actions.GetEnvironmentVariable(EnvVars.Root(this.Options.Language));
236+
CodeQlPlatform = Actions.GetEnvironmentVariable(EnvVars.Platform);
237237

238-
TrapDir =
239-
Actions.GetEnvironmentVariable($"CODEQL_EXTRACTOR_{this.Options.Language.UpperCaseName}_TRAP_DIR") ??
240-
throw new InvalidEnvironmentException($"The environment variable CODEQL_EXTRACTOR_{this.Options.Language.UpperCaseName}_TRAP_DIR has not been set.");
238+
TrapDir = RequireEnvironmentVariable(EnvVars.TrapDir(this.Options.Language));
239+
SourceArchiveDir = RequireEnvironmentVariable(EnvVars.SourceArchiveDir(this.Options.Language));
240+
}
241241

242-
SourceArchiveDir =
243-
Actions.GetEnvironmentVariable($"CODEQL_EXTRACTOR_{this.Options.Language.UpperCaseName}_SOURCE_ARCHIVE_DIR") ??
244-
throw new InvalidEnvironmentException($"The environment variable CODEQL_EXTRACTOR_{this.Options.Language.UpperCaseName}_SOURCE_ARCHIVE_DIR has not been set.");
242+
/// <summary>
243+
/// Retrieves the value of an environment variable named <paramref name="name"> or throws
244+
/// an exception if no such environment variable has been set.
245+
/// </summary>
246+
/// <param name="name">The name of the environment variable.</param>
247+
/// <returns>The value of the environment variable.</returns>
248+
/// <exception cref="InvalidEnvironmentException">
249+
/// Thrown if the environment variable is not set.
250+
/// </exception>
251+
protected string RequireEnvironmentVariable(string name)
252+
{
253+
return Actions.GetEnvironmentVariable(name) ??
254+
throw new InvalidEnvironmentException($"The environment variable {name} has not been set.");
245255
}
246256

247257
protected string TrapDir { get; }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Semmle.Autobuild.Shared
2+
{
3+
public static class EnvVars
4+
{
5+
public const string Platform = "CODEQL_PLATFORM";
6+
public static string Root(Language language) => $"CODEQL_EXTRACTOR_{language.UpperCaseName}_ROOT";
7+
public static string TrapDir(Language language) => $"CODEQL_EXTRACTOR_{language.UpperCaseName}_TRAP_DIR";
8+
public static string SourceArchiveDir(Language language) => $"CODEQL_EXTRACTOR_{language.UpperCaseName}_SOURCE_ARCHIVE_DIR";
9+
}
10+
}

0 commit comments

Comments
 (0)