1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.InputStreamReader ; 29 import java.io.OutputStream ; 30 import java.io.OutputStreamWriter ; 31 import java.io.PrintWriter ; 32 import java.util.ArrayList ; 33 import java.util.Collections ; 34 import java.util.List ; 35 import java.util.StringTokenizer ; 36 import java.util.regex.Matcher ; 37 import java.util.regex.Pattern ; 38 import org.apache.tools.ant.BuildException; 39 import org.apache.tools.ant.Project; 40 import org.apache.tools.ant.Task; 41 42 47 public final class IncrementSpecificationVersions extends Task { 48 49 private File nbroot; 50 private List <String > modules; 51 private int stickyLevel = -1; 52 53 public IncrementSpecificationVersions() {} 54 55 public void setNbroot(File f) { 56 nbroot = f; 57 } 58 59 public void setModules(String m) { 60 modules = new ArrayList <String >(); 61 for (Object o : Collections.list(new StringTokenizer (m, ", "))) { 62 modules.add((String ) o); 63 } 64 } 65 66 public void setBranch(boolean b) { 67 setStickyLevel(b ? 2 : 1); 68 } 69 70 73 public void setStickyLevel(int stickyLevel) { 74 if (this.stickyLevel != -1) { 75 throw new BuildException("Only one stickyLevel or branch attribute can be used!"); 76 } 77 78 this.stickyLevel = stickyLevel; 79 } 80 81 public void execute() throws BuildException { 82 if (nbroot == null || modules == null) { 83 throw new BuildException("Missing params 'nbroot' or 'modules'", getLocation()); 84 } 85 MODULE: for (String module : modules) { 86 File dir = new File (nbroot, module.replace('/', File.separatorChar)); 87 if (!dir.isDirectory()) { 88 log("No such directory " + dir + "; skipping", Project.MSG_WARN); 89 continue; 90 } 91 try { 92 File pp = new File (dir, "nbproject" + File.separatorChar + "project.properties"); 93 if (pp.isFile()) { 94 String [] lines = gulp(pp, "ISO-8859-1"); 95 for (int i = 0; i < lines.length; i++) { 96 Matcher m1 = Pattern.compile("(spec\\.version\\.base=)(.+)").matcher(lines[i]); 97 if (m1.matches()) { 98 String old = m1.group(2); 99 String nue = increment(old, stickyLevel, false); 100 if (nue != null) { 101 lines[i] = m1.group(1) + nue; 102 spit(pp, "ISO-8859-1", lines); 103 log("Incrementing " + old + " -> " + nue + " in " + pp); 104 } else { 105 log(pp + ":" + (i + 1) + ": Unsupported old version number " + old + " (must be x.y.0 in trunk or x.y.z in branch); skipping", Project.MSG_WARN); 106 } 107 continue MODULE; 108 } 109 } 110 } else { 111 if (!new File (dir, "nbproject" + File.separatorChar + "project.xml").isFile()) { 112 log("No such file " + pp + "; unprojectized module?", Project.MSG_WARN); 113 } 114 } 115 File mf = new File (dir, "manifest.mf"); 116 if (mf.isFile()) { 117 String [] lines = gulp(mf, "UTF-8"); 118 for (int i = 0; i < lines.length; i++) { 119 Matcher m1 = Pattern.compile("(OpenIDE-Module-Specification-Version: )(.+)").matcher(lines[i]); 120 if (m1.matches()) { 121 String old = m1.group(2); 122 String nue = increment(old, stickyLevel, true); 123 if (nue != null) { 124 lines[i] = m1.group(1) + nue; 125 spit(mf, "UTF-8", lines); 126 log("Incrementing " + old + " -> " + nue + " in " + mf); 127 } else { 128 log(mf + ":" + (i + 1) + ": Unsupported old version number " + old + " (must be x.y in trunk or x.y.z in branch); skipping", Project.MSG_WARN); 129 } 130 continue MODULE; 131 } 132 } 133 } else { 134 log("No such file " + mf + "; not a real module?", Project.MSG_WARN); 135 } 136 log("Could not find any specification version in " + dir + "; skipping", Project.MSG_WARN); 137 } catch (IOException e) { 138 throw new BuildException("While processing " + dir + ": " + e, e, getLocation()); 139 } 140 } 141 } 142 143 146 static String increment(String old, int stickyLevel, boolean manifest) throws NumberFormatException { 147 String nue = null; 148 149 switch (stickyLevel) { 150 case 1: if (manifest) { 152 Matcher m2 = Pattern.compile("([0-9]+\\.)([0-9]+)").matcher(old); 153 if (m2.matches()) { 154 nue = m2.group(1) + (Integer.parseInt(m2.group(2)) + 1); 155 } 156 } else { 157 Matcher m2 = Pattern.compile("([0-9]+\\.)([0-9]+)(\\.0)").matcher(old); 158 if (m2.matches()) { 159 nue = m2.group(1) + (Integer.parseInt(m2.group(2)) + 1) + m2.group(3); 160 } 161 } 162 break; 163 case 2: if (manifest) { 165 Matcher m2 = Pattern.compile("([0-9]+\\.[0-9]+\\.)([0-9]+)").matcher(old); 166 if (m2.matches()) { 167 nue = m2.group(1) + (Integer.parseInt(m2.group(2)) + 1); 168 } else if (old.matches("[0-9]+\\.[0-9]+")) { 169 nue = old + ".1"; 170 } 171 } else { 172 Matcher m2 = Pattern.compile("([0-9]+\\.[0-9]+\\.)([0-9]+)").matcher(old); 173 if (m2.matches()) { 174 nue = m2.group(1) + (Integer.parseInt(m2.group(2)) + 1); 175 } 176 } 177 break; 178 default: 179 if (stickyLevel < 1) { 180 throw new BuildException("Invalid sticky level: " + stickyLevel); 181 } 182 int[] segments = new int[stickyLevel + 1]; 183 StringTokenizer tok = new StringTokenizer (old, "."); 184 for (int i = 0; i < segments.length && tok.hasMoreElements(); i++) { 185 segments[i] = Integer.parseInt(tok.nextToken()); 186 } 187 segments[stickyLevel]++; 188 nue = ""; 189 String pref = ""; 190 for (int i = 0; i < segments.length; i++) { 191 nue += pref; 192 nue += segments[i]; 193 pref = "."; 194 } 195 break; 196 } 197 198 return nue; 199 } 200 201 private static String [] gulp(File file, String enc) throws IOException { 202 InputStream is = new FileInputStream (file); 203 try { 204 BufferedReader r = new BufferedReader (new InputStreamReader (is, enc)); 205 List <String > l = new ArrayList <String >(); 206 String line; 207 while ((line = r.readLine()) != null) { 208 l.add(line); 209 } 210 return l.toArray(new String [l.size()]); 211 } finally { 212 is.close(); 213 } 214 } 215 216 private static void spit(File file, String enc, String [] lines) throws IOException { 217 OutputStream os = new FileOutputStream (file); 218 try { 219 PrintWriter w = new PrintWriter (new OutputStreamWriter (os, enc)); 220 for (String line : lines) { 221 w.println(line); 222 } 223 w.flush(); 224 } finally { 225 os.close(); 226 } 227 } 228 229 } 230 | Popular Tags |