use of com.hubspot.jinjava.loader.FileLocator in project curiostack by curioswitch.
the class ConvertConfigsToJsonTask method exec.
@TaskAction
void exec() {
var jinJava = new Jinjava(JinjavaConfig.newBuilder().withTrimBlocks(true).build());
try {
jinJava.setResourceLocator(new FileLocator(getProject().getProjectDir()));
} catch (FileNotFoundException e) {
throw new IllegalStateException("Could not initialize Jinjava");
}
var project = getProject();
for (var file : getInputFiles()) {
Path path = file.toPath();
if (!path.toString().endsWith(".tf.yml") && !path.toString().endsWith(".tf.yaml")) {
var outDir = outputDir.resolve(project.getProjectDir().toPath().relativize(path.getParent()));
if (path.toString().contains(".jinja.")) {
var outPath = outDir.resolve(path.getFileName().toString().replace(".jinja.", "."));
try {
Files.createDirectories(outDir);
Files.writeString(outPath, jinJava.render(Files.readString(path), ImmutableMap.of()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} else {
// Copy non-config files (usually templates) as is.
project.copy(copy -> {
copy.from(path);
copy.into(outDir);
});
}
continue;
}
String jsonFilename = getNameWithoutExtension(path.getFileName().toString()) + ".json";
Path relativePath = getProject().getProjectDir().toPath().relativize(path);
final Path outPath;
if (relativePath.getNameCount() > 1) {
if (!relativePath.startsWith("modules")) {
outPath = outputDir.resolve(flattenFilename(relativePath, jsonFilename));
} else {
// Modules will always have a top-level directory where this should go.
outPath = outputDir.resolve(relativePath.subpath(0, 3)).resolveSibling(flattenFilename(relativePath, jsonFilename));
}
} else {
outPath = outputDir.resolve(project.getProjectDir().toPath().relativize(file.toPath())).resolveSibling(jsonFilename);
}
try {
createDirectories(outPath.getParent());
} catch (IOException e) {
throw new UncheckedIOException("Could not create output directory.", e);
}
workerExecutor.noIsolation().submit(ConvertToJson.class, parameters -> {
parameters.getProjectDirectory().set(project.getProjectDir());
parameters.getSourceFile().set(file);
parameters.getOutFile().set(outPath.toFile());
});
}
}
Aggregations