Skip to content

Commit

Permalink
Merge pull request #42512 from zanozbot/main
Browse files Browse the repository at this point in the history
Generate `docker-compose.yml` from includes
  • Loading branch information
aloubyansky authored Aug 15, 2024
2 parents cbebd63 + d7d04d3 commit c06e5df
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TargetFile> codestartFiles, Map<String, Object> data)
throws IOException {
List<String> includes = (List<String>) data.getOrDefault(NAME, new ArrayList<String>());

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void checkSelectedDefaultStrategy() {
Map<String, String> 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(),
Expand All @@ -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
Expand Down

0 comments on commit c06e5df

Please sign in to comment.