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

changeset 113
24f32dbd88cd
parent 92
5a32534f2604
equal deleted inserted replaced
112:206e91a8dd18 113:24f32dbd88cd
1 package de.unixwork.uwproj; 1 package de.unixwork.uwproj;
2 2
3 import org.w3c.dom.Element; 3 import org.w3c.dom.Element;
4 import org.w3c.dom.Node;
5 import org.w3c.dom.NodeList;
6 4
7 import java.util.Arrays; 5 import java.util.Arrays;
6 import java.util.Collection;
8 import java.util.LinkedList; 7 import java.util.LinkedList;
9 import java.util.List; 8 import java.util.List;
10 9
11 import static de.unixwork.uwproj.Util.*; 10 import static de.unixwork.uwproj.Util.shId;
12 import static java.util.function.Predicate.not; 11 import static java.util.function.Predicate.not;
13 12
14 public class Target { 13 public final class Target {
15 private String name = ""; 14 private final String name;
16 private String prefix; 15 private final String prefix;
17 16
18 private final List<String> dependencies = new LinkedList<>(); 17 private final List<String> dependencies = new LinkedList<>();
19 private final List<Define> defines = new LinkedList<>(); 18 private final List<Define> defines = new LinkedList<>();
20 private final List<Feature> features = new LinkedList<>(); 19 private final List<Feature> features = new LinkedList<>();
21 private final List<Option> options = new LinkedList<>(); 20 private final List<Option> options = new LinkedList<>();
22 21
23 private boolean allDependencies = false; 22 private boolean allDependencies = false;
24 23
25 public static Target parse(Project project, Element element) throws Exception { 24 public static Target defaultTarget() {
26 var target = new Target(); 25 return new Target();
26 }
27 27
28 String name = element.getAttribute("name"); 28 private Target() {
29 if (!name.isBlank()) { 29 name = "default";
30 target.setName(name); 30 prefix = "";
31 allDependencies = true;
32 }
33
34 public Target(Element element) {
35 this.name = element.getAttribute("name").trim();
36 if (this.name.isEmpty()) {
37 prefix = "";
38 } else {
39 prefix = shId(name.toUpperCase());
31 } 40 }
32 41 Util.getChildElements(element).forEach(elm -> {
33 NodeList nodes = element.getChildNodes(); 42 switch (elm.getNodeName()) {
34 for (int i = 0; i < nodes.getLength(); i++) { 43 case "feature" -> features.add(new Feature(elm));
35 Node node = nodes.item(i); 44 case "define" -> defines.add(new Define(
36 if (node.getNodeType() == Node.ELEMENT_NODE) { 45 elm.getAttribute("name"),
37 Element elm = (Element) node; 46 elm.getAttribute("value")
38 String n = elm.getNodeName(); 47 ));
39 if (n.equals("feature")) { 48 case "dependencies" -> Arrays.stream(Util.getContent(elm).split(","))
40 target.addFeature(Feature.parse(project, elm)); 49 .map(String::trim)
41 } else if (n.equals("define")) { 50 .filter(not(String::isBlank))
42 target.addDefine( 51 .map(Util::shId)
43 elm.getAttribute("name"), 52 .forEach(dependencies::add);
44 elm.getAttribute("value") 53 case "alldependencies" -> this.allDependencies = true;
45 ); 54 case "option" -> options.add(new Option(elm));
46 } else if (n.equals("dependencies")) {
47 Arrays.stream(Util.getContent(elm).split(","))
48 .map(String::trim)
49 .filter(not(String::isBlank))
50 .forEach(target::addDependency);
51 } else if (n.equals("alldependencies")) {
52 target.setAllDependencies(true);
53 } else if (n.equals("option")) {
54 var opt = Option.parse(elm);
55 target.addOption(opt);
56 project.addOption(opt);
57 }
58 } 55 }
59 } 56 });
60
61 if (isNullOrBlank(target.prefix) && isNotNullOrBlank(target.name)) {
62 target.setPrefix(name.toUpperCase());
63 }
64
65 return target;
66 } 57 }
67 58
68 public String getName() { 59 public String getName() {
69 return name; 60 return name;
70 } 61 }
71 62
72 public void setName(String name) {
73 this.name = name;
74 }
75
76 public String getPrefix() { 63 public String getPrefix() {
77 return prefix; 64 return prefix;
78 } 65 }
79 66
80 public void setPrefix(String prefix) {
81 this.prefix = prefix;
82 }
83
84 private String withPrefix(String id) { 67 private String withPrefix(String id) {
85 return prefix == null ? id : String.format("%s_%s", prefix, id); 68 return prefix.isEmpty() ? id : String.format("%s_%s", prefix, id);
86 } 69 }
87 70
88 public String getCFlags() { 71 public String getCFlags() {
89 return withPrefix("CFLAGS"); 72 return withPrefix("CFLAGS");
90 } 73 }
95 78
96 public String getCxxFlags() { 79 public String getCxxFlags() {
97 return withPrefix("CXXFLAGS"); 80 return withPrefix("CXXFLAGS");
98 } 81 }
99 82
100 public void clearDependencies() {
101 dependencies.clear();
102 }
103
104 public List<String> getDependencies() { 83 public List<String> getDependencies() {
105 return dependencies; 84 return dependencies;
106 } 85 }
107 86
108 public void addDependency(String dependency) { 87 public void replaceAllDependencies(Collection<String> deps) {
109 // we have to add the sanitized identifier to the list of dependencies 88 dependencies.clear();
110 dependencies.add(shId(dependency)); 89 dependencies.addAll(deps);
111 } 90 }
112 91
113 public boolean isAllDependencies() { 92 public boolean wantsAllDependencies() {
114 return allDependencies; 93 return allDependencies;
115 }
116
117 public void setAllDependencies(boolean allDependencies) {
118 this.allDependencies = allDependencies;
119 }
120
121 public void addDefine(String name, String value) {
122 if (name.isBlank()) {
123 throw new IllegalArgumentException("define element requires name attribute");
124 }
125 defines.add(new Define(name, value));
126 } 94 }
127 95
128 public List<Define> getDefines() { 96 public List<Define> getDefines() {
129 return defines; 97 return defines;
130 } 98 }
131 99
132 public void addFeature(Feature f) {
133 features.add(f);
134 }
135
136 public List<Feature> getFeatures() { 100 public List<Feature> getFeatures() {
137 return features; 101 return features;
138 }
139
140 public void addOption(Option o) {
141 options.add(o);
142 } 102 }
143 103
144 public List<Option> getOptions() { 104 public List<Option> getOptions() {
145 return options; 105 return options;
146 } 106 }

mercurial