Skip to content

Commit

Permalink
Reviewed client Util, adding docs (com-lihaoyi#3846)
Browse files Browse the repository at this point in the history
Pull request: com-lihaoyi#3846
  • Loading branch information
lefou authored Oct 26, 2024
1 parent f66304d commit 8c646cb
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions main/client/src/mill/main/client/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,19 @@ static String sha1Hash(String path) throws NoSuchAlgorithmException {
}

/**
* Reads a file, ignoring empty or comment lines
* Reads a file, ignoring empty or comment lines, interpolating env variables.
*
* @return The non-empty lines of the files or an empty list, if the file does not exists
*/
public static List<String> readOptsFileLines(final File file) {
final List<String> vmOptions = new LinkedList<>();
try (final Scanner sc = new Scanner(file)) {
final Map<String, String> env = System.getenv();
while (sc.hasNextLine()) {
String arg = sc.nextLine();
String trimmed = arg.trim();
if (!trimmed.isEmpty() && !trimmed.startsWith("#")) {
vmOptions.add(interpolateEnvVars(arg, System.getenv()));
vmOptions.add(interpolateEnvVars(arg, env));
}
}
} catch (FileNotFoundException e) {
Expand All @@ -155,7 +156,11 @@ public static List<String> readOptsFileLines(final File file) {
return vmOptions;
}

public static String interpolateEnvVars(String input, Map<String, String> env){
/**
* Interpolate variables in the form of <code>${VARIABLE}</code> based on the given Map <code>env</code>.
* Missing vars will be replaced by the empty string.
*/
public static String interpolateEnvVars(String input, Map<String, String> env) {
Matcher matcher = envInterpolatorPattern.matcher(input);
// StringBuilder to store the result after replacing
StringBuffer result = new StringBuffer();
Expand All @@ -174,6 +179,6 @@ public static String interpolateEnvVars(String input, Map<String, String> env){
return result.toString();
}

static Pattern envInterpolatorPattern = Pattern.compile("\\$\\{(\\$|[A-Z_][A-Z0-9_]*)\\}");
private static Pattern envInterpolatorPattern = Pattern.compile("\\$\\{(\\$|[A-Z_][A-Z0-9_]*)\\}");

}

0 comments on commit 8c646cb

Please sign in to comment.