Search in sources :

Example 1 with ExtensionCatalog

use of io.quarkus.registry.catalog.ExtensionCatalog in project quarkus by quarkusio.

the class UpdateCommandHandler method resolveRecommendedState.

private static ProjectState resolveRecommendedState(ProjectState currentState, ExtensionCatalog latestCatalog, MessageWriter log) {
    if (currentState.getPlatformBoms().isEmpty()) {
        return currentState;
    }
    if (currentState.getExtensions().isEmpty()) {
        return currentState;
    }
    final ExtensionMap extensionInfo = new ExtensionMap();
    for (TopExtensionDependency dep : currentState.getExtensions()) {
        extensionInfo.add(new ExtensionInfo(dep));
    }
    for (Extension e : latestCatalog.getExtensions()) {
        final ExtensionInfo candidate = extensionInfo.get(e.getArtifact().getKey());
        if (candidate != null && candidate.latestMetadata == null) {
            // if the latestMetadata has already been initialized, it's already the preferred one
            // that could happen if an artifact has relocated
            candidate.latestMetadata = e;
        }
    }
    final List<ExtensionInfo> unknownExtensions = new ArrayList<>(0);
    final List<Extension> updateCandidates = new ArrayList<>(extensionInfo.size());
    final Map<String, ExtensionMap> updateCandidatesByOrigin = new HashMap<>();
    for (ExtensionInfo i : extensionInfo.values()) {
        if (i.latestMetadata == null) {
            unknownExtensions.add(i);
        } else {
            updateCandidates.add(i.latestMetadata);
            for (ExtensionOrigin o : i.latestMetadata.getOrigins()) {
                updateCandidatesByOrigin.computeIfAbsent(o.getId(), k -> new ExtensionMap()).add(i);
            }
        }
    }
    if (extensionInfo.isEmpty()) {
        return currentState;
    }
    if (!unknownExtensions.isEmpty()) {
        log.warn("The configured Quarkus registries did not provide any compatibility information for the following extensions in the context of the currently recommended Quarkus platforms:");
        unknownExtensions.forEach(e -> log.warn(" " + e.currentDep.getArtifact().toCompactCoords()));
    }
    final List<ExtensionCatalog> recommendedOrigins;
    try {
        recommendedOrigins = getRecommendedOrigins(latestCatalog, updateCandidates);
    } catch (QuarkusCommandException e) {
        log.info("Failed to find a compatible configuration update for the project");
        return currentState;
    }
    int collectedUpdates = 0;
    for (ExtensionCatalog recommendedOrigin : recommendedOrigins) {
        final ExtensionMap candidates = updateCandidatesByOrigin.get(recommendedOrigin.getId());
        for (Extension e : recommendedOrigin.getExtensions()) {
            final ExtensionInfo info = candidates.get(e.getArtifact().getKey());
            if (info != null && info.recommendedMetadata == null) {
                info.setRecommendedMetadata(e);
                if (++collectedUpdates == updateCandidates.size()) {
                    break;
                }
            }
        }
    }
    final ProjectState.Builder stateBuilder = ProjectState.builder();
    for (ExtensionCatalog c : recommendedOrigins) {
        if (c.isPlatform()) {
            stateBuilder.addPlatformBom(c.getBom());
        }
    }
    final Map<String, ExtensionProvider.Builder> extProviders = new LinkedHashMap<>(recommendedOrigins.size());
    for (ExtensionInfo info : extensionInfo.values()) {
        final TopExtensionDependency ext = info.getRecommendedDependency();
        stateBuilder.addExtensionDependency(ext);
        extProviders.computeIfAbsent(ext.getProviderKey(), k -> ExtensionProvider.builder().setOrigin(ext.getOrigin())).addExtension(ext);
    }
    extProviders.values().forEach(b -> stateBuilder.addExtensionProvider(b.build()));
    for (ModuleState module : currentState.getModules()) {
        final ModuleState.Builder moduleBuilder = ModuleState.builder().setMainModule(module.isMain()).setWorkspaceModule(module.getWorkspaceModule());
        for (TopExtensionDependency dep : module.getExtensions()) {
            final TopExtensionDependency recommendedDep = extensionInfo.get(dep.getKey()).getRecommendedDependency();
            moduleBuilder.addExtensionDependency(recommendedDep);
            final ExtensionOrigin origin = recommendedDep.getOrigin();
            if (origin != null && origin.isPlatform()) {
                moduleBuilder.addPlatformBom(origin.getBom());
            }
        }
        stateBuilder.addModule(moduleBuilder.build());
    }
    return stateBuilder.build();
}
Also used : OriginPreference(io.quarkus.registry.catalog.selection.OriginPreference) ExtensionOrigins(io.quarkus.registry.catalog.selection.ExtensionOrigins) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) QuarkusCommandInvocation(io.quarkus.devtools.commands.data.QuarkusCommandInvocation) ArtifactKey(io.quarkus.maven.dependency.ArtifactKey) ArtifactCoords(io.quarkus.maven.dependency.ArtifactCoords) Map(java.util.Map) QuarkusCommandOutcome(io.quarkus.devtools.commands.data.QuarkusCommandOutcome) ExtensionCatalog(io.quarkus.registry.catalog.ExtensionCatalog) ModuleState(io.quarkus.devtools.project.state.ModuleState) PlatformInfo(io.quarkus.devtools.commands.handlers.InfoCommandHandler.PlatformInfo) ExtensionProvider(io.quarkus.devtools.project.state.ExtensionProvider) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) OriginSelector(io.quarkus.registry.catalog.selection.OriginSelector) ExtensionOrigin(io.quarkus.registry.catalog.ExtensionOrigin) List(java.util.List) ApplicationModel(io.quarkus.bootstrap.model.ApplicationModel) TopExtensionDependency(io.quarkus.devtools.project.state.TopExtensionDependency) QuarkusCommandException(io.quarkus.devtools.commands.data.QuarkusCommandException) MessageWriter(io.quarkus.devtools.messagewriter.MessageWriter) Extension(io.quarkus.registry.catalog.Extension) OriginCombination(io.quarkus.registry.catalog.selection.OriginCombination) Collections(java.util.Collections) ProjectState(io.quarkus.devtools.project.state.ProjectState) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ExtensionCatalog(io.quarkus.registry.catalog.ExtensionCatalog) ModuleState(io.quarkus.devtools.project.state.ModuleState) QuarkusCommandException(io.quarkus.devtools.commands.data.QuarkusCommandException) LinkedHashMap(java.util.LinkedHashMap) Extension(io.quarkus.registry.catalog.Extension) ProjectState(io.quarkus.devtools.project.state.ProjectState) TopExtensionDependency(io.quarkus.devtools.project.state.TopExtensionDependency) ExtensionOrigin(io.quarkus.registry.catalog.ExtensionOrigin)

Example 2 with ExtensionCatalog

use of io.quarkus.registry.catalog.ExtensionCatalog in project quarkus by quarkusio.

the class UpdateCommandHandler method addOrigins.

private static void addOrigins(final List<ExtensionOrigins> extOrigins, Extension e) {
    ExtensionOrigins.Builder eoBuilder = null;
    for (ExtensionOrigin o : e.getOrigins()) {
        if (!(o instanceof ExtensionCatalog)) {
            continue;
        }
        final ExtensionCatalog c = (ExtensionCatalog) o;
        final OriginPreference op = (OriginPreference) c.getMetadata().get("origin-preference");
        if (op == null) {
            continue;
        }
        if (eoBuilder == null) {
            eoBuilder = ExtensionOrigins.builder(e.getArtifact().getKey());
        }
        eoBuilder.addOrigin(c, op);
    }
    if (eoBuilder != null) {
        extOrigins.add(eoBuilder.build());
    }
}
Also used : ExtensionOrigins(io.quarkus.registry.catalog.selection.ExtensionOrigins) OriginPreference(io.quarkus.registry.catalog.selection.OriginPreference) ExtensionCatalog(io.quarkus.registry.catalog.ExtensionCatalog) ExtensionOrigin(io.quarkus.registry.catalog.ExtensionOrigin)

Example 3 with ExtensionCatalog

use of io.quarkus.registry.catalog.ExtensionCatalog in project quarkus by quarkusio.

the class CreateProjectCommandHandler method addOrigins.

private static void addOrigins(final List<ExtensionOrigins> extOrigins, Extension e) {
    ExtensionOrigins.Builder eoBuilder = null;
    for (ExtensionOrigin o : e.getOrigins()) {
        if (!(o instanceof ExtensionCatalog)) {
            continue;
        }
        final ExtensionCatalog c = (ExtensionCatalog) o;
        final OriginPreference op = (OriginPreference) c.getMetadata().get("origin-preference");
        if (op == null) {
            continue;
        }
        if (eoBuilder == null) {
            eoBuilder = ExtensionOrigins.builder(e.getArtifact().getKey());
        }
        eoBuilder.addOrigin(c, op);
    }
    if (eoBuilder != null) {
        extOrigins.add(eoBuilder.build());
    }
}
Also used : ExtensionOrigins(io.quarkus.registry.catalog.selection.ExtensionOrigins) OriginPreference(io.quarkus.registry.catalog.selection.OriginPreference) ExtensionCatalog(io.quarkus.registry.catalog.ExtensionCatalog) ExtensionOrigin(io.quarkus.registry.catalog.ExtensionOrigin)

Example 4 with ExtensionCatalog

use of io.quarkus.registry.catalog.ExtensionCatalog in project quarkus by quarkusio.

the class ToolsUtils method resolvePlatformDescriptorDirectly.

public static ExtensionCatalog resolvePlatformDescriptorDirectly(String bomGroupId, String bomArtifactId, String bomVersion, MavenArtifactResolver artifactResolver, MessageWriter log) {
    // TODO remove this method once we have the registry service available
    if (bomVersion == null) {
        throw new IllegalArgumentException("BOM version was not provided");
    }
    Artifact catalogCoords = new DefaultArtifact(bomGroupId == null ? ToolsConstants.DEFAULT_PLATFORM_BOM_GROUP_ID : bomGroupId, (bomArtifactId == null ? ToolsConstants.DEFAULT_PLATFORM_BOM_ARTIFACT_ID : bomArtifactId) + BootstrapConstants.PLATFORM_DESCRIPTOR_ARTIFACT_ID_SUFFIX, bomVersion, "json", bomVersion);
    Path platformJson = null;
    try {
        log.debug("Resolving platform descriptor %s", catalogCoords);
        platformJson = artifactResolver.resolve(catalogCoords).getArtifact().getFile().toPath();
    } catch (Exception e) {
        if (bomGroupId == null && catalogCoords.getArtifactId().startsWith("quarkus-bom")) {
            catalogCoords = new DefaultArtifact(ToolsConstants.IO_QUARKUS, "quarkus-bom" + BootstrapConstants.PLATFORM_DESCRIPTOR_ARTIFACT_ID_SUFFIX, catalogCoords.getClassifier(), catalogCoords.getExtension(), catalogCoords.getVersion());
            try {
                log.debug("Resolving platform descriptor %s", catalogCoords);
                platformJson = artifactResolver.resolve(catalogCoords).getArtifact().getFile().toPath();
            } catch (Exception e2) {
            }
        }
        if (platformJson == null) {
            final StringBuilder sb = new StringBuilder();
            sb.append("Failed to resolve extension catalog for ");
            sb.append(PlatformArtifacts.ensureBomArtifact(new ArtifactCoords(catalogCoords.getGroupId(), catalogCoords.getArtifactId(), catalogCoords.getClassifier(), catalogCoords.getExtension(), catalogCoords.getVersion())).toCompactCoords());
            sb.append(". Make sure the groupId, artifactId and version are spelled correctly and the relevant Maven repositories are configured.");
            throw new RuntimeException(sb.toString(), e);
        }
    }
    ExtensionCatalog catalog;
    try {
        catalog = ExtensionCatalog.fromFile(platformJson);
    } 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());
                int memberIndex = 0;
                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 = ExtensionCatalog.fromFile(jsonPath);
                        } catch (Exception e) {
                            log.warn("Failed to resolve member catalog " + m, e);
                            continue;
                        }
                    }
                    final OriginPreference originPreference = new OriginPreference(1, 1, 1, ++memberIndex, 1);
                    Map<String, Object> metadata = new HashMap<>(memberCatalog.getMetadata());
                    metadata.put("origin-preference", originPreference);
                    ExtensionCatalog.Mutable mutableMemberCatalog = memberCatalog.mutable();
                    mutableMemberCatalog.setMetadata(metadata);
                    catalogs.add(mutableMemberCatalog.build());
                }
                catalog = CatalogMergeUtility.merge(catalogs);
            }
        }
    }
    return catalog;
}
Also used : Path(java.nio.file.Path) ArtifactCoords(io.quarkus.maven.ArtifactCoords) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExtensionCatalog(io.quarkus.registry.catalog.ExtensionCatalog) IOException(java.io.IOException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) IOException(java.io.IOException) Collection(java.util.Collection) OriginPreference(io.quarkus.registry.catalog.selection.OriginPreference) HashMap(java.util.HashMap) Map(java.util.Map) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 5 with ExtensionCatalog

use of io.quarkus.registry.catalog.ExtensionCatalog in project quarkus by quarkusio.

the class MavenProjectBuildFile method getProject.

public static QuarkusProject getProject(Artifact projectPom, Model projectModel, Path projectDir, Properties projectProps, MavenArtifactResolver artifactResolver, ExtensionCatalogResolver catalogResolver, MessageWriter log, Supplier<String> defaultQuarkusVersion) throws RegistryResolutionException {
    final List<ArtifactCoords> managedDeps;
    final Supplier<List<ArtifactCoords>> deps;
    final List<ArtifactCoords> importedPlatforms;
    final String quarkusVersion;
    if (projectPom == null) {
        managedDeps = Collections.emptyList();
        deps = () -> Collections.emptyList();
        importedPlatforms = Collections.emptyList();
        // TODO allow multiple streams in the same catalog for now
        // defaultQuarkusVersion.get();
        quarkusVersion = null;
    } else {
        final ArtifactDescriptorResult descriptor = describe(artifactResolver, projectPom);
        managedDeps = toArtifactCoords(descriptor.getManagedDependencies());
        deps = () -> toArtifactCoords(descriptor.getDependencies());
        importedPlatforms = collectPlatformDescriptors(managedDeps, log);
        quarkusVersion = getQuarkusVersion(managedDeps);
    }
    final ExtensionCatalog extensionCatalog;
    if (catalogResolver.hasRegistries()) {
        try {
            if (!importedPlatforms.isEmpty()) {
                extensionCatalog = catalogResolver.resolveExtensionCatalog(importedPlatforms);
            } else {
                extensionCatalog = quarkusVersion == null ? catalogResolver.resolveExtensionCatalog() : catalogResolver.resolveExtensionCatalog(quarkusVersion);
            }
        } catch (RegistryResolutionException e) {
            throw new RuntimeException("Failed to resolve extension catalog", e);
        }
    } else {
        if (importedPlatforms.isEmpty()) {
            extensionCatalog = ToolsUtils.resolvePlatformDescriptorDirectly(null, null, quarkusVersion, artifactResolver, log);
        } else {
            extensionCatalog = ToolsUtils.mergePlatforms(importedPlatforms, artifactResolver);
        }
    }
    final MavenProjectBuildFile extensionManager = new MavenProjectBuildFile(projectDir, extensionCatalog, projectModel, deps, managedDeps, projectProps, projectPom == null ? null : artifactResolver);
    final List<ResourceLoader> codestartResourceLoaders = codestartLoadersBuilder().catalog(extensionCatalog).artifactResolver(artifactResolver).build();
    return QuarkusProject.of(projectDir, extensionCatalog, codestartResourceLoaders, log, extensionManager);
}
Also used : RegistryResolutionException(io.quarkus.registry.RegistryResolutionException) ResourceLoader(io.quarkus.platform.descriptor.loader.json.ResourceLoader) ArtifactCoords(io.quarkus.maven.ArtifactCoords) ArrayList(java.util.ArrayList) List(java.util.List) ExtensionCatalog(io.quarkus.registry.catalog.ExtensionCatalog) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult)

Aggregations

ExtensionCatalog (io.quarkus.registry.catalog.ExtensionCatalog)47 Extension (io.quarkus.registry.catalog.Extension)19 ArrayList (java.util.ArrayList)15 ArtifactCoords (io.quarkus.maven.ArtifactCoords)14 ExtensionOrigin (io.quarkus.registry.catalog.ExtensionOrigin)12 IOException (java.io.IOException)11 Path (java.nio.file.Path)11 Test (org.junit.jupiter.api.Test)11 HashMap (java.util.HashMap)10 QuarkusCommandException (io.quarkus.devtools.commands.data.QuarkusCommandException)9 OriginPreference (io.quarkus.registry.catalog.selection.OriginPreference)9 List (java.util.List)9 Map (java.util.Map)9 QuarkusCommandOutcome (io.quarkus.devtools.commands.data.QuarkusCommandOutcome)8 MessageWriter (io.quarkus.devtools.messagewriter.MessageWriter)8 QuarkusProject (io.quarkus.devtools.project.QuarkusProject)7 ExtensionOrigins (io.quarkus.registry.catalog.selection.ExtensionOrigins)7 Collections (java.util.Collections)7 MavenArtifactResolver (io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver)6 File (java.io.File)6