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

org.jreleaser.model.internal.packagers.JibSpec 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.packagers;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.jreleaser.model.Active;
import org.jreleaser.model.internal.common.Artifact;
import org.jreleaser.model.internal.common.Domain;
import org.jreleaser.util.PlatformUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;
import static java.util.Collections.unmodifiableSet;
import static java.util.stream.Collectors.toSet;
import static org.jreleaser.model.Constants.KEY_PLATFORM;
import static org.jreleaser.util.StringUtils.isNotBlank;

/**
 * @author Andres Almiray
 * @since 1.6.0
 */
public final class JibSpec extends AbstractJibConfiguration implements Domain {
    private static final long serialVersionUID = -7696534492476787337L;

    private final Map matchers = new LinkedHashMap<>();
    private Artifact artifact;
    private String name;

    @JsonIgnore
    private final org.jreleaser.model.api.packagers.JibSpec immutable = new org.jreleaser.model.api.packagers.JibSpec() {
        private static final long serialVersionUID = 3806780814992744269L;

        private Set registries;

        @Override
        public org.jreleaser.model.api.common.Artifact getArtifact() {
            return artifact.asImmutable();
        }

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

        @Override
        public Map getMatchers() {
            return unmodifiableMap(matchers);
        }

        @Override
        public String getTemplateDirectory() {
            return JibSpec.this.getTemplateDirectory();
        }

        @Override
        public List getSkipTemplates() {
            return unmodifiableList(JibSpec.this.getSkipTemplates());
        }

        @Override
        public String getBaseImage() {
            return JibSpec.this.getBaseImage();
        }

        @Override
        public String getCreationTime() {
            return JibSpec.this.getCreationTime();
        }

        @Override
        public Format getFormat() {
            return JibSpec.this.getFormat();
        }

        @Override
        public Map getEnvironment() {
            return unmodifiableMap(JibSpec.this.getEnvironment());
        }

        @Override
        public Set getVolumes() {
            return unmodifiableSet(JibSpec.this.getVolumes());
        }

        @Override
        public Set getExposedPorts() {
            return unmodifiableSet(JibSpec.this.getExposedPorts());
        }

        @Override
        public String getUser() {
            return JibSpec.this.getUser();
        }

        @Override
        public String getWorkingDirectory() {
            return JibSpec.this.getWorkingDirectory();
        }

        @Override
        public Map getLabels() {
            return unmodifiableMap(JibSpec.this.getLabels());
        }

        @Override
        public Set getImageNames() {
            return unmodifiableSet(JibSpec.this.getImageNames());
        }

        @Override
        public Set getRegistries() {
            if (null == registries) {
                registries = JibSpec.this.getRegistries().stream()
                    .map(JibConfiguration.Registry::asImmutable)
                    .collect(toSet());
            }
            return registries;
        }

        @Override
        public Active getActive() {
            return JibSpec.this.getActive();
        }

        @Override
        public boolean isEnabled() {
            return JibSpec.this.isEnabled();
        }

        @Override
        public Map asMap(boolean full) {
            return unmodifiableMap(JibSpec.this.asMap(full));
        }

        @Override
        public String getPrefix() {
            return JibSpec.this.prefix();
        }

        @Override
        public Map getExtraProperties() {
            return unmodifiableMap(JibSpec.this.getExtraProperties());
        }
    };

    public org.jreleaser.model.api.packagers.JibSpec asImmutable() {
        return immutable;
    }

    @Override
    public void merge(JibSpec source) {
        super.merge(source);
        this.name = merge(this.name, source.name);
        this.artifact = source.artifact;
        setMatchers(merge(this.matchers, source.matchers));
    }

    public Artifact getArtifact() {
        return artifact;
    }

    public void setArtifact(Artifact artifact) {
        this.artifact = artifact;
        this.artifact.select();
    }

    public String getName() {
        return name;
    }

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

    public Map getMatchers() {
        return matchers;
    }

    public void setMatchers(Map matchers) {
        this.matchers.putAll(matchers);
    }

    @Override
    public Map asMap(boolean full) {
        Map props = super.asMap(full);

        if (!props.isEmpty()) {
            Map map = new LinkedHashMap<>();
            map.put(name, props);
            return map;
        }

        return props;
    }

    @Override
    protected void asMap(boolean full, Map props) {
        props.put("matchers", matchers);
        if (null != artifact) {
            props.put("artifact", artifact.asMap(full));
        }
    }

    public boolean matches(Artifact artifact) {
        boolean matched = true;

        for (Map.Entry e : matchers.entrySet()) {
            String key = e.getKey();
            if (KEY_PLATFORM.equals(key)) {
                matched &= isNotBlank(artifact.getPlatform()) && PlatformUtils.isCompatible(String.valueOf(e.getValue()), artifact.getPlatform());
            } else if (artifact.getExtraProperties().containsKey(key)) {
                matched &= e.getValue().equals(artifact.getExtraProperties().get(key));
            }
        }

        return matched;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy