Skip to content

Commit

Permalink
quarkiverse#192 Change OpenApiGeneratorCodeGenBase#shouldRun to thro…
Browse files Browse the repository at this point in the history
…w an exception rather than return false (quarkiverse#314)

* quarkiverse#192 Change OpenApiGeneratorCodeGenBase#shouldRun to throw an exception rather than return fals

* quarkiverse#192 add input-base-dir path on properties to generate-code-tests step

* Revert "quarkiverse#192 add input-base-dir path on properties to generate-code-tests step"

This reverts commit db74b9e.

* quarkiverse#192 add condition to test path
  • Loading branch information
brunobaiano committed Apr 28, 2023
1 parent b7cdc32 commit 84060f8
Showing 1 changed file with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import org.eclipse.microprofile.config.Config;
Expand Down Expand Up @@ -66,11 +67,19 @@ public String inputDirectory() {

@Override
public boolean shouldRun(Path sourceDir, Config config) {
String inputBaseDir = getInputBaseDirRelativeToModule(sourceDir, config);
if (inputBaseDir != null && !Files.isDirectory(Path.of(inputBaseDir))) {
throw new OpenApiGeneratorException(String.format("Invalid path on %s: %s", INPUT_BASE_DIR, inputBaseDir));
}
return inputBaseDir != null || Files.isDirectory(sourceDir);
Optional<String> inputBaseDir = getInputBaseDirRelativeToModule(sourceDir, config);
inputBaseDir.ifPresentOrElse(s -> {
if (!Files.isDirectory(Path.of(s))) {
throw new OpenApiGeneratorException(String.format("Invalid path on %s: %s", INPUT_BASE_DIR, s));
}
}, () -> {
if (!Files.isDirectory(sourceDir)
&& !sourceDir.endsWith(Path.of("src", "test", this.inputDirectory()))) {
throw new OpenApiGeneratorException(String.format("Invalid path on %s: %s", INPUT_BASE_DIR, sourceDir));
}
});

return true;
}

protected boolean isRestEasyReactive(CodeGenContext context) {
Expand All @@ -82,8 +91,8 @@ protected boolean isRestEasyReactive(CodeGenContext context) {
@Override
public boolean trigger(CodeGenContext context) throws CodeGenException {
final Path outDir = context.outDir();
String inputBaseDir = getInputBaseDirRelativeToModule(context.inputDir(), context.config());
final Path openApiDir = inputBaseDir != null ? Path.of(inputBaseDir) : context.inputDir();
Optional<String> inputBaseDir = getInputBaseDirRelativeToModule(context.inputDir(), context.config());
final Path openApiDir = inputBaseDir.map(Path::of).orElseGet(context::inputDir);
final List<String> filesToInclude = context.config().getOptionalValues(INCLUDE_FILES, String.class).orElse(List.of());
final List<String> filesToExclude = context.config().getOptionalValues(EXCLUDE_FILES, String.class).orElse(List.of());

Expand Down Expand Up @@ -133,7 +142,8 @@ private static boolean isExtensionCapabilityPresent(CodeGenContext context, Stri
}

// TODO: do not generate if the output dir has generated files and the openapi file has the same checksum of the previous run
protected void generate(final Config config, final Path openApiFilePath, final Path outDir, boolean isRestEasyReactive) {
protected void generate(final Config config, final Path openApiFilePath, final Path outDir,
boolean isRestEasyReactive) {
final String basePackage = getBasePackage(config, openApiFilePath);
final Boolean verbose = config.getOptionalValue(VERBOSE_PROPERTY_NAME, Boolean.class).orElse(false);
final Boolean validateSpec = config.getOptionalValue(VALIDATE_SPEC_PROPERTY_NAME, Boolean.class).orElse(true);
Expand Down Expand Up @@ -192,10 +202,10 @@ private String getBasePackage(final Config config, final Path openApiFilePath) {
.orElse(String.format("%s.%s", DEFAULT_PACKAGE, getSanitizedFileName(openApiFilePath)));
}

private String getInputBaseDirRelativeToModule(final Path sourceDir, final Config config) {
private Optional<String> getInputBaseDirRelativeToModule(final Path sourceDir, final Config config) {
return config.getOptionalValue(INPUT_BASE_DIR, String.class).map(inputBaseDir -> {
int srcIndex = sourceDir.toString().lastIndexOf("src");
return srcIndex < 0 ? null : sourceDir.toString().substring(0, srcIndex) + inputBaseDir;
}).orElse(null);
});
}
}

0 comments on commit 84060f8

Please sign in to comment.