1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import java.io.PrintWriter ; 28 import java.io.Writer ; 29 import java.util.Collections ; 30 import java.util.HashSet ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import java.util.StringTokenizer ; 34 import java.util.TreeMap ; 35 import java.util.TreeSet ; 36 import org.apache.tools.ant.BuildException; 37 import org.apache.tools.ant.Project; 38 import org.apache.tools.ant.Task; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.w3c.dom.NodeList ; 42 import org.xml.sax.InputSource ; 43 import org.xml.sax.SAXException ; 44 45 50 public final class CheckModuleConfigs extends Task { 51 52 private File nbroot; 53 54 public CheckModuleConfigs() {} 55 56 public void setNbroot(File f) { 57 nbroot = f; 58 } 59 60 public void execute() throws BuildException { 61 if (nbroot == null) { 62 throw new BuildException("Must define 'nbroot' param", getLocation()); 63 } 64 File buildPropertiesFile = new File (nbroot, "nbbuild" + File.separatorChar + "build.properties"); 65 File clusterPropertiesFile = new File (nbroot, "nbbuild" + File.separatorChar + "cluster.properties"); 66 File goldenFile = new File (nbroot, "ide" + File.separatorChar + "golden" + File.separatorChar + "moduleconfigs.txt"); 67 File masterProjectXml = new File (nbroot, "nbbuild" + File.separatorChar + "nbproject" + File.separatorChar + "project.xml"); 68 @SuppressWarnings ("unchecked") 69 Map <String ,String > properties = getProject().getProperties(); 70 Map <String ,Set <String >> configs = loadModuleConfigs(properties, buildPropertiesFile); 71 Map <String ,Set <String >> clusters = loadModuleClusters(properties, clusterPropertiesFile); 72 Set <String > allClusterModules = new TreeSet <String >(); 73 for (Set <String > s : clusters.values()) { 74 allClusterModules.addAll(s); 75 } 76 try { 77 writeModuleConfigs(goldenFile, configs, buildPropertiesFile); 78 } catch (IOException e) { 79 throw new BuildException("Could not write to " + goldenFile, e, getLocation()); 80 } 81 try { 82 writeMasterProjectXml(masterProjectXml, allClusterModules); 83 } catch (SAXException e) { 84 throw new BuildException("Could not write to " + masterProjectXml, e, getLocation()); 85 } catch (IOException e) { 86 throw new BuildException("Could not write to " + masterProjectXml, e, getLocation()); 87 } 88 Set <String > s; 89 97 105 113 s = new TreeSet <String >(configs.get("javadoc")); 115 s.removeAll(configs.get("stable")); 116 s.removeAll(configs.get("daily-alpha-nbms")); 117 if (!s.isEmpty()) { 118 log(buildPropertiesFile + ": warning: javadoc config contains entries not in stable and daily-alpha-nbms configs: " + s); 119 } 120 136 Set <String > stable = configs.get("stable"); 138 s = new TreeSet <String >(stable); 139 s.removeAll(allClusterModules); 140 if (!s.isEmpty()) { 141 log(buildPropertiesFile + ": warning: stable config not equal to listed cluster modules: " + s); 142 } 143 s = new TreeSet <String >(allClusterModules); 144 s.removeAll(stable); 145 if (!s.isEmpty()) { 146 log(buildPropertiesFile + ": warning: stable config not equal to listed cluster modules: " + s); 147 } 148 Set <String > platform = configs.get("platform"); 150 Set <String > platformCluster = clusters.get("nb.cluster.platform"); 151 s = new TreeSet <String >(platform); 152 s.removeAll(platformCluster); 153 if (!s.isEmpty()) { 154 log(buildPropertiesFile + ": warning: platform config not equal to platform cluster modules: " + s); 155 } 156 s = new TreeSet <String >(platformCluster); 157 s.removeAll(platform); 158 if (!s.isEmpty()) { 159 log(buildPropertiesFile + ": warning: platform config not equal to platform cluster modules: " + s); 160 } 161 } 162 163 @SuppressWarnings ("unchecked") 164 private Set <String > split(String list) { 165 return new HashSet (Collections.list(new StringTokenizer (list, ", "))); 166 } 167 168 private Map <String ,Set <String >> loadModuleConfigs(Map <String ,String > buildProperties, File buildPropertiesFile) { 169 Map <String ,Set <String >> configs = new TreeMap <String ,Set <String >>(); 170 for (String k : buildProperties.keySet()) { 171 String prefix = "config.modules."; 172 if (!k.startsWith(prefix)) { 173 continue; 174 } 175 String config = k.substring(prefix.length()); 176 Set <String > modules = new TreeSet <String >(split(buildProperties.get(k))); 177 String fixedK = "config.fixedmodules." + config; 178 String fixed = buildProperties.get(fixedK); 179 if (fixed != null) { 180 modules.addAll(split(fixed)); 181 } else { 182 log(buildPropertiesFile + ": warning: have " + k + " but no " + fixedK, Project.MSG_WARN); 183 } 184 configs.put(config, modules); 185 } 186 return configs; 187 } 188 189 private void writeModuleConfigs(File goldenFile, Map <String ,Set <String >> configs, File buildPropertiesFile) throws IOException { 190 log("Writing moduleconfigs " + configs.keySet() + " from " + buildPropertiesFile + " to " + goldenFile); 191 Writer w = new FileWriter (goldenFile); try { 193 PrintWriter pw = new PrintWriter (w); 194 pw.println("# To update, run: ant -f nbbuild/build.xml check-module-configs"); 195 for (Map.Entry <String ,Set <String >> entry : configs.entrySet()) { 196 String config = entry.getKey(); 197 for (String module : entry.getValue()) { 198 pw.println(config + ':' + module); 199 } 200 } 201 pw.flush(); 202 } finally { 203 w.close(); 204 } 205 } 206 207 private Map <String ,Set <String >> loadModuleClusters(Map <String ,String > clusterProperties, File clusterPropertiesFile) { 208 String l = clusterProperties.get("nb.clusters.list"); 209 if (l == null) { 210 log(clusterPropertiesFile + ": warning: no definition for nb.clusters.list", Project.MSG_WARN); 211 return Collections.emptyMap(); 212 } 213 Map <String ,Set <String >> clusters = new TreeMap <String ,Set <String >>(); 214 for (String cluster : split(l)) { 215 l = clusterProperties.get(cluster); 216 if (l == null) { 217 log(clusterPropertiesFile + ": warning: no definition for " + cluster, Project.MSG_WARN); 218 continue; 219 } 220 clusters.put(cluster, new TreeSet <String >(split(l))); 221 } 222 return clusters; 223 } 224 225 private void writeMasterProjectXml(File masterProjectXml, Set <String > allClusterModules) throws IOException , SAXException { 226 log("Writing module list to " + masterProjectXml); 227 Document doc = XMLUtil.parse(new InputSource (masterProjectXml.toURI().toString()), false, true, null, null); 228 NodeList nl = doc.getElementsByTagName("subprojects"); 229 if (nl.getLength() != 1) { 230 throw new IOException ("No or multiple <subprojects>"); 231 } 232 Element sp = (Element ) nl.item(0); 233 nl = sp.getChildNodes(); 234 while (nl.getLength() > 0) { 235 sp.removeChild(nl.item(0)); 236 } 237 sp.appendChild(doc.createComment(" To update, run: ant -f nbbuild/build.xml check-module-configs ")); 238 for (String module : allClusterModules) { 239 if (new File (nbroot, (module + "/nbproject/project.xml").replace('/', File.separatorChar)).isFile()) { 240 Element e = doc.createElementNS("http://www.netbeans.org/ns/freeform-project/1", "project"); 241 e.appendChild(doc.createTextNode("../" + module)); 242 sp.appendChild(e); 243 } else { 244 sp.appendChild(doc.createComment(" Unprojectized: " + module + " ")); 245 } 246 } 247 OutputStream os = new FileOutputStream (masterProjectXml); 248 try { 249 XMLUtil.write(doc, os); 250 } finally { 251 os.close(); 252 } 253 } 254 255 } 256 | Popular Tags |