Skip to content

Commit

Permalink
Merge pull request #18824 from ebullient/registry-list
Browse files Browse the repository at this point in the history
CLI: set registry client system property
  • Loading branch information
aloubyansky authored Jul 19, 2021
2 parents 6410f1d + c3a9bee commit c935245
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.registry.RegistryResolutionException;
import picocli.CommandLine;
import picocli.CommandLine.Mixin;

Expand Down Expand Up @@ -86,7 +87,7 @@ Integer dryRunList(CommandLine.Help help, BuildTool buildTool) {
return CommandLine.ExitCode.OK;
}

Integer listPlatformCategories() throws QuarkusCommandException {
Integer listPlatformCategories() throws QuarkusCommandException, RegistryResolutionException {
QuarkusProject qp = registryClient.createQuarkusProject(projectRoot(), targetQuarkusVersion,
BuildTool.MAVEN, output);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.registry.RegistryResolutionException;
import picocli.CommandLine;
import picocli.CommandLine.Mixin;

Expand Down Expand Up @@ -112,12 +113,13 @@ Integer dryRunList(CommandLine.Help help, BuildTool buildTool) {
dryRunOutput.put("List installable extensions", Boolean.toString(installable));
dryRunOutput.put("Search pattern", searchPattern);
dryRunOutput.put("Category", category);
dryRunOutput.put("Registry Client", Boolean.toString(registryClient.enabled()));

output.info(help.createTextTable(dryRunOutput).toString());
return CommandLine.ExitCode.OK;
}

Integer listPlatformExtensions() throws QuarkusCommandException {
Integer listPlatformExtensions() throws QuarkusCommandException, RegistryResolutionException {
QuarkusProject qp = registryClient.createQuarkusProject(projectRoot(), targetQuarkusVersion,
BuildTool.MAVEN, output);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ static BuildSystemRunner getRunner(OutputOptionMixin output, PropertiesOptions p
switch (buildTool) {
default:
case MAVEN:
return new MavenRunner(output, propertiesOptions, projectRoot);
return new MavenRunner(output, propertiesOptions, registryClient, projectRoot);
case GRADLE_KOTLIN_DSL:
return new GradleRunner(output, propertiesOptions, projectRoot, BuildTool.GRADLE_KOTLIN_DSL);
return new GradleRunner(output, propertiesOptions, registryClient, projectRoot, BuildTool.GRADLE_KOTLIN_DSL);
case GRADLE:
return new GradleRunner(output, propertiesOptions, projectRoot, BuildTool.GRADLE);
return new GradleRunner(output, propertiesOptions, registryClient, projectRoot, BuildTool.GRADLE);
case JBANG:
return new JBangRunner(output, propertiesOptions, registryClient, projectRoot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@
import io.quarkus.cli.common.OutputOptionMixin;
import io.quarkus.cli.common.PropertiesOptions;
import io.quarkus.cli.common.RunModeOption;
import io.quarkus.cli.registry.RegistryClientMixin;
import io.quarkus.devtools.project.BuildTool;

public class GradleRunner implements BuildSystemRunner {
public static final String[] windowsWrapper = { "gradlew.cmd", "gradlew.bat" };
public static final String otherWrapper = "gradlew";

final OutputOptionMixin output;
final RegistryClientMixin registryClient;
final Path projectRoot;
final BuildTool buildTool;
final PropertiesOptions propertiesOptions;

public GradleRunner(OutputOptionMixin output, PropertiesOptions propertiesOptions, Path projectRoot, BuildTool buildTool) {
public GradleRunner(OutputOptionMixin output, PropertiesOptions propertiesOptions, RegistryClientMixin registryClient,
Path projectRoot, BuildTool buildTool) {
this.output = output;
this.projectRoot = projectRoot;
this.buildTool = buildTool;
this.propertiesOptions = propertiesOptions;
this.registryClient = registryClient;
verifyBuildFile();
}

Expand Down Expand Up @@ -218,6 +222,7 @@ void setGradleProperties(ArrayDeque<String> args, boolean batchMode) {
// Make sure we stay where we should
args.add("--project-dir=" + projectRoot.toAbsolutePath());
}
args.add(registryClient.getRegistryClientProperty());

// add any other discovered properties
args.addAll(flattenMappedProperties(propertiesOptions.properties));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public BuildCommandArgs prepareBuild(BuildOptions buildOptions, RunModeOption ru

args.add("build");
args.addAll(flattenMappedProperties(propertiesOptions.properties));
args.add(registryClient.getRegistryClientProperty());
args.addAll(params);
args.add(getMainPath());
return prependExecutable(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.quarkus.cli.common.OutputOptionMixin;
import io.quarkus.cli.common.PropertiesOptions;
import io.quarkus.cli.common.RunModeOption;
import io.quarkus.cli.registry.RegistryClientMixin;
import io.quarkus.devtools.commands.AddExtensions;
import io.quarkus.devtools.commands.ListCategories;
import io.quarkus.devtools.commands.ListExtensions;
Expand All @@ -33,13 +34,16 @@ public class MavenRunner implements BuildSystemRunner {
static final String otherWrapper = "mvnw";

final OutputOptionMixin output;
final RegistryClientMixin registryClient;
final PropertiesOptions propertiesOptions;
final Path projectRoot;

public MavenRunner(OutputOptionMixin output, PropertiesOptions propertiesOptions, Path projectRoot) {
public MavenRunner(OutputOptionMixin output, PropertiesOptions propertiesOptions, RegistryClientMixin registryClient,
Path projectRoot) {
this.output = output;
this.projectRoot = projectRoot;
this.propertiesOptions = propertiesOptions;
this.registryClient = registryClient;
verifyBuildFile();
}

Expand Down Expand Up @@ -199,6 +203,7 @@ void setMavenProperties(ArrayDeque<String> args, boolean batchMode) {

// add specified properties
args.addAll(flattenMappedProperties(propertiesOptions.properties));
args.add(registryClient.getRegistryClientProperty());
}

void verifyBuildFile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,28 @@
import io.quarkus.maven.ArtifactCoords;
import io.quarkus.platform.tools.ToolsUtils;
import io.quarkus.registry.ExtensionCatalogResolver;
import io.quarkus.registry.RegistryResolutionException;
import io.quarkus.registry.catalog.ExtensionCatalog;
import picocli.CommandLine;

public class RegistryClientMixin {
final static boolean VALIDATE = !Boolean.parseBoolean(System.getenv("REGISTRY_CLIENT_TEST"));

@CommandLine.Option(names = { "--refresh" }, description = "Refresh the local Quarkus extension registry cache")
boolean refresh;
/** @see io.quarkus.cli.registry.ToggleRegistryClientMixin#setRegistryClient */
public final String getRegistryClientProperty() {
return "-DquarkusRegistryClient=" + Boolean.toString(enabled());
}

@CommandLine.Option(names = {
"--refresh" }, description = "Refresh the local Quarkus extension registry cache", defaultValue = "false")
boolean refresh = false;

public boolean enabled() {
return true;
}

public QuarkusProject createQuarkusProject(Path projectRoot, TargetQuarkusVersionGroup targetVersion, BuildTool buildTool,
OutputOptionMixin log) {
OutputOptionMixin log) throws RegistryResolutionException {
ExtensionCatalog catalog = getExtensionCatalog(targetVersion, log);
if (VALIDATE && catalog.getQuarkusCoreVersion().startsWith("1.")) {
throw new UnsupportedOperationException("The version 2 CLI can not be used with Quarkus 1.x projects.\n"
Expand All @@ -34,7 +41,8 @@ public QuarkusProject createQuarkusProject(Path projectRoot, TargetQuarkusVersio
return QuarkusProjectHelper.getProject(projectRoot, catalog, buildTool, log);
}

ExtensionCatalog getExtensionCatalog(TargetQuarkusVersionGroup targetVersion, OutputOptionMixin log) {
ExtensionCatalog getExtensionCatalog(TargetQuarkusVersionGroup targetVersion, OutputOptionMixin log)
throws RegistryResolutionException {
log.debug("Resolving Quarkus extension catalog for " + targetVersion);
QuarkusProjectHelper.setMessageWriter(log);

Expand All @@ -52,23 +60,19 @@ ExtensionCatalog getExtensionCatalog(TargetQuarkusVersionGroup targetVersion, Ou

final ExtensionCatalogResolver catalogResolver = getExtensionCatalogResolver(log);

try {
if (!catalogResolver.hasRegistries()) {
log.debug("Falling back to direct resolution of the platform bom");
// Fall back to previous methods of finding registries (e.g. client has been disabled)
return ToolsUtils.resolvePlatformDescriptorDirectly(null, null, Version.clientVersion(),
QuarkusProjectHelper.artifactResolver(), log);
}

if (targetVersion.isStreamSpecified()) {
return catalogResolver.resolveExtensionCatalog(targetVersion.getStream());
}

refreshRegistryCache(log);
return catalogResolver.resolveExtensionCatalog();
} catch (Exception e) {
throw new RuntimeException("Failed to resolve the Quarkus extension catalog", e);
if (!catalogResolver.hasRegistries()) {
log.debug("Falling back to direct resolution of the platform bom");
// Fall back to previous methods of finding registries (e.g. client has been disabled)
return ToolsUtils.resolvePlatformDescriptorDirectly(null, null, Version.clientVersion(),
QuarkusProjectHelper.artifactResolver(), log);
}

if (targetVersion.isStreamSpecified()) {
return catalogResolver.resolveExtensionCatalog(targetVersion.getStream());
}

refreshRegistryCache(log);
return catalogResolver.resolveExtensionCatalog();
}

private ExtensionCatalogResolver getExtensionCatalogResolver(OutputOptionMixin log) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

public class ToggleRegistryClientMixin extends RegistryClientMixin {

boolean useRegistryClient;

@CommandLine.Option(names = { "--registry-client" }, description = "Use the Quarkus extension catalog", negatable = true)
boolean useRegistryClient = false;
void setRegistryClient(boolean enabled) {
System.setProperty("quarkusRegistryClient", Boolean.toString(enabled));
useRegistryClient = enabled;
}

@Override
public boolean enabled() {
Expand Down
15 changes: 13 additions & 2 deletions devtools/cli/src/test/java/io/quarkus/cli/CliDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import org.junit.jupiter.api.Assertions;

import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.registry.config.RegistriesConfigLocator;
import picocli.CommandLine;

public class CliDriver {
Expand All @@ -22,6 +24,12 @@ public class CliDriver {

private static final String localRepo = convertToProperty("maven.repo.local");

public static void afterEachCleanup() {
System.clearProperty(RegistriesConfigLocator.CONFIG_FILE_PATH_PROPERTY);
System.clearProperty("quarkusRegistryClient");
QuarkusProjectHelper.reset();
}

public static void preserveLocalRepoSettings(Collection<String> args) {
if (localRepo != null) {
args.add(localRepo);
Expand Down Expand Up @@ -159,6 +167,10 @@ public static Result invokeValidateExtensionList(Path projectRoot) throws Except

Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code. Result:\n" + result);

Assertions.assertFalse(result.stdout.contains("camel-"),
"camel extensions should not appear in the list of installable extensions. Found:\n" + result);

return result;
}

Expand Down Expand Up @@ -256,9 +268,8 @@ public static Result invokeExtensionListInstallable(Path projectRoot) throws Exc
"Expected OK return code. Result:\n" + result);
Assertions.assertTrue(result.stdout.contains("quarkus-hibernate-orm"),
"quarkus-hibernate-orm should be listed as an installable extension. Found:\n" + result);
Assertions.assertFalse(result.stdout.contains("quarkus-qute"),
Assertions.assertFalse(result.stdout.matches("quarkus-qute"),
"quarkus-qute should not be listed as an installable extension. Found:\n" + result);

return result;
}

Expand Down
43 changes: 40 additions & 3 deletions devtools/cli/src/test/java/io/quarkus/cli/CliNonProjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import io.quarkus.registry.config.RegistriesConfigLocator;
import picocli.CommandLine;

public class CliNonProjectTest {
Expand All @@ -35,7 +34,7 @@ public void verifyEmptyDirectory() throws Exception {
"Directory list operation should succeed");
Assertions.assertEquals(0, files.length,
"Directory should be empty. Found: " + Arrays.toString(files));
System.clearProperty(RegistriesConfigLocator.CONFIG_FILE_PATH_PROPERTY);
CliDriver.afterEachCleanup();
}

@Test
Expand All @@ -59,6 +58,45 @@ public void testListPlatformExtensions() throws Exception {
"Should contain '2.0.0.CR3' in the list of extensions (origin), found: " + result.stdout);
}

@Test
public void testListPlatformExtensionsRegistryClient() throws Exception {
// Dry run: Make sure registry-client system property is true
CliDriver.Result result = CliDriver.execute(workspaceRoot, "ext", "list", "-e",
"--dry-run", "--registry-client");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code." + result);

String noSpaces = result.stdout.replaceAll("[\\s\\p{Z}]", "");
Assertions.assertTrue(noSpaces.contains("RegistryClienttrue"),
"Should contain 'Registry Client true', found: " + result.stdout);
Assertions.assertTrue(Boolean.getBoolean("quarkusRegistryClient"),
"Registry Client property should be set to true");

// Dry run: Make sure registry-client system property is false
result = CliDriver.execute(workspaceRoot, "ext", "list", "-e",
"--dry-run", "--no-registry-client");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code." + result);

noSpaces = result.stdout.replaceAll("[\\s\\p{Z}]", "");
Assertions.assertTrue(noSpaces.contains("RegistryClientfalse"),
"Should contain 'Registry Client false', found: " + result.stdout);
Assertions.assertFalse(Boolean.getBoolean("quarkusRegistryClient"),
"Registry Client property should be set to false");

// Dry run: Make sure registry client property is set (default = false) TODO
result = CliDriver.execute(workspaceRoot, "ext", "list", "-e",
"--dry-run");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code." + result);

noSpaces = result.stdout.replaceAll("[\\s\\p{Z}]", "");
Assertions.assertTrue(noSpaces.contains("RegistryClientfalse"),
"Should contain 'Registry Client false', found: " + result.stdout);
Assertions.assertFalse(Boolean.getBoolean("quarkusRegistryClient"),
"Registry Client property should be set to false");
}

@Test
public void testBuildOutsideOfProject() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "build", "-e");
Expand Down Expand Up @@ -89,7 +127,6 @@ public void testCreateAppDryRun() throws Exception {

@Test
public void testRegistryRefresh() throws Exception {

// List extensions of a specified platform version
CliDriver.Result result = CliDriver.execute(workspaceRoot, "registry", "--refresh", "-e");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -38,6 +39,11 @@ public void setupTestDirectories() throws Exception {
project = workspaceRoot.resolve("code-with-quarkus");
}

@AfterEach
public void afterEachCleanup() throws Exception {
CliDriver.afterEachCleanup();
}

@BeforeAll
static void startGradleDaemon() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--gradle", "--verbose", "-e", "-B",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -24,6 +25,11 @@ public void setupTestDirectories() throws Exception {
project = workspaceRoot.resolve("code-with-quarkus");
}

@AfterEach
public void afterEachCleanup() throws Exception {
CliDriver.afterEachCleanup();
}

@Test
public void testCreateAppDefaults() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--jbang", "--verbose", "-e", "-B");
Expand Down
Loading

0 comments on commit c935245

Please sign in to comment.