1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.File ; 23 import java.util.*; 24 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.Target; 28 import org.apache.tools.ant.Task; 29 30 39 public class NbMerge extends Task { 40 41 private File dest; 42 private Vector<String > modules = new Vector<String > (); private Vector<String > buildmodules = new Vector<String > (); private Vector<String > fixedmodules = new Vector<String > (); private Vector<String > buildfixedmodules = new Vector<String > (); private Vector<String > failedmodules = new Vector<String > (); private Vector<String > builtmodules = new Vector<String > (); private Vector<String > mergemodules = new Vector<String > (); private Vector<String > builttargets = new Vector<String > (); private String targetprefix = "all-"; 51 private List<File > topdirs = new ArrayList<File > (); 52 private List<Suppress> suppress = new LinkedList<Suppress> (); 53 private boolean failonerror = true; private boolean mergedependentmodules = false; private String dummyName; 56 private Target dummy; 57 private Hashtable targets; 58 private String builtmodulesproperty = ""; 61 62 public void setDest (File f) { 63 dest = f; 64 } 65 66 67 public void setFailOnError (boolean b) { 68 failonerror = b; 69 } 70 71 74 public void setBuiltModulesProperty (String s) { 75 builtmodulesproperty = s; 76 } 77 78 79 public void setMergeDependentModules (boolean b) { 80 mergedependentmodules = b; 81 } 82 83 84 public void setFixedModules (String s) { 85 StringTokenizer tok = new StringTokenizer (s, ", "); 86 fixedmodules = new Vector<String > (); 87 while (tok.hasMoreTokens ()) 88 fixedmodules.addElement (tok.nextToken ()); 89 } 90 91 92 public void setModules (String s) { 93 StringTokenizer tok = new StringTokenizer (s, ", "); 94 modules = new Vector<String > (); 95 while (tok.hasMoreTokens ()) 96 modules.addElement (tok.nextToken ()); 97 } 98 99 103 public void setTargetprefix (String s) { 104 targetprefix = s; 105 } 106 107 110 public void setTopdir (File t) { 111 topdirs.add (t); 112 } 113 114 115 public class Topdir { 116 117 public void setPath (File t) { 118 topdirs.add (t); 119 } 120 } 121 127 public Topdir createTopdir () { 128 return new Topdir (); 129 } 130 131 132 public class Suppress { 133 String locale; 135 String iftest; 136 String unlesstest; 137 138 public void setLocale (String l) { 139 locale = l; 140 } 141 142 public void setIf (String p) { 143 iftest = p; 144 } 145 146 public void setUnless (String p) { 147 unlesstest = p; 148 } 149 } 150 156 public Suppress createSuppress () { 157 Suppress s = new Suppress (); 158 suppress.add (s); 159 return s; 160 } 161 162 163 164 private void fixedModulesBuild() throws BuildException { 165 dummy = new Target (); 169 dummyName = "nbmerge-" + getOwningTarget().getName(); 170 targets = getProject().getTargets(); 171 while (targets.contains (dummyName)) 172 dummyName += "-x"; 173 dummy.setName (dummyName); 174 for (String fixedmodule : buildfixedmodules) { 175 dummy.addDependency (targetprefix + fixedmodule); 176 } 177 getProject().addTarget(dummy); 178 179 getProject().setProperty("fixedmodules-built", "1" ); 180 @SuppressWarnings ("unchecked") 181 Vector<Target> fullList = getProject().topoSort(dummyName, targets); 182 Vector doneList = getProject().topoSort(getOwningTarget().getName(), targets); 184 List<Target> todo = new ArrayList<Target>(fullList.subList(0, fullList.indexOf(dummy))); 185 todo.removeAll(doneList.subList(0, doneList.indexOf(getOwningTarget()))); 186 log("Going to execute targets " + todo); 187 for (Target nexttargit: todo) { 188 String targetname = nexttargit.getName(); 189 if ( builttargets.indexOf(targetname) < 0 ) { 190 System.out.println(""); System.out.println(targetname + ":"); 192 try { 193 nexttargit.execute(); 194 } catch (BuildException ex) { 195 log("Failed to build target: " + targetname, Project.MSG_ERR); 196 throw ex; 197 } 198 builttargets.addElement(targetname); 199 } 200 } 201 202 builtmodules.addAll(buildfixedmodules); log("fixedmodules=" + buildfixedmodules, Project.MSG_DEBUG); 204 log("builtmodules=" + builtmodules, Project.MSG_VERBOSE); 205 } 206 207 208 private void modulesBuild() throws BuildException { 209 if ( ! failonerror ) { 210 for (String module : buildmodules) { 212 dummy = new Target (); 213 dummyName = "nbmerge-" + getOwningTarget().getName() + "-" + module; 214 while (targets.contains (dummyName)) 215 dummyName += "-x"; 216 dummy.setName (dummyName); 217 dummy.addDependency (targetprefix + module); 218 getProject().addTarget(dummy); 219 @SuppressWarnings ("unchecked") 220 Vector<Target> fullList = getProject().topoSort(dummyName, targets); 221 @SuppressWarnings ("unchecked") 223 Vector<Target> doneList = getProject().topoSort(getOwningTarget().getName(), targets); 224 List<Target> todo = new ArrayList<Target>(fullList.subList(0, fullList.indexOf(dummy))); 225 todo.removeAll(doneList.subList(0, doneList.indexOf(getOwningTarget()))); 226 227 Iterator<Target> targit = todo.iterator(); 228 try { 229 while (targit.hasNext()) { 230 Target nexttargit = targit.next(); 231 String targetname = nexttargit.getName(); 232 if ( builttargets.indexOf(targetname) < 0 ) { 233 System.out.println(); System.out.println(targetname + ":"); 234 nexttargit.execute(); 235 builttargets.addElement(targetname); 236 } 237 238 } 239 builtmodules.addElement(module); 240 } catch (BuildException BE) { 241 log(BE.toString(), Project.MSG_WARN); 242 BE.printStackTrace(); 243 failedmodules.addElement(module); 244 } 245 } 246 log("builtmodules=" + builtmodules, Project.MSG_VERBOSE); 247 log("failedmodules=" + failedmodules, Project.MSG_VERBOSE); 248 } 249 } 250 251 public void execute () throws BuildException { 252 if (topdirs.isEmpty ()) { 253 throw new BuildException("You must set at least one topdir attribute", getLocation()); 254 } 255 256 buildfixedmodules.addAll(fixedmodules); 257 buildmodules.addAll(modules); 258 259 if (( modules.size() > 0 ) && ( fixedmodules.size() == 0 ) && (! failonerror)) { 260 log("Unable to build without fixedmodules set", Project.MSG_WARN); 261 log("Swapping modules list with fixedmodules list", Project.MSG_WARN); 262 buildfixedmodules.addAll(modules); 263 buildmodules.removeAllElements(); 264 } 265 266 if (( failonerror ) && ( modules.size() > 0 )) { 267 buildfixedmodules.addAll(modules); 270 buildmodules.removeAllElements(); 271 } 272 273 fixedModulesBuild(); 275 276 modulesBuild(); 278 279 dataMerge(); 281 282 if (builtmodules.size() > 0 ) { 284 log("builtmodules=" + builtmodules); 285 log("builttargets=" + builttargets); 286 if (failedmodules.size() > 0 ) { 287 log("SOME MODULES FAILED TO BUILD, BUT THEIR BuildException WAS CAUGHT.", Project.MSG_WARN); 288 log("failedmodules=" + failedmodules, Project.MSG_WARN); 289 } 290 291 if ( mergemodules.size() > 0 ) { 292 if ( builtmodulesproperty.length() > 0 ) { 293 Vector<String > setmodules = new Vector<String >(); 294 setmodules.addAll(mergemodules); 296 setmodules.removeAll(fixedmodules); 298 if (( ! modules.containsAll(setmodules)) || ( ! setmodules.containsAll(modules))) { 300 String bm = setmodules.toString(); 301 bm = bm.substring( 1, bm.length() - 1); 302 if (bm.length() > 0 ) { 303 log("Setting property \"" + builtmodulesproperty + "\" to new value " + bm); getProject().setUserProperty(builtmodulesproperty, bm); 305 } 306 } 307 } 308 } 309 310 } else { 311 throw new BuildException("No modules were built", getLocation()); 312 } 313 314 } 315 316 317 private void dataMerge() throws BuildException { 318 List<String > suppressedlocales = new LinkedList<String > (); 319 Iterator it = suppress.iterator (); 320 while (it.hasNext ()) { 321 Suppress s = (Suppress) it.next (); 322 if (s.iftest != null && getProject().getProperty(s.iftest) == null) { 323 continue; 324 } else if (s.unlesstest != null && getProject().getProperty(s.unlesstest) != null) { 325 continue; 326 } 327 log ("Suppressing locale: " + s.locale); 328 suppressedlocales.add (s.locale); 329 } 330 331 UpdateTracking tr = new UpdateTracking( dest.getAbsolutePath() ); 332 log ( dest.getAbsolutePath() ); 333 while (it.hasNext ()) { 334 String locale = (String ) it.next (); 335 tr.removeLocalized(locale); 336 } 337 } 338 } 339 | Popular Tags |