Search in sources :

Example 1 with FileLocator

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());
        });
    }
}
Also used : Path(java.nio.file.Path) Jinjava(com.hubspot.jinjava.Jinjava) FileLocator(com.hubspot.jinjava.loader.FileLocator) FileNotFoundException(java.io.FileNotFoundException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

Jinjava (com.hubspot.jinjava.Jinjava)1 FileLocator (com.hubspot.jinjava.loader.FileLocator)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Path (java.nio.file.Path)1 TaskAction (org.gradle.api.tasks.TaskAction)1