From 8af488dbd96f902872171d76b69bd48971897067 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 14 Nov 2023 14:46:19 +0000 Subject: [PATCH] Add backward compatible support for multiple extensions in CodeGenProvider --- .../quarkus/deployment/CodeGenProvider.java | 18 +++++++++++- .../deployment/dev/CodeGenWatcher.java | 28 ++++++++++--------- .../deployment/AvroCodeGenProviderBase.java | 13 ++++++--- .../deployment/AvroIDLCodeGenProvider.java | 4 +-- .../AvroProtocolCodeGenProvider.java | 4 +-- .../deployment/AvroSchemaCodeGenProvider.java | 4 +-- .../quarkus/grpc/deployment/GrpcCodeGen.java | 4 +-- 7 files changed, 49 insertions(+), 26 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/CodeGenProvider.java b/core/deployment/src/main/java/io/quarkus/deployment/CodeGenProvider.java index c0c759cbb110e..e5910422f46af 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/CodeGenProvider.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/CodeGenProvider.java @@ -22,11 +22,27 @@ public interface CodeGenProvider { /** * File extension that CodeGenProvider will generate code from + * Deprecated: use inputExtensions instead * * @return file extension */ + @Deprecated + default String inputExtension() { + return null; + } + + /** + * File extensions that CodeGenProvider will generate code from + * + * @return file extensions + */ @NotNull - String inputExtension(); + default String[] inputExtensions() { + if (inputExtension() != null) { + return new String[] { inputExtension() }; + } + return new String[] {}; + } /** * Name of the directory containing input files for a given {@link CodeGenProvider} implementation diff --git a/core/deployment/src/main/java/io/quarkus/deployment/dev/CodeGenWatcher.java b/core/deployment/src/main/java/io/quarkus/deployment/dev/CodeGenWatcher.java index 76b76cfe8dd0e..a6ad84813053c 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/dev/CodeGenWatcher.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/dev/CodeGenWatcher.java @@ -41,19 +41,21 @@ class CodeGenWatcher { final Config config = CodeGenerator.getConfig(curatedApplication.getApplicationModel(), LaunchMode.DEVELOPMENT, properties, deploymentClassLoader); for (CodeGenData codeGen : codeGens) { - watchers.add(new FSWatchUtil.Watcher(codeGen.sourceDir, codeGen.provider.inputExtension(), - modifiedPaths -> { - codeGenLock.lock(); - try { - CodeGenerator.trigger(deploymentClassLoader, - codeGen, - curatedApplication.getApplicationModel(), config, false); - } catch (Exception any) { - log.warn("Code generation failed", any); - } finally { - codeGenLock.unlock(); - } - })); + for (String ext : codeGen.provider.inputExtensions()) { + watchers.add(new FSWatchUtil.Watcher(codeGen.sourceDir, ext, + modifiedPaths -> { + codeGenLock.lock(); + try { + CodeGenerator.trigger(deploymentClassLoader, + codeGen, + curatedApplication.getApplicationModel(), config, false); + } catch (Exception any) { + log.warn("Code generation failed", any); + } finally { + codeGenLock.unlock(); + } + })); + } } fsWatchUtil = new FSWatchUtil(); fsWatchUtil.observe(watchers, 500); diff --git a/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroCodeGenProviderBase.java b/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroCodeGenProviderBase.java index b759abf4bf197..b9f29f2a61fb8 100644 --- a/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroCodeGenProviderBase.java +++ b/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroCodeGenProviderBase.java @@ -4,6 +4,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -11,6 +12,7 @@ import java.util.Locale; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.avro.generic.GenericData; import org.eclipse.microprofile.config.Config; @@ -86,7 +88,8 @@ private Collection gatherAllFiles(Path importPath) throws CodeGenException } try { return Files.find(importPath, 20, - (path, ignored) -> Files.isRegularFile(path) && path.toString().endsWith("." + inputExtension())) + (path, ignored) -> Files.isRegularFile(path) + && Arrays.stream(inputExtensions()).anyMatch(ext -> path.toString().endsWith("." + ext))) .map(Path::toAbsolutePath) .collect(Collectors.toList()); } catch (IOException e) { @@ -194,9 +197,11 @@ private boolean getBooleanProperty(String propName, boolean defaultValue) { } public String[] getImports(Config config) { - return config.getOptionalValue("avro.codegen." + inputExtension() + ".imports", String.class) - .map(i -> i.split(",")) - .orElse(EMPTY); + return Arrays.stream(inputExtensions()) + .flatMap(ext -> config.getOptionalValue("avro.codegen." + ext + ".imports", String.class) + .map(i -> Arrays.stream(i.split(","))).stream()) + .reduce(Stream.empty(), Stream::concat) + .toArray(String[]::new); } @Override diff --git a/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroIDLCodeGenProvider.java b/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroIDLCodeGenProvider.java index fd89cec14c1a9..ccbc6ac761f23 100644 --- a/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroIDLCodeGenProvider.java +++ b/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroIDLCodeGenProvider.java @@ -18,8 +18,8 @@ public String providerId() { } @Override - public String inputExtension() { - return "avdl"; + public String[] inputExtensions() { + return new String[] { "avdl" }; } @Override diff --git a/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroProtocolCodeGenProvider.java b/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroProtocolCodeGenProvider.java index 41e414648e4ae..c7df5be5c0788 100644 --- a/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroProtocolCodeGenProvider.java +++ b/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroProtocolCodeGenProvider.java @@ -22,8 +22,8 @@ public String providerId() { } @Override - public String inputExtension() { - return "avpr"; + public String[] inputExtensions() { + return new String[] { "avpr" }; } @Override diff --git a/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroSchemaCodeGenProvider.java b/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroSchemaCodeGenProvider.java index 505c865fae661..2ceaf8d038ded 100644 --- a/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroSchemaCodeGenProvider.java +++ b/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroSchemaCodeGenProvider.java @@ -26,8 +26,8 @@ public String providerId() { } @Override - public String inputExtension() { - return "avsc"; + public String[] inputExtensions() { + return new String[] { "avsc" }; } void init() { diff --git a/extensions/grpc/codegen/src/main/java/io/quarkus/grpc/deployment/GrpcCodeGen.java b/extensions/grpc/codegen/src/main/java/io/quarkus/grpc/deployment/GrpcCodeGen.java index 6c5417239d0a3..fb6c31f12404e 100644 --- a/extensions/grpc/codegen/src/main/java/io/quarkus/grpc/deployment/GrpcCodeGen.java +++ b/extensions/grpc/codegen/src/main/java/io/quarkus/grpc/deployment/GrpcCodeGen.java @@ -70,8 +70,8 @@ public String providerId() { } @Override - public String inputExtension() { - return "proto"; + public String[] inputExtensions() { + return new String[] { "proto" }; } @Override