From 315a25498bae4ca4cfdfd2cab932e48d59f07a00 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 8 Jan 2024 10:56:31 +0200 Subject: [PATCH] Make Picocli version providers unremovable classes (cherry picked from commit 8161071196200925c2be70c0664a6f8898c7e905) --- .../picocli/deployment/PicocliProcessor.java | 13 ++++++++++ .../it/picocli/EntryWithVersionCommand.java | 9 +++++++ .../quarkus/it/picocli/VersionProvider.java | 22 ++++++++++++++++ .../io/quarkus/it/picocli/TestVersion.java | 25 +++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 integration-tests/picocli/src/main/java/io/quarkus/it/picocli/EntryWithVersionCommand.java create mode 100644 integration-tests/picocli/src/main/java/io/quarkus/it/picocli/VersionProvider.java create mode 100644 integration-tests/picocli/src/test/java/io/quarkus/it/picocli/TestVersion.java diff --git a/extensions/picocli/deployment/src/main/java/io/quarkus/picocli/deployment/PicocliProcessor.java b/extensions/picocli/deployment/src/main/java/io/quarkus/picocli/deployment/PicocliProcessor.java index b2ed32c390f8b..18c452474e5f7 100644 --- a/extensions/picocli/deployment/src/main/java/io/quarkus/picocli/deployment/PicocliProcessor.java +++ b/extensions/picocli/deployment/src/main/java/io/quarkus/picocli/deployment/PicocliProcessor.java @@ -12,6 +12,7 @@ import io.quarkus.arc.deployment.AdditionalBeanBuildItem; import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem; import io.quarkus.arc.deployment.AutoAddScopeBuildItem; +import io.quarkus.arc.deployment.UnremovableBeanBuildItem; import io.quarkus.arc.processor.AnnotationsTransformer; import io.quarkus.arc.processor.BuiltinScope; import io.quarkus.deployment.Feature; @@ -76,6 +77,7 @@ IndexDependencyBuildItem picocliIndexDependency() { void picocliRunner(ApplicationIndexBuildItem applicationIndex, CombinedIndexBuildItem combinedIndex, BuildProducer additionalBean, + BuildProducer unremovableBean, BuildProducer quarkusApplicationClass, BuildProducer annotationsTransformer) { IndexView index = combinedIndex.getIndex(); @@ -99,6 +101,17 @@ void picocliRunner(ApplicationIndexBuildItem applicationIndex, additionalBean.produce(AdditionalBeanBuildItem.unremovableOf(DefaultPicocliCommandLineFactory.class)); quarkusApplicationClass.produce(new QuarkusApplicationClassBuildItem(PicocliRunner.class)); } + + // Make all classes that can be instantiated by IFactory unremovable + unremovableBean.produce(UnremovableBeanBuildItem.beanTypes(CommandLine.ITypeConverter.class, + CommandLine.IVersionProvider.class, + CommandLine.IModelTransformer.class, + CommandLine.IModelTransformer.class, + CommandLine.IDefaultValueProvider.class, + CommandLine.IParameterConsumer.class, + CommandLine.IParameterPreprocessor.class, + CommandLine.INegatableOptionTransformer.class, + CommandLine.IHelpFactory.class)); } private List classesAnnotatedWith(IndexView indexView, String annotationClassName) { diff --git a/integration-tests/picocli/src/main/java/io/quarkus/it/picocli/EntryWithVersionCommand.java b/integration-tests/picocli/src/main/java/io/quarkus/it/picocli/EntryWithVersionCommand.java new file mode 100644 index 0000000000000..c2121fe991b46 --- /dev/null +++ b/integration-tests/picocli/src/main/java/io/quarkus/it/picocli/EntryWithVersionCommand.java @@ -0,0 +1,9 @@ +package io.quarkus.it.picocli; + +import io.quarkus.picocli.runtime.annotations.TopCommand; +import picocli.CommandLine; + +@TopCommand +@CommandLine.Command(mixinStandardHelpOptions = true, versionProvider = VersionProvider.class) +public class EntryWithVersionCommand { +} diff --git a/integration-tests/picocli/src/main/java/io/quarkus/it/picocli/VersionProvider.java b/integration-tests/picocli/src/main/java/io/quarkus/it/picocli/VersionProvider.java new file mode 100644 index 0000000000000..e06d7c92156f7 --- /dev/null +++ b/integration-tests/picocli/src/main/java/io/quarkus/it/picocli/VersionProvider.java @@ -0,0 +1,22 @@ +package io.quarkus.it.picocli; + +import jakarta.inject.Singleton; + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +import picocli.CommandLine; + +@Singleton +public class VersionProvider implements CommandLine.IVersionProvider { + + private final String version; + + public VersionProvider(@ConfigProperty(name = "some.version", defaultValue = "0.0.1") String version) { + this.version = version; + } + + @Override + public String[] getVersion() throws Exception { + return new String[] { version }; + } +} diff --git a/integration-tests/picocli/src/test/java/io/quarkus/it/picocli/TestVersion.java b/integration-tests/picocli/src/test/java/io/quarkus/it/picocli/TestVersion.java new file mode 100644 index 0000000000000..1218095d3de49 --- /dev/null +++ b/integration-tests/picocli/src/test/java/io/quarkus/it/picocli/TestVersion.java @@ -0,0 +1,25 @@ +package io.quarkus.it.picocli; + +import static io.quarkus.it.picocli.TestUtils.createConfig; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusProdModeTest; + +public class TestVersion { + + @RegisterExtension + static final QuarkusProdModeTest config = createConfig("version-app", EntryWithVersionCommand.class, + VersionProvider.class) + .overrideConfigKey("some.version", "1.1") + .setCommandLineParameters("--version"); + + @Test + public void simpleTest() { + Assertions.assertThat(config.getStartupConsoleOutput()).containsOnlyOnce("1.1"); + Assertions.assertThat(config.getExitCode()).isZero(); + } + +}