src/main/java/de/unixwork/uwproj/PkgConfigPackage.java

Sun, 10 Sep 2023 14:29:20 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 10 Sep 2023 14:29:20 +0200
changeset 46
096f3105b3b1
parent 41
75ee588d5d9e
child 63
ac47c2fb2c4a
permissions
-rw-r--r--

add support for pkgconf version constraints - fixes #294

package de.unixwork.uwproj;

import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.stream.Collectors;

import static de.unixwork.uwproj.Util.isNotNullOrBlank;

public class PkgConfigPackage {
    private String name;
    private String atLeastVersion;
    private String exactVersion;
    private String maxVersion;

    public static PkgConfigPackage parse(Element e) throws Exception {
        var p = new PkgConfigPackage();
        String name = Util.getContent(e);
        if (name.isBlank()) {
            throw new Exception("pkgconfig element: value required");
        } else {
            p.setName(name);
        }
        p.setAtLeastVersion(e.getAttribute("atleast"));
        p.setExactVersion(e.getAttribute("exact"));
        p.setMaxVersion(e.getAttribute("max"));
        return p;
    }

    public String getName() {
        return name;
    }

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

    public String getAtLeastVersion() {
        return atLeastVersion;
    }

    public void setAtLeastVersion(String atLeastVersion) {
        this.atLeastVersion = atLeastVersion;
    }

    public String getMaxVersion() {
        return maxVersion;
    }

    public void setMaxVersion(String maxVersion) {
        this.maxVersion = maxVersion;
    }

    public String getExactVersion() {
        return exactVersion;
    }

    public void setExactVersion(String exactVersion) {
        this.exactVersion = exactVersion;
    }

    public String getPkgConfigTestQuery() {
        // pkg-config is a more broken piece of software than we thought
        // it is not possible to combine any flags
        // e.g. --atleast-version and --max-version together will return true,
        // even when the max-version is violated
        // or other example: combining any version flag with --cflags or
        // --libs does not print the flags
        // therefore we have to execute multiple query first, and obtain the flags later

        final var commands = new ArrayList<String>();
        if (isNotNullOrBlank(atLeastVersion)) {
            commands.add("$PKG_CONFIG --atleast-version="+atLeastVersion+" "+name);
        }
        if (isNotNullOrBlank(exactVersion)) {
            commands.add("$PKG_CONFIG --exact-version="+exactVersion+" "+name);
        }
        if (isNotNullOrBlank(maxVersion)) {
            commands.add("$PKG_CONFIG --max-version="+maxVersion+" "+name);
        }
        if (commands.isEmpty()) {
            commands.add("$PKG_CONFIG "+name);
        }
        return String.join(" && ", commands);
    }
}

mercurial