Index: antsrc/org/netbeans/nbbuild/NbMerge.java =================================================================== RCS file: /cvs/nbbuild/antsrc/org/netbeans/nbbuild/NbMerge.java,v retrieving revision 1.21 retrieving revision 1.21.98.1 diff -u -r1.21 -r1.21.98.1 --- antsrc/org/netbeans/nbbuild/NbMerge.java 11 Dec 2002 17:07:18 -0000 1.21 +++ antsrc/org/netbeans/nbbuild/NbMerge.java 9 Oct 2003 16:14:05 -0000 1.21.98.1 @@ -232,6 +232,8 @@ } builtmodules.addElement(module); } catch (BuildException BE) { + fix (module); + log(BE.toString(), Project.MSG_WARN); BE.printStackTrace(); failedmodules.addElement(module); @@ -240,6 +242,31 @@ log("builtmodules=" + builtmodules, Project.MSG_VERBOSE); log("failedmodules=" + failedmodules, Project.MSG_VERBOSE); } + } + + private void fix (String module) throws BuildException { + System.out.println("FIXING MODULE: " + module); + File projectFile = new File (project.getBaseDir(), "build.xml"); + System.out.println("base: " + projectFile); + for (int j = 0; j < topdirs.size (); j++) { + File topdir = (File) topdirs.get (j); + File netbeans = new File (new File (topdir, module), "build.xml"); + System.out.println("looking for build file: " + netbeans); + if (netbeans.exists ()) { + try { + FixClassPath.main (new String[] { + "--main", + projectFile.toString (), + netbeans.toString () + }); + } catch (Exception ex) { + throw new BuildException (ex); + } + + return; + } + } + throw new BuildException ("Build script not found for module " + module); } public void execute () throws BuildException { Index: antsrc/org/netbeans/nbbuild/FixClassPath.java =================================================================== RCS file: antsrc/org/netbeans/nbbuild/FixClassPath.java diff -N antsrc/org/netbeans/nbbuild/FixClassPath.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ antsrc/org/netbeans/nbbuild/FixClassPath.java 9 Oct 2003 16:14:05 -0000 1.1.2.7 @@ -0,0 +1,359 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.netbeans.nbbuild; + +import java.io.*; +import java.nio.*; +import java.nio.channels.*; +import java.util.regex.*; + +/** Assistent in changing of build scripts. + * + * @author Jaroslav Tulach + */ +public class FixClassPath { + private static final String OLD = "netbeans/lib/openide.jar"; + private static final String[] NEW = { + "actions/netbeans/lib/openide-actions.jar", + "fs/netbeans/lib/openide-fs.jar", + "nodes/netbeans/lib/openide-nodes.jar", + "util/netbeans/lib/openide-util.jar", + "dialogs/netbeans/lib/openide-dialogs.jar", + "options/netbeans/lib/openide-options.jar", + "windows/netbeans/lib/openide-windows.jar", + "explorer/netbeans/lib/openide-explorer.jar", + "modules/netbeans/lib/openide-modules.jar", + "awt/netbeans/lib/openide-awt.jar", + "text/netbeans/lib/openide-text.jar" + }; + private static final String[] MANIFEST = { + "org.openide.actions > 4.20", + "org.openide.filesystems > 4.20", + "org.openide.nodes > 4.20", + "org.openide.util > 4.20", + "org.openide.dialogs > 4.20", + "org.openide.options > 4.20", + "org.openide.windows > 4.20", + "org.openide.explorer > 4.20", + "org.openide.modules > 4.20", + "org.openide.awt > 4.20", + "org.openide.text > 4.20" + }; + private static final String[] BUILD = { + "openide/actions", + "openide/fs", + "openide/nodes", + "openide/util", + "openide/dialogs", + "openide/options", + "openide/windows", + "openide/explorer", + "openide/modules", + "openide/awt", + "openide/text" + }; + + + private static final Pattern PATTERN = Pattern.compile (".*pathelement.*" + OLD + ".*"); + private static final Pattern IDE_DEP = Pattern.compile ("OpenIDE-Module-IDE-Dependencies.*"); + private static final Pattern MODULE_DEP = Pattern.compile ("OpenIDE-Module-Module-Dependencies.*"); + + /** Nothing */ + private FixClassPath() { + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) throws Exception { + File main = null; + + for (int i = 0; i < args.length; i++) { + if ("--main".equals (args[i])) { + main = new File (args[i + 1]); + if (!main.exists ()) { + throw new IOException ("Main file does not exists " + main); + } + i++; + continue; + } + + File f = new File (args[i]); + if (f.exists ()) { + String[] usedJars = fix (f); + File manifest = new File (f.getParentFile (), "manifest.mf"); + if (manifest.exists ()) { + manifest (manifest, usedJars); + } else { + log ("Manifest not found: " + manifest); + } + + if (main != null) { + fixMain (main, f, usedJars); + } + + } else { + System.err.println("File " + f + " does not exists"); + } + } + } + + /** Finds a file, scans it replaces opende.jar + * @return array of strings, clone of NEW with only those items that + * were used + */ + private static String[] fix (File buildFile) throws IOException { + long longLen = buildFile.length(); + if (longLen > Integer.MAX_VALUE) throw new IOException ("Too big file: " + buildFile); + + + + byte[] arr = new byte[(int)longLen]; + + FileInputStream is = new FileInputStream (buildFile); + int read = is.read (arr); + if (longLen != read) throw new IOException ("Not fully read: " + longLen + " was: " + read); + is.close (); + + String buffer = new String (arr); + + log ("Processing " + buildFile); + rewrite (buildFile, buffer, NEW); + + if (!build (buildFile)) { + throw new IOException ("It has to build correctly after replace: " + buildFile); + } + + log ("Compiled successfully"); + + String[] removeUnnecessary = (String[])NEW.clone (); + for (int i = 0; i < removeUnnecessary.length; i++) { + String remember = removeUnnecessary[i]; + removeUnnecessary[i] = null; + System.out.println(" removed " + remember); + rewrite (buildFile, buffer, removeUnnecessary); + if (!build (buildFile)) { + // return it back + removeUnnecessary[i] = remember; + System.out.println(" bad, returned back"); + } else { + System.out.println(" compiled ok, kept removed"); + } + } + + rewrite (buildFile, buffer, removeUnnecessary); + + log ("The build file: " + buildFile + " is now using following JARs:"); + for (int i = 0; i < removeUnnecessary.length; i++) { + if (removeUnnecessary[i] != null) { + log (" " + removeUnnecessary[i]); + } + } + + return removeUnnecessary; + } + + /** Rewrites the file with replacements of packages. + */ + private static void rewrite (File file, String buffer, String[] replacements) + throws IOException { + Matcher m = PATTERN.matcher (buffer); + + if (!m.find()) { + throw new IOException ("Pattern not found: " + file); + } + + int from = m.start(); + int to = m.end (); + + String original = buffer.subSequence(from, to).toString(); + + StringBuffer replace = new StringBuffer (); + replace.append (buffer.subSequence (0, from)); + String line = ""; + for (int i = 0; i < replacements.length; i++) { + if (replacements[i] != null) { + replace.append (line); + replace.append (original.replaceAll(OLD, replacements[i])); + line = "\n"; + } + } + replace.append (buffer.subSequence(to, buffer.length())); + + + FileOutputStream out = new FileOutputStream (file); + out.write (replace.toString().getBytes()); + out.close (); + } + + /** Builds a build file. + */ + private static boolean build (File f) throws IOException { + Process p = Runtime.getRuntime().exec (new String[] { "ant", "-f", f.toString(), "clean", "compile" }); + new CopyThread (p.getInputStream()); + new CopyThread (p.getErrorStream()); + try { + return p.waitFor () == 0; + } catch (InterruptedException ex) { + return false; + } + } + + /** Logs. + */ + private static void log (String s) { + System.out.println(s); + } + + private static final class CopyThread extends Thread { + private InputStream is; + + public CopyThread (InputStream is) { + this.is = is; + start (); + } + + public void run () { + try { + int ch; + for (;;) { + ch = is.read (); + if (ch == -1) return; + System.err.write (ch); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } + + + /** Updates the manifest file, if found. + */ + private static void manifest (File f, String[] usedJars) throws IOException { + long longLen = f.length(); + if (longLen > Integer.MAX_VALUE) throw new IOException ("Too big file: " + f); + + + + byte[] arr = new byte[(int)longLen]; + + FileInputStream is = new FileInputStream (f); + int read = is.read (arr); + if (longLen != read) throw new IOException ("Not fully read: " + longLen + " was: " + read); + is.close (); + + String buffer = new String (arr); + + log ("Processing manifest " + f); + + Matcher ide = IDE_DEP.matcher(buffer); + if (ide.find ()) { + // remove that line + buffer = buffer.substring (0, ide.start()) + buffer.substring (ide.end () + 1); + } + + Matcher m = MODULE_DEP.matcher (buffer); + StringBuffer sb = new StringBuffer (); + String delim; + int where; + if (m.find ()) { + where = m.end (); + sb.append (buffer.subSequence(0, where)); + delim = ", "; + } else { + where = buffer.indexOf('\n'); + sb.append (buffer.subSequence(0, where)); + sb.append ("\n"); + sb.append ("OpenIDE-Module-Module-Dependencies: "); + delim = ""; + } + + + for (int i = 0; i < usedJars.length; i++) { + if (usedJars[i] != null) { + sb.append (delim); + sb.append (MANIFEST[i]); + delim = ", "; + } + } + + sb.append (buffer.substring(where)); + + + FileOutputStream out = new FileOutputStream (f); + out.write (sb.toString().getBytes()); + out.close (); + } + + /** Updates the main file. + */ + private static void fixMain (File main, File script, String[] usedJars) throws IOException { + long longLen = main.length(); + if (longLen > Integer.MAX_VALUE) throw new IOException ("Too big file: " + main); + + + + byte[] arr = new byte[(int)longLen]; + + FileInputStream is = new FileInputStream (main); + int read = is.read (arr); + if (longLen != read) throw new IOException ("Not fully read: " + longLen + " was: " + read); + is.close (); + + String buffer = new String (arr); + + log ("Processing main script for" + script); + + String toSearch = script.getParent(); + if (toSearch.endsWith ("/")) { + toSearch = toSearch.substring (0, toSearch.length() - 1); + } + int start; + int end; + for (;;) { + Matcher m = Pattern.compile ("