|
1 | 1 | package io.quarkiverse.openapi.server.generator.deployment.codegen; |
2 | 2 |
|
| 3 | +import static io.quarkiverse.openapi.server.generator.deployment.ServerCodegenConfig.DEFAULT_DIR; |
| 4 | + |
3 | 5 | import java.io.File; |
4 | 6 | import java.nio.file.Files; |
5 | 7 | import java.nio.file.Path; |
6 | 8 | import java.util.Arrays; |
| 9 | +import java.util.Optional; |
7 | 10 |
|
8 | 11 | import org.eclipse.microprofile.config.Config; |
9 | 12 | import org.slf4j.Logger; |
@@ -36,27 +39,34 @@ public String inputDirectory() { |
36 | 39 | return "resources"; |
37 | 40 | } |
38 | 41 |
|
39 | | - private Path getInputBaseDir(final Path sourceDir, final Config config) { |
40 | | - return config.getOptionalValue(CodegenConfig.getInputBaseDirPropertyName(), String.class) |
41 | | - .map(inputBaseDir -> { |
42 | | - int srcIndex = sourceDir.toString().lastIndexOf("src"); |
43 | | - return Path.of(sourceDir.toString().substring(0, srcIndex), inputBaseDir); |
44 | | - }).orElse(Path.of(sourceDir.toString(), "openapi")); |
| 42 | + private Optional<String> getInputBaseDirRelativeToModule(final Path sourceDir, final Config config) { |
| 43 | + return config.getOptionalValue(CodegenConfig.getInputBaseDirPropertyName(), String.class).map(baseDir -> { |
| 44 | + int srcIndex = sourceDir.toString().lastIndexOf("src"); |
| 45 | + return srcIndex < 0 ? null : Path.of(sourceDir.toString().substring(0, srcIndex), baseDir).toString(); |
| 46 | + }); |
45 | 47 | } |
46 | 48 |
|
47 | 49 | @Override |
48 | 50 | public boolean shouldRun(Path sourceDir, Config config) { |
49 | | - boolean specIsPresent = config.getOptionalValue(CodegenConfig.getSpecPropertyName(), String.class).isPresent(); |
50 | | - if (!specIsPresent) { |
| 51 | + Optional<String> possibleSpecPropertyName = config.getOptionalValue(CodegenConfig.getSpecPropertyName(), String.class); |
| 52 | + if (possibleSpecPropertyName.isEmpty()) { |
51 | 53 | log.warn("The {} property is not present, the code generation will be ignored", |
52 | 54 | CodegenConfig.getSpecPropertyName()); |
| 55 | + return false; |
| 56 | + } |
| 57 | + String specPropertyName = possibleSpecPropertyName.get(); |
| 58 | + String relativeInputBaseDir = getInputBaseDirRelativeToModule(sourceDir, config).orElse(null); |
| 59 | + if (relativeInputBaseDir != null) { |
| 60 | + return Files.exists(Path.of(relativeInputBaseDir).resolve(specPropertyName)); |
| 61 | + } else { |
| 62 | + return Files.exists(sourceDir.resolve(DEFAULT_DIR).resolve(specPropertyName)); |
53 | 63 | } |
54 | | - return specIsPresent; |
55 | 64 | } |
56 | 65 |
|
57 | 66 | @Override |
58 | 67 | public boolean trigger(CodeGenContext context) throws CodeGenException { |
59 | | - final Path openApiDir = getInputBaseDir(context.inputDir(), context.config()); |
| 68 | + final Path openApiDir = Path.of(getInputBaseDirRelativeToModule(context.inputDir(), context.config()) |
| 69 | + .orElse(context.inputDir().resolve(DEFAULT_DIR).toString())); |
60 | 70 |
|
61 | 71 | validateOpenApiDir(context, openApiDir); |
62 | 72 |
|
|
0 commit comments