JS: Move getMegabyteCountFromPrefixedEnv into a shared place

This commit is contained in:
Asger F
2023-08-09 11:34:57 +02:00
parent cf53956d39
commit bc47646a79
2 changed files with 34 additions and 20 deletions

View File

@@ -1,5 +1,6 @@
package com.semmle.js.extractor;
import com.semmle.util.data.UnitParser;
import com.semmle.util.exception.UserError;
import com.semmle.util.process.Env;
import com.semmle.util.process.Env.Var;
@@ -7,7 +8,7 @@ import com.semmle.util.process.Env.Var;
public class EnvironmentVariables {
public static final String CODEQL_EXTRACTOR_JAVASCRIPT_ROOT_ENV_VAR =
"CODEQL_EXTRACTOR_JAVASCRIPT_ROOT";
public static final String CODEQL_EXTRACTOR_JAVASCRIPT_SCRATCH_DIR_ENV_VAR =
"CODEQL_EXTRACTOR_JAVASCRIPT_SCRATCH_DIR";
@@ -19,6 +20,36 @@ public class EnvironmentVariables {
public static final String CODEQL_DIST_ENV_VAR = "CODEQL_DIST";
/**
* Returns a number of megabytes by reading an environment variable with the given suffix,
* or the default value if not set.
* <p>
* The following prefixes are tried:
* <code>CODEQL_EXTRACTOR_JAVASCRIPT_</code>,
* <code>LGTM_</code>,
* <code>SEMMLE_</code>.
*/
public static int getMegabyteCountFromPrefixedEnv(String suffix, int defaultValue) {
String envVar = "CODEQL_EXTRACTOR_JAVASCRIPT_" + suffix;
String value = Env.systemEnv().get(envVar);
if (value == null || value.length() == 0) {
envVar = "LGTM_" + suffix;
value = Env.systemEnv().get(envVar);
}
if (value == null || value.length() == 0) {
envVar = "SEMMLE_" + suffix;
value = Env.systemEnv().get(envVar);
}
if (value == null || value.length() == 0) {
return defaultValue;
}
Integer amount = UnitParser.parseOpt(value, UnitParser.MEGABYTES);
if (amount == null) {
throw new UserError("Invalid value for " + envVar + ": '" + value + "'");
}
return amount;
}
/**
* Gets the extractor root based on the <code>CODEQL_EXTRACTOR_JAVASCRIPT_ROOT</code> or <code>
* SEMMLE_DIST</code> or environment variable, or <code>null</code> if neither is set.

View File

@@ -273,23 +273,6 @@ public class TypeScriptParser {
return result;
}
private static int getMegabyteCountFromPrefixedEnv(String suffix, int defaultValue) {
String envVar = "SEMMLE_" + suffix;
String value = Env.systemEnv().get(envVar);
if (value == null || value.length() == 0) {
envVar = "LGTM_" + suffix;
value = Env.systemEnv().get(envVar);
}
if (value == null || value.length() == 0) {
return defaultValue;
}
Integer amount = UnitParser.parseOpt(value, UnitParser.MEGABYTES);
if (amount == null) {
throw new UserError("Invalid value for " + envVar + ": '" + value + "'");
}
return amount;
}
/** Start the Node.js parser wrapper process. */
private void setupParserWrapper() {
verifyNodeInstallation();
@@ -297,8 +280,8 @@ public class TypeScriptParser {
int mainMemoryMb =
typescriptRam != 0
? typescriptRam
: getMegabyteCountFromPrefixedEnv(TYPESCRIPT_RAM_SUFFIX, 2000);
int reserveMemoryMb = getMegabyteCountFromPrefixedEnv(TYPESCRIPT_RAM_RESERVE_SUFFIX, 400);
: EnvironmentVariables.getMegabyteCountFromPrefixedEnv(TYPESCRIPT_RAM_SUFFIX, 2000);
int reserveMemoryMb = EnvironmentVariables.getMegabyteCountFromPrefixedEnv(TYPESCRIPT_RAM_RESERVE_SUFFIX, 400);
System.out.println("Memory for TypeScript process: " + mainMemoryMb + " MB, and " + reserveMemoryMb + " MB reserve");