Skip to content

Commit bc47646

Browse files
committed
JS: Move getMegabyteCountFromPrefixedEnv into a shared place
1 parent cf53956 commit bc47646

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

javascript/extractor/src/com/semmle/js/extractor/EnvironmentVariables.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.semmle.js.extractor;
22

3+
import com.semmle.util.data.UnitParser;
34
import com.semmle.util.exception.UserError;
45
import com.semmle.util.process.Env;
56
import com.semmle.util.process.Env.Var;
67

78
public class EnvironmentVariables {
89
public static final String CODEQL_EXTRACTOR_JAVASCRIPT_ROOT_ENV_VAR =
910
"CODEQL_EXTRACTOR_JAVASCRIPT_ROOT";
10-
11+
1112
public static final String CODEQL_EXTRACTOR_JAVASCRIPT_SCRATCH_DIR_ENV_VAR =
1213
"CODEQL_EXTRACTOR_JAVASCRIPT_SCRATCH_DIR";
1314

@@ -19,6 +20,36 @@ public class EnvironmentVariables {
1920

2021
public static final String CODEQL_DIST_ENV_VAR = "CODEQL_DIST";
2122

23+
/**
24+
* Returns a number of megabytes by reading an environment variable with the given suffix,
25+
* or the default value if not set.
26+
* <p>
27+
* The following prefixes are tried:
28+
* <code>CODEQL_EXTRACTOR_JAVASCRIPT_</code>,
29+
* <code>LGTM_</code>,
30+
* <code>SEMMLE_</code>.
31+
*/
32+
public static int getMegabyteCountFromPrefixedEnv(String suffix, int defaultValue) {
33+
String envVar = "CODEQL_EXTRACTOR_JAVASCRIPT_" + suffix;
34+
String value = Env.systemEnv().get(envVar);
35+
if (value == null || value.length() == 0) {
36+
envVar = "LGTM_" + suffix;
37+
value = Env.systemEnv().get(envVar);
38+
}
39+
if (value == null || value.length() == 0) {
40+
envVar = "SEMMLE_" + suffix;
41+
value = Env.systemEnv().get(envVar);
42+
}
43+
if (value == null || value.length() == 0) {
44+
return defaultValue;
45+
}
46+
Integer amount = UnitParser.parseOpt(value, UnitParser.MEGABYTES);
47+
if (amount == null) {
48+
throw new UserError("Invalid value for " + envVar + ": '" + value + "'");
49+
}
50+
return amount;
51+
}
52+
2253
/**
2354
* Gets the extractor root based on the <code>CODEQL_EXTRACTOR_JAVASCRIPT_ROOT</code> or <code>
2455
* SEMMLE_DIST</code> or environment variable, or <code>null</code> if neither is set.

javascript/extractor/src/com/semmle/ts/extractor/TypeScriptParser.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -273,32 +273,15 @@ private List<String> getNodeJsRuntimeInvocation(String... args) {
273273
return result;
274274
}
275275

276-
private static int getMegabyteCountFromPrefixedEnv(String suffix, int defaultValue) {
277-
String envVar = "SEMMLE_" + suffix;
278-
String value = Env.systemEnv().get(envVar);
279-
if (value == null || value.length() == 0) {
280-
envVar = "LGTM_" + suffix;
281-
value = Env.systemEnv().get(envVar);
282-
}
283-
if (value == null || value.length() == 0) {
284-
return defaultValue;
285-
}
286-
Integer amount = UnitParser.parseOpt(value, UnitParser.MEGABYTES);
287-
if (amount == null) {
288-
throw new UserError("Invalid value for " + envVar + ": '" + value + "'");
289-
}
290-
return amount;
291-
}
292-
293276
/** Start the Node.js parser wrapper process. */
294277
private void setupParserWrapper() {
295278
verifyNodeInstallation();
296279

297280
int mainMemoryMb =
298281
typescriptRam != 0
299282
? typescriptRam
300-
: getMegabyteCountFromPrefixedEnv(TYPESCRIPT_RAM_SUFFIX, 2000);
301-
int reserveMemoryMb = getMegabyteCountFromPrefixedEnv(TYPESCRIPT_RAM_RESERVE_SUFFIX, 400);
283+
: EnvironmentVariables.getMegabyteCountFromPrefixedEnv(TYPESCRIPT_RAM_SUFFIX, 2000);
284+
int reserveMemoryMb = EnvironmentVariables.getMegabyteCountFromPrefixedEnv(TYPESCRIPT_RAM_RESERVE_SUFFIX, 400);
302285

303286
System.out.println("Memory for TypeScript process: " + mainMemoryMb + " MB, and " + reserveMemoryMb + " MB reserve");
304287

0 commit comments

Comments
 (0)