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

Mon, 01 Dec 2025 21:27:09 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 01 Dec 2025 21:27:09 +0100
changeset 177
8872934e5fb2
parent 165
f4f11f755ee7
permissions
-rw-r--r--

resolve dependencies between standard config vars

fixes #768

package de.unixwork.uwproj;

import org.w3c.dom.Element;

public final class ConfigVar extends AbstractOption {
    private static final String DEFAULT_PREFIX = "/usr";

    private final String varName;
    private final String value;
    private final boolean exec;

    public ConfigVar(String varName) {
        this.varName = varName;
        arg = varName.toLowerCase().replace('_', '-');
        value = varName.equals("prefix") ? DEFAULT_PREFIX : "";
        desc = getDefaultHelpText(varName).replace("%default", value);
        exec = false;
    }

    public ConfigVar(Element e) {
        this(e, e.getNodeName().replace('-', '_'));
    }

    public ConfigVar(Element e, String defaultVarName) {
        super();
        varName = Util.getAttrOrDefault(e, "name", defaultVarName);
        desc = Util.getAttrOrDefault(e, "option-help", getDefaultHelpText(varName));
        if (desc.isEmpty()) {
            // no help text, add an option only when requested
            arg = e.getAttribute("option");
        } else {
            // there is a help text, generate an option if none is specified
            arg = Util.getAttrOrDefault(e, "option", varName.toLowerCase().replace('_', '-'));
        }

        if (e.hasChildNodes()) {
            value = Util.getContent(e);
        } else if (arg.equals("prefix")) {
            // special treatment for prefix
            value = DEFAULT_PREFIX;
        } else {
            value = "";
        }
        exec = Boolean.parseBoolean(e.getAttribute("exec"));

        if (exec) {
            desc = desc.replace("%default", String.format("{{%s}}", value));
        } else {
            desc = desc.replace("%default", value);
        }
    }

    /**
     * A set of default help texts for standard config variables.
     * <p>
     * Returns an empty string for custom config variables.
     *
     * @param varName the variable name
     * @return the default help text
     */
    private static String getDefaultHelpText(String varName) {
        return switch (varName) {
            case "prefix" -> "path prefix for architecture-independent files [%default]";
            case "exec_prefix" -> "path prefix for architecture-dependent files [PREFIX]";
            case "bindir" -> "user executables [EPREFIX/bin]";
            case "sbindir" -> "system admin executables [EPREFIX/sbin]";
            case "libexecdir" -> "program executables [EPREFIX/libexec]";
            case "sysconfdir" -> "system configuration files [PREFIX/etc]";
            case "sharedstatedir" -> "modifiable architecture-independent data [PREFIX/com]";
            case "localstatedir" -> "modifiable single-machine data [PREFIX/var]";
            case "runstatedir" -> "run-time variable data [LOCALSTATEDIR/run]";
            case "libdir" -> "object code libraries [EPREFIX/lib]";
            case "includedir" -> "C header files [PREFIX/include]";
            case "datarootdir" -> "read-only architecture-independent data root [PREFIX/share]";
            case "datadir" -> "read-only architecture-independent data [DATAROOTDIR]";
            case "infodir" -> "info documentation [DATAROOTDIR/info]";
            case "mandir" -> "man documentation [DATAROOTDIR/man]";
            case "localedir" -> "locale-dependent data [DATAROOTDIR/locale]";
            default -> "";
        };
    }

    /**
     * The name of the variable where the value will be stored.
     * <p>
     * This is also the name of the variable that shall be added to the make config.
     *
     * @return the variable name
     */
    @Override
    public String getVarName() {
        return varName;
    }

    @Override
    public String getHelpTextArg() {
        return "--" + arg;
    }

    /**
     * The value with which this variable shall be initialized.
     * <p>
     * If {@link #isExec()} returns {@code true}, this value is used as the command to execute.
     *
     * @return the initial value for this variable
     */
    public String getValue() {
        return value;
    }

    /**
     * Indicates whether the value is a command to execute.
     *
     * @return {@code true} if the value is a command, {@code false} otherwise.
     */
    public boolean isExec() {
        return exec;
    }
}

mercurial