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

org.jreleaser.model.internal.assemble.AbstractAssembler Maven / Gradle / Ivy

The newest version!
/*
 * SPDX-License-Identifier: Apache-2.0
 *
 * Copyright 2020-2024 The JReleaser authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jreleaser.model.internal.assemble;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.jreleaser.model.Stereotype;
import org.jreleaser.model.internal.catalog.swid.SwidTag;
import org.jreleaser.model.internal.common.AbstractActivatable;
import org.jreleaser.model.internal.common.Artifact;
import org.jreleaser.model.internal.common.FileSet;
import org.jreleaser.model.internal.common.Glob;
import org.jreleaser.model.internal.platform.Platform;
import org.jreleaser.mustache.TemplateContext;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_NAME;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_STEREOTYPE;
import static org.jreleaser.mustache.MustacheUtils.applyTemplates;
import static org.jreleaser.util.StringUtils.isNotBlank;

/**
 * @author Andres Almiray
 * @since 0.2.0
 */
public abstract class AbstractAssembler, A extends org.jreleaser.model.api.assemble.Assembler> extends AbstractActivatable implements Assembler {
    private static final long serialVersionUID = -7134019171123897997L;

    @JsonIgnore
    private final Set outputs = new LinkedHashSet<>();
    private final Map extraProperties = new LinkedHashMap<>();
    private final Set artifacts = new LinkedHashSet<>();
    private final List files = new ArrayList<>();
    private final List fileSets = new ArrayList<>();
    private final Platform platform = new Platform();
    private final Set skipTemplates = new LinkedHashSet<>();
    private final SwidTag swid = new SwidTag();
    @JsonIgnore
    private final String type;
    @JsonIgnore
    private String name;

    private String templateDirectory;
    protected Boolean exported;
    private Stereotype stereotype;

    protected AbstractAssembler(String type) {
        this.type = type;
    }

    @Override
    public void merge(S source) {
        super.merge(source);
        this.exported = merge(this.exported, source.exported);
        this.name = merge(this.name, source.getName());
        this.platform.merge(source.getPlatform());
        this.stereotype = merge(this.stereotype, source.getStereotype());
        this.templateDirectory = merge(this.templateDirectory, source.getTemplateDirectory());
        this.swid.merge(source.getSwid());
        setSkipTemplates(merge(this.skipTemplates, source.getSkipTemplates()));
        setOutputs(merge(this.outputs, source.getOutputs()));
        setArtifacts(merge(this.artifacts, source.getArtifacts()));
        setFileSets(merge(this.fileSets, source.getFileSets()));
        setFiles(merge(this.files, source.getFiles()));
        setExtraProperties(merge(this.extraProperties, source.getExtraProperties()));
    }

    @Override
    public TemplateContext props() {
        TemplateContext props = new TemplateContext();
        applyTemplates(props, resolvedExtraProperties());
        props.set(KEY_DISTRIBUTION_NAME, name);
        props.set(KEY_DISTRIBUTION_STEREOTYPE, getStereotype());
        return props;
    }

    @Override
    public Stereotype getStereotype() {
        return stereotype;
    }

    @Override
    public void setStereotype(Stereotype stereotype) {
        this.stereotype = stereotype;
    }

    @Override
    public void setStereotype(String str) {
        setStereotype(Stereotype.of(str));
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public Platform getPlatform() {
        return platform;
    }

    @Override
    public void setPlatform(Platform platform) {
        this.platform.merge(platform);
    }

    @Override
    public String getTemplateDirectory() {
        return templateDirectory;
    }

    @Override
    public void setTemplateDirectory(String templateDirectory) {
        this.templateDirectory = templateDirectory;
    }

    @Override
    public Set getSkipTemplates() {
        return skipTemplates;
    }

    @Override
    public void setSkipTemplates(Set skipTemplates) {
        this.skipTemplates.clear();
        this.skipTemplates.addAll(skipTemplates);
    }

    @Override
    public void addSkipTemplates(Set templates) {
        this.skipTemplates.addAll(templates);
    }

    @Override
    public void addSkipTemplate(String template) {
        if (isNotBlank(template)) {
            this.skipTemplates.add(template.trim());
        }
    }

    @Override
    public boolean isExported() {
        return null == exported || exported;
    }

    @Override
    public void setExported(Boolean exported) {
        this.exported = exported;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public Set getOutputs() {
        return Artifact.sortArtifacts(outputs);
    }

    @Override
    public void setOutputs(Set output) {
        this.outputs.clear();
        this.outputs.addAll(output);
    }

    @Override
    public void addOutput(Artifact artifact) {
        if (null != artifact) {
            this.outputs.add(artifact);
        }
    }

    @Override
    public Map getExtraProperties() {
        return extraProperties;
    }

    @Override
    public void setExtraProperties(Map extraProperties) {
        this.extraProperties.clear();
        this.extraProperties.putAll(extraProperties);
    }

    @Override
    public void addExtraProperties(Map extraProperties) {
        this.extraProperties.putAll(extraProperties);
    }

    @Override
    public String prefix() {
        return getType();
    }

    @Override
    public Set getArtifacts() {
        return Artifact.sortArtifacts(artifacts);
    }

    @Override
    public void setArtifacts(Set artifacts) {
        this.artifacts.clear();
        this.artifacts.addAll(artifacts);
    }

    @Override
    public void addArtifacts(Set artifacts) {
        this.artifacts.addAll(artifacts);
    }

    @Override
    public void addArtifact(Artifact artifact) {
        if (null != artifact) {
            this.artifacts.add(artifact);
        }
    }

    @Override
    public List getFiles() {
        return files;
    }

    @Override
    public void setFiles(List files) {
        this.files.clear();
        this.files.addAll(files);
    }

    @Override
    public void addFiles(List files) {
        this.files.addAll(files);
    }

    @Override
    public void addFile(Glob file) {
        if (null != file) {
            this.files.add(file);
        }
    }

    @Override
    public List getFileSets() {
        return fileSets;
    }

    @Override
    public void setFileSets(List fileSets) {
        this.fileSets.clear();
        this.fileSets.addAll(fileSets);
    }

    @Override
    public void addFileSets(List files) {
        this.fileSets.addAll(files);
    }

    @Override
    public void addFileSet(FileSet file) {
        if (null != file) {
            this.fileSets.add(file);
        }
    }

    @Override
    public SwidTag getSwid() {
        return swid;
    }

    @Override
    public void setSwid(SwidTag swid) {
        this.swid.merge(swid);
    }

    @Override
    public Map asMap(boolean full) {
        if (!full && !isEnabled()) return Collections.emptyMap();

        Map props = new LinkedHashMap<>();
        props.put("enabled", isEnabled());
        props.put("exported", isExported());
        props.put("active", getActive());
        props.put("stereotype", stereotype);
        if (full || platform.isSet()) props.put("platform", platform.asMap(full));
        asMap(full, props);
        props.put("templateDirectory", templateDirectory);
        props.put("skipTemplates", skipTemplates);
        props.put("swid", swid.asMap(full));
        Map> mappedArtifacts = new LinkedHashMap<>();
        int i = 0;
        for (Artifact artifact : artifacts) {
            mappedArtifacts.put("artifact " + (i++), artifact.asMap(full));
        }
        props.put("artifacts", mappedArtifacts);
        Map> mappedFiles = new LinkedHashMap<>();
        for (i = 0; i < files.size(); i++) {
            mappedFiles.put("file " + i, files.get(i).asMap(full));
        }
        props.put("files", mappedFiles);
        Map> mappedFileSets = new LinkedHashMap<>();
        for (i = 0; i < fileSets.size(); i++) {
            mappedFileSets.put("fileSet " + i, fileSets.get(i).asMap(full));
        }
        props.put("fileSets", mappedFileSets);
        props.put("extraProperties", getExtraProperties());

        Map map = new LinkedHashMap<>();
        map.put(getName(), props);
        return map;
    }

    protected abstract void asMap(boolean full, Map props);
}