11package com .semmle .js .extractor ;
22
3+ import com .semmle .util .data .UnitParser ;
34import com .semmle .util .exception .UserError ;
45import com .semmle .util .process .Env ;
56import com .semmle .util .process .Env .Var ;
67
78public 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.
0 commit comments