From 7768158136fd6742ab7312ea6b95486174628f69 Mon Sep 17 00:00:00 2001 From: Alexey Loubyansky Date: Fri, 18 Jun 2021 12:32:32 +0200 Subject: [PATCH] Fixes version selection for non-platform extensions when the stream is not specified --- .../handlers/CreateProjectCommandHandler.java | 62 ++++++++++--------- .../handlers/QuarkusCommandHandlers.java | 15 +++-- .../registry/ExtensionCatalogResolver.java | 33 ++++++---- .../registry/union/ElementCatalogBuilder.java | 5 +- 4 files changed, 67 insertions(+), 48 deletions(-) diff --git a/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/commands/handlers/CreateProjectCommandHandler.java b/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/commands/handlers/CreateProjectCommandHandler.java index da63eabfe1873..bdf3f6f86f61d 100644 --- a/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/commands/handlers/CreateProjectCommandHandler.java +++ b/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/commands/handlers/CreateProjectCommandHandler.java @@ -24,6 +24,7 @@ import io.quarkus.devtools.commands.data.QuarkusCommandInvocation; import io.quarkus.devtools.commands.data.QuarkusCommandOutcome; import io.quarkus.devtools.messagewriter.MessageIcons; +import io.quarkus.devtools.messagewriter.MessageWriter; import io.quarkus.devtools.project.codegen.ProjectGenerator; import io.quarkus.devtools.project.extensions.Extensions; import io.quarkus.maven.ArtifactCoords; @@ -31,6 +32,7 @@ import io.quarkus.registry.catalog.Extension; import io.quarkus.registry.catalog.ExtensionCatalog; import io.quarkus.registry.catalog.ExtensionOrigin; +import io.quarkus.registry.catalog.json.JsonCatalogMerger; import io.quarkus.registry.union.ElementCatalog; import io.quarkus.registry.union.ElementCatalogBuilder; import java.io.IOException; @@ -70,21 +72,27 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws } } - final List extensionsToAdd = computeExtensionsFromQuery(invocation, extensionsQuery); - if (extensionsToAdd == null) { - throw new QuarkusCommandException("Failed to create project because of invalid extensions"); - } + List extensionsToAdd = computeRequiredExtensions(invocation.getExtensionsCatalog(), extensionsQuery, + invocation.log()); ExtensionCatalog mainPlatform = invocation.getExtensionsCatalog(); // legacy platform initialization - final List platformsToImport = getPlatformsToImport(mainPlatform, extensionsToAdd); - final List platformBoms = new ArrayList<>(Math.max(platformsToImport.size(), 1)); - if (platformsToImport.size() > 0) { - mainPlatform = platformsToImport.get(0); - for (ExtensionCatalog platform : platformsToImport) { - if (platform.getBom().getArtifactId().equals("quarkus-bom")) { - mainPlatform = platform; + final List extensionOrigins = getExtensionOrigins(mainPlatform, extensionsToAdd); + final List platformBoms = new ArrayList<>(Math.max(extensionOrigins.size(), 1)); + if (extensionOrigins.size() > 0) { + // necessary to set the versions from the selected origins + extensionsToAdd = computeRequiredExtensions(JsonCatalogMerger.merge(extensionOrigins), extensionsQuery, + invocation.log()); + // collect platform BOMs to import + boolean sawFirstPlatform = false; + for (ExtensionCatalog c : extensionOrigins) { + if (!c.isPlatform()) { + continue; + } + if (c.getBom().getArtifactId().equals("quarkus-bom") || !sawFirstPlatform) { + mainPlatform = c; + sawFirstPlatform = true; } - platformBoms.add(platform.getBom()); + platformBoms.add(c.getBom()); } } else { platformBoms.add(mainPlatform.getBom()); @@ -94,7 +102,7 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws for (Extension e : extensionsToAdd) { ArtifactCoords coords = e.getArtifact(); for (ExtensionOrigin origin : e.getOrigins()) { - if (origin.getBom() != null && platformBoms.contains(origin.getBom())) { + if (origin.isPlatform() && origin.getBom() != null && platformBoms.contains(origin.getBom())) { coords = Extensions.stripVersion(coords); break; } @@ -162,8 +170,17 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws return QuarkusCommandOutcome.success(); } + private List computeRequiredExtensions(ExtensionCatalog catalog, + final Set extensionsQuery, MessageWriter log) throws QuarkusCommandException { + final List extensionsToAdd = computeExtensionsFromQuery(catalog, extensionsQuery, log); + if (extensionsToAdd == null) { + throw new QuarkusCommandException("Failed to create project because of invalid extensions"); + } + return extensionsToAdd; + } + @SuppressWarnings("unchecked") - private List getPlatformsToImport(ExtensionCatalog extensionCatalog, List extensionsToAdd) + private List getExtensionOrigins(ExtensionCatalog extensionCatalog, List extensionsToAdd) throws QuarkusCommandException { final ElementCatalog ec = (ElementCatalog) extensionCatalog.getMetadata() .get("element-catalog"); @@ -186,21 +203,6 @@ private List getPlatformsToImport(ExtensionCatalog extensionCa .collect(Collectors.toList()); eKeys.add(quarkusCore.getArtifact().getGroupId() + ":" + quarkusCore.getArtifact().getArtifactId()); } - List catalogs = ElementCatalogBuilder.getMembersForElements(ec, eKeys); - List filtered = null; - for (int i = 0; i < catalogs.size(); ++i) { - final ExtensionCatalog c = catalogs.get(i); - if (c.isPlatform()) { - if (filtered != null) { - filtered.add(c); - } - } else if (filtered == null) { - filtered = new ArrayList<>(catalogs.size()); - for (int j = 0; j < i; ++j) { - filtered.add(catalogs.get(j)); - } - } - } - return filtered == null ? catalogs : filtered; + return ElementCatalogBuilder.getMembersForElements(ec, eKeys); } } diff --git a/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/commands/handlers/QuarkusCommandHandlers.java b/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/commands/handlers/QuarkusCommandHandlers.java index 798894023835c..72e4815ead14c 100644 --- a/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/commands/handlers/QuarkusCommandHandlers.java +++ b/independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/commands/handlers/QuarkusCommandHandlers.java @@ -7,10 +7,12 @@ import io.quarkus.devtools.commands.data.QuarkusCommandInvocation; import io.quarkus.devtools.commands.data.SelectionResult; +import io.quarkus.devtools.messagewriter.MessageWriter; import io.quarkus.devtools.project.extensions.Extensions; import io.quarkus.maven.ArtifactCoords; import io.quarkus.maven.ArtifactKey; import io.quarkus.registry.catalog.Extension; +import io.quarkus.registry.catalog.ExtensionCatalog; import io.quarkus.registry.catalog.json.JsonExtension; import java.util.ArrayList; import java.util.Collection; @@ -29,10 +31,10 @@ final class QuarkusCommandHandlers { private QuarkusCommandHandlers() { } - static List computeExtensionsFromQuery(final QuarkusCommandInvocation invocation, - final Set extensionsQuery) { - final Collection extensionCatalog = invocation.getExtensionsCatalog().getExtensions(); + static List computeExtensionsFromQuery(ExtensionCatalog catalog, + final Set extensionsQuery, MessageWriter log) { final ArrayList builder = new ArrayList<>(); + final Collection extensionCatalog = catalog.getExtensions(); for (String query : extensionsQuery) { final int countColons = StringUtils.countMatches(query, ":"); if (countColons > 1) { @@ -67,7 +69,7 @@ static List computeExtensionsFromQuery(final QuarkusCommandInvocation final Collection candidates = result.getExtensions(); if (candidates.isEmpty()) { // No matches at all. - invocation.log().error("Cannot find a dependency matching '" + query + "', maybe a typo?"); + log.error("Cannot find a dependency matching '" + query + "', maybe a typo?"); return null; } sb.append(ERROR_ICON + " Multiple extensions matching '").append(query).append("'"); @@ -75,7 +77,7 @@ static List computeExtensionsFromQuery(final QuarkusCommandInvocation .append(extension.managementKey())); sb.append(System.lineSeparator()) .append(" try using the exact name or the full GAV (group id, artifact id, and version)."); - invocation.log().info(sb.toString()); + log.info(sb.toString()); return null; } @@ -85,7 +87,8 @@ static List computeExtensionsFromQuery(final QuarkusCommandInvocation static List computeCoordsFromQuery(final QuarkusCommandInvocation invocation, final Set extensionsQuery) { - final List extensions = computeExtensionsFromQuery(invocation, extensionsQuery); + final List extensions = computeExtensionsFromQuery(invocation.getExtensionsCatalog(), extensionsQuery, + invocation.log()); return extensions == null ? null : extensions.stream().map(e -> Extensions.stripVersion(e.getArtifact())).collect(Collectors.toList()); } diff --git a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/ExtensionCatalogResolver.java b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/ExtensionCatalogResolver.java index 13d628a5f1b74..121ce08b79f07 100644 --- a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/ExtensionCatalogResolver.java +++ b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/ExtensionCatalogResolver.java @@ -256,10 +256,7 @@ private void collectPlatforms(PlatformCatalog catalog, List collectedP public ExtensionCatalog resolveExtensionCatalog() throws RegistryResolutionException { - final int registriesTotal = registries.size(); - if (registriesTotal == 0) { - throw new RegistryResolutionException("No registries configured"); - } + ensureRegistriesConfigured(); final List catalogs = new ArrayList<>(); final ElementCatalogBuilder catalogBuilder = ElementCatalogBuilder.newInstance(); @@ -285,6 +282,16 @@ public ExtensionCatalog resolveExtensionCatalog() throws RegistryResolutionExcep catalogs.add(ec); addUnion(union, ec); } + + final Map> registriesByQuarkusCore = new HashMap<>(2); + registriesByQuarkusCore.put(release.getQuarkusCoreVersion(), + getRegistriesForQuarkusVersion(release.getQuarkusCoreVersion())); + final String upstreamQuarkusVersion = release.getUpstreamQuarkusCoreVersion(); + if (upstreamQuarkusVersion != null && !registriesByQuarkusCore.containsKey(upstreamQuarkusVersion)) { + registriesByQuarkusCore.put(upstreamQuarkusVersion, + getRegistriesForQuarkusVersion(upstreamQuarkusVersion)); + } + appendNonPlatformExtensions(registriesByQuarkusCore, union, catalogs); } } } @@ -391,16 +398,13 @@ public ExtensionCatalog resolveExtensionCatalog(String quarkusCoreVersion) throw public ExtensionCatalog resolveExtensionCatalog(StreamCoords streamCoords) throws RegistryResolutionException { - final int registriesTotal = registries.size(); - if (registriesTotal == 0) { - throw new RegistryResolutionException("No registries configured"); - } + ensureRegistriesConfigured(); final List catalogs = new ArrayList<>(); final ElementCatalogBuilder catalogBuilder = ElementCatalogBuilder.newInstance(); - String platformKey = streamCoords.getPlatformKey(); - String streamId = streamCoords.getStreamId(); + final String platformKey = streamCoords.getPlatformKey(); + final String streamId = streamCoords.getStreamId(); PlatformStream stream = null; RegistryExtensionResolver registry = null; @@ -494,9 +498,16 @@ public ExtensionCatalog resolveExtensionCatalog(StreamCoords streamCoords) throw return catalog; } + private void ensureRegistriesConfigured() throws RegistryResolutionException { + final int registriesTotal = registries.size(); + if (registriesTotal == 0) { + throw new RegistryResolutionException("No registries configured"); + } + } + private void addUnion(final UnionBuilder union, final ExtensionCatalog ec) { final MemberBuilder builder = union.getOrCreateMember( - ec.getBom().getGroupId() + ":" + ec.getBom().getArtifactId(), ec.getBom().getVersion(), ec); + ec.getId(), ec.getBom().getVersion(), ec); ec.getExtensions() .forEach(e -> builder .addElement(e.getArtifact().getGroupId() + ":" + e.getArtifact().getArtifactId())); diff --git a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/union/ElementCatalogBuilder.java b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/union/ElementCatalogBuilder.java index 705aa391c15fa..43f3fefb61a55 100644 --- a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/union/ElementCatalogBuilder.java +++ b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/union/ElementCatalogBuilder.java @@ -172,6 +172,10 @@ private UnionBuilder(UnionVersion version, ElementCatalogBuilder catalogBuild this.catalogBuilder = catalogBuilder; } + public UnionVersion version() { + return version; + } + public MemberBuilder getOrCreateMember(Object memberKey, Object memberVersion) { return getOrCreateMember(memberKey, memberVersion, null); } @@ -336,7 +340,6 @@ public static void dump(PrintStream ps, ElementCatalog catalog) { } public static List getMembersForElements(ElementCatalog elementCatalog, Collection elementKeys) { - final Map>> unionVersions = new TreeMap<>(UnionVersion::compareTo); for (Object elementKey : elementKeys) { final Element e = elementCatalog.get(elementKey);