Skip to content

Commit

Permalink
Make Picocli version providers unremovable classes
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jan 9, 2024
1 parent fca79fe commit 8161071
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,6 +77,7 @@ IndexDependencyBuildItem picocliIndexDependency() {
void picocliRunner(ApplicationIndexBuildItem applicationIndex,
CombinedIndexBuildItem combinedIndex,
BuildProducer<AdditionalBeanBuildItem> additionalBean,
BuildProducer<UnremovableBeanBuildItem> unremovableBean,
BuildProducer<QuarkusApplicationClassBuildItem> quarkusApplicationClass,
BuildProducer<AnnotationsTransformerBuildItem> annotationsTransformer) {
IndexView index = combinedIndex.getIndex();
Expand All @@ -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<DotName> classesAnnotatedWith(IndexView indexView, String annotationClassName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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 };
}
}
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit 8161071

Please sign in to comment.