Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Picocli version providers unremovable classes #38083

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - so this is doing it for all instances even if not referenced by picocli config.

Just curious why you do it this way when we only have to do it for those pointed to by users annotation?

Ease of coding in quarkus or that you don't believe it will matter enough in extra memory used ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - so this is doing it for all instances even if not referenced by picocli config.

All it does it make beans of this type unremovable - nothing more

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious why you do it this way when we only have to do it for those pointed to by users annotation?

This way is much much cleaner and also makes chances of copy-paste errors in the code far lower.

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();
}

}