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

org.jreleaser.model.internal.common.EnvironmentVariables 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.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.jreleaser.model.internal.JReleaserContext;
import org.jreleaser.mustache.TemplateContext;

import java.util.LinkedHashMap;
import java.util.Map;

import static java.util.Collections.unmodifiableMap;
import static java.util.stream.Collectors.toMap;
import static org.jreleaser.mustache.Templates.resolveTemplate;

/**
 * @author Andres Almiray
 * @since 0.13.0
 */
public final class EnvironmentVariables extends AbstractModelObject implements Domain {
    private static final long serialVersionUID = -4764413898945637009L;

    private final Map universal = new LinkedHashMap<>();
    private final Map unix = new LinkedHashMap<>();
    private final Map linux = new LinkedHashMap<>();
    private final Map osx = new LinkedHashMap<>();
    private final Map windows = new LinkedHashMap<>();

    @JsonIgnore
    private final org.jreleaser.model.api.common.EnvironmentVariables immutable = new org.jreleaser.model.api.common.EnvironmentVariables() {
        private static final long serialVersionUID = -8852099385145553688L;

        @Override
        public Map getUniversal() {
            return unmodifiableMap(universal);
        }

        @Override
        public Map getUnix() {
            return unmodifiableMap(unix);
        }

        @Override
        public Map getLinux() {
            return unmodifiableMap(linux);
        }

        @Override
        public Map getOsx() {
            return unmodifiableMap(osx);
        }

        @Override
        public Map getWindows() {
            return unmodifiableMap(windows);
        }

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

    public org.jreleaser.model.api.common.EnvironmentVariables asImmutable() {
        return immutable;
    }

    @Override
    public void merge(EnvironmentVariables source) {
        setUniversal(merge(this.universal, source.universal));
        setUnix(merge(this.unix, source.unix));
        setLinux(merge(this.linux, source.linux));
        setOsx(merge(this.osx, source.osx));
        setWindows(merge(this.windows, source.windows));
    }

    public Map getUniversal() {
        return universal;
    }

    public void setUniversal(Map universal) {
        this.universal.clear();
        this.universal.putAll(universal);
    }

    public Map getUnix() {
        return unix;
    }

    public void setUnix(Map unix) {
        this.unix.clear();
        this.unix.putAll(unix);
    }

    public Map getLinux() {
        return linux;
    }

    public void setLinux(Map linux) {
        this.linux.clear();
        this.linux.putAll(linux);
    }

    public Map getOsx() {
        return osx;
    }

    public void setOsx(Map osx) {
        this.osx.clear();
        this.osx.putAll(osx);
    }

    public Map getWindows() {
        return windows;
    }

    public void setWindows(Map windows) {
        this.windows.clear();
        this.windows.putAll(windows);
    }

    public boolean isSet() {
        return
            !universal.isEmpty() ||
                !unix.isEmpty() ||
                !linux.isEmpty() ||
                !osx.isEmpty() ||
                !windows.isEmpty();
    }

    public Map getResolvedUniversal(JReleaserContext context) {
        TemplateContext props = context.fullProps();

        return universal.entrySet().stream()
            .collect(toMap(Map.Entry::getKey, e -> resolveTemplate(e.getValue(), props)));
    }

    public Map getResolvedUnix(JReleaserContext context) {
        TemplateContext props = context.fullProps();

        return unix.entrySet().stream()
            .collect(toMap(Map.Entry::getKey, e -> resolveTemplate(e.getValue(), props)));
    }

    public Map getResolvedLinux(JReleaserContext context) {
        TemplateContext props = context.fullProps();

        return linux.entrySet().stream()
            .collect(toMap(Map.Entry::getKey, e -> resolveTemplate(e.getValue(), props)));
    }

    public Map getResolvedOsx(JReleaserContext context) {
        TemplateContext props = context.fullProps();

        return osx.entrySet().stream()
            .collect(toMap(Map.Entry::getKey, e -> resolveTemplate(e.getValue(), props)));
    }

    public Map getResolvedWindows(JReleaserContext context) {
        TemplateContext props = context.fullProps();

        return windows.entrySet().stream()
            .collect(toMap(Map.Entry::getKey, e -> resolveTemplate(e.getValue(), props)));
    }

    @Override
    public Map asMap(boolean full) {
        Map map = new LinkedHashMap<>();
        map.put("universal", universal);
        map.put("unix", unix);
        map.put("linux", linux);
        map.put("osx", osx);
        map.put("windows", windows);
        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy