Skip to content

Commit

Permalink
Devtools: spport pulling in the complete platform catalog from a sing…
Browse files Browse the repository at this point in the history
…le member bom
  • Loading branch information
aloubyansky committed Jun 18, 2021
1 parent 6a50b57 commit 5c5db4c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ private List<Extension> computeRequiredExtensions(ExtensionCatalog catalog,
@SuppressWarnings("unchecked")
private List<ExtensionCatalog> getExtensionOrigins(ExtensionCatalog extensionCatalog, List<Extension> extensionsToAdd)
throws QuarkusCommandException {
final ElementCatalog<ExtensionCatalog> ec = (ElementCatalog<ExtensionCatalog>) extensionCatalog.getMetadata()
.get("element-catalog");
final ElementCatalog<ExtensionCatalog> ec = ElementCatalogBuilder.getElementCatalog(extensionCatalog,
ExtensionCatalog.class);
if (ec == null) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.maven.ArtifactCoords;
import io.quarkus.registry.PlatformStackIndex;
import io.quarkus.registry.catalog.ExtensionCatalog;
import io.quarkus.registry.catalog.json.JsonCatalogMapperHelper;
import io.quarkus.registry.catalog.json.JsonCatalogMerger;
import io.quarkus.registry.catalog.json.JsonExtensionCatalog;
import io.quarkus.registry.union.ElementCatalogBuilder;
import io.quarkus.registry.union.ElementCatalogBuilder.UnionBuilder;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -115,11 +119,54 @@ public static ExtensionCatalog resolvePlatformDescriptorDirectly(String bomGroup
throw new RuntimeException("Failed to resolve the default platform JSON descriptor", e);
}
}
ExtensionCatalog catalog;
try {
return JsonCatalogMapperHelper.deserialize(platformJson, JsonExtensionCatalog.class);
catalog = JsonCatalogMapperHelper.deserialize(platformJson, JsonExtensionCatalog.class);
} catch (IOException e) {
throw new RuntimeException("Failed to deserialize extension catalog " + platformJson, e);
}
Map<String, Object> md = catalog.getMetadata();
if (md != null) {
Object o = md.get("platform-release");
if (o instanceof Map) {
Object members = ((Map<?, ?>) o).get("members");
if (members instanceof Collection) {
final Collection<?> memberList = (Collection<?>) members;
final List<ExtensionCatalog> catalogs = new ArrayList<>(memberList.size());
final ElementCatalogBuilder<ExtensionCatalog> elementsBuilder = ElementCatalogBuilder.newInstance();
final UnionBuilder<ExtensionCatalog> union = elementsBuilder
.getOrCreateUnion(PlatformStackIndex.initial());

for (Object m : memberList) {
if (!(m instanceof String)) {
continue;
}
final ExtensionCatalog memberCatalog;
if (catalog.getId().equals(m)) {
memberCatalog = catalog;
} else {
try {
final ArtifactCoords coords = ArtifactCoords.fromString((String) m);
catalogCoords = new DefaultArtifact(coords.getGroupId(), coords.getArtifactId(),
coords.getClassifier(), coords.getType(), coords.getVersion());
log.debug("Resolving platform descriptor %s", catalogCoords);
final Path jsonPath = artifactResolver.resolve(catalogCoords).getArtifact().getFile().toPath();
memberCatalog = JsonCatalogMapperHelper.deserialize(jsonPath,
JsonExtensionCatalog.class);
} catch (Exception e) {
log.warn("Failed to resolve member catalog " + m, e);
continue;
}
}
catalogs.add(memberCatalog);
ElementCatalogBuilder.addUnionMember(union, memberCatalog);
}
catalog = JsonCatalogMerger.merge(catalogs);
ElementCatalogBuilder.setElementCatalog(catalog, elementsBuilder.build());
}
}
}
return catalog;
}

public static ExtensionCatalog mergePlatforms(List<ArtifactCoords> platforms, MavenArtifactResolver artifactResolver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import io.quarkus.registry.config.RegistriesConfig;
import io.quarkus.registry.config.RegistriesConfigLocator;
import io.quarkus.registry.config.RegistryConfig;
import io.quarkus.registry.union.ElementCatalog;
import io.quarkus.registry.union.ElementCatalogBuilder;
import io.quarkus.registry.union.ElementCatalogBuilder.MemberBuilder;
import io.quarkus.registry.union.ElementCatalogBuilder.UnionBuilder;
import java.io.File;
import java.net.URL;
Expand Down Expand Up @@ -280,7 +278,7 @@ public ExtensionCatalog resolveExtensionCatalog() throws RegistryResolutionExcep
for (ArtifactCoords bom : release.getMemberBoms()) {
final ExtensionCatalog ec = registry.resolvePlatformExtensions(bom);
catalogs.add(ec);
addUnion(union, ec);
ElementCatalogBuilder.addUnionMember(union, ec);
}

final Map<String, List<RegistryExtensionResolver>> registriesByQuarkusCore = new HashMap<>(2);
Expand All @@ -298,10 +296,7 @@ public ExtensionCatalog resolveExtensionCatalog() throws RegistryResolutionExcep
}

final ExtensionCatalog catalog = JsonCatalogMerger.merge(catalogs);
final ElementCatalog<ExtensionCatalog> elements = catalogBuilder.build();
if (!elements.isEmpty()) {
catalog.getMetadata().put("element-catalog", elements);
}
ElementCatalogBuilder.setElementCatalog(catalog, catalogBuilder.build());
return catalog;
}

Expand Down Expand Up @@ -476,7 +471,7 @@ public ExtensionCatalog resolveExtensionCatalog(StreamCoords streamCoords) throw
for (ArtifactCoords bom : release.getMemberBoms()) {
final ExtensionCatalog ec = registry.resolvePlatformExtensions(bom);
catalogs.add(ec);
addUnion(union, ec);
ElementCatalogBuilder.addUnionMember(union, ec);
}

final Map<String, List<RegistryExtensionResolver>> registriesByQuarkusCore = new HashMap<>(2);
Expand All @@ -490,11 +485,7 @@ public ExtensionCatalog resolveExtensionCatalog(StreamCoords streamCoords) throw

appendNonPlatformExtensions(registriesByQuarkusCore, union, catalogs);
final ExtensionCatalog catalog = JsonCatalogMerger.merge(catalogs);

final ElementCatalog<ExtensionCatalog> elements = catalogBuilder.build();
if (!elements.isEmpty()) {
catalog.getMetadata().put("element-catalog", elements);
}
ElementCatalogBuilder.setElementCatalog(catalog, catalogBuilder.build());
return catalog;
}

Expand All @@ -505,14 +496,6 @@ private void ensureRegistriesConfigured() throws RegistryResolutionException {
}
}

private void addUnion(final UnionBuilder<ExtensionCatalog> union, final ExtensionCatalog ec) {
final MemberBuilder<ExtensionCatalog> builder = union.getOrCreateMember(
ec.getId(), ec.getBom().getVersion(), ec);
ec.getExtensions()
.forEach(e -> builder
.addElement(e.getArtifact().getGroupId() + ":" + e.getArtifact().getArtifactId()));
}

public ExtensionCatalog resolveExtensionCatalog(Collection<ArtifactCoords> platforms)
throws RegistryResolutionException {
if (platforms.isEmpty()) {
Expand Down Expand Up @@ -587,7 +570,8 @@ private void appendNonPlatformExtensions(
if (nonPlatformCatalog != null) {
extensionCatalogs.add(nonPlatformCatalog);
if (union != null) {
addUnion(union, nonPlatformCatalog);
final UnionBuilder<ExtensionCatalog> union1 = union;
ElementCatalogBuilder.addUnionMember(union1, nonPlatformCatalog);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import io.quarkus.registry.union.UnionVersion;

class PlatformStackIndex implements UnionVersion {
public class PlatformStackIndex implements UnionVersion {

static PlatformStackIndex initial() {
public static PlatformStackIndex initial() {
return new PlatformStackIndex(0, 0, 0);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.registry.union;

import io.quarkus.registry.catalog.ExtensionCatalog;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -367,4 +368,21 @@ public static <T> List<T> getMembersForElements(ElementCatalog<T> elementCatalog
return Collections.emptyList();
}

public static void addUnionMember(final UnionBuilder<ExtensionCatalog> union, final ExtensionCatalog ec) {
final MemberBuilder<ExtensionCatalog> builder = union.getOrCreateMember(
ec.getId(), ec.getBom().getVersion(), ec);
ec.getExtensions()
.forEach(e -> builder
.addElement(e.getArtifact().getGroupId() + ":" + e.getArtifact().getArtifactId()));
}

public static void setElementCatalog(ExtensionCatalog extCatalog, ElementCatalog<?> elemCatalog) {
if (!elemCatalog.isEmpty()) {
extCatalog.getMetadata().put("element-catalog", elemCatalog);
}
}

public static <T> ElementCatalog<T> getElementCatalog(ExtensionCatalog extCatalog, Class<T> t) {
return (ElementCatalog<T>) extCatalog.getMetadata().get("element-catalog");
}
}

0 comments on commit 5c5db4c

Please sign in to comment.