mirror of
https://github.com/github/codeql.git
synced 2026-04-25 00:35:20 +02:00
JS: Move getMegabyteCountFromPrefixedEnv into a shared place
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user