001: package tide.compiler;
002:
003: import tide.project.ProjectUtils;
004: import java.io.*;
005: import java.util.*;
006: import tide.editor.linemessages.*;
007:
008: public final class JavaCompilerCall {
009: // these file should NOT be part of published jar files...
010: final public static String compilerBatchName = "compile.bat";
011:
012: final private static boolean createCompilerBatch = false;
013:
014: private JavaCompilerCall() {
015: }
016:
017: /** Compile all the java files in the specified source location
018: * -J-XX:+ShowMessageBoxOnError attach Visualstudio !
019: * -J-XX:OnError="userdump.exe %p"
020: */
021: public static Process compile_(File javaCompiler,
022: Collection<File> srcFiles, File sourceBase,
023: File classesDest, List<File> classPath,
024: String compilerOptions) throws Exception {
025:
026: if (!javaCompiler.exists())
027: throw new Exception("Java compiler no found: "
028: + javaCompiler);
029:
030: if (!classesDest.exists()) {
031: classesDest.mkdirs();
032: }
033:
034: File argsTempFile = File.createTempFile("tide_comp_args",
035: "temp"); // Sometimes fails to delete on exit !!
036: argsTempFile.deleteOnExit();
037:
038: // create the relative .java files names
039: String srcBase = sourceBase.getAbsolutePath();
040: if (!srcBase.endsWith(File.pathSeparator))
041: srcBase += File.pathSeparator;
042: int srcLen = srcBase.length();
043:
044: FileOutputStream classesFile = null;
045: try {
046: //File cf = new File(sourceBase, classesToCompileFileName);
047: //cf.deleteOnExit();
048: classesFile = new FileOutputStream(argsTempFile);
049: PrintWriter pw = new PrintWriter(classesFile);
050: for (File f : srcFiles) {
051: String relName = f.getAbsolutePath().substring(srcLen);
052: //System.out.println(""+relName);
053: pw.append(relName);
054: pw.append("\r\n");
055:
056: String cn = relName.substring(0, relName.length() - 5)
057: .replace('\\', '.');
058: //System.out.println("cn="+cn);
059: LineMessagesManager.getInstance().removeMessagesFor(cn,
060: CompilationMessage.class);
061:
062: }
063: pw.close();
064: } finally {
065: classesFile.close();
066: }
067:
068: List<String> javaCCommandAndArguments = new ArrayList<String>();
069: javaCCommandAndArguments.add(javaCompiler.getAbsolutePath());
070:
071: // destination
072: javaCCommandAndArguments.add("-d");
073: javaCCommandAndArguments.add(classesDest.getAbsolutePath());
074:
075: // options
076: if (compilerOptions != null
077: && compilerOptions.trim().length() > 0) {
078: //javaCCommandAndArguments.addAll( Arrays.asList(compilerOptions.split(" ") ));
079: javaCCommandAndArguments.addAll(ProjectUtils.splitArgs(
080: compilerOptions, false));
081: }
082:
083: // classpath
084: List<File> cp = new ArrayList<File>();
085: cp.add(classesDest); // always present: contain the already compiled classes
086: if (classPath != null) {
087: cp.addAll(classPath);
088: }
089: javaCCommandAndArguments.add("-classpath");
090: StringBuilder cpsb = new StringBuilder();
091: for (int i = 0; i < cp.size(); i++) {
092: cpsb.append(cp.get(i).getAbsolutePath());
093: if (i < cp.size() - 1)
094: cpsb.append(System.getProperty("path.separator"));
095: }
096: javaCCommandAndArguments.add(cpsb.toString());
097:
098: //javaCCommandAndArguments.add( "@JavaC_options" );
099: javaCCommandAndArguments.add("@"
100: + argsTempFile.getAbsolutePath()); //classesToCompileFileName );
101:
102: // write the batch (practical to debug)
103:
104: if (createCompilerBatch) {
105: PrintWriter batchFileWriter = new PrintWriter(new File(
106: sourceBase, compilerBatchName));
107: try {
108: for (String item : javaCCommandAndArguments) {
109: batchFileWriter.append(item + " ");
110: }
111: } catch (Exception e) {
112: // ignore: not fatal
113: e.printStackTrace();
114: } finally {
115: batchFileWriter.close();
116: }
117: }
118:
119: long startTime = System.currentTimeMillis();
120: // start the process in the sourceBase directory
121: Process proc = Runtime.getRuntime().exec(
122: javaCCommandAndArguments
123: .toArray(new String[javaCCommandAndArguments
124: .size()]), null, sourceBase);
125:
126: return proc;
127: }
128: }
|