Skip to content

Commit

Permalink
Merge pull request #18003 from aloubyansky/non-platform-extension-fixes
Browse files Browse the repository at this point in the history
Fixes version selection for non-platform extensions when the stream is not specified
  • Loading branch information
aloubyansky authored Jun 18, 2021
2 parents d4a513e + 7768158 commit 9c1c33e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
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;
import io.quarkus.platform.tools.ToolsUtils;
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;
Expand Down Expand Up @@ -70,21 +72,27 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
}
}

final List<Extension> extensionsToAdd = computeExtensionsFromQuery(invocation, extensionsQuery);
if (extensionsToAdd == null) {
throw new QuarkusCommandException("Failed to create project because of invalid extensions");
}
List<Extension> extensionsToAdd = computeRequiredExtensions(invocation.getExtensionsCatalog(), extensionsQuery,
invocation.log());

ExtensionCatalog mainPlatform = invocation.getExtensionsCatalog(); // legacy platform initialization
final List<ExtensionCatalog> platformsToImport = getPlatformsToImport(mainPlatform, extensionsToAdd);
final List<ArtifactCoords> 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<ExtensionCatalog> extensionOrigins = getExtensionOrigins(mainPlatform, extensionsToAdd);
final List<ArtifactCoords> 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());
Expand All @@ -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;
}
Expand Down Expand Up @@ -162,8 +170,17 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
return QuarkusCommandOutcome.success();
}

private List<Extension> computeRequiredExtensions(ExtensionCatalog catalog,
final Set<String> extensionsQuery, MessageWriter log) throws QuarkusCommandException {
final List<Extension> 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<ExtensionCatalog> getPlatformsToImport(ExtensionCatalog extensionCatalog, List<Extension> extensionsToAdd)
private List<ExtensionCatalog> getExtensionOrigins(ExtensionCatalog extensionCatalog, List<Extension> extensionsToAdd)
throws QuarkusCommandException {
final ElementCatalog<ExtensionCatalog> ec = (ElementCatalog<ExtensionCatalog>) extensionCatalog.getMetadata()
.get("element-catalog");
Expand All @@ -186,21 +203,6 @@ private List<ExtensionCatalog> getPlatformsToImport(ExtensionCatalog extensionCa
.collect(Collectors.toList());
eKeys.add(quarkusCore.getArtifact().getGroupId() + ":" + quarkusCore.getArtifact().getArtifactId());
}
List<ExtensionCatalog> catalogs = ElementCatalogBuilder.getMembersForElements(ec, eKeys);
List<ExtensionCatalog> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,10 +31,10 @@ final class QuarkusCommandHandlers {
private QuarkusCommandHandlers() {
}

static List<Extension> computeExtensionsFromQuery(final QuarkusCommandInvocation invocation,
final Set<String> extensionsQuery) {
final Collection<Extension> extensionCatalog = invocation.getExtensionsCatalog().getExtensions();
static List<Extension> computeExtensionsFromQuery(ExtensionCatalog catalog,
final Set<String> extensionsQuery, MessageWriter log) {
final ArrayList<Extension> builder = new ArrayList<>();
final Collection<Extension> extensionCatalog = catalog.getExtensions();
for (String query : extensionsQuery) {
final int countColons = StringUtils.countMatches(query, ":");
if (countColons > 1) {
Expand Down Expand Up @@ -67,15 +69,15 @@ static List<Extension> computeExtensionsFromQuery(final QuarkusCommandInvocation
final Collection<Extension> 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("'");
candidates.forEach(extension -> sb.append(System.lineSeparator()).append(" * ")
.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;
}

Expand All @@ -85,7 +87,8 @@ static List<Extension> computeExtensionsFromQuery(final QuarkusCommandInvocation

static List<ArtifactCoords> computeCoordsFromQuery(final QuarkusCommandInvocation invocation,
final Set<String> extensionsQuery) {
final List<Extension> extensions = computeExtensionsFromQuery(invocation, extensionsQuery);
final List<Extension> extensions = computeExtensionsFromQuery(invocation.getExtensionsCatalog(), extensionsQuery,
invocation.log());
return extensions == null ? null
: extensions.stream().map(e -> Extensions.stripVersion(e.getArtifact())).collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,7 @@ private void collectPlatforms(PlatformCatalog catalog, List<Platform> collectedP

public ExtensionCatalog resolveExtensionCatalog() throws RegistryResolutionException {

final int registriesTotal = registries.size();
if (registriesTotal == 0) {
throw new RegistryResolutionException("No registries configured");
}
ensureRegistriesConfigured();

final List<ExtensionCatalog> catalogs = new ArrayList<>();
final ElementCatalogBuilder<ExtensionCatalog> catalogBuilder = ElementCatalogBuilder.newInstance();
Expand All @@ -285,6 +282,16 @@ public ExtensionCatalog resolveExtensionCatalog() throws RegistryResolutionExcep
catalogs.add(ec);
addUnion(union, ec);
}

final Map<String, List<RegistryExtensionResolver>> 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);
}
}
}
Expand Down Expand Up @@ -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<ExtensionCatalog> catalogs = new ArrayList<>();
final ElementCatalogBuilder<ExtensionCatalog> 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;
Expand Down Expand Up @@ -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<ExtensionCatalog> union, final ExtensionCatalog ec) {
final MemberBuilder<ExtensionCatalog> 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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ private UnionBuilder(UnionVersion version, ElementCatalogBuilder<T> catalogBuild
this.catalogBuilder = catalogBuilder;
}

public UnionVersion version() {
return version;
}

public MemberBuilder<T> getOrCreateMember(Object memberKey, Object memberVersion) {
return getOrCreateMember(memberKey, memberVersion, null);
}
Expand Down Expand Up @@ -336,7 +340,6 @@ public static <T> void dump(PrintStream ps, ElementCatalog<T> catalog) {
}

public static <T> List<T> getMembersForElements(ElementCatalog<T> elementCatalog, Collection<String> elementKeys) {

final Map<UnionVersion, Map<Object, Member<T>>> unionVersions = new TreeMap<>(UnionVersion::compareTo);
for (Object elementKey : elementKeys) {
final Element<T> e = elementCatalog.get(elementKey);
Expand Down

0 comments on commit 9c1c33e

Please sign in to comment.