Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.netbeans.nbbuild; |
15 |
|
16 |
import java.io.*; |
17 |
import java.nio.*; |
18 |
import java.nio.channels.*; |
19 |
import java.util.regex.*; |
20 |
|
21 |
/** Assistent in changing of build scripts. |
22 |
* |
23 |
* @author Jaroslav Tulach |
24 |
*/ |
25 |
public class FixClassPath { |
26 |
private static final String OLD = "netbeans/lib/openide.jar"; |
27 |
private static final String[] NEW = { |
28 |
"actions/netbeans/lib/openide-actions.jar", |
29 |
"fs/netbeans/lib/openide-fs.jar", |
30 |
"nodes/netbeans/lib/openide-nodes.jar", |
31 |
"util/netbeans/lib/openide-util.jar", |
32 |
"dialogs/netbeans/lib/openide-dialogs.jar", |
33 |
"options/netbeans/lib/openide-options.jar", |
34 |
"windows/netbeans/lib/openide-windows.jar", |
35 |
"explorer/netbeans/lib/openide-explorer.jar", |
36 |
"modules/netbeans/lib/openide-modules.jar", |
37 |
"awt/netbeans/lib/openide-awt.jar", |
38 |
"text/netbeans/lib/openide-text.jar" |
39 |
}; |
40 |
private static final String[] MANIFEST = { |
41 |
"org.openide.actions > 4.20", |
42 |
"org.openide.filesystems > 4.20", |
43 |
"org.openide.nodes > 4.20", |
44 |
"org.openide.util > 4.20", |
45 |
"org.openide.dialogs > 4.20", |
46 |
"org.openide.options > 4.20", |
47 |
"org.openide.windows > 4.20", |
48 |
"org.openide.explorer > 4.20", |
49 |
"org.openide.modules > 4.20", |
50 |
"org.openide.awt > 4.20", |
51 |
"org.openide.text > 4.20" |
52 |
}; |
53 |
private static final String[] BUILD = { |
54 |
"openide/actions", |
55 |
"openide/fs", |
56 |
"openide/nodes", |
57 |
"openide/util", |
58 |
"openide/dialogs", |
59 |
"openide/options", |
60 |
"openide/windows", |
61 |
"openide/explorer", |
62 |
"openide/modules", |
63 |
"openide/awt", |
64 |
"openide/text" |
65 |
}; |
66 |
|
67 |
|
68 |
private static final Pattern PATTERN = Pattern.compile (".*pathelement.*" + OLD + ".*"); |
69 |
private static final Pattern IDE_DEP = Pattern.compile ("OpenIDE-Module-IDE-Dependencies.*"); |
70 |
private static final Pattern MODULE_DEP = Pattern.compile ("OpenIDE-Module-Module-Dependencies.*"); |
71 |
|
72 |
/** Nothing */ |
73 |
private FixClassPath() { |
74 |
} |
75 |
|
76 |
/** |
77 |
* @param args the command line arguments |
78 |
*/ |
79 |
public static void main(String[] args) throws Exception { |
80 |
File main = null; |
81 |
|
82 |
for (int i = 0; i < args.length; i++) { |
83 |
if ("--main".equals (args[i])) { |
84 |
main = new File (args[i + 1]); |
85 |
if (!main.exists ()) { |
86 |
throw new IOException ("Main file does not exists " + main); |
87 |
} |
88 |
i++; |
89 |
continue; |
90 |
} |
91 |
|
92 |
File f = new File (args[i]); |
93 |
if (f.exists ()) { |
94 |
String[] usedJars = fix (f); |
95 |
File manifest = new File (f.getParentFile (), "manifest.mf"); |
96 |
if (manifest.exists ()) { |
97 |
manifest (manifest, usedJars); |
98 |
} else { |
99 |
log ("Manifest not found: " + manifest); |
100 |
} |
101 |
|
102 |
if (main != null) { |
103 |
fixMain (main, f, usedJars); |
104 |
} |
105 |
|
106 |
} else { |
107 |
System.err.println("File " + f + " does not exists"); |
108 |
} |
109 |
} |
110 |
} |
111 |
|
112 |
/** Finds a file, scans it replaces opende.jar |
113 |
* @return array of strings, clone of NEW with only those items that |
114 |
* were used |
115 |
*/ |
116 |
private static String[] fix (File buildFile) throws IOException { |
117 |
long longLen = buildFile.length(); |
118 |
if (longLen > Integer.MAX_VALUE) throw new IOException ("Too big file: " + buildFile); |
119 |
|
120 |
|
121 |
|
122 |
byte[] arr = new byte[(int)longLen]; |
123 |
|
124 |
FileInputStream is = new FileInputStream (buildFile); |
125 |
int read = is.read (arr); |
126 |
if (longLen != read) throw new IOException ("Not fully read: " + longLen + " was: " + read); |
127 |
is.close (); |
128 |
|
129 |
String buffer = new String (arr); |
130 |
|
131 |
log ("Processing " + buildFile); |
132 |
rewrite (buildFile, buffer, NEW); |
133 |
|
134 |
if (!build (buildFile)) { |
135 |
throw new IOException ("It has to build correctly after replace: " + buildFile); |
136 |
} |
137 |
|
138 |
log ("Compiled successfully"); |
139 |
|
140 |
String[] removeUnnecessary = (String[])NEW.clone (); |
141 |
for (int i = 0; i < removeUnnecessary.length; i++) { |
142 |
String remember = removeUnnecessary[i]; |
143 |
removeUnnecessary[i] = null; |
144 |
System.out.println(" removed " + remember); |
145 |
rewrite (buildFile, buffer, removeUnnecessary); |
146 |
if (!build (buildFile)) { |
147 |
// return it back |
148 |
removeUnnecessary[i] = remember; |
149 |
System.out.println(" bad, returned back"); |
150 |
} else { |
151 |
System.out.println(" compiled ok, kept removed"); |
152 |
} |
153 |
} |
154 |
|
155 |
rewrite (buildFile, buffer, removeUnnecessary); |
156 |
|
157 |
log ("The build file: " + buildFile + " is now using following JARs:"); |
158 |
for (int i = 0; i < removeUnnecessary.length; i++) { |
159 |
if (removeUnnecessary[i] != null) { |
160 |
log (" " + removeUnnecessary[i]); |
161 |
} |
162 |
} |
163 |
|
164 |
return removeUnnecessary; |
165 |
} |
166 |
|
167 |
/** Rewrites the file with replacements of packages. |
168 |
*/ |
169 |
private static void rewrite (File file, String buffer, String[] replacements) |
170 |
throws IOException { |
171 |
Matcher m = PATTERN.matcher (buffer); |
172 |
|
173 |
if (!m.find()) { |
174 |
throw new IOException ("Pattern not found: " + file); |
175 |
} |
176 |
|
177 |
int from = m.start(); |
178 |
int to = m.end (); |
179 |
|
180 |
String original = buffer.subSequence(from, to).toString(); |
181 |
|
182 |
StringBuffer replace = new StringBuffer (); |
183 |
replace.append (buffer.subSequence (0, from)); |
184 |
String line = ""; |
185 |
for (int i = 0; i < replacements.length; i++) { |
186 |
if (replacements[i] != null) { |
187 |
replace.append (line); |
188 |
replace.append (original.replaceAll(OLD, replacements[i])); |
189 |
line = "\n"; |
190 |
} |
191 |
} |
192 |
replace.append (buffer.subSequence(to, buffer.length())); |
193 |
|
194 |
|
195 |
FileOutputStream out = new FileOutputStream (file); |
196 |
out.write (replace.toString().getBytes()); |
197 |
out.close (); |
198 |
} |
199 |
|
200 |
/** Builds a build file. |
201 |
*/ |
202 |
private static boolean build (File f) throws IOException { |
203 |
Process p = Runtime.getRuntime().exec (new String[] { "ant", "-f", f.toString(), "clean", "compile" }); |
204 |
new CopyThread (p.getInputStream()); |
205 |
new CopyThread (p.getErrorStream()); |
206 |
try { |
207 |
return p.waitFor () == 0; |
208 |
} catch (InterruptedException ex) { |
209 |
return false; |
210 |
} |
211 |
} |
212 |
|
213 |
/** Logs. |
214 |
*/ |
215 |
private static void log (String s) { |
216 |
System.out.println(s); |
217 |
} |
218 |
|
219 |
private static final class CopyThread extends Thread { |
220 |
private InputStream is; |
221 |
|
222 |
public CopyThread (InputStream is) { |
223 |
this.is = is; |
224 |
start (); |
225 |
} |
226 |
|
227 |
public void run () { |
228 |
try { |
229 |
int ch; |
230 |
for (;;) { |
231 |
ch = is.read (); |
232 |
if (ch == -1) return; |
233 |
System.err.write (ch); |
234 |
} |
235 |
} catch (IOException ex) { |
236 |
ex.printStackTrace(); |
237 |
} |
238 |
} |
239 |
} |
240 |
|
241 |
|
242 |
/** Updates the manifest file, if found. |
243 |
*/ |
244 |
private static void manifest (File f, String[] usedJars) throws IOException { |
245 |
long longLen = f.length(); |
246 |
if (longLen > Integer.MAX_VALUE) throw new IOException ("Too big file: " + f); |
247 |
|
248 |
|
249 |
|
250 |
byte[] arr = new byte[(int)longLen]; |
251 |
|
252 |
FileInputStream is = new FileInputStream (f); |
253 |
int read = is.read (arr); |
254 |
if (longLen != read) throw new IOException ("Not fully read: " + longLen + " was: " + read); |
255 |
is.close (); |
256 |
|
257 |
String buffer = new String (arr); |
258 |
|
259 |
log ("Processing manifest " + f); |
260 |
|
261 |
Matcher ide = IDE_DEP.matcher(buffer); |
262 |
if (ide.find ()) { |
263 |
// remove that line |
264 |
buffer = buffer.substring (0, ide.start()) + buffer.substring (ide.end () + 1); |
265 |
} |
266 |
|
267 |
Matcher m = MODULE_DEP.matcher (buffer); |
268 |
StringBuffer sb = new StringBuffer (); |
269 |
String delim; |
270 |
int where; |
271 |
if (m.find ()) { |
272 |
where = m.end (); |
273 |
sb.append (buffer.subSequence(0, where)); |
274 |
delim = ", "; |
275 |
} else { |
276 |
where = buffer.indexOf('\n'); |
277 |
sb.append (buffer.subSequence(0, where)); |
278 |
sb.append ("\n"); |
279 |
sb.append ("OpenIDE-Module-Module-Dependencies: "); |
280 |
delim = ""; |
281 |
} |
282 |
|
283 |
|
284 |
for (int i = 0; i < usedJars.length; i++) { |
285 |
if (usedJars[i] != null) { |
286 |
sb.append (delim); |
287 |
sb.append (MANIFEST[i]); |
288 |
delim = ", "; |
289 |
} |
290 |
} |
291 |
|
292 |
sb.append (buffer.substring(where)); |
293 |
|
294 |
|
295 |
FileOutputStream out = new FileOutputStream (f); |
296 |
out.write (sb.toString().getBytes()); |
297 |
out.close (); |
298 |
} |
299 |
|
300 |
/** Updates the main file. |
301 |
*/ |
302 |
private static void fixMain (File main, File script, String[] usedJars) throws IOException { |
303 |
long longLen = main.length(); |
304 |
if (longLen > Integer.MAX_VALUE) throw new IOException ("Too big file: " + main); |
305 |
|
306 |
|
307 |
|
308 |
byte[] arr = new byte[(int)longLen]; |
309 |
|
310 |
FileInputStream is = new FileInputStream (main); |
311 |
int read = is.read (arr); |
312 |
if (longLen != read) throw new IOException ("Not fully read: " + longLen + " was: " + read); |
313 |
is.close (); |
314 |
|
315 |
String buffer = new String (arr); |
316 |
|
317 |
log ("Processing main script for" + script); |
318 |
|
319 |
String toSearch = script.getParent(); |
320 |
if (toSearch.endsWith ("/")) { |
321 |
toSearch = toSearch.substring (0, toSearch.length() - 1); |
322 |
} |
323 |
int start; |
324 |
int end; |
325 |
for (;;) { |
326 |
Matcher m = Pattern.compile ("<target *name=\"all-" + toSearch + "\" *depends=.*(all-openide)[, \"]").matcher (buffer); |
327 |
if (m.find ()) { |
328 |
start = m.start (1); |
329 |
end = m.end (1); |
330 |
break; |
331 |
} |
332 |
|
333 |
int last = toSearch.indexOf ('/'); |
334 |
if (last == -1) { |
335 |
throw new IOException ("Cannot find reference to " + script + " in " + main); |
336 |
} |
337 |
toSearch = toSearch.substring (last + 1); |
338 |
} |
339 |
|
340 |
StringBuffer sb = new StringBuffer (); |
341 |
String delim = ""; |
342 |
sb.append (buffer.subSequence(0, start)); |
343 |
|
344 |
for (int i = 0; i < usedJars.length; i++) { |
345 |
if (usedJars[i] != null) { |
346 |
sb.append (delim); |
347 |
sb.append ("all-"); |
348 |
sb.append (BUILD[i]); |
349 |
delim = ","; |
350 |
} |
351 |
} |
352 |
|
353 |
sb.append (buffer.substring (end)); |
354 |
|
355 |
FileOutputStream out = new FileOutputStream (main); |
356 |
out.write (sb.toString().getBytes()); |
357 |
out.close (); |
358 |
} |
359 |
} |