All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.devtools.codestarts.core.DefaultCodestartProjectDefinition Maven / Gradle / Ivy

There is a newer version: 3.17.0.CR1
Show newest version
package io.quarkus.devtools.codestarts.core;

import static io.quarkus.devtools.codestarts.core.CodestartCatalogs.findLanguageName;
import static io.quarkus.devtools.codestarts.core.CodestartCatalogs.findRequiredCodestart;
import static io.quarkus.devtools.codestarts.core.CodestartData.buildCodestartProjectData;
import static io.quarkus.devtools.codestarts.core.CodestartData.buildDependenciesData;
import static java.util.Objects.requireNonNull;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.quarkus.devtools.codestarts.Codestart;
import io.quarkus.devtools.codestarts.CodestartProjectDefinition;
import io.quarkus.devtools.codestarts.CodestartProjectInput;
import io.quarkus.devtools.codestarts.CodestartType;
import io.quarkus.devtools.codestarts.utils.NestedMaps;

public final class DefaultCodestartProjectDefinition implements CodestartProjectDefinition {

    private static final Comparator PROCESSING_ORDER = Comparator.comparingInt(Codestart::getTypeOrder)
            .thenComparing(Codestart::getName);

    private static final Comparator SHARED_DATA_MERGE_ORDER = PROCESSING_ORDER;

    private final String languageName;
    private final List codestarts;
    private final CodestartProjectInput projectInput;

    private DefaultCodestartProjectDefinition(CodestartProjectInput projectInput,
            String languageName,
            List codestarts) {
        this.projectInput = requireNonNull(projectInput, "codestartInput is required");
        this.languageName = requireNonNull(languageName, "languageName is required");
        this.codestarts = requireNonNull(codestarts, "codestarts is required");
    }

    public static CodestartProjectDefinition of(CodestartProjectInput projectInput,
            Collection codestarts) {
        final String languageName = findLanguageName(codestarts);
        findRequiredCodestart(codestarts, CodestartType.PROJECT);
        final List sorted = codestarts.stream()
                .sorted(PROCESSING_ORDER)
                .collect(Collectors.toList());
        return new DefaultCodestartProjectDefinition(projectInput, languageName, sorted);
    }

    @Override
    public void generate(Path targetDirectory) throws IOException {
        CodestartProjectGeneration.generateProject(this, targetDirectory);
    }

    @Override
    public List getCodestarts() {
        return codestarts;
    }

    @Override
    public CodestartProjectInput getProjectInput() {
        return projectInput;
    }

    @Override
    public Optional getCodestart(CodestartType type) {
        return CodestartCatalogs.findCodestart(codestarts, type);
    }

    @Override
    public Codestart getRequiredCodestart(CodestartType type) {
        return findRequiredCodestart(codestarts, type);
    }

    @Override
    public String getLanguageName() {
        return languageName;
    }

    @Override
    public Map getSharedData() {
        final Stream> codestartsGlobal = getCodestarts().stream()
                .sorted(SHARED_DATA_MERGE_ORDER)
                .map(c -> c.getSharedData(getLanguageName()));
        return NestedMaps.deepMerge(Stream.concat(codestartsGlobal, Stream.of(getProjectInput().getData())));
    }

    @Override
    public Map getDepsData() {
        return buildDependenciesData(getCodestarts().stream(), getLanguageName(),
                getProjectInput().getDependencies(), getProjectInput().getBoms());
    }

    @Override
    public Map getCodestartProjectData() {
        return buildCodestartProjectData(getBaseCodestarts(), getExtraCodestarts());
    }

    @Override
    public List getBaseCodestarts() {
        return getCodestarts().stream().filter(c -> c.getSpec().getType().isBase()).collect(Collectors.toList());
    }

    @Override
    public List getExtraCodestarts() {
        return getCodestarts().stream().filter(c -> !c.getSpec().getType().isBase()).collect(Collectors.toList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy