Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reviewed client Util, adding docs #3846

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_]*)\\}");

}
Loading