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 8, 2024
1 parent 3b727fa commit 45d88ca
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import java.util.List;
import java.util.stream.Collectors;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.MethodInfo;

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 +80,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 +104,19 @@ void picocliRunner(ApplicationIndexBuildItem applicationIndex,
additionalBean.produce(AdditionalBeanBuildItem.unremovableOf(DefaultPicocliCommandLineFactory.class));
quarkusApplicationClass.produce(new QuarkusApplicationClassBuildItem(PicocliRunner.class));
}

// make the arguments of @Command unremovable beans if necessary
Collection<AnnotationInstance> commandInstances = index.getAnnotations(CommandLine.Command.class);
for (AnnotationInstance commandInstance : commandInstances) {
AnnotationValue versionProvider = commandInstance.value("versionProvider");
if (versionProvider != null) {
ClassInfo versionProviderInfo = index.getClassByName(versionProvider.asClass().name());
if (versionProviderInfo != null) {
// TODO: should we make these beans even when they are not annotated as such?
unremovableBean.produce(UnremovableBeanBuildItem.beanTypes(versionProviderInfo.name()));
}
}
}
}

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 45d88ca

Please sign in to comment.