Search in sources :

Example 1 with Extension

use of io.quarkus.registry.catalog.Extension 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 Extension

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

the class CodestartResourceLoadersBuilder method getCodestartResourceLoaders.

private static List<ResourceLoader> getCodestartResourceLoaders(String baseCodestartsArtifactCoords, Collection<String> extraCodestartsArtifactCoords, ExtensionCatalog catalog, MavenArtifactResolver mvn) {
    final Map<String, Artifact> codestartsArtifacts = new LinkedHashMap<>();
    if (catalog != null) {
        // Load codestarts from each extensions codestart artifacts
        for (Extension e : catalog.getExtensions()) {
            final String artifactCoords = getCodestartArtifact(e);
            if (artifactCoords == null || codestartsArtifacts.containsKey(artifactCoords)) {
                continue;
            }
            codestartsArtifacts.put(artifactCoords, DependencyNodeUtils.toArtifact(artifactCoords));
        }
    }
    // Load base codestart artifacts
    if (baseCodestartsArtifactCoords != null) {
        codestartsArtifacts.put(baseCodestartsArtifactCoords, DependencyNodeUtils.toArtifact(baseCodestartsArtifactCoords));
    }
    if (catalog != null) {
        // Load codestarts from catalog codestart artifacts
        final List<String> catalogCodestartArtifacts = getCodestartArtifacts(catalog);
        for (String artifactCoords : catalogCodestartArtifacts) {
            if (codestartsArtifacts.containsKey(artifactCoords)) {
                continue;
            }
            codestartsArtifacts.put(artifactCoords, DependencyNodeUtils.toArtifact(artifactCoords));
        }
    }
    // Load codestarts from the given artifacts
    for (String codestartArtifactCoords : extraCodestartsArtifactCoords) {
        codestartsArtifacts.put(codestartArtifactCoords, DependencyNodeUtils.toArtifact(codestartArtifactCoords));
    }
    final List<ResourceLoader> codestartResourceLoaders = new ArrayList<>(codestartsArtifacts.size());
    for (Artifact a : codestartsArtifacts.values()) {
        try {
            final File artifactFile = mvn.resolve(a).getArtifact().getFile();
            codestartResourceLoaders.add(resolveFileResourceLoader(artifactFile));
        } catch (Exception e) {
            throw new RuntimeException("Failed to resolve codestart artifact " + a, e);
        }
    }
    return codestartResourceLoaders;
}
Also used : Extension(io.quarkus.registry.catalog.Extension) ResourceLoaders.resolveFileResourceLoader(io.quarkus.platform.descriptor.loader.json.ResourceLoaders.resolveFileResourceLoader) ResourceLoader(io.quarkus.platform.descriptor.loader.json.ResourceLoader) ArrayList(java.util.ArrayList) File(java.io.File) Artifact(org.eclipse.aether.artifact.Artifact) ExtensionProcessor.getCodestartArtifact(io.quarkus.platform.catalog.processor.ExtensionProcessor.getCodestartArtifact) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with Extension

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

the class ExtensionPredicateTest method acceptKeywordInArtifactId.

@Test
void acceptKeywordInArtifactId() {
    ExtensionPredicate predicate = new ExtensionPredicate("foo");
    Extension extension = Extension.builder().setArtifact(new ArtifactCoords("g", "foo-bar", null, "jar", "1.0")).build();
    assertThat(predicate).accepts(extension);
}
Also used : Extension(io.quarkus.registry.catalog.Extension) ArtifactCoords(io.quarkus.maven.ArtifactCoords) Test(org.junit.jupiter.api.Test)

Example 4 with Extension

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

the class ExtensionPredicateTest method acceptKeywordInLabel.

@Test
void acceptKeywordInLabel() {
    ExtensionPredicate predicate = new ExtensionPredicate("foo");
    Extension extension = Extension.builder().setArtifact(new ArtifactCoords("g", "a", null, "jar", "1.0")).setMetadata(Extension.MD_KEYWORDS, Arrays.asList("foo", "bar")).build();
    assertThat(predicate).accepts(extension);
}
Also used : Extension(io.quarkus.registry.catalog.Extension) ArtifactCoords(io.quarkus.maven.ArtifactCoords) Test(org.junit.jupiter.api.Test)

Example 5 with Extension

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

the class CreateProjectHelper method completeCatalog.

/**
 * This method checks whether extensions to be added are specified using complete artifact coordinates,
 * in which case they are resolved and added to the catalog so that their codestarts are picked up by the code generator.
 *
 * @param catalog original extension catalog
 * @param extensions extra extensions to add to the catalog
 * @param mvn Maven artifact resolver
 * @return complete extension catalog
 * @throws BootstrapMavenException in case of a failure to resolve extensions requested by the user
 */
public static ExtensionCatalog completeCatalog(ExtensionCatalog catalog, Collection<String> extensions, MavenArtifactResolver mvn) {
    ExtensionCatalog.Mutable mutableCatalog = null;
    for (String extArg : extensions) {
        if (isFullArtifactCoords(extArg)) {
            var coords = ArtifactCoords.fromString(extArg.trim());
            final Path extJar;
            try {
                extJar = mvn.resolve(new DefaultArtifact(coords.getGroupId(), coords.getArtifactId(), coords.getClassifier(), coords.getType(), coords.getVersion())).getArtifact().getFile().toPath();
            } catch (BootstrapMavenException e) {
                throw new RuntimeException("Failed to resolve extension " + coords, e);
            }
            final Extension ext = PathTree.ofDirectoryOrArchive(extJar).apply(BootstrapConstants.EXTENSION_METADATA_PATH, visit -> {
                if (visit == null) {
                    return null;
                }
                try {
                    return Extension.fromFile(visit.getPath());
                } catch (IOException e) {
                    throw new IllegalStateException("Failed to parse Quarkus extension metadata " + visit.getPath());
                }
            });
            if (ext != null) {
                if (mutableCatalog == null) {
                    mutableCatalog = catalog.mutable();
                }
                var i = mutableCatalog.getExtensions().iterator();
                boolean add = true;
                while (i.hasNext()) {
                    final ArtifactCoords catalogCoords = i.next().getArtifact();
                    if (catalogCoords.getKey().equals(ext.getArtifact().getKey())) {
                        if (catalogCoords.getVersion().equals(ext.getArtifact().getVersion())) {
                            add = false;
                        } else {
                            i.remove();
                        }
                        break;
                    }
                }
                if (add) {
                    mutableCatalog.addExtension(ext);
                }
            }
        }
    }
    if (mutableCatalog != null) {
        catalog = mutableCatalog.build();
    }
    return catalog;
}
Also used : Path(java.nio.file.Path) ArtifactCoords(io.quarkus.maven.ArtifactCoords) ExtensionCatalog(io.quarkus.registry.catalog.ExtensionCatalog) IOException(java.io.IOException) BootstrapMavenException(io.quarkus.bootstrap.resolver.maven.BootstrapMavenException) Extension(io.quarkus.registry.catalog.Extension) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Aggregations

Extension (io.quarkus.registry.catalog.Extension)39 ArtifactCoords (io.quarkus.maven.ArtifactCoords)21 ExtensionCatalog (io.quarkus.registry.catalog.ExtensionCatalog)21 Test (org.junit.jupiter.api.Test)16 ArrayList (java.util.ArrayList)15 Map (java.util.Map)11 IOException (java.io.IOException)10 List (java.util.List)10 HashMap (java.util.HashMap)9 QuarkusCommandInvocation (io.quarkus.devtools.commands.data.QuarkusCommandInvocation)8 SelectionResult (io.quarkus.devtools.commands.data.SelectionResult)8 ExtensionOrigin (io.quarkus.registry.catalog.ExtensionOrigin)8 Collectors (java.util.stream.Collectors)8 QuarkusCommandException (io.quarkus.devtools.commands.data.QuarkusCommandException)7 QuarkusCommandOutcome (io.quarkus.devtools.commands.data.QuarkusCommandOutcome)7 Collection (java.util.Collection)7 Collections (java.util.Collections)7 MessageWriter (io.quarkus.devtools.messagewriter.MessageWriter)6 ArtifactKey (io.quarkus.maven.ArtifactKey)5 LinkedHashMap (java.util.LinkedHashMap)5