|
7 | 7 | import java.security.MessageDigest;
|
8 | 8 | import java.security.NoSuchAlgorithmException;
|
9 | 9 | import java.util.Locale;
|
| 10 | +import java.util.Map; |
| 11 | +import java.util.regex.Matcher; |
| 12 | +import java.util.regex.Pattern; |
10 | 13 |
|
11 | 14 | public class Util {
|
12 | 15 |
|
@@ -71,4 +74,48 @@ public static String hexArray(byte[] bytes) {
|
71 | 74 | public static boolean hasConsole() {
|
72 | 75 | return hasConsole0;
|
73 | 76 | }
|
| 77 | + |
| 78 | + public static String readYamlHeader(java.nio.file.Path buildFile) throws java.io.IOException { |
| 79 | + java.util.List<String> lines = java.nio.file.Files.readAllLines(buildFile); |
| 80 | + String yamlString = lines.stream() |
| 81 | + .filter(line -> line.startsWith("//|")) |
| 82 | + .map(line -> line.substring(4)) // Remove the `//|` prefix |
| 83 | + .collect(java.util.stream.Collectors.joining("\n")); |
| 84 | + |
| 85 | + return yamlString; |
| 86 | + } |
| 87 | + |
| 88 | + private static String envInterpolatorPattern0 = "(\\$|[A-Z_][A-Z0-9_]*)"; |
| 89 | + private static Pattern envInterpolatorPattern = |
| 90 | + Pattern.compile("\\$\\{" + envInterpolatorPattern0 + "\\}|\\$" + envInterpolatorPattern0); |
| 91 | + |
| 92 | + /** |
| 93 | + * Interpolate variables in the form of <code>${VARIABLE}</code> based on the given Map <code>env</code>. |
| 94 | + * Missing vars will be replaced by the empty string. |
| 95 | + */ |
| 96 | + public static String interpolateEnvVars(String input, Map<String, String> env0) { |
| 97 | + Matcher matcher = envInterpolatorPattern.matcher(input); |
| 98 | + // StringBuilder to store the result after replacing |
| 99 | + StringBuffer result = new StringBuffer(); |
| 100 | + |
| 101 | + Map<String, String> env = new java.util.HashMap<>(); |
| 102 | + env.putAll(env0); |
| 103 | + |
| 104 | + env.put("MILL_VERSION", mill.constants.BuildInfo.millVersion); |
| 105 | + while (matcher.find()) { |
| 106 | + String match = matcher.group(1); |
| 107 | + if (match == null) match = matcher.group(2); |
| 108 | + if (match.equals("$")) { |
| 109 | + matcher.appendReplacement(result, "\\$"); |
| 110 | + } else { |
| 111 | + String envVarValue; |
| 112 | + mill.constants.DebugLog.println("MATCH " + match); |
| 113 | + envVarValue = env.containsKey(match) ? env.get(match) : ""; |
| 114 | + matcher.appendReplacement(result, envVarValue); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + matcher.appendTail(result); // Append the remaining part of the string |
| 119 | + return result.toString(); |
| 120 | + } |
74 | 121 | }
|
0 commit comments