|
| 1 | +package io.quarkus.devtools.commands.handlers; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import io.quarkus.devtools.commands.data.QuarkusCommandException; |
| 10 | +import io.quarkus.maven.dependency.ArtifactKey; |
| 11 | +import io.quarkus.registry.CatalogMergeUtility; |
| 12 | +import io.quarkus.registry.catalog.Extension; |
| 13 | +import io.quarkus.registry.catalog.ExtensionCatalog; |
| 14 | + |
| 15 | +public class RegistryProjectInfo { |
| 16 | + |
| 17 | + public static RegistryProjectInfo fromCatalogWithKeys(ExtensionCatalog registryCatalog, |
| 18 | + Collection<ArtifactKey> extensionKeys) |
| 19 | + throws QuarkusCommandException { |
| 20 | + return fromCatalogWithExtensions(registryCatalog, collectExtensions(registryCatalog, extensionKeys)); |
| 21 | + } |
| 22 | + |
| 23 | + public static RegistryProjectInfo fromCatalogWithExtensions(ExtensionCatalog registryCatalog, List<Extension> extensions) |
| 24 | + throws QuarkusCommandException { |
| 25 | + final List<ExtensionCatalog> extensionOrigins = CreateProjectCommandHandler.getExtensionOrigins(registryCatalog, |
| 26 | + extensions); |
| 27 | + |
| 28 | + ExtensionCatalog primaryCatalog = registryCatalog; |
| 29 | + if (!extensionOrigins.isEmpty()) { |
| 30 | + // necessary to set the versions from the selected origins |
| 31 | + final ExtensionCatalog mergedCatalog = CatalogMergeUtility.merge(extensionOrigins); |
| 32 | + extensions = syncWithCatalog(mergedCatalog, extensions); |
| 33 | + primaryCatalog = getPrimaryCatalog(extensionOrigins); |
| 34 | + } else { |
| 35 | + extensions = syncWithCatalog(primaryCatalog, extensions); |
| 36 | + } |
| 37 | + |
| 38 | + return new RegistryProjectInfo(primaryCatalog, extensionOrigins, extensions); |
| 39 | + } |
| 40 | + |
| 41 | + private static ExtensionCatalog getPrimaryCatalog(List<ExtensionCatalog> extensionOrigins) { |
| 42 | + ExtensionCatalog primaryCatalog; |
| 43 | + primaryCatalog = null; |
| 44 | + for (ExtensionCatalog c : extensionOrigins) { |
| 45 | + if (c.isPlatform()) { |
| 46 | + if (c.getBom().getArtifactId().equals("quarkus-bom")) { |
| 47 | + primaryCatalog = c; |
| 48 | + break; |
| 49 | + } else if (primaryCatalog == null) { |
| 50 | + primaryCatalog = c; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + return primaryCatalog; |
| 55 | + } |
| 56 | + |
| 57 | + private static List<Extension> collectExtensions(ExtensionCatalog catalog, Collection<ArtifactKey> extensionKeys) { |
| 58 | + var result = new ArrayList<Extension>(extensionKeys.size()); |
| 59 | + var catalogExtensions = toMap(catalog.getExtensions()); |
| 60 | + for (var key : extensionKeys) { |
| 61 | + var catalogExt = catalogExtensions.get(key); |
| 62 | + if (catalogExt == null) { |
| 63 | + throw new IllegalArgumentException("Failed to locate " + key + " in the catalog"); |
| 64 | + } |
| 65 | + result.add(catalogExt); |
| 66 | + } |
| 67 | + return result; |
| 68 | + } |
| 69 | + |
| 70 | + private static List<Extension> syncWithCatalog(ExtensionCatalog catalog, List<Extension> extensions) { |
| 71 | + var result = new ArrayList<Extension>(extensions.size()); |
| 72 | + var catalogExtensions = toMap(catalog.getExtensions()); |
| 73 | + for (var e : extensions) { |
| 74 | + var catalogExt = catalogExtensions.get(e.getArtifact().getKey()); |
| 75 | + result.add(catalogExt == null ? e : catalogExt); |
| 76 | + } |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + private static Map<ArtifactKey, Extension> toMap(Collection<Extension> extensions) { |
| 81 | + var result = new HashMap<ArtifactKey, Extension>(extensions.size()); |
| 82 | + for (var e : extensions) { |
| 83 | + result.put(e.getArtifact().getKey(), e); |
| 84 | + } |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + private final ExtensionCatalog primaryPlatformBom; |
| 89 | + private final List<ExtensionCatalog> extensionOrigins; |
| 90 | + private final List<Extension> extensions; |
| 91 | + |
| 92 | + public RegistryProjectInfo(ExtensionCatalog primaryPlatformBom, List<ExtensionCatalog> extensionOrigins, |
| 93 | + List<Extension> extensions) { |
| 94 | + this.primaryPlatformBom = primaryPlatformBom; |
| 95 | + this.extensionOrigins = extensionOrigins; |
| 96 | + this.extensions = extensions; |
| 97 | + } |
| 98 | + |
| 99 | + public ExtensionCatalog getPrimaryPlatformBom() { |
| 100 | + return primaryPlatformBom; |
| 101 | + } |
| 102 | + |
| 103 | + public List<ExtensionCatalog> getExtensionOrigins() { |
| 104 | + return extensionOrigins; |
| 105 | + } |
| 106 | + |
| 107 | + public List<Extension> getExtensions() { |
| 108 | + return extensions; |
| 109 | + } |
| 110 | +} |
0 commit comments