From d7d04d32b012f175f0ece93ffc7fdd82e591969d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDan=20O=C5=BEbot?= Date: Wed, 14 Aug 2024 10:40:17 +0200 Subject: [PATCH] Implement docker-compose file strategy --- .../quarkus/project/quarkus/codestart.yml | 1 + .../CodestartFileStrategyHandler.java | 3 +- ...erComposeCodestartFileStrategyHandler.java | 57 +++++++++++++++++++ .../core/CodestartProcessorTest.java | 3 + 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/strategy/DockerComposeCodestartFileStrategyHandler.java diff --git a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/project/quarkus/codestart.yml b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/project/quarkus/codestart.yml index 2822b07c03a85..4385f60945648 100644 --- a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/project/quarkus/codestart.yml +++ b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/project/quarkus/codestart.yml @@ -8,6 +8,7 @@ output-strategy: "README.adoc": forbidden "readme.adoc": forbidden ".gitignore": append + "docker-compose-include-*": docker-compose-includes "src/main/resources/META-INF/resources/index.html": content-merge "src/main/resources/application.yml": smart-config-merge "src/main/resources/application.properties": forbidden diff --git a/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/strategy/CodestartFileStrategyHandler.java b/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/strategy/CodestartFileStrategyHandler.java index 56af40804f4e1..1883e850ceada 100644 --- a/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/strategy/CodestartFileStrategyHandler.java +++ b/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/strategy/CodestartFileStrategyHandler.java @@ -27,7 +27,8 @@ public interface CodestartFileStrategyHandler { new ForbiddenCodestartFileStrategyHandler(), new SmartConfigMergeCodestartFileStrategyHandler(), new SmartPomMergeCodestartFileStrategyHandler(), - new SmartPackageFileStrategyHandler()) + new SmartPackageFileStrategyHandler(), + new DockerComposeCodestartFileStrategyHandler()) .collect(Collectors.toMap(CodestartFileStrategyHandler::name, Function.identity())); String name(); diff --git a/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/strategy/DockerComposeCodestartFileStrategyHandler.java b/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/strategy/DockerComposeCodestartFileStrategyHandler.java new file mode 100644 index 0000000000000..f14c2448b76b4 --- /dev/null +++ b/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/strategy/DockerComposeCodestartFileStrategyHandler.java @@ -0,0 +1,57 @@ +package io.quarkus.devtools.codestarts.core.strategy; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import io.quarkus.devtools.codestarts.CodestartStructureException; +import io.quarkus.devtools.codestarts.core.reader.TargetFile; + +public class DockerComposeCodestartFileStrategyHandler implements CodestartFileStrategyHandler { + + static final String NAME = "docker-compose-includes"; + + final String INCLUDE_LINE_IDENTIFIER = "include:\n"; + + @Override + public String name() { + return NAME; + } + + @Override + @SuppressWarnings("unchecked") + public void process(Path targetDirectory, String relativePath, List codestartFiles, Map data) + throws IOException { + List includes = (List) data.getOrDefault(NAME, new ArrayList()); + + for (TargetFile targetFile : codestartFiles) { + String filename = targetFile.getSourceName(); + writeFile(targetDirectory.resolve(filename), targetFile.getContent()); + + includes.add(filename); + } + data.put(NAME, includes); + + StringBuilder content = new StringBuilder(INCLUDE_LINE_IDENTIFIER); + for (String include : includes) { + content.append(" - ").append(include).append("\n"); + } + + final Path targetPath = targetDirectory.resolve("docker-compose.yml"); + writeFile(targetPath, content.toString()); + } + + @Override + public void writeFile(final Path targetPath, final String content) throws IOException { + if (Files.exists(targetPath) && !Files.readString(targetPath).startsWith(INCLUDE_LINE_IDENTIFIER)) { + throw new CodestartStructureException( + "Target file already exists: " + targetPath.toString()); + } + + Files.writeString(targetPath, content); + } + +} diff --git a/independent-projects/tools/codestarts/src/test/java/io/quarkus/devtools/codestarts/core/CodestartProcessorTest.java b/independent-projects/tools/codestarts/src/test/java/io/quarkus/devtools/codestarts/core/CodestartProcessorTest.java index 3fc71c03dea02..539a2aace8c4f 100644 --- a/independent-projects/tools/codestarts/src/test/java/io/quarkus/devtools/codestarts/core/CodestartProcessorTest.java +++ b/independent-projects/tools/codestarts/src/test/java/io/quarkus/devtools/codestarts/core/CodestartProcessorTest.java @@ -19,6 +19,7 @@ void checkSelectedDefaultStrategy() { Map spec = new HashMap<>(); spec.put("test/foo.tt", "forbidden"); spec.put("*", "replace"); + spec.put("docker-compose-include-test.yml", "docker-compose-includes"); final CodestartProcessor processor = new CodestartProcessor( MessageWriter.info(), @@ -29,6 +30,8 @@ void checkSelectedDefaultStrategy() { assertThat(processor.getSelectedDefaultStrategy()).isEqualTo(CodestartFileStrategyHandler.BY_NAME.get("replace")); assertThat(processor.getStrategy("test/foo.tt")).hasValue(CodestartFileStrategyHandler.BY_NAME.get("forbidden")); + assertThat(processor.getStrategy("docker-compose-include-test.yml")) + .hasValue(CodestartFileStrategyHandler.BY_NAME.get("docker-compose-includes")); } @Test