# HG changeset patch # User Mike Becker # Date 1706446961 -3600 # Node ID 24f32dbd88cd6e95fe99152b5c149941cb73517d # Parent 206e91a8dd183ee963c6441b28059e7896494308 minimize application code diff -r 206e91a8dd18 -r 24f32dbd88cd src/main/java/de/unixwork/uwproj/ConfigVar.java --- a/src/main/java/de/unixwork/uwproj/ConfigVar.java Sun Jan 28 13:26:47 2024 +0100 +++ b/src/main/java/de/unixwork/uwproj/ConfigVar.java Sun Jan 28 14:02:41 2024 +0100 @@ -2,34 +2,24 @@ import org.w3c.dom.Element; -public class ConfigVar { - private String varName; - private String value; - private boolean exec = false; +public final class ConfigVar { + private final String varName; + private final String value; + private final boolean exec; - public static ConfigVar parse(Element e) { - return parse(e, false); + public ConfigVar(Element e) { + this(e, false); } - public static ConfigVar parse(Element e, boolean varNameIsElemName) { - ConfigVar var = new ConfigVar(); - + public ConfigVar(Element e, boolean varNameIsElemName) { if (varNameIsElemName) { // take the node name literally and don't apply shId() - var.varName = e.getNodeName().toUpperCase(); + varName = e.getNodeName().toUpperCase(); } else { - String name = e.getAttribute("name"); - if (name.isBlank()) { - System.err.println("-element: name attribute required"); - return null; - } - var.varName = name; + varName = e.getAttribute("name"); } - - var.value = Util.getContent(e); - var.exec = Boolean.parseBoolean(e.getAttribute("exec")); - - return var; + value = Util.getContent(e); + exec = Boolean.parseBoolean(e.getAttribute("exec")); } public String getVarName() { diff -r 206e91a8dd18 -r 24f32dbd88cd src/main/java/de/unixwork/uwproj/Define.java --- a/src/main/java/de/unixwork/uwproj/Define.java Sun Jan 28 13:26:47 2024 +0100 +++ b/src/main/java/de/unixwork/uwproj/Define.java Sun Jan 28 14:02:41 2024 +0100 @@ -1,6 +1,6 @@ package de.unixwork.uwproj; -public class Define { +public final class Define { private final String name; private final String value; @@ -8,7 +8,17 @@ this.name = name; this.value = value; } - + + public String toFlags() { + if (name.isBlank()) { + return ""; + } else if (value.isBlank()) { + return "-D" + name; + } else { + return "-D" + name + "=" + value; + } + } + public String getName() { return name; } @@ -16,14 +26,4 @@ public String getValue() { return value; } - - public String toFlags() { - if(name == null) { - return ""; - } else if(value == null) { - return "-D"+name; - } else { - return "-D"+name + "=" + value; - } - } } diff -r 206e91a8dd18 -r 24f32dbd88cd src/main/java/de/unixwork/uwproj/Dependency.java --- a/src/main/java/de/unixwork/uwproj/Dependency.java Sun Jan 28 13:26:47 2024 +0100 +++ b/src/main/java/de/unixwork/uwproj/Dependency.java Sun Jan 28 14:02:41 2024 +0100 @@ -1,112 +1,81 @@ package de.unixwork.uwproj; import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import java.util.*; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.function.Predicate; import java.util.stream.Collectors; -import static de.unixwork.uwproj.Util.isNullOrBlank; - -public class Dependency { - private String name; - private String platform; - private String toolchain; - private String not; +public final class Dependency { + private final String id; + private final String name; + private final String platform; + private final String toolchain; + private final String not; private final List flags = new LinkedList<>(); private final List pkgconfig = new LinkedList<>(); private final List tests = new LinkedList<>(); - private final StringBuilder make = new StringBuilder(); + private final List make = new LinkedList<>(); private final List lang = new LinkedList<>(); - private int num = 0; - - public static Dependency parse(Element element) throws Exception { - Dependency d = new Dependency(); - - String name = element.getAttribute("name"); - String platform = element.getAttribute("platform"); - String toolchain = element.getAttribute("toolchain"); - String not = element.getAttribute("not"); - if (!name.isEmpty()) { - d.setName(name); - } - if (!platform.isEmpty()) { - d.setPlatform(platform); - } - if (!toolchain.isEmpty()) { - d.setToolchain(toolchain); - } - if (!not.isEmpty()) { - d.setNotString(not); - } + public Dependency(Element element) { + name = element.getAttribute("name"); + id = Util.shId(name); + platform = element.getAttribute("platform"); + toolchain = element.getAttribute("toolchain"); + not = element.getAttribute("not"); - NodeList nodes = element.getChildNodes(); - for (int i = 0; i < nodes.getLength(); i++) { - Node node = nodes.item(i); - if (node.getNodeType() == Node.ELEMENT_NODE) { - Element elm = (Element) node; - String n = elm.getNodeName(); - if (n.equals("cflags") || n.equals("cxxflags") || n.equals("ldflags")) { - Optional.ofNullable(ConfigVar.parse(elm, true)).ifPresent(d::addFlags); - } else if (n.equals("pkgconfig")) { - PkgConfigPackage pcp = PkgConfigPackage.parse(elm); - d.addPkgconfig(pcp); - } else if (n.equals("test")) { - d.tests.add(Util.getContent(elm)); - } else if (n.equals("make")) { - d.addMake(Util.getContent(elm)); - } else if (n.equals("lang")) { - d.lang.add(Util.getContent(elm).toLowerCase()); - } + Util.getChildElements(element).forEach(elm -> { + switch (elm.getNodeName()) { + case "cflags", "cxxflags", "ldflags" -> flags.add(new ConfigVar(elm, true)); + case "pkgconfig" -> pkgconfig.add(new PkgConfigPackage(elm)); + case "test" -> tests.add(Util.getContent(elm)); + case "make" -> make.add(Util.getContent(elm)); + case "lang" -> lang.add(Util.getContent(elm).toLowerCase()); } - } + }); + } - return d; + /** + * Returns the ID of this dependency, if it has a name. + * @return the ID that can be used for shell scripts - empty if dependency has not a name + */ + public String getId() { + return id; + } + + public boolean hasName() { + return !name.isEmpty(); } public String getName() { return name; } - public void setName(String name) { - this.name = name; - } - public String getPlatform() { return platform; } public String getFullName() { String fname = name; - if (!isNullOrBlank(platform)) { + if (!platform.isBlank()) { fname += " platform=\"" + platform + '"'; } - if (!isNullOrBlank(toolchain)) { + if (!toolchain.isBlank()) { fname += " toolchain=\"" + toolchain + '"'; } return fname; } - public void setNotString(String not) { - this.not = not; - } - public List getNotList() { - return isNullOrBlank(not) ? - Collections.emptyList() : - Arrays.stream(not.split(",")).map(String::trim).collect(Collectors.toList()); - } - - public void setPlatform(String platform) { - this.platform = platform; - } - - public void setToolchain(String toolchain) { - this.toolchain = toolchain; + return Arrays.stream(not.split(",")) + .map(String::trim) + .filter(Predicate.not(String::isEmpty)) + .collect(Collectors.toList()); } public String getToolchain() { return toolchain; } @@ -115,37 +84,16 @@ return flags; } - public void addFlags(ConfigVar var) { - this.flags.add(var); - } - public List getPkgconfig() { return pkgconfig; } - public void addPkgconfig(PkgConfigPackage pkgconfig) { - this.pkgconfig.add(pkgconfig); - } - - public int getNum() { - return num; - } - - public void setNum(int num) { - this.num = num; - } - public List getTests() { return tests; } - public void addMake(String m) { - make.append(m.trim()); - make.append('\n'); - } - public String getMake() { - return make.toString(); + return String.join("\n", make); } public List getLang() { diff -r 206e91a8dd18 -r 24f32dbd88cd src/main/java/de/unixwork/uwproj/Feature.java --- a/src/main/java/de/unixwork/uwproj/Feature.java Sun Jan 28 13:26:47 2024 +0100 +++ b/src/main/java/de/unixwork/uwproj/Feature.java Sun Jan 28 14:02:41 2024 +0100 @@ -1,56 +1,30 @@ package de.unixwork.uwproj; import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; import java.util.List; -import static de.unixwork.uwproj.Util.isNotNullOrBlank; import static de.unixwork.uwproj.Util.shId; -public class Feature { - private String name; - private String arg; - private boolean auto; - private String desc; - - private TargetData targetData; - - public static Feature parse(Project project, Element e) throws Exception { - Feature feature = new Feature(); - String name = e.getAttribute("name"); - String arg = e.getAttribute("arg"); - - if (name.isBlank()) { - throw new Exception("feature element requires name attribute"); - } +public final class Feature { + private final String name; + private final String arg; + private final boolean auto; + private final String desc; + private final TargetData targetData; - feature.setName(name); - if (arg.isBlank()) { - feature.setArg(name); - } else { - feature.setArg(arg); - } - - feature.setAuto(Boolean.parseBoolean(e.getAttribute("default"))); - feature.setTargetData(TargetData.parse(e)); - project.addFeature(feature); + public Feature(Element e) { + name = e.getAttribute("name"); + arg = Util.getAttrOrDefault(e, "arg", name); + auto = Boolean.parseBoolean(e.getAttribute("default")); + targetData = new TargetData(e); - // TODO: when Option also receives this feature, we might move this to TargetData.parse() - NodeList nodes = e.getChildNodes(); - for (int i = 0; i < nodes.getLength(); i++) { - Node node = nodes.item(i); - if (node.getNodeType() == Node.ELEMENT_NODE) { - Element elm = (Element) node; - String n = elm.getNodeName(); - if (n.equals("desc")) { - feature.setDesc(Util.getContent(elm)); - } - } - } - - return feature; + // TODO: when Option also receives descriptions, we will move desc to TargetData + desc = Util.getChildElements(e) + .filter(c -> c.getNodeName().equals("desc")) + .map(Util::getContent) + .findAny() + .orElse(""); } public String getVarName() { @@ -70,49 +44,29 @@ } public boolean hasMake() { - return isNotNullOrBlank(targetData.getMake()); + return targetData.hasMake(); } public String getName() { return name; } - public void setName(String name) { - this.name = name; - } - public String getArg() { return arg; } - public void setArg(String arg) { - this.arg = arg; - } - public String getDesc() { return desc; } - public void setDesc(String desc) { - this.desc = desc; - } - public boolean isAuto() { return auto; } - public void setAuto(boolean auto) { - this.auto = auto; - } - public TargetData getTargetData() { return targetData; } - public void setTargetData(TargetData targetData) { - this.targetData = targetData; - } - /** * Generates help text for the feature option. *

@@ -139,7 +93,7 @@ builder.append(opt); // Stop, if there is no description - if (desc == null || desc.isBlank()) return builder.toString(); + if (desc.isBlank()) return builder.toString(); // Prepare the description by replacing some unwanted spaces final var hdesc = desc.trim() diff -r 206e91a8dd18 -r 24f32dbd88cd src/main/java/de/unixwork/uwproj/NamedDependency.java --- a/src/main/java/de/unixwork/uwproj/NamedDependency.java Sun Jan 28 13:26:47 2024 +0100 +++ b/src/main/java/de/unixwork/uwproj/NamedDependency.java Sun Jan 28 14:02:41 2024 +0100 @@ -4,27 +4,24 @@ import static de.unixwork.uwproj.Util.shId; -public class NamedDependency { - private String name; - private List subdependencies; +public final class NamedDependency { + private final String name; + private final List subdependencies; - public String getName() { - return name; - } - - public void setName(String name) { + public NamedDependency(String name, List subdependencies) { this.name = name; + this.subdependencies = subdependencies; } public String getId() { return shId(name); } + public String getName() { + return name; + } + public List getSubdependencies() { return subdependencies; } - - public void setSubdependencies(List subdependency) { - this.subdependencies = subdependency; - } } diff -r 206e91a8dd18 -r 24f32dbd88cd src/main/java/de/unixwork/uwproj/Option.java --- a/src/main/java/de/unixwork/uwproj/Option.java Sun Jan 28 13:26:47 2024 +0100 +++ b/src/main/java/de/unixwork/uwproj/Option.java Sun Jan 28 14:02:41 2024 +0100 @@ -1,8 +1,6 @@ package de.unixwork.uwproj; import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; import java.util.LinkedList; import java.util.List; @@ -10,54 +8,30 @@ import static de.unixwork.uwproj.Util.shId; -public class Option { - private String arg; +public final class Option { + private final String arg; private final LinkedList values = new LinkedList<>(); private final LinkedList defaults = new LinkedList<>(); - public static Option parse(Element element) throws Exception { - var opt = new Option(); - String arg = element.getAttribute("arg"); - if (arg.isBlank()) { - throw new Exception("Option has no argument string"); - } - opt.setArgument(arg); - - NodeList nodes = element.getChildNodes(); - for (int i = 0; i < nodes.getLength(); i++) { - Node node = nodes.item(i); - if (node.getNodeType() == Node.ELEMENT_NODE) { - Element elm = (Element) node; - String n = elm.getNodeName(); - if (n.equals("value")) { - opt.values.add(new OptionValue(opt, - elm.getAttribute("str"), - TargetData.parse(elm) - )); - } else if (n.equals("default")) { - var def = new OptionDefault(opt); - def.setValueName(elm.getAttribute("value")); - String defPlatform = elm.getAttribute(("platform")); - if (!defPlatform.isBlank()) { - def.setPlatform(defPlatform); - } - opt.defaults.add(def); - } + public Option(Element element) { + arg = element.getAttribute("arg"); + Util.getChildElements(element).forEach(elm -> { + switch (elm.getNodeName()) { + case "value" -> values.add(new OptionValue(this, + elm.getAttribute("str"), + new TargetData(elm))); + case "default" -> defaults.add(new OptionDefault(this, + elm.getAttribute("value"), + elm.getAttribute("platform"))); } - } - - return opt; + }); } public String getArgument() { return arg; } - public void setArgument(String arg) { - this.arg = arg; - } - public String getVarName() { return shId("OPT_" + arg.toUpperCase()); } diff -r 206e91a8dd18 -r 24f32dbd88cd src/main/java/de/unixwork/uwproj/OptionDefault.java --- a/src/main/java/de/unixwork/uwproj/OptionDefault.java Sun Jan 28 13:26:47 2024 +0100 +++ b/src/main/java/de/unixwork/uwproj/OptionDefault.java Sun Jan 28 14:02:41 2024 +0100 @@ -1,30 +1,24 @@ package de.unixwork.uwproj; -public class OptionDefault { +public final class OptionDefault { private final Option option; - private String valueName; - private String platform; + private final String valueName; + private final String platform; - public OptionDefault(Option opt) { - option = opt; + public OptionDefault(Option option, String valueName, String platform) { + this.option = option; + this.valueName = valueName; + this.platform = platform; } public String getValueName() { return valueName; } - public void setValueName(String valueName) { - this.valueName = valueName; - } - public String getPlatform() { return platform; } - public void setPlatform(String platform) { - this.platform = platform; - } - public String getFunc() { return option.getValueFunc(valueName); } diff -r 206e91a8dd18 -r 24f32dbd88cd src/main/java/de/unixwork/uwproj/OptionValue.java --- a/src/main/java/de/unixwork/uwproj/OptionValue.java Sun Jan 28 13:26:47 2024 +0100 +++ b/src/main/java/de/unixwork/uwproj/OptionValue.java Sun Jan 28 14:02:41 2024 +0100 @@ -2,9 +2,7 @@ import java.util.List; -import static de.unixwork.uwproj.Util.isNotNullOrBlank; - -public class OptionValue { +public final class OptionValue { private final Option option; private final String value; @@ -21,7 +19,6 @@ return value; } - public List getDependencies() { return targetData.getDependencies(); } @@ -39,6 +36,6 @@ } public boolean hasMake() { - return isNotNullOrBlank(targetData.getMake()); + return targetData.hasMake(); } } diff -r 206e91a8dd18 -r 24f32dbd88cd src/main/java/de/unixwork/uwproj/PkgConfigPackage.java --- a/src/main/java/de/unixwork/uwproj/PkgConfigPackage.java Sun Jan 28 13:26:47 2024 +0100 +++ b/src/main/java/de/unixwork/uwproj/PkgConfigPackage.java Sun Jan 28 14:02:41 2024 +0100 @@ -3,64 +3,37 @@ import org.w3c.dom.Element; public class PkgConfigPackage { - private String id; - private String name; - private String atleast; - private String exact; - private String max; + private final String id; + private final String name; + private final String atleast; + private final String exact; + private final String max; - 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.setId(Util.shId(name)); - } - p.setAtleast(e.getAttribute("atleast")); - p.setExact(e.getAttribute("exact")); - p.setMax(e.getAttribute("max")); - return p; + public PkgConfigPackage(Element e) { + name = Util.getContent(e); + id = Util.shId(name); + atleast = e.getAttribute("atleast"); + exact = e.getAttribute("exact"); + max = e.getAttribute("max"); } public String getId() { return id; } - public void setId(String id) { - this.id = id; - } - public String getName() { return name; } - public void setName(String name) { - this.name = name; - } - public String getAtleast() { return atleast; } - public void setAtleast(String atleast) { - this.atleast = atleast; - } - public String getMax() { return max; } - public void setMax(String max) { - this.max = max; - } - public String getExact() { return exact; } - - public void setExact(String exact) { - this.exact = exact; - } } diff -r 206e91a8dd18 -r 24f32dbd88cd src/main/java/de/unixwork/uwproj/Project.java --- a/src/main/java/de/unixwork/uwproj/Project.java Sun Jan 28 13:26:47 2024 +0100 +++ b/src/main/java/de/unixwork/uwproj/Project.java Sun Jan 28 14:02:41 2024 +0100 @@ -1,14 +1,8 @@ package de.unixwork.uwproj; import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import javax.xml.parsers.DocumentBuilderFactory; -import java.io.File; import java.util.*; -import java.util.stream.Collectors; -import java.util.stream.Stream; public class Project { private final HashMap> namedDependencies = new HashMap<>(); @@ -17,70 +11,50 @@ private final List

diff -r 206e91a8dd18 -r 24f32dbd88cd test/configure2 --- a/test/configure2 Sun Jan 28 13:26:47 2024 +0100 +++ b/test/configure2 Sun Jan 28 14:02:41 2024 +0100 @@ -501,7 +501,6 @@ cat >> $TEMP_DIR/make.mk << __EOF__ # Dependency: libxml2 xml = libxml2 - __EOF__ print_check_msg "$dep_checked_libxml2" "yes\n" dep_checked_libxml2=1 @@ -534,7 +533,6 @@ cat >> "$TEMP_DIR/make.mk" << __EOF__ MVAR = 123 MVAR += 123 - __EOF__ break done @@ -569,7 +567,6 @@ TEMP_CXXFLAGS="$TEMP_CXXFLAGS -Da=b" cat >> "$TEMP_DIR/make.mk" << __EOF__ UIOBJ += graphics_cairo.o - __EOF__ return 0 }