allow atfiles to relativize for ${DIR} patterns

This commit is contained in:
Jonathan Shook 2024-01-11 16:48:47 -06:00
parent 77680f480f
commit e2d8f332fb
3 changed files with 24 additions and 1 deletions

View File

@ -104,6 +104,7 @@ public class NBAtFile {
filepathSpec=(filepathSpec.endsWith(".yaml") ? filepathSpec : filepathSpec+".yaml");
Path atPath = Path.of(filepathSpec);
String argsdata = "";
try {
argsdata = Files.readString(atPath);
@ -128,13 +129,28 @@ public class NBAtFile {
throw new RuntimeException("You can not traverse a non-map object type with spec '" + spec + "'");
}
}
return formatted(scopeOfInclude, fmt);
LinkedList<String> formatted = formatted(scopeOfInclude, fmt);
formatted=interposePath(formatted, atPath);
return formatted;
} else {
throw new RuntimeException("Unable to match at-file specifier: " + spec + " to pattern '" + includePattern.pattern() + "'");
}
}
private static LinkedList<String> interposePath(LinkedList<String> formatted, Path atPath) {
Path parent = (atPath.getNameCount()==1 ? Path.of(".") : atPath.getParent());
ListIterator<String> iter = formatted.listIterator();
while (iter.hasNext()) {
String word = iter.next();
String modified = word.replaceAll("\\$\\{DIR}",parent.toString());
iter.remove();
iter.add(modified);
}
return formatted;
}
private static LinkedList<String> formatted(Object scopeOfInclude, NBAtFileFormats fmt) {
LinkedList<String> emitted = new LinkedList<>();
if (scopeOfInclude instanceof Map<?,?> map) {

View File

@ -31,6 +31,12 @@ class NBAtFileTest {
assertThat(strings).containsExactly("arg1","arg2","arg3");
}
@Test
public void testRelativizedPaths() {
LinkedList<String> strings = NBAtFile.includeAt("@src/test/resources/atfiles/relativized.yaml");
assertThat(strings).containsExactly("--option1=src/test/resources/atfiles/value1");
}
@Test
public void testParseSimpleMapDefaultFmt() {
LinkedList<String> strings = NBAtFile.includeAt("@src/test/resources/atfiles/simple_map.yaml");

View File

@ -0,0 +1 @@
- --option1=${DIR}/value1